rugments 1.0.0.beta1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/LICENSE +52 -0
- data/README.md +195 -0
- data/bin/rugmentize +6 -0
- data/lib/rugments/cli.rb +357 -0
- data/lib/rugments/formatter.rb +29 -0
- data/lib/rugments/formatters/html.rb +142 -0
- data/lib/rugments/formatters/null.rb +17 -0
- data/lib/rugments/formatters/terminal256.rb +174 -0
- data/lib/rugments/lexer.rb +431 -0
- data/lib/rugments/lexers/apache/keywords.yml +453 -0
- data/lib/rugments/lexers/apache.rb +67 -0
- data/lib/rugments/lexers/apple_script.rb +366 -0
- data/lib/rugments/lexers/c.rb +210 -0
- data/lib/rugments/lexers/clojure.rb +109 -0
- data/lib/rugments/lexers/coffeescript.rb +172 -0
- data/lib/rugments/lexers/common_lisp.rb +343 -0
- data/lib/rugments/lexers/conf.rb +22 -0
- data/lib/rugments/lexers/cpp.rb +63 -0
- data/lib/rugments/lexers/csharp.rb +85 -0
- data/lib/rugments/lexers/css.rb +269 -0
- data/lib/rugments/lexers/dart.rb +102 -0
- data/lib/rugments/lexers/diff.rb +39 -0
- data/lib/rugments/lexers/elixir.rb +105 -0
- data/lib/rugments/lexers/erb.rb +54 -0
- data/lib/rugments/lexers/erlang.rb +116 -0
- data/lib/rugments/lexers/factor.rb +300 -0
- data/lib/rugments/lexers/gherkin/keywords.rb +13 -0
- data/lib/rugments/lexers/gherkin.rb +135 -0
- data/lib/rugments/lexers/go.rb +176 -0
- data/lib/rugments/lexers/groovy.rb +102 -0
- data/lib/rugments/lexers/haml.rb +226 -0
- data/lib/rugments/lexers/handlebars.rb +77 -0
- data/lib/rugments/lexers/haskell.rb +181 -0
- data/lib/rugments/lexers/html.rb +92 -0
- data/lib/rugments/lexers/http.rb +78 -0
- data/lib/rugments/lexers/ini.rb +55 -0
- data/lib/rugments/lexers/io.rb +66 -0
- data/lib/rugments/lexers/java.rb +74 -0
- data/lib/rugments/lexers/javascript.rb +258 -0
- data/lib/rugments/lexers/literate_coffeescript.rb +31 -0
- data/lib/rugments/lexers/literate_haskell.rb +34 -0
- data/lib/rugments/lexers/llvm.rb +82 -0
- data/lib/rugments/lexers/lua/builtins.rb +21 -0
- data/lib/rugments/lexers/lua.rb +120 -0
- data/lib/rugments/lexers/make.rb +114 -0
- data/lib/rugments/lexers/markdown.rb +151 -0
- data/lib/rugments/lexers/matlab/builtins.rb +10 -0
- data/lib/rugments/lexers/matlab.rb +70 -0
- data/lib/rugments/lexers/moonscript.rb +108 -0
- data/lib/rugments/lexers/nginx.rb +69 -0
- data/lib/rugments/lexers/nim.rb +149 -0
- data/lib/rugments/lexers/objective_c.rb +188 -0
- data/lib/rugments/lexers/ocaml.rb +109 -0
- data/lib/rugments/lexers/perl.rb +195 -0
- data/lib/rugments/lexers/php/builtins.rb +192 -0
- data/lib/rugments/lexers/php.rb +162 -0
- data/lib/rugments/lexers/plain_text.rb +23 -0
- data/lib/rugments/lexers/prolog.rb +62 -0
- data/lib/rugments/lexers/properties.rb +53 -0
- data/lib/rugments/lexers/puppet.rb +126 -0
- data/lib/rugments/lexers/python.rb +225 -0
- data/lib/rugments/lexers/qml.rb +70 -0
- data/lib/rugments/lexers/r.rb +55 -0
- data/lib/rugments/lexers/racket.rb +540 -0
- data/lib/rugments/lexers/ruby.rb +413 -0
- data/lib/rugments/lexers/rust.rb +188 -0
- data/lib/rugments/lexers/sass/common.rb +172 -0
- data/lib/rugments/lexers/sass.rb +72 -0
- data/lib/rugments/lexers/scala.rb +140 -0
- data/lib/rugments/lexers/scheme.rb +109 -0
- data/lib/rugments/lexers/scss.rb +32 -0
- data/lib/rugments/lexers/sed.rb +167 -0
- data/lib/rugments/lexers/shell.rb +150 -0
- data/lib/rugments/lexers/slim.rb +222 -0
- data/lib/rugments/lexers/smalltalk.rb +114 -0
- data/lib/rugments/lexers/sml.rb +345 -0
- data/lib/rugments/lexers/sql.rb +138 -0
- data/lib/rugments/lexers/swift.rb +153 -0
- data/lib/rugments/lexers/tcl.rb +189 -0
- data/lib/rugments/lexers/tex.rb +70 -0
- data/lib/rugments/lexers/toml.rb +68 -0
- data/lib/rugments/lexers/vb.rb +162 -0
- data/lib/rugments/lexers/viml/keywords.rb +11 -0
- data/lib/rugments/lexers/viml.rb +99 -0
- data/lib/rugments/lexers/xml.rb +57 -0
- data/lib/rugments/lexers/yaml.rb +362 -0
- data/lib/rugments/plugins/redcarpet.rb +28 -0
- data/lib/rugments/regex_lexer.rb +432 -0
- data/lib/rugments/template_lexer.rb +23 -0
- data/lib/rugments/text_analyzer.rb +46 -0
- data/lib/rugments/theme.rb +202 -0
- data/lib/rugments/themes/base16.rb +128 -0
- data/lib/rugments/themes/colorful.rb +65 -0
- data/lib/rugments/themes/github.rb +69 -0
- data/lib/rugments/themes/monokai.rb +88 -0
- data/lib/rugments/themes/monokai_sublime.rb +89 -0
- data/lib/rugments/themes/thankful_eyes.rb +69 -0
- data/lib/rugments/token.rb +180 -0
- data/lib/rugments/util.rb +99 -0
- data/lib/rugments/version.rb +3 -0
- data/lib/rugments.rb +33 -0
- metadata +149 -0
@@ -0,0 +1,362 @@
|
|
1
|
+
module Rugments
|
2
|
+
module Lexers
|
3
|
+
class YAML < RegexLexer
|
4
|
+
title 'YAML'
|
5
|
+
desc "Yaml Ain't Markup Language (yaml.org)"
|
6
|
+
mimetypes 'text/x-yaml'
|
7
|
+
tag 'yaml'
|
8
|
+
aliases 'yml'
|
9
|
+
|
10
|
+
def self.analyze_text(text)
|
11
|
+
# look for the %YAML directive
|
12
|
+
return 1 if text =~ /\A\s*%YAML/m
|
13
|
+
end
|
14
|
+
|
15
|
+
filenames '*.yaml', '*.yml'
|
16
|
+
# NB: Tabs are forbidden in YAML, which is why you see things
|
17
|
+
# like /[ ]+/.
|
18
|
+
|
19
|
+
# reset the indentation levels
|
20
|
+
def reset_indent
|
21
|
+
puts ' yaml: reset_indent' if @debug
|
22
|
+
@indent_stack = [0]
|
23
|
+
@next_indent = 0
|
24
|
+
@block_scalar_indent = nil
|
25
|
+
end
|
26
|
+
|
27
|
+
def indent
|
28
|
+
fail 'empty indent stack!' if @indent_stack.empty?
|
29
|
+
@indent_stack.last
|
30
|
+
end
|
31
|
+
|
32
|
+
def dedent?(level)
|
33
|
+
level < indent
|
34
|
+
end
|
35
|
+
|
36
|
+
def indent?(level)
|
37
|
+
level > indent
|
38
|
+
end
|
39
|
+
|
40
|
+
# Save a possible indentation level
|
41
|
+
def save_indent(match)
|
42
|
+
@next_indent = match.size
|
43
|
+
puts " yaml: indent: #{indent}/#{@next_indent}" if @debug
|
44
|
+
puts " yaml: popping indent stack - before: #{@indent_stack}" if @debug
|
45
|
+
if dedent?(@next_indent)
|
46
|
+
@indent_stack.pop while dedent?(@next_indent)
|
47
|
+
puts " yaml: popping indent stack - after: #{@indent_stack}" if @debug
|
48
|
+
puts " yaml: indent: #{indent}/#{@next_indent}" if @debug
|
49
|
+
|
50
|
+
# dedenting to a state not previously indented to is an error
|
51
|
+
[match[0...indent], match[indent..-1]]
|
52
|
+
else
|
53
|
+
[match, '']
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
def continue_indent(match)
|
58
|
+
puts ' yaml: continue_indent' if @debug
|
59
|
+
@next_indent += match.size
|
60
|
+
end
|
61
|
+
|
62
|
+
def set_indent(match, opts = {})
|
63
|
+
if indent < @next_indent
|
64
|
+
@indent_stack << @next_indent
|
65
|
+
end
|
66
|
+
|
67
|
+
@next_indent += match.size unless opts[:implicit]
|
68
|
+
end
|
69
|
+
|
70
|
+
plain_scalar_start = /[^ \t\n\r\f\v?:,\[\]{}#&*!\|>'"%@`]/
|
71
|
+
|
72
|
+
start { reset_indent }
|
73
|
+
|
74
|
+
state :basic do
|
75
|
+
rule /#.*$/, Comment::Single
|
76
|
+
end
|
77
|
+
|
78
|
+
state :root do
|
79
|
+
mixin :basic
|
80
|
+
|
81
|
+
rule /\n+/, Text
|
82
|
+
|
83
|
+
# trailing or pre-comment whitespace
|
84
|
+
rule /[ ]+(?=#|$)/, Text
|
85
|
+
|
86
|
+
rule /^%YAML\b/ do
|
87
|
+
token Name::Tag
|
88
|
+
reset_indent
|
89
|
+
push :yaml_directive
|
90
|
+
end
|
91
|
+
|
92
|
+
rule /^%TAG\b/ do
|
93
|
+
token Name::Tag
|
94
|
+
reset_indent
|
95
|
+
push :tag_directive
|
96
|
+
end
|
97
|
+
|
98
|
+
# doc-start and doc-end indicators
|
99
|
+
rule /^(?:---|\.\.\.)(?= |$)/ do
|
100
|
+
token Name::Namespace
|
101
|
+
reset_indent
|
102
|
+
push :block_line
|
103
|
+
end
|
104
|
+
|
105
|
+
# indentation spaces
|
106
|
+
rule /[ ]*(?!\s|$)/ do |m|
|
107
|
+
text, err = save_indent(m[0])
|
108
|
+
token Text, text
|
109
|
+
token Error, err
|
110
|
+
push :block_line; push :indentation
|
111
|
+
end
|
112
|
+
end
|
113
|
+
|
114
|
+
state :indentation do
|
115
|
+
rule(/\s*?\n/) { token Text; pop! 2 }
|
116
|
+
# whitespace preceding block collection indicators
|
117
|
+
rule /[ ]+(?=[-:?](?:[ ]|$))/ do |m|
|
118
|
+
token Text
|
119
|
+
continue_indent(m[0])
|
120
|
+
end
|
121
|
+
|
122
|
+
# block collection indicators
|
123
|
+
rule(/[?:-](?=[ ]|$)/) { |m| token Punctuation::Indicator; set_indent m[0] }
|
124
|
+
|
125
|
+
# the beginning of a block line
|
126
|
+
rule(/[ ]*/) { |m| token Text; continue_indent(m[0]); pop! }
|
127
|
+
end
|
128
|
+
|
129
|
+
# indented line in the block context
|
130
|
+
state :block_line do
|
131
|
+
# line end
|
132
|
+
rule /[ ]*(?=#|$)/, Text, :pop!
|
133
|
+
rule /[ ]+/, Text
|
134
|
+
# tags, anchors, and aliases
|
135
|
+
mixin :descriptors
|
136
|
+
# block collections and scalars
|
137
|
+
mixin :block_nodes
|
138
|
+
# flow collections and quoed scalars
|
139
|
+
mixin :flow_nodes
|
140
|
+
|
141
|
+
# a plain scalar
|
142
|
+
rule /(?=#{plain_scalar_start}|[?:-][^ \t\n\r\f\v])/ do
|
143
|
+
token Name::Variable
|
144
|
+
push :plain_scalar_in_block_context
|
145
|
+
end
|
146
|
+
end
|
147
|
+
|
148
|
+
state :descriptors do
|
149
|
+
# a full-form tag
|
150
|
+
rule /!<[0-9A-Za-z;\/?:@&=+$,_.!~*'()\[\]%-]+>/, Keyword::Type
|
151
|
+
|
152
|
+
# a tag in the form '!', '!suffix' or '!handle!suffix'
|
153
|
+
rule %r{
|
154
|
+
(?:![\w-]+)? # handle
|
155
|
+
!(?:[\w;/?:@&=+$,.!~*\'()\[\]%-]*) # suffix
|
156
|
+
}x, Keyword::Type
|
157
|
+
|
158
|
+
# an anchor
|
159
|
+
rule /&[\w-]+/, Name::Label
|
160
|
+
|
161
|
+
# an alias
|
162
|
+
rule /\*[\w-]+/, Name::Variable
|
163
|
+
end
|
164
|
+
|
165
|
+
state :block_nodes do
|
166
|
+
# implicit key
|
167
|
+
rule /:(?=\s|$)/ do |m|
|
168
|
+
token Punctuation::Indicator
|
169
|
+
set_indent m[0], implicit: true
|
170
|
+
end
|
171
|
+
|
172
|
+
# literal and folded scalars
|
173
|
+
rule /[\|>]/ do
|
174
|
+
token Punctuation::Indicator
|
175
|
+
push :block_scalar_content
|
176
|
+
push :block_scalar_header
|
177
|
+
end
|
178
|
+
end
|
179
|
+
|
180
|
+
state :flow_nodes do
|
181
|
+
rule /\[/, Punctuation::Indicator, :flow_sequence
|
182
|
+
rule /\{/, Punctuation::Indicator, :flow_mapping
|
183
|
+
rule /'/, Str::Single, :single_quoted_scalar
|
184
|
+
rule /"/, Str::Double, :double_quoted_scalar
|
185
|
+
end
|
186
|
+
|
187
|
+
state :flow_collection do
|
188
|
+
rule /\s+/m, Text
|
189
|
+
mixin :basic
|
190
|
+
rule /[?:,]/, Punctuation::Indicator
|
191
|
+
mixin :descriptors
|
192
|
+
mixin :flow_nodes
|
193
|
+
|
194
|
+
rule /(?=#{plain_scalar_start})/ do
|
195
|
+
push :plain_scalar_in_flow_context
|
196
|
+
end
|
197
|
+
end
|
198
|
+
|
199
|
+
state :flow_sequence do
|
200
|
+
rule /\]/, Punctuation::Indicator, :pop!
|
201
|
+
mixin :flow_collection
|
202
|
+
end
|
203
|
+
|
204
|
+
state :flow_mapping do
|
205
|
+
rule /\}/, Punctuation::Indicator, :pop!
|
206
|
+
mixin :flow_collection
|
207
|
+
end
|
208
|
+
|
209
|
+
state :block_scalar_content do
|
210
|
+
rule /\n+/, Text
|
211
|
+
|
212
|
+
# empty lines never dedent, but they might be part of the scalar.
|
213
|
+
rule /^[ ]+$/ do |m|
|
214
|
+
text = m[0]
|
215
|
+
indent_size = text.size
|
216
|
+
|
217
|
+
indent_mark = @block_scalar_indent || indent_size
|
218
|
+
|
219
|
+
token Text, text[0...indent_mark]
|
220
|
+
token Name::Constant, text[indent_mark..-1]
|
221
|
+
end
|
222
|
+
|
223
|
+
# TODO: ^ doesn't actually seem to affect the match at all.
|
224
|
+
# Find a way to work around this limitation.
|
225
|
+
rule /^[ ]*/ do |m|
|
226
|
+
token Text
|
227
|
+
|
228
|
+
indent_size = m[0].size
|
229
|
+
|
230
|
+
dedent_level = @block_scalar_indent || indent
|
231
|
+
@block_scalar_indent ||= indent_size
|
232
|
+
|
233
|
+
if indent_size < dedent_level
|
234
|
+
pop! 2
|
235
|
+
end
|
236
|
+
end
|
237
|
+
|
238
|
+
rule /[^\n\r\f\v]+/, Name::Constant
|
239
|
+
end
|
240
|
+
|
241
|
+
state :block_scalar_header do
|
242
|
+
# optional indentation indicator and chomping flag, in either order
|
243
|
+
rule %r{
|
244
|
+
(
|
245
|
+
([1-9])[+-]? | [+-]?([1-9])?
|
246
|
+
)(?=[ ]|$)
|
247
|
+
}x do |m|
|
248
|
+
@block_scalar_indent = nil
|
249
|
+
goto :ignored_line
|
250
|
+
next if m[0].empty?
|
251
|
+
|
252
|
+
increment = m[1] || m[2]
|
253
|
+
if increment
|
254
|
+
@block_scalar_indent = indent + increment.to_i
|
255
|
+
end
|
256
|
+
|
257
|
+
token Punctuation::Indicator
|
258
|
+
end
|
259
|
+
end
|
260
|
+
|
261
|
+
state :ignored_line do
|
262
|
+
mixin :basic
|
263
|
+
rule /[ ]+/, Text
|
264
|
+
rule /\n/, Text, :pop!
|
265
|
+
end
|
266
|
+
|
267
|
+
state :quoted_scalar_whitespaces do
|
268
|
+
# leading and trailing whitespace is ignored
|
269
|
+
rule /^[ ]+/, Text
|
270
|
+
rule /[ ]+$/, Text
|
271
|
+
|
272
|
+
rule /\n+/m, Text
|
273
|
+
|
274
|
+
rule /[ ]+/, Name::Variable
|
275
|
+
end
|
276
|
+
|
277
|
+
state :single_quoted_scalar do
|
278
|
+
mixin :quoted_scalar_whitespaces
|
279
|
+
rule /\\'/, Str::Escape
|
280
|
+
rule /'/, Str, :pop!
|
281
|
+
rule /[^\s']+/, Str
|
282
|
+
end
|
283
|
+
|
284
|
+
state :double_quoted_scalar do
|
285
|
+
rule /"/, Str, :pop!
|
286
|
+
mixin :quoted_scalar_whitespaces
|
287
|
+
# escapes
|
288
|
+
rule /\\[0abt\tn\nvfre "\\N_LP]/, Str::Escape
|
289
|
+
rule /\\(?:x[0-9A-Fa-f]{2}|u[0-9A-Fa-f]{4}|U[0-9A-Fa-f]{8})/,
|
290
|
+
Str::Escape
|
291
|
+
rule /[^ \t\n\r\f\v"\\]+/, Str
|
292
|
+
end
|
293
|
+
|
294
|
+
state :plain_scalar_in_block_context_new_line do
|
295
|
+
rule /^[ ]+\n/, Text
|
296
|
+
rule /\n+/m, Text
|
297
|
+
rule /^(?=---|\.\.\.)/ do
|
298
|
+
pop! 3
|
299
|
+
end
|
300
|
+
|
301
|
+
# dedent detection
|
302
|
+
rule /^[ ]*/ do |m|
|
303
|
+
token Text
|
304
|
+
pop!
|
305
|
+
|
306
|
+
indent_size = m[0].size
|
307
|
+
|
308
|
+
# dedent = end of scalar
|
309
|
+
if indent_size <= indent
|
310
|
+
pop!
|
311
|
+
save_indent(m[0])
|
312
|
+
push :indentation
|
313
|
+
end
|
314
|
+
end
|
315
|
+
end
|
316
|
+
|
317
|
+
state :plain_scalar_in_block_context do
|
318
|
+
# the : indicator ends a scalar
|
319
|
+
rule /[ ]*(?=:[ \n]|:$)/, Text, :pop!
|
320
|
+
rule /[ ]*:/, Str
|
321
|
+
rule /[ ]+(?=#)/, Text, :pop!
|
322
|
+
rule /[ ]+$/, Text
|
323
|
+
# check for new documents or dedents at the new line
|
324
|
+
rule /\n+/ do
|
325
|
+
token Text
|
326
|
+
push :plain_scalar_in_block_context_new_line
|
327
|
+
end
|
328
|
+
|
329
|
+
rule /[ ]+/, Str
|
330
|
+
# regular non-whitespace characters
|
331
|
+
rule /[^\s:]+/, Str
|
332
|
+
end
|
333
|
+
|
334
|
+
state :plain_scalar_in_flow_context do
|
335
|
+
rule /[ ]*(?=[,:?\[\]{}])/, Text, :pop!
|
336
|
+
rule /[ ]+(?=#)/, Text, :pop!
|
337
|
+
rule /^[ ]+/, Text
|
338
|
+
rule /[ ]+$/, Text
|
339
|
+
rule /\n+/, Text
|
340
|
+
rule /[ ]+/, Name::Variable
|
341
|
+
rule /[^\s,:?\[\]{}]+/, Name::Variable
|
342
|
+
end
|
343
|
+
|
344
|
+
state :yaml_directive do
|
345
|
+
rule /([ ]+)(\d+\.\d+)/ do
|
346
|
+
groups Text, Num
|
347
|
+
goto :ignored_line
|
348
|
+
end
|
349
|
+
end
|
350
|
+
|
351
|
+
state :tag_directive do
|
352
|
+
rule %r{
|
353
|
+
([ ]+)(!|![\w-]*!) # prefix
|
354
|
+
([ ]+)(!|!?[\w;/?:@&=+$,.!~*'()\[\]%-]+) # tag handle
|
355
|
+
}x do
|
356
|
+
groups Text, Keyword::Type, Text, Keyword::Type
|
357
|
+
goto :ignored_line
|
358
|
+
end
|
359
|
+
end
|
360
|
+
end
|
361
|
+
end
|
362
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
# this file is not require'd from the root. To use this plugin, run:
|
2
|
+
#
|
3
|
+
# require 'rouge/plugins/redcarpet'
|
4
|
+
|
5
|
+
module Rugments
|
6
|
+
module Plugins
|
7
|
+
module Redcarpet
|
8
|
+
def block_code(code, language)
|
9
|
+
lexer = Lexer.find_fancy(language, code) || Lexers::PlainText
|
10
|
+
|
11
|
+
# XXX HACK: Redcarpet strips hard tabs out of code blocks,
|
12
|
+
# so we assume you're not using leading spaces that aren't tabs,
|
13
|
+
# and just replace them here.
|
14
|
+
if lexer.tag == 'make'
|
15
|
+
code.gsub! /^ /, "\t"
|
16
|
+
end
|
17
|
+
|
18
|
+
formatter = rouge_formatter(lexer)
|
19
|
+
formatter.format(lexer.lex(code))
|
20
|
+
end
|
21
|
+
|
22
|
+
# override this method for custom formatting behavior
|
23
|
+
def rouge_formatter(lexer)
|
24
|
+
Formatters::HTML.new(css_class: "highlight #{lexer.tag}")
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|