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,11 @@
1
+ module Rouge
2
+ module Lexers
3
+ class Text < Lexer
4
+ tag 'text'
5
+
6
+ def stream_tokens(stream, &b)
7
+ yield Token['Text'], stream
8
+ end
9
+ end
10
+ end
11
+ end
@@ -1,24 +1,37 @@
1
1
  module Rouge
2
2
  class Theme
3
+ class Style < Hash
4
+ def render(selector, &b)
5
+ return enum_for(:render, selector).to_a.join("\n") unless b
6
+
7
+ return if empty?
8
+
9
+ yield "#{selector} {"
10
+ yield " color: #{self[:fg]};" if self[:fg]
11
+ yield " background-color: #{self[:bg]};" if self[:bg]
12
+ yield " font-weight: bold;" if self[:bold]
13
+ yield " font-style: italic;" if self[:italic]
14
+ yield " text-decoration: underline;" if self[:underline]
15
+
16
+ (self[:rules] || []).each do |rule|
17
+ yield " #{rule};"
18
+ end
19
+
20
+ yield "}"
21
+ end
22
+ end
23
+
3
24
  class << self
4
25
  def styles
5
26
  @styles ||= {}
6
27
  end
7
28
 
8
- def main_style
9
- @main_style ||= {}
10
- end
11
-
12
29
  def style(*tokens)
13
- opts = {}
14
- opts = tokens.pop if tokens.last.is_a? Hash
15
-
16
- if tokens.empty?
17
- @main_style = opts
18
- end
30
+ style = Style.new
31
+ style.merge!(tokens.pop) if tokens.last.is_a? Hash
19
32
 
20
33
  tokens.each do |tok|
21
- styles[tok.to_s] = opts
34
+ styles[tok.to_s] = style
22
35
  end
23
36
  end
24
37
 
@@ -41,59 +54,35 @@ module Rouge
41
54
 
42
55
  class CSSTheme < Theme
43
56
  def initialize(opts={})
44
- @opts = opts
45
- end
46
-
47
- def render
48
- out = []
49
- stream { |line| out << line }
50
- out.join("\n")
57
+ @scope = opts[:scope] || '.highlight'
51
58
  end
52
59
 
53
- def stream(&b)
54
- return enum_for(:stream) unless block_given?
60
+ def render(&b)
61
+ return enum_for(:render).to_a.join("\n") unless b
55
62
 
56
63
  self.class.styles.each do |tokname, style|
57
- stream_single(Token[tokname], style, &b)
64
+ style.render(css_selector(Token[tokname]), &b)
58
65
  end
59
-
60
- render_stanza('.highlight', self.class.main_style, &b)
61
66
  end
62
67
 
63
68
  private
64
- def stream_single(tok, style, &b)
65
- render_stanza(css_selector(tok), style, &b)
66
- end
67
-
68
- def render_stanza(selector, style, &b)
69
- return if style.empty?
70
-
71
- yield "#{selector} {"
72
- yield " color: #{style[:fg]};" if style[:fg]
73
- yield " background-color: #{style[:bg]};" if style[:bg]
74
- yield " font-weight: bold;" if style[:bold]
75
- yield " font-style: italic;" if style[:italic]
76
- yield " text-decoration: underline;" if style[:underline]
77
-
78
- (style[:rules] || []).each do |rule|
79
- yield " #{rule};"
80
- end
81
-
82
- yield "}"
83
- end
84
-
85
69
  def css_selector(token)
86
70
  tokens = [token]
87
71
  parent = token.parent
88
72
 
89
73
  inflate_token(token).map do |tok|
90
- base = ".highlight"
91
- base << " .#{tok.shortname}" unless tok.shortname.empty?
74
+ raise "unknown token: #{tok.inspect}" if tok.shortname.nil?
92
75
 
93
- base
76
+ single_css_selector(tok)
94
77
  end.join(', ')
95
78
  end
96
79
 
80
+ def single_css_selector(token)
81
+ return @scope if token == Token['Text']
82
+
83
+ "#{@scope} .#{token.shortname}"
84
+ end
85
+
97
86
  # yield all of the tokens that should be styled the same
98
87
  # as the given token. Essentially this recursively all of
99
88
  # the subtokens, except those which are more specifically
@@ -0,0 +1,63 @@
1
+ module Rouge
2
+ module Themes
3
+ # stolen from pygments
4
+ class Colorful < CSSTheme
5
+ style 'Text', :fg => "#bbbbbb", :bg => 'black'
6
+
7
+ style 'Comment', :fg => "#888"
8
+ style 'Comment.Preproc', :fg => "#579"
9
+ style 'Comment.Special', :fg => "#cc0000", :bold => true
10
+
11
+ style 'Keyword', :fg => "#080", :bold => true
12
+ style 'Keyword.Pseudo', :fg => "#038"
13
+ style 'Keyword.Type', :fg => "#339"
14
+
15
+ style 'Operator', :fg => "#333"
16
+ style 'Operator.Word', :fg => "#000", :bold => true
17
+
18
+ style 'Name.Builtin', :fg => "#007020"
19
+ style 'Name.Function', :fg => "#06B", :bold => true
20
+ style 'Name.Class', :fg => "#B06", :bold => true
21
+ style 'Name.Namespace', :fg => "#0e84b5", :bold => true
22
+ style 'Name.Exception', :fg => "#F00", :bold => true
23
+ style 'Name.Variable', :fg => "#963"
24
+ style 'Name.Variable.Instance', :fg => "#33B"
25
+ style 'Name.Variable.Class', :fg => "#369"
26
+ style 'Name.Variable.Global', :fg => "#d70", :bold => true
27
+ style 'Name.Constant', :fg => "#036", :bold => true
28
+ style 'Name.Label', :fg => "#970", :bold => true
29
+ style 'Name.Entity', :fg => "#800", :bold => true
30
+ style 'Name.Attribute', :fg => "#00C"
31
+ style 'Name.Tag', :fg => "#070"
32
+ style 'Name.Decorator', :fg => "#555", :bold => true
33
+
34
+ style 'Literal.String', :bg => "#fff0f0"
35
+ style 'Literal.String.Char', :fg => "#04D"
36
+ style 'Literal.String.Doc', :fg => "#D42"
37
+ style 'Literal.String.Interpol', :bg => "#eee"
38
+ style 'Literal.String.Escape', :fg => "#666", :bold => true
39
+ style 'Literal.String.Regex', :fg => "#000", :bg => "#fff0ff"
40
+ style 'Literal.String.Symbol', :fg => "#A60"
41
+ style 'Literal.String.Other', :fg => "#D20"
42
+
43
+ style 'Literal.Number', :fg => "#60E", :bold => true
44
+ style 'Literal.Number.Integer', :fg => "#00D", :bold => true
45
+ style 'Literal.Number.Float', :fg => "#60E", :bold => true
46
+ style 'Literal.Number.Hex', :fg => "#058", :bold => true
47
+ style 'Literal.Number.Oct', :fg => "#40E", :bold => true
48
+
49
+ style 'Generic.Heading', :fg => "#000080", :bold => true
50
+ style 'Generic.Subheading', :fg => "#800080", :bold => true
51
+ style 'Generic.Deleted', :fg => "#A00000"
52
+ style 'Generic.Inserted', :fg => "#00A000"
53
+ style 'Generic.Error', :fg => "#FF0000"
54
+ style 'Generic.Emph', :italic => true
55
+ style 'Generic.Strong', :bold => true
56
+ style 'Generic.Prompt', :fg => "#c65d09", :bold => true
57
+ style 'Generic.Output', :fg => "#888"
58
+ style 'Generic.Traceback', :fg => "#04D"
59
+
60
+ style 'Error', :fg => "#F00", :bg => "#FAA"
61
+ end
62
+ end
63
+ end
@@ -18,32 +18,36 @@ module Rouge
18
18
  backlit = '#4df4ff'
19
19
  schrill = '#ffb000'
20
20
 
21
- style :fg => unicorn, :bg => krasna
21
+ style 'Text', :fg => unicorn, :bg => krasna
22
22
 
23
23
  style 'Comment', :fg => cool_as_ice
24
24
  style 'Error',
25
25
  'Generic.Error', :fg => aluminum1, :bg => scarletred2
26
26
  style 'Keyword', :fg => sandy, :bold => true
27
- style 'Operator', :fg => backlit, :bold => true
27
+ style 'Operator',
28
+ 'Punctuation', :fg => backlit, :bold => true
28
29
  style 'Comment.Preproc',
29
30
  'Comment.Multiline',
30
31
  'Comment.Single',
31
32
  'Comment.Special', :fg => cool_as_ice, :italic => true
32
33
  style 'Generic.Deleted', :fg => scarletred2
34
+ style 'Generic.Inserted', :fg => go_get_it
33
35
  style 'Generic.Emph', :italic => true
34
36
  style 'Generic.Subheading', :fg => '#800080', :bold => true
35
37
  style 'Generic.Traceback', :fg => '#0040D0'
36
38
  style 'Keyword.Constant', :fg => pink_merengue, :bold => true
37
39
  style 'Keyword.Namespace',
38
40
  'Keyword.Pseudo',
39
- 'Keyword.Reserved', :fg => schrill, :bold => true
41
+ 'Keyword.Reserved',
42
+ 'Generic.Heading', :fg => schrill, :bold => true
40
43
  style 'Keyword.Type',
41
44
  'Name.Constant',
42
45
  'Name.Class',
43
46
  'Name.Decorator',
44
47
  'Name.Namespace',
45
48
  'Name.Builtin.Pseudo',
46
- 'Name.Exception', :fg => go_get_it, :bold => true
49
+ 'Name.Exception',
50
+ 'Name.Tag', :fg => go_get_it, :bold => true
47
51
  style 'Literal.Number', :fg => pink_merengue, :bold => true
48
52
  style 'Literal.String', :fg => dune, :bold => true
49
53
  style 'Literal.String.Escape',
@@ -51,15 +55,13 @@ module Rouge
51
55
  'Literal.String.Interpol',
52
56
  'Literal.String.Other',
53
57
  'Literal.String.Symbol', :fg => backlit, :bold => true
54
- style 'Name.Attribute', :fg => '#7D9029'
55
58
  style 'Name.Builtin', :fg => sandy
56
59
  style 'Name.Entity', :fg => '#999999', :bold => true
57
- style 'Name.Label', :fg => '#A0A000'
58
- style 'Name.Tag', :fg => '#008000', :bold => true
59
60
  style 'Text.Whitespace', :fg => '#BBBBBB'
60
61
  style 'Name.Variable',
61
- 'Name.Function', :fg => chilly
62
-
62
+ 'Name.Function',
63
+ 'Name.Label',
64
+ 'Name.Attribute', :fg => chilly
63
65
  end
64
66
  end
65
67
  end
@@ -3,6 +3,7 @@ module Rouge
3
3
  attr_reader :name
4
4
  attr_reader :parent
5
5
  attr_accessor :shortname
6
+ alias to_s name
6
7
 
7
8
  def make_single(name)
8
9
  name = name.to_s
@@ -40,6 +41,15 @@ module Rouge
40
41
  @sub_tokens ||= {}
41
42
  end
42
43
 
44
+ def ancestors(&b)
45
+ return enum_for(:ancestors) unless block_given?
46
+
47
+ if parent
48
+ yield self
49
+ parent.ancestors(&b)
50
+ end
51
+ end
52
+
43
53
  def ===(other)
44
54
  immediate = if self.class == other.class
45
55
  self == other
@@ -62,17 +72,19 @@ module Rouge
62
72
  end
63
73
 
64
74
  def get(name)
75
+ return name if name.is_a? Token
76
+
65
77
  base[name]
66
78
  end
67
79
 
80
+ alias [] get
81
+
68
82
  def token(name, shortname)
69
83
  tok = get(name)
70
84
  tok.shortname = shortname
71
85
  tok
72
86
  end
73
87
 
74
- alias [] get
75
-
76
88
  def each_token(&b)
77
89
  recurse = proc do |token|
78
90
  b.call(token)
@@ -1,5 +1,5 @@
1
1
  module Rouge
2
2
  def self.version
3
- "0.0.2"
3
+ "0.0.3"
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.0.2
4
+ version: 0.0.3
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: 2012-09-01 00:00:00.000000000 Z
12
+ date: 2012-09-04 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: see the description for now
15
15
  email:
@@ -21,7 +21,12 @@ files:
21
21
  - Gemfile
22
22
  - lib/rouge/lexers/shell.rb
23
23
  - lib/rouge/lexers/javascript.rb
24
+ - lib/rouge/lexers/diff.rb
25
+ - lib/rouge/lexers/text.rb
26
+ - lib/rouge/lexers/html.rb
27
+ - lib/rouge/lexers/css.rb
24
28
  - lib/rouge/themes/thankful_eyes.rb
29
+ - lib/rouge/themes/colorful.rb
25
30
  - lib/rouge/token.rb
26
31
  - lib/rouge/formatters/html.rb
27
32
  - lib/rouge/version.rb