Module: Uv

Defined in:
lib/uv.rb,
lib/uv/utility.rb,
lib/uv/render_processor.rb

Defined Under Namespace

Classes: RenderProcessor

Class Method Summary collapse

Class Method Details

.alpha_blend(bg, fg) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/uv/utility.rb', line 10

def Uv.alpha_blend bg, fg
   unless bg =~ /^#((\d|[ABCDEF]){3}|(\d|[ABCDEF]){6}|(\d|[ABCDEF]){8})$/i
      raise(ArgumentError, "Malformed background color '#{bg}'" )
   end
   unless fg =~ /^#((\d|[ABCDEF]){3}|(\d|[ABCDEF]){6}|(\d|[ABCDEF]){8})$/i
      raise(ArgumentError, "Malformed foreground color '#{fg}'" )
   end
   
   if bg.size == 4
      tbg =  (fg[1,1].hex * 0xff / 0xf).to_s(16).upcase.rjust(2, '0')
      tbg += (fg[2,1].hex * 0xff / 0xf).to_s(16).upcase.rjust(2, '0')
      tbg += (fg[3,1].hex * 0xff / 0xf).to_s(16).upcase.rjust(2, '0')
      bg = "##{tbg}"
   end
   
   result = ""
   if fg.size == 4
      result += (fg[1,1].hex * 0xff / 0xf).to_s(16).upcase.rjust(2, '0')
      result += (fg[2,1].hex * 0xff / 0xf).to_s(16).upcase.rjust(2, '0')
      result += (fg[3,1].hex * 0xff / 0xf).to_s(16).upcase.rjust(2, '0')
   elsif fg.size == 9
      if bg.size == 7
         div0 = bg[1..-1].hex
         div1, alpha = fg[1..-1].hex.divmod( 0x100 )
         3.times {      
            div0, mod0 = div0.divmod( 0x100 )
            div1, mod1 = div1.divmod( 0x100 )
            result = ((mod0 * alpha + mod1 * ( 0x100 - alpha ) ) / 0x100).to_s(16).upcase.rjust(2, '0') + result
         } 
      else
         div_a, alpha_a = bg[1..-1].hex.divmod( 0x100 )
         div_b, alpha_b = fg[1..-1].hex.divmod( 0x100 )
         alpha = alpha_a + alpha_b * (0x100 - alpha_a)
         3.times {
            div_b, c_b = div_b.divmod( 0x100 )
            div_a, c_a = div_a.divmod( 0x100 )
            result = ((c_a * alpha_a + ( 0x100 - alpha_a ) * alpha_b * c_b ) / alpha).to_s(16).upcase.rjust(2, '0') + result
         } 
      end
      #result = "FF00FF"
   else
      result = fg[1..-1]
   end
   "##{result}"
end

.copy_files(output, output_dir) ⇒ Object



12
13
14
15
16
17
# File 'lib/uv.rb', line 12

def Uv.copy_files output, output_dir
   Uv.paths.each do |dir|
      dir_name = File.join( dir, "render", output, "files" )
      FileUtils.cp_r( Dir.glob(File.join( dir_name, "." )), output_dir ) if File.exists?( dir_name )
   end
end

.debug(text, syntax_name) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
# File 'lib/uv.rb', line 89

def Uv.debug text, syntax_name
   unless @syntaxes
      @syntaxes = {}
      Dir.glob( File.join(File.dirname(__FILE__), '..', 'syntax', '*.syntax') ).each do |f| 
         @syntaxes[File.basename(f, '.syntax')] = Textpow::SyntaxNode.load( f )
      end
   end
   processor = Textpow::DebugProcessor.new

   @syntaxes[syntax_name].parse( text, processor )
end

.foreground(bg) ⇒ Object



2
3
4
5
6
7
8
# File 'lib/uv/utility.rb', line 2

def Uv.foreground bg
   fg = "#FFFFFF"
   3.times do |i|
      fg = "#000000" if bg[i*2+1, 2].hex > 0xFF / 2 
   end
   fg
end

.init_syntaxesObject



19
20
21
22
23
24
25
26
# File 'lib/uv.rb', line 19

def Uv.init_syntaxes
   @syntaxes = {}
   Uv.paths.each do |dir|
     Dir.glob( File.join(dir, 'syntax', '*.syntax') ).each do |f| 
       @syntaxes[File.basename(f, '.syntax')] = Textpow::SyntaxNode.load( f )
     end
   end
end

.normalize_color(settings, color, fg = false) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
# File 'lib/uv/utility.rb', line 56

def Uv.normalize_color settings, color, fg = false
   if color
      if fg
         alpha_blend( settings["foreground"] ? settings["foreground"] : "#000000FF", color )
      else
         alpha_blend( settings["background"] ? settings["background"] : "#000000FF", color )
      end
   else
      color
   end
end

.parse(text, output = "xhtml", syntax_name = nil, line_numbers = false, render_style = "mac_classic", headers = false) ⇒ Object



79
80
81
82
83
84
85
86
87
# File 'lib/uv.rb', line 79

def Uv.parse text, output = "xhtml", syntax_name = nil, line_numbers = false, render_style = "mac_classic", headers = false
   init_syntaxes unless @syntaxes
   renderer = self.renderer(output, render_style)
   css_class = render_style
   render_options = YAML.load( File.open(  renderer ) )
   render_processor = RenderProcessor.new( render_options, line_numbers, headers )
   @syntaxes[syntax_name].parse( text,  render_processor )
   render_processor.string
end

.pathsObject



8
9
10
# File 'lib/uv.rb', line 8

def Uv.paths
   @paths ||= [File.join(File.dirname(__FILE__), ".." ) ]
end

.renderer(output, render_style) ⇒ Object

Raises:

  • (ArgumentError)


44
45
46
47
48
49
50
# File 'lib/uv.rb', line 44

def Uv.renderer(output, render_style)
  Uv.paths.each do |dir|
    render_path = File.join(dir, 'render', output , "#{render_style}.render")
    return render_path if File.exist?(render_path)
  end
  raise( ArgumentError, "Style #{render_style} could not be found for #{output}" )
end

.syntax_for_file(file_name) ⇒ Object



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
# File 'lib/uv.rb', line 52

def Uv.syntax_for_file file_name
   init_syntaxes unless @syntaxes
   first_line = ""
   File.open( file_name, 'r' ) { |f|
      while (first_line = f.readline).strip.size == 0; end
   }
   result = []
   @syntaxes.each do |key, value|
      assigned = false
      if value.fileTypes
         value.fileTypes.each do |t|
            if t == File.basename( file_name ) || t == File.extname( file_name )[1..-1]
               result << [key, value] 
               assigned = true
               break
            end
         end
      end
      unless assigned
         if value.firstLineMatch && value.firstLineMatch =~ first_line
            result << [key, value] 
         end
      end
   end
   result
end

.syntaxesObject



28
29
30
31
32
33
34
# File 'lib/uv.rb', line 28

def Uv.syntaxes
   Uv.paths.collect do |dir|
      Dir.glob( File.join(dir, 'syntax', '*.syntax') ).collect do |f| 
         File.basename(f, '.syntax')
      end
   end.flatten
end

.themesObject



36
37
38
39
40
41
42
# File 'lib/uv.rb', line 36

def Uv.themes
   Uv.paths.collect do |dir|
      Dir.glob( File.join(dir, 'render', 'xhtml', 'files', 'css', '*.css') ).collect do |f| 
         File.basename(f, '.css')
      end
   end.flatten
end