Module: Uv

Defined in:
lib/uv.rb,
lib/uv/engine.rb,
lib/uv/utility.rb,
lib/uv/version.rb,
lib/uv/syntaxes.rb,
lib/uv/render_processor.rb,
lib/uv/finding_syntax_nodes.rb

Defined Under Namespace

Classes: Engine, RenderProcessor

Constant Summary collapse

VERSION =
'0.0.7.5'

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.debug(text, syntax_name) ⇒ Object

Parses <arg1> text with Textpow::DebugProcessor, using the given syntax



74
75
76
# File 'lib/uv.rb', line 74

def debug
  @debug
end

.default_styleObject

Returns the value of attribute default_style.



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

def default_style
  @default_style
end

.render_pathObject

Returns the value of attribute render_path.



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

def render_path
  @render_path
end

.syntax_pathObject

Returns the value of attribute syntax_path.



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

def syntax_path
  @syntax_path
end

.syntaxesObject

Returns the value of attribute syntaxes.



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

def syntaxes
  @syntaxes
end

.theme_pathObject

Returns the value of attribute theme_path.



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

def theme_path
  @theme_path
end

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

Copies files from the [ruby-uv/render/<arg1>/files/] to the <arg2> output directory



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

def Uv.copy_files output, output_dir
  Uv.path.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

.default_syntax(extensions) ⇒ Object



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

def Uv.default_syntax(extensions)
  puts "Uv Error: No syntax match found for '#{extensions.join(', ')}', defaulting to 'plain_text' syntax" if @debug
  raise "Uv Error: Syntax files failed to load (No 'plain_text' parser was loaded).  Loaded syntaxes: [#{@syntaxes.keys.join(', ')}]" unless load_syntax('plain_text')
  load_syntax('plain_text')
end

.find_syntaxes(extensions = [], first_line = '', slow_search = false) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/uv/finding_syntax_nodes.rb', line 19

def Uv.find_syntaxes(extensions=[], first_line='', slow_search=false)
  puts "Finding syntaxes for: Extensions: [#{extensions.join(', ')}], First Line: '#{first_line.inspect}'" if @debug
  init_syntaxes if @syntaxes.empty?
  syntaxes_found = []; extensions.delete(''); extensions.compact!
  case slow_search
  when false
     syntaxes_found << load_syntax(extensions.first) unless extensions.empty?
  when true
    syntaxes_found += extensions.collect {|ext| find_syntaxes_by_ext(ext) }.flatten.compact
    syntaxes_found += find_syntaxes_by_first_line(first_line) if syntaxes_found.empty?
  end
  syntaxes_found.compact!
  syntaxes_found << default_syntax(extensions) if syntaxes_found.empty?
  puts "Syntaxes found: [#{syntaxes_found.collect(&:name).join(', ')}]" if @debug and not syntaxes_found.empty?
  syntaxes_found
end

.find_syntaxes_by_ext(ext) ⇒ Object



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

def Uv.find_syntaxes_by_ext(ext)
  return [load_syntax(ext)] if @syntaxes.key?(ext)
  puts "No syntax found... loading all syntaxes (including all syntax.fileTypes)" if @debug
  load_all_syntaxes
  return [load_syntax(ext)] if @syntaxes.key?(ext)
  []
end

.find_syntaxes_by_first_line(first_line) ⇒ Object



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

def Uv.find_syntaxes_by_first_line(first_line)
  return [] if first_line == ''
  puts "Searching syntaxes by first line..." if @debug
  @syntaxes.values.compact.collect do |syntax_node|
    return [syntax_node] if syntax_node && syntax_node.firstLineMatch && syntax_node.firstLineMatch =~ first_line
  end
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

.get_first_line(text) ⇒ Object



67
68
69
70
71
# File 'lib/uv.rb', line 67

def Uv.get_first_line(text)
  stripped_text = text.strip
  first_break = stripped_text.index(/[\n\r]/) || stripped_text.length - 1
  stripped_text[0..first_break].strip
end

.init_syntaxesObject

Loads @syntaxes with all the syntax names. DOES NOT load the syntax files into TextPow



5
6
7
8
9
10
11
12
# File 'lib/uv/syntaxes.rb', line 5

def Uv.init_syntaxes
  puts "Uv: Finding all syntax names..." if @debug
  syntax_names.each do |syntax_name|
    puts "Uv: Setting @syntax[#{syntax_name}]" if @debug and !@syntaxes.key?(syntax_name)
    @syntaxes[syntax_name] = nil unless @syntaxes.key?(syntax_name)
  end
  @syntaxes
end

.load_all_syntaxes(force_reload = false) ⇒ Object

Loads @syntaxes with all the syntax names. Load the syntax files into Textpow instences. ALSO loads the declared_file_types @force_reload: boolean. Forces the syntax to be reloaded, overriding any loaded syntax



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

def Uv.load_all_syntaxes(force_reload=false)
  puts "Uv: Loading all syntaxes..." if @debug
  syntax_names.each do |syntax_name|
    load_syntax(syntax_name, force_reload)
    load_syntax_declared_file_types(@syntaxes[syntax_name])
  end
  @syntaxes
end

.load_syntax(syntax_name, force_reload = false) ⇒ Object

Loads @syntaxes with a Textpow instance @force_reload: boolean. Forces the syntax to be reloaded, overriding any loaded syntax



29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/uv/syntaxes.rb', line 29

def Uv.load_syntax(syntax_name, force_reload=false)
  filename = File.join(@syntax_path, "#{syntax_name}.syntax")
  begin
    if force_reload
      @syntaxes[syntax_name] = Textpow::SyntaxNode.load(filename)
    else
      @syntaxes[syntax_name] ||= Textpow::SyntaxNode.load(filename)
    end
    puts "#{syntax_name}.syntax loaded" if @debug
  rescue => e
    puts "Error in loading syntax: #{syntax_name}.  Returning: #{@syntaxes[syntax_name].class}.  Error: #{e.message}" if @debug
  end
  @syntaxes[syntax_name]
end

.load_syntax_declared_file_types(syntax_node) ⇒ Object



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

def Uv.load_syntax_declared_file_types(syntax_node)
  syntax_node.fileTypes.each do |file_type|
    puts "Syntax: #{syntax_node.name} loading FileType: #{file_type}" if @debug
    @syntaxes[file_type] ||= syntax_node
  end if syntax_node and syntax_node.fileTypes
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 = nil, headers = false) ⇒ Object

Parses <arg1> text using RenderProcessor.load(Textpow::SyntaxNode.parse(text)), returns the vailid <output>



59
60
61
62
63
64
65
# File 'lib/uv.rb', line 59

def Uv.parse text, output = "xhtml", syntax_name = nil, line_numbers = false, render_style = nil, headers = false
  syntaxes = find_syntaxes([syntax_name], get_first_line(text))
  return text if syntaxes.empty?
  RenderProcessor.load(output, render_style, line_numbers, headers) do |processor|
    syntaxes.first.parse(text, processor)
  end.string
end

.pathObject

Returns the root path for Uv: [‘bin’,‘lib’…]



25
26
27
28
# File 'lib/uv.rb', line 25

def Uv.path
  result = []
  result << File.join(File.dirname(__FILE__), ".." )
end

.syntax_for_file(file_name) ⇒ Object

Guesses the correct syntax based on the input files fileType. If the FileType doesnt containt a valid syntax name, each syntax has it’s first line matched againts the the file’s first line



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

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

.syntax_namesObject

Returns the list of file names in the syntax dir (without ‘.syntax’ extension)



32
33
34
35
36
# File 'lib/uv.rb', line 32

def Uv.syntax_names
  Dir.glob( File.join(@syntax_path, '*.syntax') ).collect do |f|
    File.basename(f, '.syntax')
  end
end

.syntax_node_for(syntax) ⇒ Object

Returns the Textpow::SyntaxNode for this syntax; Ex: syntax_node_for(‘ruby’) => Textpow::SyntaxNode.load(‘ruby.syntax’)

Raises:

  • ArgumentError if no syntax is found



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/uv/finding_syntax_nodes.rb', line 54

def self.syntax_node_for(syntax)
  if !@syntaxes.key?(syntax)
    filename = File.join(@syntax_path, "#{syntax}.syntax")
    @syntaxes[syntax] = if File.exist?(filename)
      Textpow::SyntaxNode.load(filename)
    else
      false
    end
  end
  if !@syntaxes[syntax]
    if syntax == '' or syntax.nil?
      puts "No syntax supplied, defaulting to 'plain_text' syntax"
      syntax = "plain_text"
    else
      raise ArgumentError, "Syntax not found.  No #{syntax}.syntax file in #{@syntax_path}"
    end
  end
  @syntaxes[syntax]
end

.themesObject

Returns the list of .css files in the theme directory



52
53
54
55
56
# File 'lib/uv.rb', line 52

def Uv.themes
  Dir.glob( File.join(@theme_path, '*.css') ).collect do |f|
    File.basename(f, '.css')
  end
end