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 = XRB::Entities::HTML5) ⇒ MarkupParser

Returns a new instance of MarkupParser.



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

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



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

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



116
117
118
# File 'lib/utopia/content/markup.rb', line 116

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

#cdata(string) ⇒ Object



155
156
157
# File 'lib/utopia/content/markup.rb', line 155

def cdata(string)
  @delegate.write(string[9..-4])
end

#close_tag(name, offset) ⇒ Object



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

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



147
148
149
# File 'lib/utopia/content/markup.rb', line 147

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

#doctype(string) ⇒ Object



143
144
145
# File 'lib/utopia/content/markup.rb', line 143

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

#instruction(string) ⇒ Object



151
152
153
# File 'lib/utopia/content/markup.rb', line 151

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

#open_tag_begin(name, offset) ⇒ Object



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

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

#open_tag_end(self_closing) ⇒ Object



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

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



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

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

#text(string) ⇒ Object



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

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