Class: Amrita::Formatter

Class Amrita::Formatter |
In: | lib/amrita/format.rb
Parent: | Object

A Formatter object prints a Node object as HTML text.

Methods

format format_attr_of_element format_element_attrs new set_attr_filter taginfo_of_element

Attributes

:asxml | [RW] | If set, single tag like `<hr>` will be printed as `<hr />` Default is false
:compact_space | [RW] | If set, the spaces and new-lines in text will be compacted. Default is false
:delete_span | [RW] | If set, `<span>` with no attribute will be deleted. Default is true
:escape | [RW] | If set, text will be escaped by Sanitizer#sanitize_text Default is true.
:stream | [R] |

Included modules

Amrita

Public Class methods

new(stream="", tagdict=DefaultHtmlTagInfo, &element_filter)

stream is any object that has << method. All output is done by << method.

    # File lib/amrita/format.rb, line 154
        def initialize(stream="", tagdict=DefaultHtmlTagInfo, &element_filter)
          @stream = stream
          @tagdict = tagdict
          @escape = true
          @asxml = false
          @delete_span = true
          @sanitized = false
          @compact_space = false
          @element_filter = element_filter
        end

Public Instance methods

format(node, stream=nil)

Format a Node object.

If `stream` is given, the output is changed to it in this method.

    # File lib/amrita/format.rb, line 168
        def format(node, stream=nil)
          node = Node::to_node(node)
          with_stream(stream) do
            node.format(self)
          end
        end

set_attr_filter(hash)

set replacing attribute.

         f.set_attr_filter(:__id=>:id)

<p __id="x"> is printed as <p id="x">

    # File lib/amrita/format.rb, line 234
        def set_attr_filter(hash)
          src = [ "def self.format_attr(a, flag)" ]
          src << "case a.key"
          hash.each do |key, val|
            src << ] when "#{key}" ; format_attr_default(Attr.new(:#{val}, a.value), flag)]
          end
          src << "else; format_attr_default(a, flag)"
          src << "end"
          src << "end"
          src << ""
          #puts src

          eval src.join("\n")
        rescue SctiptError, NameError
          puts src
        end

format_attr_of_element(attr, element)

    # File lib/amrita/format.rb, line 295
        def format_attr_of_element(attr, element)
          taginfo = taginfo_of_element(element)
          format_attr(attr, taginfo && taginfo.url_attr?(attr.key))
        end

format_element_attrs(element)

    # File lib/amrita/format.rb, line 300
        def format_element_attrs(element)
          format_attrs(element.attrs, taginfo_of_element(element))
        end

taginfo_of_element(element)

    # File lib/amrita/format.rb, line 304
        def taginfo_of_element(element)
          @tagdict.get_tag_info(element.tagname_symbol)
        end