class HtmlToAnsi::Html::Conversions::AnsiFormatter
Public Instance Methods
append_text(element, ansi_node, stack)
click to toggle source
# File lib/html_to_ansi/html/conversions.rb, line 162 def append_text element, ansi_node, stack text = element.to_s text.gsub!(/[\s]+/, ' ') unless stack.include?(Extra::PRE) text.upcase! if stack.include?(Extra::UPPERCASE) ansi_node.append TextNode.new(text, stack) end
format(document)
click to toggle source
# File lib/html_to_ansi/html/conversions.rb, line 119 def format document @document = document @ansi_root = InlineNode.new @list_index = [] format_recursively @document.root, @ansi_root, [Attribute::NORMAL] output = @ansi_root.render output += (@ansi_root.children.last.kind_of?(BlockNode) ? "\n" : "") end
format_children(element, node, stack)
click to toggle source
# File lib/html_to_ansi/html/conversions.rb, line 169 def format_children element, node, stack element.each { |e| format_recursively e, node, stack } end
format_list_item(element, node, stack)
click to toggle source
# File lib/html_to_ansi/html/conversions.rb, line 181 def format_list_item element, node, stack i = 0 unless @list_index.empty? @list_index[-1] = @list_index[-1] + 1 i = @list_index[-1] end b = BreakNode.new node.append b if element.parent.name == 'ol' b.append TextNode.new("#{i}. ", stack) else b.append TextNode.new("* ", stack) end format_children element, b, stack end
format_paragraph(element, node, stack)
click to toggle source
# File lib/html_to_ansi/html/conversions.rb, line 175 def format_paragraph element, node, stack paragraph = BlockNode.new node.append paragraph format_children element, paragraph, stack end
format_recursively(element, ansi_node, stack)
click to toggle source
# File lib/html_to_ansi/html/conversions.rb, line 128 def format_recursively element, ansi_node, stack if element.is_a?(REXML::Text) append_text element, ansi_node, stack else current = [] case element.name when 'b', 'strong', 'em' current.push Attribute::BOLD format_children element, ansi_node, stack + current when 'i', 'u' current.push Attribute::UNDERSCORE format_children element, ansi_node, stack + current when 'h1', 'h2', 'h3', 'h4', 'h5' current.push Attribute::BOLD, Extra::UPPERCASE format_paragraph element, ansi_node, stack + current when 'p' format_paragraph element, ansi_node, stack when 'ol', 'ul' @list_index.push 0 format_paragraph element, ansi_node, stack @list_index.pop when 'li' format_list_item element, ansi_node, stack when 'pre' current.push Extra::PRE format_children element, ansi_node, stack + current when 'br' ansi_node.append TextNode.new("\n", stack + [Extra::PRE]) else format_children element, ansi_node, stack end end end