coderay_bash 0.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.md ADDED
@@ -0,0 +1,16 @@
1
+ Bash scanner for highlighting scripts with CodeRay
2
+ ==================================================
3
+
4
+ ### Usage
5
+
6
+ require 'rubygems'
7
+ require 'coderay_bash'
8
+
9
+ plain = File.read('some_script.sh')
10
+ @body = CodeRay.scan(plain, :bash).div
11
+
12
+ ### in your template then do some thing like
13
+
14
+ <%= @body %>
15
+
16
+ For more information see [CodeRay we pages](http://coderay.rubychan.de/)
data/Rakefile ADDED
@@ -0,0 +1,14 @@
1
+ # -*- coding: utf-8 -*-
2
+ # vi: fenc=utf-8:expandtab:ts=2:sw=2:sts=2
3
+ #
4
+ # @author: Petr Kovar <pejuko@gmail.com>
5
+ $KCODE='UTF8'
6
+
7
+ require 'rake/gempackagetask'
8
+ require 'rake/clean'
9
+
10
+ CLEAN << "coverage" << "pkg" << "README.html" << "CHANGELOG.html"
11
+
12
+ task :default => [:gem]
13
+ Rake::GemPackageTask.new(eval(File.read("coderay_bash.gemspec"))) {|pkg|}
14
+
@@ -0,0 +1,29 @@
1
+ # -*- coding: utf-8 -*-
2
+ # vi: fenc=utf-8:expandtab:ts=2:sw=2:sts=2
3
+ #
4
+ # @author: Petr Kovar <pejuko@gmail.com>
5
+
6
+ require 'rubygems'
7
+ require 'find'
8
+
9
+ spec = Gem::Specification.new do |s|
10
+ s.platform = Gem::Platform::RUBY
11
+ s.summary = "Simple bash scanner for highlighting with coderay."
12
+ s.homepage = "http://github.com/pejuko/coderay_bash"
13
+ s.email = "pejuko@gmail.com"
14
+ s.authors = ["Petr Kovar"]
15
+ s.name = 'coderay_bash'
16
+ s.version = '0.1'
17
+ s.date = Time.now.strftime("%Y-%m-%d")
18
+ s.add_dependency('coderay', '< 1.0')
19
+ s.require_path = 'lib'
20
+ s.files = ["README.md", "coderay_bash.gemspec", "Rakefile"]
21
+ s.files += Dir["lib/**/*.rb"]
22
+ s.post_install_message = <<EOF
23
+ This gem was tested with coderay 0.9.3 and won't work with coderay from svn.
24
+ EOF
25
+ s.description = <<EOF
26
+ This gem was tested with coderay 0.9.3 and won't work with coderay from svn.
27
+ EOF
28
+ end
29
+
@@ -0,0 +1,177 @@
1
+ # Scanner for Bash
2
+ # Author: Petr Kovar <pejuko@gmail.com>
3
+ module CodeRay::Scanners
4
+ class Bash < Scanner
5
+
6
+ register_for :bash
7
+ file_extension 'sh'
8
+ title 'bash script'
9
+
10
+ RESERVED_WORDS = %w(
11
+ ! [[ ]] case do done elif else esac fi for function if in select then time until while { }
12
+ )
13
+
14
+ COMMANDS = %w(
15
+ : . break cd continue eval exec exit export getopts hash pwd
16
+ readonly return shift test [ ] times trap umask unset
17
+ )
18
+
19
+ BASH_COMMANDS = %w(
20
+ alias bind builtin caller command declare echo enable help let
21
+ local logout printf read set shopt source type typeset ulimit unalias
22
+ )
23
+
24
+ RESERVED = RESERVED_WORDS + COMMANDS + BASH_COMMANDS
25
+
26
+ VARIABLES = %w(
27
+ CDPATH HOME IFS MAIL MAILPATH OPTARG OPTIND PATH PS1 PS2
28
+ )
29
+
30
+ BASH_VARIABLES = %w(
31
+ BASH BASH_ARGC BASH_ARGV BASH_COMMAND BASH_ENV BASH_EXECUTION_STRING
32
+ BASH_LINENO BASH_REMATCH BASH_SOURCE BASH_SUBSHELL BASH_VERSINFO
33
+ BASH_VERSINFO[0] BASH_VERSINFO[1] BASH_VERSINFO[2] BASH_VERSINFO[3]
34
+ BASH_VERSINFO[4] BASH_VERSINFO[5] BASH_VERSION COLUMNS COMP_CWORD
35
+ COMP_LINE COMP_POINT COMP_WORDBREAKS COMP_WORDS COMPREPLAY DIRSTACK
36
+ EMACS EUID FCEDIT FIGNORE FUNCNAME GLOBIGNORE GROUPS histchars HISTCMD
37
+ HISTCONTROL HISTFILE HISTFILESIZE HISTIGNORE HISTSIZE HISTTIMEFORMAT
38
+ HOSTFILE HOSTNAME HOSTTYPE IGNOREEOF INPUTRC LANG LC_ALL LC_COLLATE
39
+ LC_CTYPE LC_MESSAGE LC_NUMERIC LINENNO LINES MACHTYPE MAILCHECK OLDPWD
40
+ OPTERR OSTYPE PIPESTATUS POSIXLY_CORRECT PPID PROMPT_COMMAND PS3 PS4 PWD
41
+ RANDOM REPLAY SECONDS SHELL SHELLOPTS SHLVL TIMEFORMAT TMOUT TMPDIR UID
42
+ )
43
+
44
+ CONSTANTS = VARIABLES + BASH_VARIABLES
45
+
46
+ def scan_tokens tokens, options
47
+
48
+ state = :initial
49
+ quote = nil
50
+
51
+ until eos?
52
+ kind = match = nil
53
+
54
+ if match = scan(/\n/)
55
+ tokens << [match, :end_line]
56
+ next
57
+ end
58
+
59
+ if state == :initial
60
+ if match = scan(/#.*/)
61
+ kind = :comment
62
+ elsif match = scan(/(?:\. |source ).*/)
63
+ kind = :reserved
64
+ elsif match = scan(/(?:\\.|,)/)
65
+ kind = :plain
66
+ elsif match = scan(/;/)
67
+ kind = :delimiter
68
+ elsif match = scan(/(?:"|`)/)
69
+ state = :quote
70
+ quote = match
71
+ tokens << [match, :plain]
72
+ tokens << [:open, :string] if quote == '"'
73
+ tokens << [:open, :shell] if quote == '`'
74
+ next
75
+ elsif match = scan(/'[^']*'/)
76
+ kind = :string
77
+ elsif match = scan(/(?: \& | > | < | \| >> | << | >\& )/ox)
78
+ kind = :bin
79
+ elsif match = scan(/\d+\.(?:\d+\.?)+/)
80
+ #version
81
+ kind = :float
82
+ elsif match = scan(/\d+\.\d+\s+/)
83
+ kind = :float
84
+ elsif match = scan(/\d+/)
85
+ kind = :integer
86
+ elsif match = scan(/ (?: \$\(\( | \)\) ) /x)
87
+ kind = :global_variable
88
+ elsif match = scan(/ \$\{ [^\}]+ \} /ox)
89
+ kind = :instance_variable
90
+ match =~ /\$\{(.*)\}/
91
+ kind = :pre_type if CONSTANTS.include?($1)
92
+ elsif match = scan(/ \$\( [^\)]+ \) /ox)
93
+ kind = :shell
94
+ elsif match = scan(/ \$\{? (?: \# | \? | \d | \* | @ | - | \$ | \! | _ ) \}? /ox)
95
+ kind = :pre_constant
96
+ elsif match = scan(/[^\s]*[A-Za-z_][A-Za-z_0-9]*\+?=/)
97
+ match =~ /(.*?)([A-Za-z_][A-Za-z_0-9]*)(\+?=)/
98
+ str = $1
99
+ pre = $2
100
+ op = $3
101
+ kind = :plain
102
+ if str.to_s.strip.empty?
103
+ kind = :instance_variable
104
+ kind = :pre_type if CONSTANTS.include?(pre)
105
+ #kind = :pre_constant if CONSTANTS.include?(pre)
106
+ tokens << [pre, kind]
107
+ tokens << [op, :operator]
108
+ next
109
+ end
110
+ elsif match = scan(/[A-Za-z_]+\[[A-Za-z_\d]+\]/)
111
+ # array
112
+ kind = :instance_variable
113
+ kind = :pre_type if CONSTANTS.include?(match)
114
+ elsif match = scan(/ \$[A-Za-z_][A-Za-z_0-9]* /ox)
115
+ kind = :instance_variable
116
+ match =~ /\$(.*)/
117
+ kind = :pre_type if CONSTANTS.include?($1)
118
+ elsif match = scan(/read \S+/)
119
+ match =~ /read(\s+)(\S+)/
120
+ tokens << ['read', :reserved]
121
+ tokens << [$1, :space]
122
+ tokens << [$2, :instance_variable]
123
+ next
124
+ elsif match = scan(/[\!\:\[\]\{\}]/)
125
+ kind = :reserved
126
+ elsif match = scan(/ [A-Za-z_][A-Za-z_\d]*;? /x)
127
+ match =~ /([^;]+);?/
128
+ if RESERVED.include?($1)
129
+ if match[/([^;]+);$/]
130
+ tokens << [$1, :reserved]
131
+ tokens << [';', :delimiter]
132
+ next
133
+ end
134
+ kind = :reserved
135
+ end
136
+ elsif match = scan(/(?: = | - | \+ | \{ | \} | \( | \) | && | \|\| | ;; | ! )/ox)
137
+ kind = :operator
138
+ elsif match = scan(/\s+/)
139
+ kind = :space
140
+ elsif match = scan(/[^ \$"'`\d]/)
141
+ kind = :plain
142
+ elsif match = scan(/.+/)
143
+ # this shouldn't be
144
+ kind = :reserved
145
+ end
146
+ elsif state == :quote
147
+ if (match = scan(/\\./))
148
+ kind = :content
149
+ elsif match = scan(/#{quote}/)
150
+ tokens << [:close, :string] if quote == '"'
151
+ tokens << [:close, :shell] if quote == "`"
152
+ quote = nil
153
+ state = :initial
154
+ kind = :plain
155
+ elsif match = scan(/ (?: \$\{?[A-Za-z_][A-Za-z_\d]*\}? | \$\{?(?:\#|\?|\d)\}? ) /x)
156
+ kind = :instance_variable
157
+ elsif (quote == '`') and (match = scan(/\$"/))
158
+ kind = :content
159
+ elsif (quote == '"') and (match = scan(/\$`/))
160
+ kind = :content
161
+ elsif match = scan(/[^#{quote}\$\\]+/)
162
+ kind = :content
163
+ else match = scan(/.+/)
164
+ # this shouldn't be
165
+ kind = :reserved
166
+ end
167
+ end
168
+
169
+ match ||= matched
170
+ tokens << [match, kind]
171
+ end
172
+
173
+ tokens
174
+ end
175
+
176
+ end
177
+ end
@@ -0,0 +1,4 @@
1
+ require 'coderay'
2
+ path = File.join File.expand_path(File.dirname(__FILE__)), "coderay/scanners/bash.rb"
3
+ p path
4
+ require path
metadata ADDED
@@ -0,0 +1,88 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: coderay_bash
3
+ version: !ruby/object:Gem::Version
4
+ hash: 9
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 1
9
+ version: "0.1"
10
+ platform: ruby
11
+ authors:
12
+ - Petr Kovar
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-08-29 00:00:00 +02:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: coderay
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - <
27
+ - !ruby/object:Gem::Version
28
+ hash: 15
29
+ segments:
30
+ - 1
31
+ - 0
32
+ version: "1.0"
33
+ type: :runtime
34
+ version_requirements: *id001
35
+ description: |
36
+ This gem was tested with coderay 0.9.3 and won't work with coderay from svn.
37
+
38
+ email: pejuko@gmail.com
39
+ executables: []
40
+
41
+ extensions: []
42
+
43
+ extra_rdoc_files: []
44
+
45
+ files:
46
+ - README.md
47
+ - coderay_bash.gemspec
48
+ - Rakefile
49
+ - lib/coderay/scanners/bash.rb
50
+ - lib/coderay_bash.rb
51
+ has_rdoc: true
52
+ homepage: http://github.com/pejuko/coderay_bash
53
+ licenses: []
54
+
55
+ post_install_message: |
56
+ This gem was tested with coderay 0.9.3 and won't work with coderay from svn.
57
+
58
+ rdoc_options: []
59
+
60
+ require_paths:
61
+ - lib
62
+ required_ruby_version: !ruby/object:Gem::Requirement
63
+ none: false
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ hash: 3
68
+ segments:
69
+ - 0
70
+ version: "0"
71
+ required_rubygems_version: !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ hash: 3
77
+ segments:
78
+ - 0
79
+ version: "0"
80
+ requirements: []
81
+
82
+ rubyforge_project:
83
+ rubygems_version: 1.3.7
84
+ signing_key:
85
+ specification_version: 3
86
+ summary: Simple bash scanner for highlighting with coderay.
87
+ test_files: []
88
+