Class: Uv::RenderProcessor

Inherits:
Object
  • Object
show all
Defined in:
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_numbers = false, headers = true, score_manager = nil) ⇒ RenderProcessor

Returns a new instance of RenderProcessor.



21
22
23
24
25
26
27
28
# File 'lib/uv/render_processor.rb', line 21

def initialize render_options, line_numbers = false, headers = true, score_manager = nil
  @score_manager  = score_manager || @@score_manager
  @render_options = render_options
  @options        = {}
  @headers        = headers
  @line_numbers   = line_numbers
  @escapeHTML     = true
end

Instance Attribute Details

#escapeHTMLObject

Returns the value of attribute escapeHTML.



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

def escapeHTML
  @escapeHTML
end

#stringObject (readonly)

Returns the value of attribute string.



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

def string
  @string
end

Class Method Details

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

Yields:

  • (processor)

Raises:

  • (ArgumentError)


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

def self.load(output, style = nil, line_numbers = false, 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_numbers, headers)
  yield processor if block_given?
  processor
end

Instance Method Details

#close_lineObject



71
72
73
74
75
76
77
78
# File 'lib/uv/render_processor.rb', line 71

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



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

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



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/uv/render_processor.rb', line 110

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



45
46
47
48
49
50
51
52
53
# File 'lib/uv/render_processor.rb', line 45

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



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/uv/render_processor.rb', line 90

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



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

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



55
56
57
58
59
60
61
# File 'lib/uv/render_processor.rb', line 55

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



128
129
130
131
132
133
134
135
136
137
# File 'lib/uv/render_processor.rb', line 128

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


41
42
43
# File 'lib/uv/render_processor.rb', line 41

def print string
  @string << string
end

#start_parsing(name) ⇒ Object



30
31
32
33
34
35
36
37
38
39
# File 'lib/uv/render_processor.rb', line 30

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