Sha256: b77e0029851e6a2f0614229da4c4a4aa5bedaa882d520a82dbb54f03dee46ce7

Contents?: true

Size: 1.33 KB

Versions: 3

Compression:

Stored size: 1.33 KB

Contents

require 'rack'

module Wee

  #
  # A class used to write out HTML documents easily.
  #
  # Usage:
  #
  #   w = Wee::HtmlWriter.new(doc='')
  #   w.start_tag('html')
  #   w.start_tag('body')
  #   w.start_tag('a', 'href' => 'http://...')
  #   w.text('link')
  #   w.end_tag('a')
  #   w.end_tag('body')
  #   w.end_tag('html')
  #
  #   p doc  # => '<html><body><a href="http://...">link</a></body></html>'
  #
  class HtmlWriter

    attr_accessor :port

    def initialize(port=[])
      @port = port
    end

    CLOSING = ">".freeze
    SINGLE_CLOSING = " />".freeze

    def start_tag(tag, attributes=nil, single=false)
      if attributes
        @port << "<#{tag}"
        attributes.each {|k, v| 
          if v
            @port << %[ #{ k }="#{ v }"] 
          else
            @port << %[ #{ k }] 
          end
        }
        @port << (single ? SINGLE_CLOSING : CLOSING)
      else
        @port << (single ? "<#{tag} />" : "<#{tag}>")
      end
    end

    def single_tag(tag, attributes=nil)
      start_tag(tag, attributes, true)
    end

    def end_tag(tag)
      @port << "</#{tag}>"
    end

    def text(str)
      @port << str.to_s
    end

    def encode_text(str)
      @port << Rack::Utils.escape_html(str.to_s)
    end

    def write(str)
      @port << str
    end
    alias << write

  end # class HtmlWriter

end # module Wee

Version data entries

3 entries across 3 versions & 2 rubygems

Version Path
mullen-wee-2.2.0 lib/wee/html_writer.rb
wee-2.1.0 lib/wee/html_writer.rb
wee-2.0.0 lib/wee/html_writer.rb