Class: Utopia::Content::MarkupParser

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

Defined Under Namespace

Classes: ParsedTag, UnbalancedTagError

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(buffer, delegate, entities = Trenni::Entities::HTML5) ⇒ MarkupParser

Returns a new instance of MarkupParser.



104
105
106
107
108
109
110
111
112
# File 'lib/utopia/content/markup.rb', line 104

def initialize(buffer, delegate, entities = Trenni::Entities::HTML5)
	@buffer = buffer
	
	@delegate = delegate
	@entities = entities
	
	@current = nil
	@stack = []
end

Class Method Details

.parse(buffer, delegate, entities = Trenni::Entities::HTML5) ⇒ Object



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

def self.parse(buffer, delegate, entities = Trenni::Entities::HTML5)
	# This is for compatibility with the existing API which passes in a string:
	buffer = Trenni::Buffer(buffer)
	
	self.new(buffer, delegate).parse!
end

Instance Method Details

#attribute(key, value) ⇒ Object



126
127
128
# File 'lib/utopia/content/markup.rb', line 126

def attribute(key, value)
	@current.tag.attributes[key] = value
end

#cdata(string) ⇒ Object



165
166
167
# File 'lib/utopia/content/markup.rb', line 165

def cdata(string)
	@delegate.write(string)
end

#close_tag(name, offset) ⇒ Object



142
143
144
145
146
147
148
149
150
151
# File 'lib/utopia/content/markup.rb', line 142

def close_tag(name, offset)
	@current = @stack.pop
	tag = @current.tag
	
	if tag.name != name
		raise UnbalancedTagError.new(@buffer, @current, ParsedTag.new(name, offset))
	end
	
	@delegate.tag_end(tag)
end

#comment(string) ⇒ Object



157
158
159
# File 'lib/utopia/content/markup.rb', line 157

def comment(string)
	@delegate.write(string)
end

#doctype(string) ⇒ Object



153
154
155
# File 'lib/utopia/content/markup.rb', line 153

def doctype(string)
	@delegate.write(string)
end

#instruction(string) ⇒ Object



161
162
163
# File 'lib/utopia/content/markup.rb', line 161

def instruction(string)
	@delegate.write(string)
end

#open_tag_begin(name, offset) ⇒ Object



122
123
124
# File 'lib/utopia/content/markup.rb', line 122

def open_tag_begin(name, offset)
	@current = ParsedTag.new(name, offset)
end

#open_tag_end(self_closing) ⇒ Object



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

def open_tag_end(self_closing)
	if self_closing
		@current.tag.closed = true
		@delegate.tag_complete(@current.tag)
	else
		@stack << @current
		@delegate.tag_begin(@current.tag)
	end
	
	@current = nil
end

#parse!Object



114
115
116
117
118
119
120
# File 'lib/utopia/content/markup.rb', line 114

def parse!
	Trenni::Parsers.parse_markup(@buffer, self, @entities)
	
	if tag = @stack.pop
		raise UnbalancedTagError.new(@buffer, tag)
	end
end

#text(string) ⇒ Object



169
170
171
# File 'lib/utopia/content/markup.rb', line 169

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