class WikiThat::HTMLGenerator

HTMLGenerators are disposable objects for translate a Mediawiki document to HTML Documents. @author Bryan T. Meyers

Attributes

errors[R]

All of the errors and warnings generated while parsing

result[R]

The output of the translation to HTML

warnings[R]

All of the errors and warnings generated while parsing

Public Class Methods

new(doc, base_url, default_namespace, sub_url, media_base) click to toggle source

Create a new WikiThat::HTMLGenerator @param [String] doc the MediaWiki document @param [String] base_url the base URL for relative links @param [String] default_namespace the default namespace for relative links @param [String] sub_url the sub URL for relative links @param [String] media_base the base URL for media sources

@returns [WikiThat::HTMLGenerator] a newly configured HTMLGenerator

# File lib/wiki-that/generator/generator.rb, line 39
def initialize(doc, base_url, default_namespace, sub_url, media_base)
  @parser   = WikiThat::Parser.new(doc, base_url, default_namespace, sub_url, media_base)
  @index    = 0
  @errors   = []
  @warnings = []
  @root     = nil
  @result   = ''
end

Public Instance Methods

generate() click to toggle source

Translate the MediaWiki document into an HTML partial

@returns [String] the resulting HTML partial

# File lib/wiki-that/generator/generator.rb, line 53
def generate
  @parser.parse
  @errors   = @parser.errors
  @warnings = @parser.warnings
  @root     = @parser.result
  unless success?
    return
  end
  @result = generate_element(@root)
end
generate_attributes(element) click to toggle source

Translate an Element's attributes into an HTML partial @param [Element] element the current element @returns [String] the resulting HTML partial

# File lib/wiki-that/generator/generator.rb, line 108
def generate_attributes(element)
  buff = ''
  element.attributes.each do |k, v|
    buff += ' '
    if v.is_a? TrueClass
      buff += k.to_s
    else
      buff += "#{k.to_s}=\"#{v}\""
    end
  end
  buff.rstrip!
  buff
end
generate_element(element) click to toggle source

Translate an Element into an HTML partial @param [Element] element the current element @returns [String] the resulting HTML partial

# File lib/wiki-that/generator/generator.rb, line 69
def generate_element(element)
  case element.type
    when :br
      '<br>'
    when :comment
      "<!--#{element.value}-->"
    when :text
      element.value ? element.value : ''
    when :nowiki, :pre
      "<#{element.type.to_s}>#{element.value}</#{element.type.to_s}>"
    when :root
      buff = ''
      element.children.each do |c|
        buff += generate_element(c)
      end
      buff
    else
      buff = "<#{element.type.to_s}"
      buff += generate_attributes(element)
      case element.type
        when :hr, :img, :references
          buff + ' />'
        else
          if element.type == :p and element.children.length == 0
            return ''
          end
          buff += '>'
          element.children.each do |c|
            buff += generate_element(c)
          end
          buff + "</#{element.type.to_s}>"
      end
  end
end
success?() click to toggle source

Check if the parsing succeeded @returns [Boolean] True iff no errors

# File lib/wiki-that/generator/generator.rb, line 126
def success?
  @errors.length == 0
end