rouge 0.3.10 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -7,11 +7,24 @@ module Rouge
7
7
  class HTML < Formatter
8
8
  tag 'html'
9
9
 
10
- # @option opts :css_class
11
- # A css class to be used for the generated <pre> tag.
10
+ # @option opts [String] :css_class ('highlight')
11
+ # @option opts [true/false] :line_numbers (false)
12
+ # @option opts [Rouge::CSSTheme] :inline_theme (nil)
13
+ # @option opts [true/false] :wrap (true)
14
+ #
15
+ # Initialize with options.
16
+ #
17
+ # If `:inline_theme` is given, then instead of rendering the
18
+ # tokens as <span> tags with CSS classes, the styles according to
19
+ # the given theme will be inlined in "style" attributes. This is
20
+ # useful for formats in which stylesheets are not available.
21
+ #
22
+ # Content will be wrapped in a `<pre>` tag with the given
23
+ # `:css_class` unless `:wrap` is set to `false`.
12
24
  def initialize(opts={})
13
25
  @css_class = opts[:css_class] || 'highlight'
14
26
  @line_numbers = opts.fetch(:line_numbers) { false }
27
+ @inline_theme = opts.fetch(:inline_theme) { nil }
15
28
  @wrap = opts.fetch(:wrap, true)
16
29
  end
17
30
 
@@ -24,6 +37,7 @@ module Rouge
24
37
  end
25
38
  end
26
39
 
40
+ private
27
41
  def stream_untableized(tokens, &b)
28
42
  yield "<pre class=#{@css_class.inspect}>" if @wrap
29
43
  tokens.each do |tok, val|
@@ -65,7 +79,6 @@ module Rouge
65
79
  yield '</pre>' if @wrap
66
80
  end
67
81
 
68
- private
69
82
  def span(tok, val, &b)
70
83
  # TODO: properly html-encode val
71
84
  val = CGI.escape_html(val)
@@ -76,9 +89,18 @@ module Rouge
76
89
  when nil
77
90
  raise "unknown token: #{tok.inspect} for #{val.inspect}"
78
91
  else
79
- yield '<span class='
80
- yield tok.shortname.inspect
81
- yield '>'
92
+ if @inline_theme
93
+ rules = @inline_theme.style_for(tok).rendered_rules
94
+
95
+ yield '<span style='
96
+ yield rules.to_a.join(';').inspect
97
+ yield '>'
98
+ else
99
+ yield '<span class='
100
+ yield tok.shortname.inspect
101
+ yield '>'
102
+ end
103
+
82
104
  yield val
83
105
  yield '</span>'
84
106
  end
@@ -24,7 +24,7 @@ module Rouge
24
24
  rule %r{\b(case|cond|end|bc|lc|if|unless|try|loop|receive|fn|defmodule|
25
25
  defp?|defprotocol|defimpl|defrecord|defmacrop?|defdelegate|
26
26
  defexception|exit|raise|throw|unless|after|rescue|catch|else)\b(?![?!])|
27
- (?<!\.)\b(do|\-\>)\b\s*}x, 'Keyword'
27
+ (?<!\.)\b(do|\-\>)\b}x, 'Keyword'
28
28
  rule /\b(import|require|use|recur|quote|unquote|super|refer)\b(?![?!])/, 'Keyword.Namespace'
29
29
  rule /(?<!\.)\b(and|not|or|when|xor|in)\b/, 'Operator.Word'
30
30
  rule %r{%=|\*=|\*\*=|\+=|\-=|\^=|\|\|=|
@@ -20,19 +20,22 @@ module Rouge
20
20
  return if empty?
21
21
 
22
22
  yield "#{selector} {"
23
- yield " color: #{fg};" if fg
24
- yield " background-color: #{bg};" if bg
25
- yield " font-weight: bold;" if self[:bold]
26
- yield " font-style: italic;" if self[:italic]
27
- yield " text-decoration: underline;" if self[:underline]
28
-
29
- (self[:rules] || []).each do |rule|
23
+ rendered_rules.each do |rule|
30
24
  yield " #{rule};"
31
25
  end
32
-
33
26
  yield "}"
34
27
  end
35
28
 
29
+ def rendered_rules(&b)
30
+ return enum_for(:rendered_rules) unless b
31
+ yield "color: #{fg}" if fg
32
+ yield "background-color: #{bg}" if bg
33
+ yield "font-weight: bold" if self[:bold]
34
+ yield "font-style: italic" if self[:italic]
35
+ yield "text-decoration: underline" if self[:underline]
36
+
37
+ (self[:rules] || []).each(&b)
38
+ end
36
39
  end
37
40
 
38
41
  def styles
@@ -142,11 +145,14 @@ module Rouge
142
145
  end
143
146
  end
144
147
 
148
+ def style_for(tok)
149
+ styles.fetch(tok.name) do
150
+ tok.parent ? style_for(tok.parent) : Style.new(self)
151
+ end
152
+ end
153
+
145
154
  private
146
155
  def css_selector(token)
147
- tokens = [token]
148
- parent = token.parent
149
-
150
156
  inflate_token(token).map do |tok|
151
157
  raise "unknown token: #{tok.inspect}" if tok.shortname.nil?
152
158
 
@@ -1,5 +1,5 @@
1
1
  module Rouge
2
2
  def self.version
3
- "0.3.10"
3
+ "0.4.0"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rouge
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.10
4
+ version: 0.4.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-08-01 00:00:00.000000000 Z
12
+ date: 2013-08-15 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  type: :runtime