Class: Utopia::Content::MarkupParser
- Inherits:
-
Object
- Object
- Utopia::Content::MarkupParser
- 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
-
.parse(buffer, delegate, entities = XRB::Entities::HTML5) ⇒ Object
Parse markup events into a delegate.
Instance Method Summary collapse
-
#attribute(key, value) ⇒ Object
Process a markup attribute.
-
#cdata(string) ⇒ Object
Process a CDATA section.
-
#close_tag(name, offset) ⇒ Object
Process a closing markup tag.
-
#comment(string) ⇒ Object
Process a markup comment.
-
#doctype(string) ⇒ Object
Process a document type declaration.
-
#initialize(buffer, delegate, entities = XRB::Entities::HTML5) ⇒ MarkupParser
constructor
Initialize a streaming markup parser and its delegate.
-
#instruction(string) ⇒ Object
Process a markup instruction.
-
#open_tag_begin(name, offset) ⇒ Object
Begin collecting an opening tag.
-
#open_tag_end(self_closing) ⇒ Object
Complete an opening tag and dispatch it to the delegate.
-
#parse! ⇒ Object
Parse the markup buffer into delegate callbacks and validate balanced tags.
-
#text(string) ⇒ Object
Process text content.
Constructor Details
#initialize(buffer, delegate, entities = XRB::Entities::HTML5) ⇒ MarkupParser
Initialize a streaming markup parser and its delegate.
135 136 137 138 139 140 141 142 143 |
# File 'lib/utopia/content/markup.rb', line 135 def initialize(buffer, delegate, entities = XRB::Entities::HTML5) @buffer = buffer @delegate = delegate @entities = entities @current = nil @stack = [] end |
Class Method Details
.parse(buffer, delegate, entities = XRB::Entities::HTML5) ⇒ Object
Parse markup events into a delegate.
124 125 126 127 128 129 |
# File 'lib/utopia/content/markup.rb', line 124 def self.parse(buffer, delegate, entities = XRB::Entities::HTML5) # This is for compatibility with the existing API which passes in a string: buffer = XRB::Buffer(buffer) self.new(buffer, delegate, entities).parse! end |
Instance Method Details
#attribute(key, value) ⇒ Object
Process a markup attribute.
167 168 169 |
# File 'lib/utopia/content/markup.rb', line 167 def attribute(key, value) @current.tag.attributes[key] = value end |
#cdata(string) ⇒ Object
Process a CDATA section.
226 227 228 |
# File 'lib/utopia/content/markup.rb', line 226 def cdata(string) @delegate.write(string[9..-4]) end |
#close_tag(name, offset) ⇒ Object
Process a closing markup tag.
191 192 193 194 195 196 197 198 199 200 |
# File 'lib/utopia/content/markup.rb', line 191 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
Process a markup comment.
212 213 214 |
# File 'lib/utopia/content/markup.rb', line 212 def comment(string) @delegate.write(string) end |
#doctype(string) ⇒ Object
Process a document type declaration.
205 206 207 |
# File 'lib/utopia/content/markup.rb', line 205 def doctype(string) @delegate.write(string) end |
#instruction(string) ⇒ Object
Process a markup instruction.
219 220 221 |
# File 'lib/utopia/content/markup.rb', line 219 def instruction(string) @delegate.write(string) end |
#open_tag_begin(name, offset) ⇒ Object
Begin collecting an opening tag.
159 160 161 |
# File 'lib/utopia/content/markup.rb', line 159 def open_tag_begin(name, offset) @current = ParsedTag.new(name, offset) end |
#open_tag_end(self_closing) ⇒ Object
Complete an opening tag and dispatch it to the delegate.
174 175 176 177 178 179 180 181 182 183 184 |
# File 'lib/utopia/content/markup.rb', line 174 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
Parse the markup buffer into delegate callbacks and validate balanced tags.
147 148 149 150 151 152 153 |
# File 'lib/utopia/content/markup.rb', line 147 def parse! XRB::Parsers.parse_markup(@buffer, self, @entities) if tag = @stack.pop raise UnbalancedTagError.new(@buffer, tag) end end |
#text(string) ⇒ Object
Process text content.
233 234 235 |
# File 'lib/utopia/content/markup.rb', line 233 def text(string) @delegate.text(string) end |