Class: Dyndoc::RenderProcessor

Inherits:
Object
  • Object
show all
Defined in:
lib/dyndoc/common/uv.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.



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

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.



30
31
32
# File 'lib/dyndoc/common/uv.rb', line 30

def escapeHTML
  @escapeHTML
end

#stringObject (readonly)

Returns the value of attribute string.



29
30
31
# File 'lib/dyndoc/common/uv.rb', line 29

def string
  @string
end

Class Method Details

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

Yields:

  • (processor)

Raises:

  • (ArgumentError)


32
33
34
35
36
37
38
39
40
# File 'lib/dyndoc/common/uv.rb', line 32

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



94
95
96
97
98
99
100
101
# File 'lib/dyndoc/common/uv.rb', line 94

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



86
87
88
89
90
91
92
# File 'lib/dyndoc/common/uv.rb', line 86

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



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/dyndoc/common/uv.rb', line 133

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



68
69
70
71
72
73
74
75
76
# File 'lib/dyndoc/common/uv.rb', line 68

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



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/dyndoc/common/uv.rb', line 113

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



103
104
105
106
107
108
109
110
111
# File 'lib/dyndoc/common/uv.rb', line 103

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



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

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



151
152
153
154
155
156
157
158
159
160
# File 'lib/dyndoc/common/uv.rb', line 151

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


64
65
66
# File 'lib/dyndoc/common/uv.rb', line 64

def print string
  @string << string
end

#start_parsing(name) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/dyndoc/common/uv.rb', line 51

def start_parsing name
  @stack       = [name]
  @string      = ""
  @line        = nil
  @line_number = 0
  ## Dyndoc.warn "render_options",@render_options["listing"]
  print @render_options["document"]["begin"] if @headers
  ## FIX (01/09/2014): "begin-line-numbers" undefined for xhtml and has to replaced by "begin" 
  print (@line_numbers ? @render_options["listing"]["begin"] : @render_options["listing"]["begin-line-numbers"] || @render_options["listing"]["begin"])
  #         opt = options @stack
  #         print opt["begin"] if opt
end