Class: Utopia::Content::MarkupParser

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

Overview

Provides a high level interface for parsing markup.

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.



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

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



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

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, entities).parse!
end

Instance Method Details

#attribute(key, value) ⇒ Object



133
134
135
# File 'lib/utopia/content/markup.rb', line 133

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

#cdata(string) ⇒ Object



172
173
174
# File 'lib/utopia/content/markup.rb', line 172

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

#close_tag(name, offset) ⇒ Object



149
150
151
152
153
154
155
156
157
158
# File 'lib/utopia/content/markup.rb', line 149

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



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

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

#doctype(string) ⇒ Object



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

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

#instruction(string) ⇒ Object



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

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

#open_tag_begin(name, offset) ⇒ Object



129
130
131
# File 'lib/utopia/content/markup.rb', line 129

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

#open_tag_end(self_closing) ⇒ Object



137
138
139
140
141
142
143
144
145
146
147
# File 'lib/utopia/content/markup.rb', line 137

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



121
122
123
124
125
126
127
# File 'lib/utopia/content/markup.rb', line 121

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

#text(string) ⇒ Object



176
177
178
# File 'lib/utopia/content/markup.rb', line 176

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