Class: Utopia::Content::Markup

Inherits:
Object
  • Object
show all
Defined in:
lib/utopia/content/markup.rb

Defined Under Namespace

Classes: UnbalancedTagError

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(buffer, delegate) ⇒ Markup

Returns a new instance of Markup.



80
81
82
83
84
# File 'lib/utopia/content/markup.rb', line 80

def initialize(buffer, delegate)
	@buffer = buffer
	@delegate = delegate
	@stack = []
end

Class Method Details

.parse!(markup, delegate) ⇒ Object



52
53
54
55
56
57
# File 'lib/utopia/content/markup.rb', line 52

def self.parse!(markup, delegate)
	# This is for compatibility with the existing API which passes in a string:
	markup = Trenni::Buffer.new(markup) if markup.is_a? String
	
	self.new(markup, delegate).parse!
end

Instance Method Details

#attribute(name, value) ⇒ Object



145
146
147
# File 'lib/utopia/content/markup.rb', line 145

def attribute(name, value)
	@stack.last[0].attributes[name.to_sym] = value
end

#begin_parse(scanner) ⇒ Object



96
97
98
# File 'lib/utopia/content/markup.rb', line 96

def begin_parse(scanner)
	@scanner = scanner
end

#begin_tag(tag_name, begin_tag_type) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/utopia/content/markup.rb', line 116

def begin_tag(tag_name, begin_tag_type)
	if begin_tag_type == :opened
		@stack << [Tag.new(tag_name, SymbolicHash.new), @scanner.pos]
	else
		current_tag, current_position = @stack.pop

		if tag_name != current_tag.name
			raise UnbalancedTagError.new(@scanner, current_position, current_tag.name, tag_name)
		end

		@delegate.tag_end(current_tag)
	end
end

#cdata(text) ⇒ Object



108
109
110
# File 'lib/utopia/content/markup.rb', line 108

def cdata(text)
	@delegate.cdata("<![CDATA[#{text}]]>")
end

#comment(text) ⇒ Object



112
113
114
# File 'lib/utopia/content/markup.rb', line 112

def comment(text)
	@delegate.cdata("<!--#{text}-->")
end

#doctype(attributes) ⇒ Object



100
101
102
# File 'lib/utopia/content/markup.rb', line 100

def doctype(attributes)
	@delegate.cdata("<!DOCTYPE #{attributes}>")
end

#finish_tag(begin_tag_type, end_tag_type) ⇒ Object



130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/utopia/content/markup.rb', line 130

def finish_tag(begin_tag_type, end_tag_type)
	if begin_tag_type == :opened # <...
		if end_tag_type == :closed # <.../>
			cur, pos = @stack.pop
			cur.closed = true

			@delegate.tag_complete(cur)
		elsif end_tag_type == :opened # <...>
			cur, pos = @stack.last

			@delegate.tag_begin(cur)
		end
	end
end

#instruction(content) ⇒ Object



149
150
151
# File 'lib/utopia/content/markup.rb', line 149

def instruction(content)
	cdata("<?#{content}?>")
end

#parse!Object



86
87
88
89
90
91
92
93
94
# File 'lib/utopia/content/markup.rb', line 86

def parse!
	Trenni::Parser.new(@buffer, self).parse!
	
	unless @stack.empty?
		current_tag, current_position = @stack.pop
		
		raise UnbalancedTagError.new(@scanner, current_position, current_tag.name, 'EOF')
	end
end

#text(text) ⇒ Object



104
105
106
# File 'lib/utopia/content/markup.rb', line 104

def text(text)
	@delegate.cdata(text)
end