Class: Uv::RenderProcessor

Inherits:
Object
  • Object
show all
Defined in:
lib/lib/uv/render_processor.rb

Constant Summary collapse

@@score_manager =
Textpow::ScoreManager.new

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(render_options, line_number = 0, headers = true, score_manager = nil) ⇒ RenderProcessor

Returns a new instance of RenderProcessor.



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

def initialize render_options, line_number = 0, headers = true, score_manager = nil
  @score_manager  = score_manager || @@score_manager
  @render_options = render_options
  @options        = {}
  @headers        = headers
  @escapeHTML     = true
  
  @line_numbers = !(line_number.nil? or (line_number == 0))
  unless @line_numbers then
    @line_number = 0
  else
    @line_number = line_number - 1
  end
  
end

Instance Attribute Details

#escapeHTMLObject

Returns the value of attribute escapeHTML.



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

def escapeHTML
  @escapeHTML
end

#stringObject (readonly)

Returns the value of attribute string.



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

def string
  @string
end

Class Method Details

.load(output, style = nil, line_number = 0, headers = false) {|processor| ... } ⇒ Object

Yields:

  • (processor)

Raises:

  • (ArgumentError)


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

def self.load(output, style = nil, line_number = 0, headers = false)
  style  ||= Uv.default_style
  renderer = File.join( Uv.render_path, output,"#{style}.render")
  raise( ArgumentError, "Output for #{output} in #{style} style is not yet implemented" ) unless File.exists?(renderer)
  options   = YAML.load_file(renderer)
  processor = RenderProcessor.new(options, line_number, headers)
  yield processor if block_given?
  processor
end

Instance Method Details

#close_lineObject



77
78
79
80
81
82
83
84
# File 'lib/lib/uv/render_processor.rb', line 77

def close_line
  stack = @stack[0..-1]
  while stack.size > 1
    opt = options stack
    print opt["end"] if opt
    stack.pop
  end
end

#close_tag(name, position) ⇒ Object



69
70
71
72
73
74
75
# File 'lib/lib/uv/render_processor.rb', line 69

def close_tag name, position
  print escape(@line[@position...position].gsub(/\n|\r/, '')) if position > @position
  @position = position
  opt = options @stack
  print opt["end"] if opt
  @stack.pop
end

#end_parsing(name) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/lib/uv/render_processor.rb', line 116

def end_parsing name
  if @line
    print escape(@line[@position..-1].gsub(/\n|\r/, '')) 
    while @stack.size > 1
      opt = options @stack
      print opt["end"] if opt
      @stack.pop
    end
    print @render_options["line"]["end"]
    print "\n"
  end
  #         opt = options @stack
  #         print opt["end"] if opt
  @stack.pop
  print @render_options["listing"]["end"]
  print @render_options["document"]["end"] if @headers
end

#escape(string) ⇒ Object



51
52
53
54
55
56
57
58
59
# File 'lib/lib/uv/render_processor.rb', line 51

def escape string
  if @render_options["filter"]
    @escaped = string
    @escaped = self.instance_eval( @render_options["filter"] )
    @escaped
  else
    string
  end
end

#new_line(line) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/lib/uv/render_processor.rb', line 96

def new_line line
  if @line
    print escape(@line[@position..-1].gsub(/\n|\r/, ''))
    close_line
    print @render_options["line"]["end"]
    print "\n" 
  end
  @position = 0
  @line_number += 1
  @line = line
  print @render_options["line"]["begin"]
  if @line_numbers
    print @render_options["line-numbers"]["begin"] 
    print @line_number.to_s.rjust(4).ljust(5)
    print @render_options["line-numbers"]["end"] 
    print " "
  end
  open_line
end

#open_lineObject



86
87
88
89
90
91
92
93
94
# File 'lib/lib/uv/render_processor.rb', line 86

def open_line
  stack = [@stack.first]
  clone = @stack[1..-1]
  while stack.size < @stack.size
    stack << clone.shift
    opt = options stack
    print opt["begin"] if opt
  end
end

#open_tag(name, position) ⇒ Object



61
62
63
64
65
66
67
# File 'lib/lib/uv/render_processor.rb', line 61

def open_tag name, position
  @stack << name
  print escape(@line[@position...position].gsub(/\n|\r/, '')) if position > @position
  @position = position
  opt = options @stack
  print opt["begin"] if opt
end

#options(stack) ⇒ Object



134
135
136
137
138
139
140
141
142
143
# File 'lib/lib/uv/render_processor.rb', line 134

def options stack
  ref = stack.join ' '
  return @options[ref] if @options.has_key? ref

  result = @render_options['tags'].max do |a, b| 
    @score_manager.score( a['selector'], ref ) <=> @score_manager.score( b['selector'], ref )
  end
  result = nil if @score_manager.score( result['selector'], ref ) == 0
  @options[ref] = result
end


47
48
49
# File 'lib/lib/uv/render_processor.rb', line 47

def print string
  @string << string
end

#start_parsing(name) ⇒ Object



37
38
39
40
41
42
43
44
45
# File 'lib/lib/uv/render_processor.rb', line 37

def start_parsing name
  @stack       = [name]
  @string      = ""
  @line        = nil
  print @render_options["document"]["begin"] if @headers
  print @render_options["listing"]["begin"]
  #         opt = options @stack
  #         print opt["begin"] if opt
end