Module: Uv

Defined in:
lib/rmthemegen.rb,
lib/rmthemegen/uv_addons.rb

Defined Under Namespace

Classes: Converters

Class Method Summary collapse

Class Method Details

.css_string(css, code_name) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/rmthemegen.rb', line 16

def Uv.css_string(css,code_name)   
   #input: a hash of css selectors=>styles
   #output :usable css
  # added by david heitzman 
   outs='<style type="text/css">'
   css.each do |key, values|
      if key == code_name
         outs += "#{code_name} {"
      else
         outs += "#{code_name} #{key} {"
      end
      values.each do |style, value|
         outs += "   #{style}: #{value};" if value
      end
      outs += "} "
   end 
   outs += "</style>"
end

.get_render_and_css(tm_theme) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/rmthemegen.rb', line 36

def Uv.get_render_and_css(tm_theme)
  # input: a string that a .tmTheme after being read with Plist::parse_xml
  # output: [{a hash containing the render data structure },{ a hash containing the  css string }]
  # added by david heitzman 

  settings = tm_theme["settings"].find { |s| ! s["name"] }["settings"]

      render = {"name" => tm_theme["name"]}
      css = {}

      standard_name = tm_theme["name"]
      code_name = "pre.#{standard_name}"

      render["tags"] = []
      count_names = {}
      tm_theme["settings"].each do |t|
         if t["scope"]
            class_name = t["name"].downcase.gsub(/\W/, ' ').gsub('.tmtheme', '').split(' ').collect{|s| s.capitalize}.join
            if class_name == ""
               class_name = "x" * t["name"].size
            end
            
            if count_names[class_name]
               tname = class_name
               class_name = "#{class_name}#{count_names[class_name]}"
               count_names[tname] += count_names[tname] + 1
            else
               count_names[class_name] = 1
            end
            
            tag = {}
            tag["selector"] = t["scope"]
            tag["begin"] = "<span class=\"#{class_name}\">"
            tag["end"] = "</span>"
            render["tags"] << tag
            
            if s = t["settings"]
               style = {}
               style["color"] = Uv.normalize_color(settings, s["foreground"], true)
               style["background-color"] = Uv.normalize_color(settings, s["background"])
               case s["fontStyle"]
                  when /bold/ then style["font-weight"] = "bold"
                  when /italic/ then style["font-style"] = "italic"
                  when /underline/ then style["text-decoration"] = "underline"
               end
               css[".#{class_name}"] = style
            end
         elsif ! t["name"]
            if s = t["settings"]
               style = {}
               style["color"] = Uv.normalize_color(settings, s["foreground"], true)
               style["background-color"] = Uv.alpha_blend(s["background"], s["background"])
               css[code_name] = style
               @style = style
               style = {}
               style["background-color"] = Uv.alpha_blend(s["selection"], s["selection"])
               style["color"] = Uv.foreground( style["background-color"] )
               css[".line-numbers"] = style
               
               tag = {}
               tag["begin"] = "<span class=\"line-numbers\">"
               tag["end"] = "</span>"
               render["line-numbers"] = tag
            end
         end
      end

      render["filter"] = "CGI.escapeHTML( @escaped )"

      tag = {}
      tag["begin"] = ""
      tag["end"]   = ""
      render["line"] = tag


      tag = {}
      tag["begin"] = "<pre class=\"#{standard_name}\">"
      tag["end"]   = "</pre>"
      render["listing"] = tag

      tag = {}
      tag["begin"] = ''

      tag["end"] = ''

      render["document"] = tag
      return [render,css_string(css, code_name)]
end

.syntaxes_hashObject



11
12
13
14
# File 'lib/rmthemegen.rb', line 11

def Uv.syntaxes_hash
   Uv.init_syntaxes unless @syntaxes
   @syntaxes 
end

.tmtheme_to_html(tm_theme, code_to_render, options) ⇒ Object



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/rmthemegen.rb', line 126

def Uv.tmtheme_to_html(tm_theme,code_to_render, options)

   #input: tm_theme        - a string containing an xml representation of a textmate theme in plist format 
   #input: code_to_render  - the code (ruby, php, python, c etc.) you want rendered as html 
   #input: options - a hash containing options such as line numbers, etc  :line_numbers => false, :render_style => "classic", :headers => false, :code_type=>nil
   #       They are the same options you can give to Uv.parse
   #output : [<css string>,<html string>]

   opts = {:line_numbers => false, :render_style => "classic", :headers => false}.merge options 
   out = ""
   begin
     # Uv::init_syntaxes
     syn=( Uv.syntax_node_for opts[:code_type].to_s  )

     processor = Textpow::DebugProcessor.new
     syn.parse( tm_theme , processor )
     render_str = Uv::get_render_and_css( Plist::parse_xml(tm_theme) )  
     render_processor = Uv::RenderProcessor.new( render_str.first, line_numbers=opts[:line_numbers], headers=opts[:headers] )
     syn.parse( code_to_render, render_processor )
     # RenderProcessor.load('xhtml', opts[:render_style], opts[:line_numbers], opts[:headers]) do |processor|
     #   syntax_node_for(opts[:code_type]).parse(code_to_parse, processor)
     # end.string

      out=[render_str.last, render_processor.string]
     # out=[render_str.last, RenderProcessor.load('xhtml', opts[:render_style], opts[:line_numbers], opts[:headers]) do |processor|
     #   syntax_node_for(opts[:code_type]).parse(code_to_parse, processor)
     # end.string]
   rescue Exception=>e
     out=['<style type="text/css"></style>',e.inspect,'<p>Error in tm_theme_to_html.</>']
   end 
out
end