rouge 0.4.0 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (69) hide show
  1. data/Gemfile +2 -0
  2. data/bin/rougify +1 -1
  3. data/lib/rouge/demos/{text → plaintext} +0 -0
  4. data/lib/rouge/formatters/html.rb +13 -8
  5. data/lib/rouge/formatters/terminal256.rb +15 -11
  6. data/lib/rouge/lexer.rb +3 -1
  7. data/lib/rouge/lexers/c.rb +65 -55
  8. data/lib/rouge/lexers/clojure.rb +19 -19
  9. data/lib/rouge/lexers/coffeescript.rb +42 -42
  10. data/lib/rouge/lexers/common_lisp.rb +49 -48
  11. data/lib/rouge/lexers/conf.rb +6 -6
  12. data/lib/rouge/lexers/cpp.rb +48 -48
  13. data/lib/rouge/lexers/csharp.rb +25 -25
  14. data/lib/rouge/lexers/css.rb +32 -33
  15. data/lib/rouge/lexers/diff.rb +8 -8
  16. data/lib/rouge/lexers/elixir.rb +30 -30
  17. data/lib/rouge/lexers/erb.rb +5 -5
  18. data/lib/rouge/lexers/erlang.rb +38 -38
  19. data/lib/rouge/lexers/factor.rb +56 -56
  20. data/lib/rouge/lexers/gherkin.rb +27 -27
  21. data/lib/rouge/lexers/go.rb +21 -21
  22. data/lib/rouge/lexers/groovy.rb +32 -32
  23. data/lib/rouge/lexers/haml.rb +35 -35
  24. data/lib/rouge/lexers/handlebars.rb +27 -27
  25. data/lib/rouge/lexers/haskell.rb +78 -79
  26. data/lib/rouge/lexers/html.rb +28 -28
  27. data/lib/rouge/lexers/http.rb +13 -13
  28. data/lib/rouge/lexers/ini.rb +13 -13
  29. data/lib/rouge/lexers/io.rb +19 -19
  30. data/lib/rouge/lexers/java.rb +29 -29
  31. data/lib/rouge/lexers/javascript.rb +49 -52
  32. data/lib/rouge/lexers/literate_haskell.rb +5 -5
  33. data/lib/rouge/lexers/llvm.rb +14 -14
  34. data/lib/rouge/lexers/lua.rb +33 -33
  35. data/lib/rouge/lexers/make.rb +26 -26
  36. data/lib/rouge/lexers/markdown.rb +49 -49
  37. data/lib/rouge/lexers/nginx.rb +19 -19
  38. data/lib/rouge/lexers/perl.rb +53 -54
  39. data/lib/rouge/lexers/php.rb +42 -43
  40. data/lib/rouge/lexers/{text.rb → plain_text.rb} +3 -2
  41. data/lib/rouge/lexers/prolog.rb +19 -19
  42. data/lib/rouge/lexers/puppet.rb +40 -40
  43. data/lib/rouge/lexers/python.rb +63 -63
  44. data/lib/rouge/lexers/r.rb +15 -15
  45. data/lib/rouge/lexers/racket.rb +29 -29
  46. data/lib/rouge/lexers/ruby.rb +94 -97
  47. data/lib/rouge/lexers/rust.rb +37 -37
  48. data/lib/rouge/lexers/sass.rb +15 -15
  49. data/lib/rouge/lexers/sass/common.rb +144 -136
  50. data/lib/rouge/lexers/scheme.rb +25 -25
  51. data/lib/rouge/lexers/scss.rb +7 -7
  52. data/lib/rouge/lexers/sed.rb +39 -39
  53. data/lib/rouge/lexers/shell.rb +45 -45
  54. data/lib/rouge/lexers/smalltalk.rb +36 -36
  55. data/lib/rouge/lexers/sql.rb +27 -27
  56. data/lib/rouge/lexers/tcl.rb +34 -34
  57. data/lib/rouge/lexers/tex.rb +24 -24
  58. data/lib/rouge/lexers/toml.rb +18 -18
  59. data/lib/rouge/lexers/viml.rb +19 -19
  60. data/lib/rouge/lexers/xml.rb +16 -16
  61. data/lib/rouge/lexers/yaml.rb +78 -81
  62. data/lib/rouge/plugins/redcarpet.rb +1 -1
  63. data/lib/rouge/regex_lexer.rb +31 -14
  64. data/lib/rouge/template_lexer.rb +1 -1
  65. data/lib/rouge/theme.rb +12 -10
  66. data/lib/rouge/themes/thankful_eyes.rb +43 -43
  67. data/lib/rouge/token.rb +145 -148
  68. data/lib/rouge/version.rb +1 -1
  69. metadata +4 -4
@@ -9,7 +9,7 @@ module Rouge
9
9
  def parent
10
10
  return @parent if instance_variable_defined? :@parent
11
11
  @parent = option(:parent) || 'html'
12
- if @parent.is_a? String
12
+ if @parent.is_a? ::String
13
13
  lexer_class = Lexer.find(@parent)
14
14
  @parent = lexer_class.new(self.options)
15
15
  end
data/lib/rouge/theme.rb CHANGED
@@ -1,5 +1,7 @@
1
1
  module Rouge
2
2
  class Theme
3
+ include Token::Tokens
4
+
3
5
  class Style < Hash
4
6
  def initialize(theme, hsh={})
5
7
  super()
@@ -75,18 +77,20 @@ module Rouge
75
77
  style = Style.new(self, style)
76
78
 
77
79
  tokens.each do |tok|
78
- styles[tok.to_s] = style
80
+ styles[tok] = style
79
81
  end
80
82
  end
81
83
 
82
84
  def get_own_style(token)
83
- token.ancestors do |anc|
84
- return styles[anc.name] if styles[anc.name]
85
+ token.token_chain.each do |anc|
86
+ return styles[anc] if styles[anc]
85
87
  end
88
+
89
+ nil
86
90
  end
87
91
 
88
92
  def get_style(token)
89
- get_own_style(token) || style['Text']
93
+ get_own_style(token) || styles[Token::Tokens::Text]
90
94
  end
91
95
 
92
96
  def name(n=nil)
@@ -140,15 +144,13 @@ module Rouge
140
144
  yield "#{@scope} table td { padding: 5px; }"
141
145
  yield "#{@scope} table .gutter { text-align: right; }"
142
146
 
143
- styles.each do |tokname, style|
144
- style.render(css_selector(Token[tokname]), &b)
147
+ styles.each do |tok, style|
148
+ style.render(css_selector(tok), &b)
145
149
  end
146
150
  end
147
151
 
148
152
  def style_for(tok)
149
- styles.fetch(tok.name) do
150
- tok.parent ? style_for(tok.parent) : Style.new(self)
151
- end
153
+ self.class.get_style(tok)
152
154
  end
153
155
 
154
156
  private
@@ -175,7 +177,7 @@ module Rouge
175
177
 
176
178
  yield tok
177
179
  tok.sub_tokens.each do |(_, st)|
178
- next if styles[st.name]
180
+ next if styles[st]
179
181
 
180
182
  inflate_token(st, &b)
181
183
  end
@@ -20,50 +20,50 @@ module Rouge
20
20
  palette :backlit => '#4df4ff'
21
21
  palette :schrill => '#ffb000'
22
22
 
23
- style 'Text', :fg => :unicorn, :bg => :krasna
24
- style 'Generic.Lineno', :fg => :eggshell_cloud, :bg => :slate_blue
23
+ style Text, :fg => :unicorn, :bg => :krasna
24
+ style Generic::Lineno, :fg => :eggshell_cloud, :bg => :slate_blue
25
25
 
26
- style 'Comment', :fg => :cool_as_ice, :italic => true
27
- style 'Comment.Preproc', :fg => :go_get_it, :bold => true, :italic => true
28
- style 'Error',
29
- 'Generic.Error', :fg => :aluminum1, :bg => :scarletred2
30
- style 'Keyword', :fg => :sandy, :bold => true
31
- style 'Operator',
32
- 'Punctuation', :fg => :backlit
33
- style 'Generic.Deleted', :fg => :scarletred2
34
- style 'Generic.Inserted', :fg => :go_get_it
35
- style 'Generic.Emph', :italic => true
36
- style 'Generic.Strong', :bold => true
37
- style 'Generic.Traceback', :fg => :eggshell_cloud, :bg => :slate_blue
38
- style 'Keyword.Constant', :fg => :pink_merengue, :bold => true
39
- style 'Keyword.Namespace',
40
- 'Keyword.Pseudo',
41
- 'Keyword.Reserved',
42
- 'Generic.Heading',
43
- 'Generic.Subheading', :fg => :schrill, :bold => true
44
- style 'Keyword.Type',
45
- 'Name.Constant',
46
- 'Name.Class',
47
- 'Name.Decorator',
48
- 'Name.Namespace',
49
- 'Name.Builtin.Pseudo',
50
- 'Name.Exception', :fg => :go_get_it, :bold => true
51
- style 'Name.Label',
52
- 'Name.Tag', :fg => :schrill, :bold => true
53
- style 'Literal.Number',
54
- 'Literal.Date',
55
- 'Literal.String.Symbol', :fg => :pink_merengue, :bold => true
56
- style 'Literal.String', :fg => :dune, :bold => true
57
- style 'Literal.String.Escape',
58
- 'Literal.String.Char',
59
- 'Literal.String.Interpol', :fg => :backlit, :bold => true
60
- style 'Name.Builtin', :bold => true
61
- style 'Name.Entity', :fg => '#999999', :bold => true
62
- style 'Text.Whitespace', :fg => '#BBBBBB'
63
- style 'Name.Function',
64
- 'Name.Property',
65
- 'Name.Attribute', :fg => :chilly
66
- style 'Name.Variable', :fg => :chilly, :bold => true
26
+ style Comment, :fg => :cool_as_ice, :italic => true
27
+ style Comment::Preproc, :fg => :go_get_it, :bold => true, :italic => true
28
+ style Error,
29
+ Generic::Error, :fg => :aluminum1, :bg => :scarletred2
30
+ style Keyword, :fg => :sandy, :bold => true
31
+ style Operator,
32
+ Punctuation, :fg => :backlit
33
+ style Generic::Deleted, :fg => :scarletred2
34
+ style Generic::Inserted, :fg => :go_get_it
35
+ style Generic::Emph, :italic => true
36
+ style Generic::Strong, :bold => true
37
+ style Generic::Traceback, :fg => :eggshell_cloud, :bg => :slate_blue
38
+ style Keyword::Constant, :fg => :pink_merengue, :bold => true
39
+ style Keyword::Namespace,
40
+ Keyword::Pseudo,
41
+ Keyword::Reserved,
42
+ Generic::Heading,
43
+ Generic::Subheading, :fg => :schrill, :bold => true
44
+ style Keyword::Type,
45
+ Name::Constant,
46
+ Name::Class,
47
+ Name::Decorator,
48
+ Name::Namespace,
49
+ Name::Builtin::Pseudo,
50
+ Name::Exception, :fg => :go_get_it, :bold => true
51
+ style Name::Label,
52
+ Name::Tag, :fg => :schrill, :bold => true
53
+ style Literal::Number,
54
+ Literal::Date,
55
+ Literal::String::Symbol, :fg => :pink_merengue, :bold => true
56
+ style Literal::String, :fg => :dune, :bold => true
57
+ style Literal::String::Escape,
58
+ Literal::String::Char,
59
+ Literal::String::Interpol, :fg => :backlit, :bold => true
60
+ style Name::Builtin, :bold => true
61
+ style Name::Entity, :fg => '#999999', :bold => true
62
+ style Text::Whitespace, :fg => '#BBBBBB'
63
+ style Name::Function,
64
+ Name::Property,
65
+ Name::Attribute, :fg => :chilly
66
+ style Name::Variable, :fg => :chilly, :bold => true
67
67
  end
68
68
  end
69
69
  end
data/lib/rouge/token.rb CHANGED
@@ -1,183 +1,180 @@
1
1
  module Rouge
2
2
  class Token
3
- attr_reader :name
4
- attr_reader :parent
5
- attr_accessor :shortname
6
- alias to_s name
3
+ class << self
4
+ def name; @name; end
5
+ def parent; @parent; end
6
+ def shortname; @shortname; end
7
7
 
8
- def make_single(name)
9
- name = name.to_s
10
- new_name = [self.name, name].compact.join('.')
8
+ def cache
9
+ @cache ||= {}
10
+ end
11
11
 
12
- new_token = self.clone
13
- parent = self
14
- new_token.instance_eval do
15
- @name = new_name
16
- @parent = parent
17
- @sub_tokens = {}
12
+ def sub_tokens
13
+ @sub_tokens ||= {}
18
14
  end
19
15
 
20
- sub_tokens[name] = new_token
16
+ def [](qualname)
17
+ return qualname unless qualname.is_a? ::String
21
18
 
22
- new_token
23
- end
19
+ Token.cache[qualname]
20
+ end
24
21
 
25
- def make(name, shortname=nil)
26
- names = name.split('.')
27
- names.inject(self) do |tok, name|
28
- tok.make_single(name)
22
+ def inspect
23
+ "<Token #{qualname}>"
29
24
  end
30
- end
31
25
 
32
- def [](name)
33
- name = name.to_s
26
+ def matches?(other)
27
+ other.token_chain.include? self
28
+ end
34
29
 
35
- name.split('.').inject(self) do |tok, name|
36
- tok.sub_tokens[name] || tok.make_single(name)
30
+ def token_chain
31
+ @token_chain ||= ancestors.take_while { |x| x != Token }.reverse
37
32
  end
38
- end
39
33
 
40
- def sub_tokens
41
- @sub_tokens ||= {}
42
- end
34
+ def qualname
35
+ @qualname ||= token_chain.map(&:name).join('.')
36
+ end
43
37
 
44
- def ancestors(&b)
45
- return enum_for(:ancestors) unless block_given?
38
+ def register!
39
+ Token.cache[self.qualname] = self
40
+ parent.sub_tokens[self.name] = self
41
+ end
46
42
 
47
- if parent
48
- yield self
49
- parent.ancestors(&b)
43
+ def make_token(name, shortname, &b)
44
+ parent = self
45
+ Class.new(parent) do
46
+ @parent = parent
47
+ @name = name
48
+ @shortname = shortname
49
+ register!
50
+ class_eval(&b) if b
51
+ end
50
52
  end
51
- end
52
53
 
53
- def ===(other)
54
- immediate = if self.class == other.class
55
- self == other
56
- else
57
- self.name == other
54
+ def token(name, shortname, &b)
55
+ tok = make_token(name, shortname, &b)
56
+ const_set(name, tok)
58
57
  end
59
58
 
60
- immediate || !!(other.parent && self === other.parent)
59
+ def each_token(&b)
60
+ Token.cache.each do |(_, t)|
61
+ b.call(t)
62
+ end
63
+ end
61
64
  end
62
65
 
63
- def inspect
64
- parts = [name.inspect]
65
- parts << shortname.inspect if shortname
66
- "#<Token #{parts.join(' ')}>"
67
- end
66
+ module Tokens
67
+ def self.token(name, shortname, &b)
68
+ tok = Token.make_token(name, shortname, &b)
69
+ const_set(name, tok)
70
+ end
68
71
 
69
- class << self
70
- def base
71
- @base ||= new
72
+ # XXX IMPORTANT XXX
73
+ # For compatibility, this list must be kept in sync with
74
+ # pygments.token.STANDARD_TYPES
75
+ # please see https://github.com/jayferd/rouge/wiki/List-of-tokens
76
+ token :Text, '' do
77
+ token :Whitespace, 'w'
72
78
  end
73
79
 
74
- def get(name)
75
- return name if name.is_a? Token
80
+ token :Error, 'err'
81
+ token :Other, 'x'
82
+
83
+ token :Keyword, 'k' do
84
+ token :Constant, 'kc'
85
+ token :Declaration, 'kd'
86
+ token :Namespace, 'kn'
87
+ token :Pseudo, 'kp'
88
+ token :Reserved, 'kr'
89
+ token :Type, 'kt'
90
+ token :Variable, 'kv'
91
+ end
76
92
 
77
- base[name]
93
+ token :Name, 'n' do
94
+ token :Attribute, 'na'
95
+ token :Builtin, 'nb' do
96
+ token :Pseudo, 'bp'
97
+ end
98
+ token :Class, 'nc'
99
+ token :Constant, 'no'
100
+ token :Decorator, 'nd'
101
+ token :Entity, 'ni'
102
+ token :Exception, 'ne'
103
+ token :Function, 'nf'
104
+ token :Property, 'py'
105
+ token :Label, 'nl'
106
+ token :Namespace, 'nn'
107
+ token :Other, 'nx'
108
+ token :Tag, 'nt'
109
+ token :Variable, 'nv' do
110
+ token :Class, 'vc'
111
+ token :Global, 'vg'
112
+ token :Instance, 'vi'
113
+ end
78
114
  end
79
115
 
80
- alias [] get
116
+ token :Literal, 'l' do
117
+ token :Date, 'ld'
118
+
119
+ token :String, 's' do
120
+ token :Backtick, 'sb'
121
+ token :Char, 'sc'
122
+ token :Doc, 'sd'
123
+ token :Double, 's2'
124
+ token :Escape, 'se'
125
+ token :Heredoc, 'sh'
126
+ token :Interpol, 'si'
127
+ token :Other, 'sx'
128
+ token :Regex, 'sr'
129
+ token :Single, 's1'
130
+ token :Symbol, 'ss'
131
+ end
81
132
 
82
- def token(name, shortname)
83
- tok = get(name)
84
- tok.shortname = shortname
85
- tok
133
+ token :Number, 'm' do
134
+ token :Float, 'mf'
135
+ token :Hex, 'mh'
136
+ token :Integer, 'mi' do
137
+ token :Long, 'il'
138
+ end
139
+ token :Oct, 'mo'
140
+ token :Bin, 'mb'
141
+ token :Other, 'mx'
142
+ end
86
143
  end
87
144
 
88
- def each_token(&b)
89
- recurse = proc do |token|
90
- b.call(token)
91
- token.sub_tokens.each_value(&recurse)
92
- end
145
+ token :Operator, 'o' do
146
+ token :Word, 'ow'
147
+ end
93
148
 
94
- base.sub_tokens.each_value(&recurse)
149
+ token :Punctuation, 'p' do
150
+ token :Indicator, 'pi'
151
+ end
152
+
153
+ token :Comment, 'c' do
154
+ token :Doc, 'cd'
155
+ token :Multiline, 'cm'
156
+ token :Preproc, 'cp'
157
+ token :Single, 'c1'
158
+ token :Special, 'cs'
159
+ end
160
+
161
+ token :Generic, 'g' do
162
+ token :Deleted, 'gd'
163
+ token :Emph, 'ge'
164
+ token :Error, 'gr'
165
+ token :Heading, 'gh'
166
+ token :Inserted, 'gi'
167
+ token :Output, 'go'
168
+ token :Prompt, 'gp'
169
+ token :Strong, 'gs'
170
+ token :Subheading, 'gu'
171
+ token :Traceback, 'gt'
172
+ token :Lineno, 'gl'
95
173
  end
96
- end
97
174
 
98
- # XXX IMPORTANT XXX
99
- # For compatibility, this list must be kept in sync with
100
- # pygments.token.STANDARD_TYPES
101
- # please see https://github.com/jayferd/rouge/wiki/List-of-tokens
102
- token 'Text', ''
103
- token 'Text.Whitespace', 'w'
104
- token 'Error', 'err'
105
- token 'Other', 'x'
106
-
107
- token 'Keyword', 'k'
108
- token 'Keyword.Constant', 'kc'
109
- token 'Keyword.Declaration', 'kd'
110
- token 'Keyword.Namespace', 'kn'
111
- token 'Keyword.Pseudo', 'kp'
112
- token 'Keyword.Reserved', 'kr'
113
- token 'Keyword.Type', 'kt'
114
-
115
- token 'Name', 'n'
116
- token 'Name.Attribute', 'na'
117
- token 'Name.Builtin', 'nb'
118
- token 'Name.Builtin.Pseudo', 'bp'
119
- token 'Name.Class', 'nc'
120
- token 'Name.Constant', 'no'
121
- token 'Name.Decorator', 'nd'
122
- token 'Name.Entity', 'ni'
123
- token 'Name.Exception', 'ne'
124
- token 'Name.Function', 'nf'
125
- token 'Name.Property', 'py'
126
- token 'Name.Label', 'nl'
127
- token 'Name.Namespace', 'nn'
128
- token 'Name.Other', 'nx'
129
- token 'Name.Tag', 'nt'
130
- token 'Name.Variable', 'nv'
131
- token 'Name.Variable.Class', 'vc'
132
- token 'Name.Variable.Global', 'vg'
133
- token 'Name.Variable.Instance', 'vi'
134
-
135
- token 'Literal', 'l'
136
- token 'Literal.Date', 'ld'
137
-
138
- token 'Literal.String', 's'
139
- token 'Literal.String.Backtick', 'sb'
140
- token 'Literal.String.Char', 'sc'
141
- token 'Literal.String.Doc', 'sd'
142
- token 'Literal.String.Double', 's2'
143
- token 'Literal.String.Escape', 'se'
144
- token 'Literal.String.Heredoc', 'sh'
145
- token 'Literal.String.Interpol', 'si'
146
- token 'Literal.String.Other', 'sx'
147
- token 'Literal.String.Regex', 'sr'
148
- token 'Literal.String.Single', 's1'
149
- token 'Literal.String.Symbol', 'ss'
150
-
151
- token 'Literal.Number', 'm'
152
- token 'Literal.Number.Float', 'mf'
153
- token 'Literal.Number.Hex', 'mh'
154
- token 'Literal.Number.Integer', 'mi'
155
- token 'Literal.Number.Integer.Long', 'il'
156
- token 'Literal.Number.Oct', 'mo'
157
-
158
- token 'Operator', 'o'
159
- token 'Operator.Word', 'ow'
160
-
161
- token 'Punctuation', 'p'
162
-
163
- token 'Comment', 'c'
164
- token 'Comment.Multiline', 'cm'
165
- token 'Comment.Preproc', 'cp'
166
- token 'Comment.Single', 'c1'
167
- token 'Comment.Special', 'cs'
168
-
169
- token 'Generic', 'g'
170
- token 'Generic.Deleted', 'gd'
171
- token 'Generic.Emph', 'ge'
172
- token 'Generic.Error', 'gr'
173
- token 'Generic.Heading', 'gh'
174
- token 'Generic.Inserted', 'gi'
175
- token 'Generic.Output', 'go'
176
- token 'Generic.Prompt', 'gp'
177
- token 'Generic.Strong', 'gs'
178
- token 'Generic.Subheading', 'gu'
179
- token 'Generic.Traceback', 'gt'
180
-
181
- token 'Generic.Lineno', 'gl'
175
+ # convenience
176
+ Num = Literal::Number
177
+ Str = Literal::String
178
+ end
182
179
  end
183
180
  end