Class: HTML::SGMLParser
- Inherits:
-
Object
- Object
- HTML::SGMLParser
- Defined in:
- lib/html/sgml-parser.rb
Direct Known Subclasses
Constant Summary collapse
- Interesting =
Regular expressions used for parsing:
/[&<]/
- Incomplete =
Regexp.compile('&([a-zA-Z][a-zA-Z0-9]*|#[0-9]*)?|' + '<([a-zA-Z][^<>]*|/([a-zA-Z][^<>]*)?|' + '![^<>]*)?')
- Entityref =
/&([a-zA-Z][-.a-zA-Z0-9]*)[^-.a-zA-Z0-9]/
- Charref =
/&#([0-9]+)[^0-9]/
- Starttagopen =
/<[>a-zA-Z]/
- Endtagopen =
/<\/[<>a-zA-Z]/
- Endbracket =
/[<>]/
- Special =
/<![^<>]*>/
- Commentopen =
/<!--/
- Commentclose =
/--[ \t\n]*>/
- Tagfind =
/[a-zA-Z][a-zA-Z0-9.-]*/
- Attrfind =
Regexp.compile('[\s,]*([a-zA-Z_][a-zA-Z_0-9.-]*)' + '(\s*=\s*' + "('[^']*'" + '|"[^"]*"' + '|[-~a-zA-Z0-9,./:+*%?!()_#=]*))?')
- Endtagfind =
/\s*\/\s*>/
- Entitydefs =
{'lt'=>'<', 'gt'=>'>', 'amp'=>'&', 'quot'=>'"', 'apos'=>'\''}
Instance Attribute Summary collapse
-
#src_range ⇒ Object
readonly
Returns the value of attribute src_range.
Instance Method Summary collapse
- #close ⇒ Object
- #feed(data) ⇒ Object
- #finish_endtag(tag) ⇒ Object
- #finish_starttag(tag, attrs) ⇒ Object
- #get_source(range) ⇒ Object
- #goahead(_end) ⇒ Object
- #handle_charref(name) ⇒ Object
- #handle_comment(data) ⇒ Object
- #handle_data(data) ⇒ Object
- #handle_data_range(rawdata, start, end_index) ⇒ Object
- #handle_endtag(tag, method) ⇒ Object
- #handle_entityref(name) ⇒ Object
- #handle_special(data) ⇒ Object
- #handle_starttag(tag, method, attrs) ⇒ Object
- #has_context(gi) ⇒ Object
-
#initialize(verbose = false) ⇒ SGMLParser
constructor
A new instance of SGMLParser.
- #parse_comment(i) ⇒ Object
- #parse_endtag(i) ⇒ Object
- #parse_special(i) ⇒ Object
- #parse_starttag(i) ⇒ Object
- #report_unbalanced(tag) ⇒ Object
- #reset ⇒ Object
- #set_range(start, end_index) ⇒ Object
- #setliteral(*args) ⇒ Object
- #setnomoretags ⇒ Object
- #unknown_charref(ref) ⇒ Object
- #unknown_endtag(tag) ⇒ Object
- #unknown_entityref(ref) ⇒ Object
- #unknown_starttag(tag, attrs) ⇒ Object
Constructor Details
#initialize(verbose = false) ⇒ SGMLParser
Returns a new instance of SGMLParser.
45 46 47 48 |
# File 'lib/html/sgml-parser.rb', line 45 def initialize(verbose=false) @verbose = verbose reset end |
Instance Attribute Details
#src_range ⇒ Object (readonly)
Returns the value of attribute src_range.
18 19 20 |
# File 'lib/html/sgml-parser.rb', line 18 def src_range @src_range end |
Instance Method Details
#close ⇒ Object
91 92 93 |
# File 'lib/html/sgml-parser.rb', line 91 def close goahead(true) end |
#feed(data) ⇒ Object
86 87 88 89 |
# File 'lib/html/sgml-parser.rb', line 86 def feed(data) @rawdata << data goahead(false) end |
#finish_endtag(tag) ⇒ Object
279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 |
# File 'lib/html/sgml-parser.rb', line 279 def finish_endtag(tag) if tag == '' found = @stack.length - 1 if found < 0 unknown_endtag(tag) return end else unless @stack.include? tag method = 'end_' + tag unless self.respond_to?(method) unknown_endtag(tag) end return end found = @stack.index(tag) #or @stack.length end while @stack.length > found tag = @stack[-1] method = 'end_' + tag if respond_to?(method) handle_endtag(tag, method) else unknown_endtag(tag) end @stack.pop end end |
#finish_starttag(tag, attrs) ⇒ Object
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 |
# File 'lib/html/sgml-parser.rb', line 261 def finish_starttag(tag, attrs) method = 'start_' + tag if self.respond_to?(method) @stack << tag handle_starttag(tag, method, attrs) return 1 else method = 'do_' + tag if self.respond_to?(method) handle_starttag(tag, method, attrs) return 0 else unknown_starttag(tag, attrs) return -1 end end end |
#get_source(range) ⇒ Object
60 61 62 63 64 65 66 |
# File 'lib/html/sgml-parser.rb', line 60 def get_source(range) start = range.first end_index = range.end exclusive = range.exclude_end? offset_range = Range.new(start-@offset, end_index-@offset, exclusive) return @rawdata[offset_range] end |
#goahead(_end) ⇒ Object
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 |
# File 'lib/html/sgml-parser.rb', line 103 def goahead(_end) rawdata = @rawdata i = 0 n = rawdata.length while i < n if @nomoretags i = handle_data_range(rawdata, i, n) break end j = rawdata.index(Interesting, i) j = n unless j i = handle_data_range(rawdata, i, j) break if (i == n) if rawdata[i] == ?< # if rawdata.index(Starttagopen, i) == i if @literal i = handle_data_range(rawdata, i, i+1) next end k = parse_starttag(i) break unless k i = k next end if rawdata.index(Endtagopen, i) == i k = parse_endtag(i) break unless k i = k @literal = false next end if rawdata.index(Commentopen, i) == i if @literal i = handle_data_range(rawdata, i, i+1) next end k = parse_comment(i) break unless k i += k next end if rawdata.index(Special, i) == i if @literal i = handle_data_range(rawdata, i, i+1) next end k = parse_special(i) break unless k i += k next end elsif rawdata[i] == ?& # if rawdata.index(Charref, i) == i end_index = i + $&.length end_index -= 1 unless rawdata[end_index-1] == ?; set_range(i, end_index) handle_charref($1) i = end_index next end if rawdata.index(Entityref, i) == i end_index = i + $&.length end_index -= 1 unless rawdata[end_index-1] == ?; set_range(i, end_index) handle_entityref($1) i = end_index next end else raise RuntimeError, 'neither < nor & ??' end # We get here only if incomplete matches but # nothing else match = rawdata.index(Incomplete, i) unless match == i i = handle_data_range(rawdata, i, i+1) next end j = match + $&.length break if j == n # Really incomplete i = handle_data_range(rawdata, i, j) end # end while if _end and i < n i = handle_data_range(rawdata, i, n) end @rawdata = rawdata[i..-1] @offset += i end |
#handle_charref(name) ⇒ Object
334 335 336 337 338 339 340 341 |
# File 'lib/html/sgml-parser.rb', line 334 def handle_charref(name) n = Integer(name) if !(0 <= n && n <= 255) unknown_charref(name) return end handle_data(n.chr) end |
#handle_comment(data) ⇒ Object
356 357 |
# File 'lib/html/sgml-parser.rb', line 356 def handle_comment(data) end |
#handle_data(data) ⇒ Object
353 354 |
# File 'lib/html/sgml-parser.rb', line 353 def handle_data(data) end |
#handle_data_range(rawdata, start, end_index) ⇒ Object
95 96 97 98 99 100 101 |
# File 'lib/html/sgml-parser.rb', line 95 def handle_data_range(rawdata, start, end_index) if end_index > start set_range(start, end_index) handle_data(rawdata[start...end_index]) end return end_index end |
#handle_endtag(tag, method) ⇒ Object
323 324 325 |
# File 'lib/html/sgml-parser.rb', line 323 def handle_endtag(tag, method) self.send(method) end |
#handle_entityref(name) ⇒ Object
343 344 345 346 347 348 349 350 351 |
# File 'lib/html/sgml-parser.rb', line 343 def handle_entityref(name) table = Entitydefs if table.include?(name) handle_data(table[name]) else unknown_entityref(name) return end end |
#handle_special(data) ⇒ Object
359 360 |
# File 'lib/html/sgml-parser.rb', line 359 def handle_special(data) end |
#handle_starttag(tag, method, attrs) ⇒ Object
319 320 321 |
# File 'lib/html/sgml-parser.rb', line 319 def handle_starttag(tag, method, attrs) self.send(method, attrs) end |
#has_context(gi) ⇒ Object
73 74 75 |
# File 'lib/html/sgml-parser.rb', line 73 def has_context(gi) @stack.include? gi end |
#parse_comment(i) ⇒ Object
193 194 195 196 197 198 199 200 201 202 203 204 205 206 |
# File 'lib/html/sgml-parser.rb', line 193 def parse_comment(i) rawdata = @rawdata if rawdata[i, 4] != '<!--' raise RuntimeError, 'unexpected call to handle_comment' end match = rawdata.index(Commentclose, i) return nil unless match matched_length = $&.length j = match src_length = match + matched_length - i set_range(i, i + src_length) handle_comment(rawdata[i+4..(j-1)]) return src_length end |
#parse_endtag(i) ⇒ Object
248 249 250 251 252 253 254 255 256 257 258 259 |
# File 'lib/html/sgml-parser.rb', line 248 def parse_endtag(i) rawdata = @rawdata j = rawdata.index(Endbracket, i + 1) return nil unless j tag = (rawdata[i+2..j-1].strip).downcase if rawdata[j] == ?> # j += 1 end set_range(i, j) finish_endtag(tag) return j end |
#parse_special(i) ⇒ Object
308 309 310 311 312 313 314 315 316 317 |
# File 'lib/html/sgml-parser.rb', line 308 def parse_special(i) rawdata = @rawdata match = rawdata.index(Endbracket, i+1) return nil unless match matched_length = $&.length src_length = match - i + matched_length set_range(i, i + src_length) handle_special(rawdata[i+1..(match-1)]) return src_length end |
#parse_starttag(i) ⇒ Object
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 |
# File 'lib/html/sgml-parser.rb', line 208 def parse_starttag(i) rawdata = @rawdata j = rawdata.index(Endbracket, i + 1) return nil unless j attrs = [] if rawdata[i+1] == ?> # # SGML shorthand: <> == <last open tag seen> k = j tag = @lasttag else match = rawdata.index(Tagfind, i + 1) unless match raise RuntimeError, 'unexpected call to parse_starttag' end k = i + 1 + ($&.length) tag = $&.downcase @lasttag = tag end while k < j break if rawdata.index(Endtagfind, k) break unless rawdata.index(Attrfind, k) matched_length = $&.length attrname, rest, attrvalue = $1, $2, $3 if not rest attrvalue = '' # was: = attrname elsif (attrvalue[0] == ?' && attrvalue[-1] == ?') or (attrvalue[0] == ?" && attrvalue[-1] == ?") attrvalue = attrvalue[1..-2] end attrs << [attrname.downcase, attrvalue] k += matched_length end if rawdata[j] == ?> # j += 1 end set_range(i, j) finish_starttag(tag, attrs) return j end |
#report_unbalanced(tag) ⇒ Object
327 328 329 330 331 332 |
# File 'lib/html/sgml-parser.rb', line 327 def report_unbalanced(tag) if @verbose print '*** Unbalanced </' + tag + '>', "\n" print '*** Stack:', self.stack, "\n" end end |
#reset ⇒ Object
50 51 52 53 54 55 56 57 58 |
# File 'lib/html/sgml-parser.rb', line 50 def reset @rawdata = '' @stack = [] @lasttag = '???' @nomoretags = false @literal = false @offset = 0 @ranges = [] end |
#set_range(start, end_index) ⇒ Object
68 69 70 71 |
# File 'lib/html/sgml-parser.rb', line 68 def set_range(start, end_index) @src_range = Range.new(start+@offset, end_index+@offset, exclusive = true) #puts "setting range #{@src_range}, text = \"#{get_source(src_range)}\"" end |
#setliteral(*args) ⇒ Object
82 83 84 |
# File 'lib/html/sgml-parser.rb', line 82 def setliteral(*args) @literal = true end |
#setnomoretags ⇒ Object
77 78 79 80 |
# File 'lib/html/sgml-parser.rb', line 77 def @nomoretags = true @literal = true end |
#unknown_charref(ref) ⇒ Object
366 367 |
# File 'lib/html/sgml-parser.rb', line 366 def unknown_charref(ref) end |
#unknown_endtag(tag) ⇒ Object
364 365 |
# File 'lib/html/sgml-parser.rb', line 364 def unknown_endtag(tag) end |
#unknown_entityref(ref) ⇒ Object
368 369 |
# File 'lib/html/sgml-parser.rb', line 368 def unknown_entityref(ref) end |
#unknown_starttag(tag, attrs) ⇒ Object
362 363 |
# File 'lib/html/sgml-parser.rb', line 362 def unknown_starttag(tag, attrs) end |