Navigation Menu

Skip to content
This repository has been archived by the owner on Dec 25, 2020. It is now read-only.

Commit

Permalink
Refactor HtmlWriter
Browse files Browse the repository at this point in the history
  • Loading branch information
mneumann committed Feb 20, 2009
1 parent 60a0f97 commit 01f1310
Showing 1 changed file with 13 additions and 31 deletions.
44 changes: 13 additions & 31 deletions lib/wee/html_writer.rb
Expand Up @@ -20,63 +20,45 @@ module Wee
#
class HtmlWriter

def initialize(port)
@port = port
attr_accessor :port

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

def start_tag(tag, attributes=nil)
CLOSING = ">".freeze
SINGLE_CLOSING = " />".freeze

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

self
end

def single_tag(tag, attributes=nil)
if attributes
@port << "<#{ tag }"
attributes.each {|k, v|
if v
@port << %[ #{ k }="#{ v }"]
else
@port << %[ #{ k }]
end
}
@port << " />"
else
@port << "<#{ tag } />"
end

self
start_tag(tag, attributes, true)
end

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

self
@port << "</#{tag}>"
end

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

self
end
alias << text

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

self
end

end # class HtmlWriter
Expand Down

0 comments on commit 01f1310

Please sign in to comment.