class Zenithal::ZenithalConverter
Constants
- SINGLETON_NAMES
Attributes
configs[RW]
document[R]
functions[RW]
variables[RW]
Public Class Methods
new(document, type = :node)
click to toggle source
# File source/zenml/converter.rb, line 13 def initialize(document, type = :node) @document = document @type = type @configs = {} @variables = {} @templates = {} @functions = {} @default_element_template = lambda{|_| empty_nodes} @default_text_template = lambda{|_| empty_nodes} reset_variables end
simple_html(document)
click to toggle source
Returns a simple converter that converts an XML document to the equivalent HTML document.
# File source/zenml/converter.rb, line 144 def self.simple_html(document) converter = Zenithal::ZenithalConverter.new(document, :text) converter.add([//], [""]) do |element| close = !SINGLETON_NAMES.include?(element.name) html = "<#{element.name}" element.attributes.each_attribute do |attribute| html << " #{attribute.name}='#{attribute.to_s}'" end html << ">" if close html << apply(element, "") html << "</#{element.name}>" end if element.name == "html" html = "<!DOCTYPE html>\n\n" + html end next html end converter.add_default(nil) do |text| next text.to_s end return converter end
Public Instance Methods
add(element_pattern, scope_pattern, &block)
click to toggle source
# File source/zenml/converter.rb, line 117 def add(element_pattern, scope_pattern, &block) @templates.store([element_pattern, scope_pattern], block) end
add_default(element_pattern, &block)
click to toggle source
# File source/zenml/converter.rb, line 125 def add_default(element_pattern, &block) if element_pattern @default_element_template = block else @default_text_template = block end end
apply(element, scope, *args)
click to toggle source
# File source/zenml/converter.rb, line 68 def apply(element, scope, *args) nodes = empty_nodes element.children.each do |child| case child when REXML::Element result_nodes = convert_element(child, scope, *args) if result_nodes nodes << result_nodes end when REXML::Text result_nodes = convert_text(child, scope, *args) if result_nodes nodes << result_nodes end end end return nodes end
apply_select(element, xpath, scope, *args)
click to toggle source
# File source/zenml/converter.rb, line 87 def apply_select(element, xpath, scope, *args) nodes = empty_nodes element.each_xpath(xpath) do |child| case child when REXML::Element result_nodes = convert_element(child, scope, *args) if result_nodes nodes << result_nodes end when REXML::Text result_nodes = convert_text(child, scope, *args) if result_nodes nodes << result_nodes end end end return nodes end
call(element, name, *args)
click to toggle source
# File source/zenml/converter.rb, line 106 def call(element, name, *args) nodes = empty_nodes @functions.each do |function_name, block| if function_name == name nodes = instance_exec(element, *args, &block) break end end return nodes end
convert(initial_scope = "")
click to toggle source
# File source/zenml/converter.rb, line 32 def convert(initial_scope = "") document = nil if @type == :text document = convert_element(@document.root, initial_scope) else document = REXML::Document.new children = convert_element(@document.root, initial_scope) children.each do |child| document.add(child) end end return document end
convert_element(element, scope, *args)
click to toggle source
# File source/zenml/converter.rb, line 46 def convert_element(element, scope, *args) nodes = nil @templates.each do |(element_pattern, scope_pattern), block| if element_pattern != nil && element_pattern.any?{|s| s === element.name} && scope_pattern.any?{|s| s === scope} nodes = instance_exec(element, scope, *args, &block) break end end return nodes || @default_element_template.call(element) end
convert_text(text, scope, *args)
click to toggle source
# File source/zenml/converter.rb, line 57 def convert_text(text, scope, *args) nodes = nil @templates.each do |(element_pattern, scope_pattern), block| if element_pattern == nil && scope_pattern.any?{|s| s === scope} nodes = instance_exec(text, scope, *args, &block) break end end return nodes || @default_text_template.call(text) end
empty_nodes()
click to toggle source
# File source/zenml/converter.rb, line 133 def empty_nodes return (@type == :text) ? "" : REXML::Nodes[] end
reset_variables()
click to toggle source
Override this method to customise how to initialise the variable hash. This method is called when creating or updating an instance.
# File source/zenml/converter.rb, line 139 def reset_variables @variables = {} end
set(name, &block)
click to toggle source
# File source/zenml/converter.rb, line 121 def set(name, &block) @functions.store(name, block) end
update(document)
click to toggle source
Changes the document to be converted. Note that this method initialises the variable hash, but not the configuration hash.
# File source/zenml/converter.rb, line 27 def update(document) @document = document reset_variables end