move render processor loading logic to RenderProcessor

This commit is contained in:
rick 2010-02-05 08:37:58 -08:00
parent f79f32437a
commit bd77c6dc37
3 changed files with 192 additions and 188 deletions

View file

@ -72,15 +72,10 @@ module Uv
end
def Uv.parse text, output = "xhtml", syntax_name = nil, line_numbers = false, render_style = nil, headers = false
render_style ||= @default_style
init_syntaxes unless @syntaxes
renderer = File.join( @render_path, output,"#{render_style}.render")
raise( ArgumentError, "Output for #{output} in #{render_style} style is not yet implemented" ) unless File.exists?(renderer)
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
RenderProcessor.load(output, render_style, line_numbers, headers) do |processor|
@syntaxes[syntax_name].parse(text, processor)
end.string
end
def Uv.debug text, syntax_name

View file

@ -1,131 +1,140 @@
require 'cgi'
module Uv
module Uv
class RenderProcessor
@@score_manager = Textpow::ScoreManager.new
attr_reader :string
attr_accessor :escapeHTML
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
class RenderProcessor
@@score_manager = Textpow::ScoreManager.new
attr_reader :string
attr_accessor :escapeHTML
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
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
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
def print string
@string << string
end
def escape string
if @render_options["filter"]
@escaped = string
@escaped = self.instance_eval( @render_options["filter"] )
@escaped
else
string
end
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
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
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
def close_line
stack = @stack[0..-1]
while stack.size > 1
opt = options stack
print opt["end"] if opt
stack.pop
end
def print string
@string << string
end
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
def escape string
if @render_options["filter"]
@escaped = string
@escaped = self.instance_eval( @render_options["filter"] )
@escaped
else
string
end
end
def new_line line
if @line
print escape(@line[@position..-1].gsub(/\n|\r/, ''))
close_line
print @render_options["line"]["end"]
print "\n"
end
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
@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
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
open_line
end
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
def close_line
stack = @stack[0..-1]
while stack.size > 1
opt = options stack
print opt["end"] if opt
stack.pop
end
# opt = options @stack
# print opt["end"] if opt
@stack.pop
print @render_options["listing"]["end"]
print @render_options["document"]["end"] if @headers
end
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
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
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
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
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
end
result = nil if @score_manager.score( result['selector'], ref ) == 0
@options[ref] = result
end
end
end

View file

@ -1,67 +1,67 @@
module Uv
def Uv.foreground bg
fg = "#FFFFFF"
3.times do |i|
fg = "#000000" if bg[i*2+1, 2].hex > 0xFF / 2
end
fg
end
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"
def Uv.foreground bg
fg = "#FFFFFF"
3.times do |i|
fg = "#000000" if bg[i*2+1, 2].hex > 0xFF / 2
end
fg
end
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
result = fg[1..-1]
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}"
end
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
#result = "FF00FF"
else
result = fg[1..-1]
end
"##{result}"
end
def Uv.normalize_color settings, color, fg = false
if color
if fg
alpha_blend( settings["foreground"] ? settings["foreground"] : "#000000FF", color )
else
color
alpha_blend( settings["background"] ? settings["background"] : "#000000FF", color )
end
end
else
color
end
end
end