rouge 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,165 @@
1
+ module Rouge
2
+ module Lexers
3
+ class CSSLexer < RegexLexer
4
+ tag 'css'
5
+ extensions 'css'
6
+
7
+ identifier = /[a-zA-Z0-9_-]+/
8
+ number = /-?(?:[0-9]+(\.[0-9]+)?|\.[0-9]+)/
9
+ keywords = %w(
10
+ azimuth background-attachment background-color
11
+ background-image background-position background-repeat
12
+ background border-bottom-color border-bottom-style
13
+ border-bottom-width border-left-color border-left-style
14
+ border-left-width border-right border-right-color
15
+ border-right-style border-right-width border-top-color
16
+ border-top-style border-top-width border-bottom
17
+ border-collapse border-left border-width border-color
18
+ border-spacing border-style border-top border caption-side
19
+ clear clip color content counter-increment counter-reset
20
+ cue-after cue-before cue cursor direction display
21
+ elevation empty-cells float font-family font-size
22
+ font-size-adjust font-stretch font-style font-variant
23
+ font-weight font height letter-spacing line-height
24
+ list-style-type list-style-image list-style-position
25
+ list-style margin-bottom margin-left margin-right
26
+ margin-top margin marker-offset marks max-height max-width
27
+ min-height min-width opacity orphans outline outline-color
28
+ outline-style outline-width overflow(?:-x -y ) padding-bottom
29
+ padding-left padding-right padding-top padding page
30
+ page-break-after page-break-before page-break-inside
31
+ pause-after pause-before pause pitch pitch-range
32
+ play-during position quotes richness right size
33
+ speak-header speak-numeral speak-punctuation speak
34
+ speech-rate stress table-layout text-align text-decoration
35
+ text-indent text-shadow text-transform top unicode-bidi
36
+ vertical-align visibility voice-family volume white-space
37
+ widows width word-spacing z-index bottom left
38
+ above absolute always armenian aural auto avoid baseline
39
+ behind below bidi-override blink block bold bolder both
40
+ capitalize center-left center-right center circle
41
+ cjk-ideographic close-quote collapse condensed continuous
42
+ crop crosshair cross cursive dashed decimal-leading-zero
43
+ decimal default digits disc dotted double e-resize embed
44
+ extra-condensed extra-expanded expanded fantasy far-left
45
+ far-right faster fast fixed georgian groove hebrew help
46
+ hidden hide higher high hiragana-iroha hiragana icon
47
+ inherit inline-table inline inset inside invert italic
48
+ justify katakana-iroha katakana landscape larger large
49
+ left-side leftwards level lighter line-through list-item
50
+ loud lower-alpha lower-greek lower-roman lowercase ltr
51
+ lower low medium message-box middle mix monospace
52
+ n-resize narrower ne-resize no-close-quote no-open-quote
53
+ no-repeat none normal nowrap nw-resize oblique once
54
+ open-quote outset outside overline pointer portrait px
55
+ relative repeat-x repeat-y repeat rgb ridge right-side
56
+ rightwards s-resize sans-serif scroll se-resize
57
+ semi-condensed semi-expanded separate serif show silent
58
+ slow slower small-caps small-caption smaller soft solid
59
+ spell-out square static status-bar super sw-resize
60
+ table-caption table-cell table-column table-column-group
61
+ table-footer-group table-header-group table-row
62
+ table-row-group text text-bottom text-top thick thin
63
+ transparent ultra-condensed ultra-expanded underline
64
+ upper-alpha upper-latin upper-roman uppercase url
65
+ visible w-resize wait wider x-fast x-high x-large x-loud
66
+ x-low x-small x-soft xx-large xx-small yes
67
+ ).join('|')
68
+
69
+ builtins = %w(
70
+ indigo gold firebrick indianred yellow darkolivegreen
71
+ darkseagreen mediumvioletred mediumorchid chartreuse
72
+ mediumslateblue black springgreen crimson lightsalmon brown
73
+ turquoise olivedrab cyan silver skyblue gray darkturquoise
74
+ goldenrod darkgreen darkviolet darkgray lightpink teal
75
+ darkmagenta lightgoldenrodyellow lavender yellowgreen thistle
76
+ violet navy orchid blue ghostwhite honeydew cornflowerblue
77
+ darkblue darkkhaki mediumpurple cornsilk red bisque slategray
78
+ darkcyan khaki wheat deepskyblue darkred steelblue aliceblue
79
+ gainsboro mediumturquoise floralwhite coral purple lightgrey
80
+ lightcyan darksalmon beige azure lightsteelblue oldlace
81
+ greenyellow royalblue lightseagreen mistyrose sienna
82
+ lightcoral orangered navajowhite lime palegreen burlywood
83
+ seashell mediumspringgreen fuchsia papayawhip blanchedalmond
84
+ peru aquamarine white darkslategray ivory dodgerblue
85
+ lemonchiffon chocolate orange forestgreen slateblue olive
86
+ mintcream antiquewhite darkorange cadetblue moccasin
87
+ limegreen saddlebrown darkslateblue lightskyblue deeppink
88
+ plum aqua darkgoldenrod maroon sandybrown magenta tan
89
+ rosybrown pink lightblue palevioletred mediumseagreen
90
+ dimgray powderblue seagreen snow mediumblue midnightblue
91
+ paleturquoise palegoldenrod whitesmoke darkorchid salmon
92
+ lightslategray lawngreen lightgreen tomato hotpink
93
+ lightyellow lavenderblush linen mediumaquamarine green
94
+ blueviolet peachpuff
95
+ ).join('|')
96
+
97
+ state :root do
98
+ mixin :basics
99
+ rule /{/, 'Punctuation', :stanza
100
+ rule /:#{identifier}/, 'Name.Decorator'
101
+ rule /\.#{identifier}/, 'Name.Class'
102
+ rule /##{identifier}/, 'Name.Function'
103
+ rule /@#{identifier}/, 'Keyword', :at_rule
104
+ rule identifier, 'Name.Tag'
105
+ rule %r([~^*!%&\[\]()<>|+=@:;,./?-]), 'Operator'
106
+ end
107
+
108
+ state :value do
109
+ mixin :basics
110
+ rule /url\(.*?\)/, 'Literal.String.Other'
111
+ rule /#[0-9a-f]{1,6}/i, 'Literal.Number' # colors
112
+ rule /#{number}(?:em|px|%|pt|pc|in|mm|m|ex|s)?\b/, 'Literal.Number'
113
+ rule /[\[\]():\/.]/, 'Punctuation'
114
+ rule /"(\\\\|\\"|[^"])*"/, 'Literal.String.Single'
115
+ rule /'(\\\\|\\'|[^'])*'/, 'Literal.String.Double'
116
+ rule identifier, 'Name'
117
+ end
118
+
119
+ state :at_rule do
120
+ rule /{(?=\s*#{identifier}\s*:)/m, 'Punctuation', :at_stanza
121
+ rule /{/, 'Punctuation', :at_body
122
+ rule /;/, 'Punctuation', :pop!
123
+ mixin :value
124
+ end
125
+
126
+ state :at_body do
127
+ mixin :at_content
128
+ mixin :root
129
+ end
130
+
131
+ state :at_stanza do
132
+ mixin :at_content
133
+ mixin :stanza
134
+ end
135
+
136
+ state :at_content do
137
+ rule /}/ do
138
+ token 'Punctuation'
139
+ pop!; pop!
140
+ end
141
+ end
142
+
143
+ state :basics do
144
+ rule /\s+/m, 'Text'
145
+ rule %r(/\*(?:.*?)\*/)m, 'Comment'
146
+ end
147
+
148
+ state :stanza do
149
+ mixin :basics
150
+ rule /}/, 'Punctuation', :pop!
151
+ rule /(?:#{keywords})\s*:/m, 'Keyword', :stanza_value
152
+ rule /(?:#{builtins})\s*:/m, 'Name.Builtin', :stanza_value
153
+ rule /#{identifier}\s*:/m, 'Name', :stanza_value
154
+ end
155
+
156
+ state :stanza_value do
157
+ rule /;/, 'Punctuation', :pop!
158
+ rule(/(?=})/) { pop! }
159
+ rule /!important\b/, 'Comment.Preproc'
160
+ rule /^@.*?$/, 'Comment.Preproc'
161
+ mixin :value
162
+ end
163
+ end
164
+ end
165
+ end
@@ -0,0 +1,28 @@
1
+ module Rouge
2
+ module Lexers
3
+ class Diff < RegexLexer
4
+ tag 'diff'
5
+ aliases 'patch'
6
+ extensions 'diff', 'patch'
7
+
8
+ state :header do
9
+ rule /^diff .*?\n(?=---|\+\+\+)/m, 'Generic.Heading'
10
+ rule /^--- .*?\n/, 'Generic.Deleted'
11
+ rule /^\+\+\+ .*?\n/, 'Generic.Inserted'
12
+ end
13
+
14
+ state :diff do
15
+ rule /@@ -\d+,\d+ \+\d+,\d+ @@.*?\n/, 'Generic.Heading'
16
+ rule /^\+.*?\n/, 'Generic.Inserted'
17
+ rule /^-.*?\n/, 'Generic.Deleted'
18
+ rule /^ .*?\n/, 'Text'
19
+ rule /^.*?\n/, 'Error'
20
+ end
21
+
22
+ state :root do
23
+ mixin :header
24
+ mixin :diff
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,65 @@
1
+ module Rouge
2
+ module Lexers
3
+ class HTML < RegexLexer
4
+ tag 'html'
5
+ extensions 'htm', 'html'
6
+
7
+ state :root do
8
+ rule /[^<&]+/m, 'Text'
9
+ rule /&\S*?;/, 'Name.Entity'
10
+ rule /<!\[CDATA\[.*?\]\]>/m, 'Comment.Preproc'
11
+ rule /<!--/, 'Comment', :comment
12
+ rule /<\?.*?\?>/m, 'Comment.Preproc' # php? really?
13
+
14
+ rule /<\s*script\s*/m do
15
+ token 'Name.Tag'
16
+ push :script_content
17
+ push :tag
18
+ end
19
+
20
+ rule /<\s*style\s*/m do
21
+ token 'Name.Tag'
22
+ push :style_content
23
+ push :tag
24
+ end
25
+
26
+ rule %r(<\s*[a-zA-Z0-9:]+), 'Name.Tag', :tag # opening tags
27
+ rule %r(<\s*/\s*[a-zA-Z0-9:]+\s*>), 'Name.Tag' # closing tags
28
+ end
29
+
30
+ state :comment do
31
+ rule /[^-]+/, 'Comment'
32
+ rule /-->/, 'Comment', :pop!
33
+ rule /-/, 'Comment'
34
+ end
35
+
36
+ state :tag do
37
+ rule /\s+/m, 'Text'
38
+ rule /[a-zA-Z0-9_:-]+\s*=/m, 'Name.Attribute', :attr
39
+ rule /[a-zA-Z0-9_:-]+/, 'Name.Attribute'
40
+ rule %r(/?\s*>)m, 'Name.Tag', :pop!
41
+ end
42
+
43
+ state :attr do
44
+ # TODO: are backslash escapes valid here?
45
+ rule /".*?"/, 'Literal.String', :pop!
46
+ rule /'.*?'/, 'Literal.String', :pop!
47
+ rule /[^\s>]+/, 'Literal.String', :pop!
48
+ end
49
+
50
+ state :script_content do
51
+ rule %r(<\s*/\s*script\s*>)m, 'Name.Tag', :pop!
52
+ rule %r(.*?(?=<\s*/\s*script\s*>))m do
53
+ delegate JavascriptLexer
54
+ end
55
+ end
56
+
57
+ state :style_content do
58
+ rule %r(<\s*/\s*style\s*>)m, 'Name.Tag', :pop!
59
+ rule %r(.*(?=<\s*/\s*style\s*>))m do
60
+ delegate CSSLexer
61
+ end
62
+ end
63
+ end
64
+ end
65
+ end
@@ -1,23 +1,28 @@
1
1
  module Rouge
2
2
  module Lexers
3
- JavascriptLexer = RegexLexer.create do
4
- option :debug, true
5
-
6
- name 'javascript'
3
+ class JavascriptLexer < RegexLexer
4
+ tag 'javascript'
7
5
  aliases 'js'
6
+ extensions 'js'
8
7
 
9
- lexer :comments_and_whitespace do
8
+ state :comments_and_whitespace do
10
9
  rule /\s+/, 'Text'
11
10
  rule /<!--/, 'Comment' # really...?
12
11
  rule %r(//.*?\n), 'Comment.Single'
13
12
  rule %r(/\*.*?\*/), 'Comment.Multiline'
14
13
  end
15
14
 
16
- lexer :slash_starts_regex do
15
+ state :slash_starts_regex do
17
16
  mixin :comments_and_whitespace
17
+
18
18
  rule %r(
19
- /(\\.|[^[/\\\n]|\[(\\.|[^\]\\\n])*])+/ # a nonempty regex
20
- (?:[gim]+\b|\B) # regex flags
19
+ / # opening slash
20
+ ( \\. # escape sequences
21
+ | [^/\\\n] # regular characters
22
+ | \[ (\\. | [^\]\\\n])* \] # character classes
23
+ )+
24
+ / # closing slash
25
+ (?:[gim]+\b|\B) # flags
21
26
  )x, 'Literal.String.Regex'
22
27
 
23
28
  # if it's not matched by the above r.e., it's not
@@ -25,10 +30,10 @@ module Rouge
25
30
  # end of the line.
26
31
  rule %r(/), 'Literal.String.Regex', :bad_regex
27
32
  rule //, 'Text', :pop!
33
+ end
28
34
 
29
- lexer :bad_regex do
30
- rule /[^\n]+/, 'Error', :pop!
31
- end
35
+ state :bad_regex do
36
+ rule /[^\n]+/, 'Error', :pop!
32
37
  end
33
38
 
34
39
  keywords = %w(
@@ -55,10 +60,11 @@ module Rouge
55
60
  window
56
61
  ).join('|')
57
62
 
58
- lexer :root do
63
+ state :root do
59
64
  rule %r(^(?=\s|/|<!--)), 'Text', :slash_starts_regex
60
65
  mixin :comments_and_whitespace
61
- rule %r(\+\+|--|~|&&|\?|\|\||\\(?=\n)|<<|>>>?|===|!==),
66
+ rule %r(\+\+ | -- | ~ | && | \|\| | \\(?=\n) | << | >>>? | ===
67
+ | !== | \? | : )x,
62
68
  'Operator', :slash_starts_regex
63
69
  rule %r([-<>+*%&|\^/!=]=?), 'Operator', :slash_starts_regex
64
70
  rule /[{(\[;,]/, 'Punctuation', :slash_starts_regex
@@ -70,14 +76,12 @@ module Rouge
70
76
  rule /(?:#{builtins})\b/, 'Name.Builtin'
71
77
  rule /[$a-zA-Z_][a-zA-Z0-9_]*/, 'Name.Other'
72
78
 
73
- rule /[0-9][0-9]*\.[0-9]+([eE][0-9]+)?[fd]?/, 'Number.Float'
74
- rule /0x[0-9a-fA-F]+/, 'Number.Hex'
75
- rule /[0-9]+/, 'Number.Integer'
79
+ rule /[0-9][0-9]*\.[0-9]+([eE][0-9]+)?[fd]?/, 'Literal.Number.Float'
80
+ rule /0x[0-9a-fA-F]+/, 'Literal.Number.Hex'
81
+ rule /[0-9]+/, 'Literal.Number.Integer'
76
82
  rule /"(\\\\|\\"|[^"])*"/, 'Literal.String.Double'
77
83
  rule /'(\\\\|\\'|[^'])*'/, 'Literal.String.Single'
78
84
  end
79
-
80
- mixin :root
81
85
  end
82
86
  end
83
87
  end
@@ -1,12 +1,13 @@
1
1
  module Rouge
2
2
  module Lexers
3
- ShellLexer = RegexLexer.create do
4
- name 'shell'
3
+ class ShellLexer < RegexLexer
4
+ tag 'shell'
5
5
  aliases 'bash', 'zsh', 'ksh', 'sh'
6
+ extensions 'sh', 'bash', 'zsh', 'ksh'
6
7
 
7
8
  KEYWORDS = %w(
8
- if fi else while do done for then return function case
9
- select continue until esac elif
9
+ if fi else while do done for then return function
10
+ select continue until esac elif in
10
11
  ).join('|')
11
12
 
12
13
  BUILTINS = %w(
@@ -18,16 +19,17 @@ module Rouge
18
19
  ulimit umask unalias unset wait
19
20
  ).join('|')
20
21
 
21
- lexer :basic do
22
+ state :basic do
22
23
  rule /#.*\n/, 'Comment'
23
24
 
24
25
  rule /\b(#{KEYWORDS})\s*\b/, 'Keyword'
26
+ rule /\bcase\b/, 'Keyword', :case
25
27
 
26
28
  rule /\b(#{BUILTINS})\s*\b(?!\.)/, 'Name.Builtin'
27
29
 
28
- rule /(\b\w+)(=)/ do |_, var, eq, &out|
29
- out.call 'Name.Variable', var
30
- out.call 'Operator', eq
30
+ rule /(\b\w+)(=)/ do |m|
31
+ group 'Name.Variable'
32
+ group 'Operator'
31
33
  end
32
34
 
33
35
  rule /[\[\]{}()=]/, 'Operator'
@@ -38,17 +40,17 @@ module Rouge
38
40
  rule /<<-?\s*(\'?)\\?(\w+)[\w\W]+?\2/, 'Literal.String'
39
41
  end
40
42
 
41
- lexer :double_quotes do
42
- rule /"/, 'Literal.String.Double', :pop!
43
- rule /\\./, 'Literal.String.Escape'
43
+ state :double_quotes do
44
+ # NB: "abc$" is literally the string abc$.
45
+ # Here we prevent :interp from interpreting $" as a variable.
46
+ rule /(?:\$#?)?"/, 'Literal.String.Double', :pop!
44
47
  mixin :interp
45
48
  rule /[^"`\\$]+/, 'Literal.String.Double'
46
49
  end
47
50
 
48
- lexer :data do
49
- # TODO: this should be its own sublexer so we can capture
50
- # interpolation and such
51
- rule /$?"/, 'Literal.String.Double', :double_quotes
51
+ state :data do
52
+ rule /\\./, 'Literal.String.Escape'
53
+ rule /\$?"/, 'Literal.String.Double', :double_quotes
52
54
 
53
55
  # single quotes are much easier than double quotes - we can
54
56
  # literally just scan until the next single quote.
@@ -57,15 +59,17 @@ module Rouge
57
59
  # single-quotes. A single-quote cannot occur within single-quotes.
58
60
  rule /$?'[^']*'/, 'Literal.String.Single'
59
61
 
62
+ rule /\*/, 'Keyword'
63
+
60
64
  rule /;/, 'Text'
61
65
  rule /\s+/, 'Text'
62
- rule /[^=\s\[\]{}()$"\'`\\<]+/, 'Text'
66
+ rule /[^=\s{}()$"\'`\\<]+/, 'Text'
63
67
  rule /\d+(?= |\Z)/, 'Number'
64
68
  rule /</, 'Text'
65
69
  mixin :interp
66
70
  end
67
71
 
68
- lexer :curly do
72
+ state :curly do
69
73
  rule /}/, 'Keyword', :pop!
70
74
  rule /:-/, 'Keyword'
71
75
  rule /[a-zA-Z0-9_]+/, 'Name.Variable'
@@ -73,24 +77,38 @@ module Rouge
73
77
  mixin :root
74
78
  end
75
79
 
76
- lexer :paren do
80
+ state :paren do
77
81
  rule /\)/, 'Keyword', :pop!
78
82
  mixin :root
79
83
  end
80
84
 
81
- lexer :math do
85
+ state :math do
82
86
  rule /\)\)/, 'Keyword', :pop!
83
87
  rule %r([-+*/%^|&]|\*\*|\|\|), 'Operator'
84
88
  rule /\d+/, 'Number'
85
89
  mixin :root
86
90
  end
87
91
 
88
- lexer :backticks do
92
+ state :case do
93
+ rule /\besac\b/, 'Keyword', :pop!
94
+ rule /\|/, 'Punctuation'
95
+ rule /\)/, 'Punctuation', :case_stanza
96
+ mixin :root
97
+ end
98
+
99
+ state :case_stanza do
100
+ rule /;;/, 'Punctuation', :pop!
101
+ mixin :root
102
+ end
103
+
104
+ state :backticks do
89
105
  rule /`/, 'Literal.String.Backtick', :pop!
90
106
  mixin :root
91
107
  end
92
108
 
93
- lexer :interp do
109
+ state :interp do
110
+ rule /\\$/, 'Literal.String.Escape' # line continuation
111
+ rule /\\./, 'Literal.String.Escape'
94
112
  rule /\$\(\(/, 'Keyword', :math
95
113
  rule /\$\(/, 'Keyword', :paren
96
114
  rule /\${#?/, 'Keyword', :curly
@@ -98,12 +116,10 @@ module Rouge
98
116
  rule /\$#?(\w+|.)/, 'Name.Variable'
99
117
  end
100
118
 
101
- lexer :root do
119
+ state :root do
102
120
  mixin :basic
103
121
  mixin :data
104
122
  end
105
-
106
- mixin :root
107
123
  end
108
124
  end
109
125
  end