Sha256: 85c8775918b506ae312fd9fdd310c0ca39f82290119ff4cdf56a2ed37a776f8f

Contents?: true

Size: 1.24 KB

Versions: 3

Compression:

Stored size: 1.24 KB

Contents

#	This file is part of the "Utopia Framework" project, and is released under the MIT license.
#	Copyright 2010 Samuel Williams. All rights reserved.
#	See <utopia.rb> for licensing details.

module Utopia
	
	class Tag
		def initialize(name, attributes = {})
			@name = name
			@attributes = attributes
			@closed = false
		end

		attr :name
		attr :attributes
		attr :closed, true

		def [](key)
			@attributes[key]
		end

		def append(text)
			@content ||= StringIO.new
			@content.write text
		end

		def to_html(content = nil, buf = StringIO.new)
			write_full_html(buf, content)
			
			return buf.string
		end
		
		def to_hash
			@attributes
		end
		
		def to_s
			buf = StringIO.new
			write_open_html(buf)
			return buf.string
		end
		
		def write_open_html(buf, terminate = false)
			buf ||= StringIO.new 
			buf.write "<#{name}"

			@attributes.each do |key, value|
				buf.write " #{key}=\"#{value}\""
			end
			
			if terminate
				buf.write "/>"
			else
				buf.write ">"
			end
		end
		
		def write_close_html(buf)
			buf.write "</#{name}>"
		end
		
		def write_full_html(buf, content = nil)
			if @closed && content == nil
				write_open_html(buf, true)
			else
				write_open_html(buf)
				buf.write(content)
				write_close_html(buf)
			end
		end
	end
	
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
utopia-0.9.61 lib/utopia/tag.rb
utopia-0.9.60 lib/utopia/tag.rb
utopia-0.9.59 lib/utopia/tag.rb