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.



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

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



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

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



131
132
133
# File 'lib/utopia/content/markup.rb', line 131

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

#cdata(string) ⇒ Object



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

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

#close_tag(name, offset) ⇒ Object



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

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



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

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

#doctype(string) ⇒ Object



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

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

#instruction(string) ⇒ Object



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

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

#open_tag_begin(name, offset) ⇒ Object



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

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

#open_tag_end(self_closing) ⇒ Object



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

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



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

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

#text(string) ⇒ Object



174
175
176
# File 'lib/utopia/content/markup.rb', line 174

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