1 | #!/usr/bin/env python2
|
2 | """
|
3 | lazylex/html.py - Low-Level HTML Processing.
|
4 |
|
5 | See lazylex/README.md for details.
|
6 |
|
7 | Conflicts between HTML5 and XML:
|
8 |
|
9 | - In XML, <source> is like any tag, and must be closed,
|
10 | - In HTML, <source> is a VOID tag, and must NOT be closedlike any tag, and must be closed,
|
11 |
|
12 | - In XML, <script> and <style> don't have special treatment
|
13 | - In HTML, they do
|
14 |
|
15 | - The header is different - <!DOCTYPE html> vs. <?xml version= ... ?>
|
16 |
|
17 | So do have a mode for <script> <style> and void tags? Upgrade HX8 into HTM8?
|
18 | """
|
19 | from __future__ import print_function
|
20 |
|
21 | try:
|
22 | from cStringIO import StringIO
|
23 | except ImportError:
|
24 | from io import StringIO # python3
|
25 | import re
|
26 | import sys
|
27 |
|
28 | if sys.version_info.major == 2:
|
29 | from typing import List, Tuple, Optional
|
30 |
|
31 |
|
32 | def log(msg, *args):
|
33 | msg = msg % args
|
34 | print(msg, file=sys.stderr)
|
35 |
|
36 |
|
37 | class LexError(Exception):
|
38 | """
|
39 | Examples of lex errors:
|
40 |
|
41 | - Tok.Invalid, like <> or &&
|
42 | - Unclosed <!-- <? <![CDATA[ <script> <style>
|
43 | """
|
44 |
|
45 | def __init__(self, s, start_pos):
|
46 | self.s = s
|
47 | self.start_pos = start_pos
|
48 |
|
49 | def __str__(self):
|
50 | return '(LexError %r)' % (self.s[self.start_pos:self.start_pos + 20])
|
51 |
|
52 |
|
53 | def FindLineNum(s, error_pos):
|
54 | current_pos = 0
|
55 | line_num = 1
|
56 | while True:
|
57 | newline_pos = s.find('\n', current_pos)
|
58 | #log('current = %d, N %d, line %d', current_pos, newline_pos, line_num)
|
59 |
|
60 | if newline_pos == -1: # this is the last line
|
61 | return line_num
|
62 | if newline_pos >= error_pos:
|
63 | return line_num
|
64 | line_num += 1
|
65 | current_pos = newline_pos + 1
|
66 |
|
67 |
|
68 | class ParseError(Exception):
|
69 | """
|
70 | Examples of parse errors
|
71 |
|
72 | - unbalanced tag structure
|
73 | - ul_table.py errors
|
74 | """
|
75 |
|
76 | def __init__(self, msg, s=None, start_pos=-1):
|
77 | self.msg = msg
|
78 | self.s = s
|
79 | self.start_pos = start_pos
|
80 |
|
81 | def __str__(self):
|
82 | if self.s is not None:
|
83 | assert self.start_pos != -1, self.start_pos
|
84 | snippet = (self.s[self.start_pos:self.start_pos + 20])
|
85 |
|
86 | line_num = FindLineNum(self.s, self.start_pos)
|
87 | else:
|
88 | snippet = ''
|
89 | line_num = -1
|
90 | msg = 'line %d: %r %r' % (line_num, self.msg, snippet)
|
91 | return msg
|
92 |
|
93 |
|
94 | class Output(object):
|
95 | """Takes an underlying input buffer and an output file. Maintains a
|
96 | position in the input buffer.
|
97 |
|
98 | Print FROM the input or print new text to the output.
|
99 | """
|
100 |
|
101 | def __init__(self, s, f, left_pos=0, right_pos=-1):
|
102 | self.s = s
|
103 | self.f = f
|
104 | self.pos = left_pos
|
105 | self.right_pos = len(s) if right_pos == -1 else right_pos
|
106 |
|
107 | def SkipTo(self, pos):
|
108 | """Skip to a position."""
|
109 | self.pos = pos
|
110 |
|
111 | def PrintUntil(self, pos):
|
112 | """Print until a position."""
|
113 | piece = self.s[self.pos:pos]
|
114 | self.f.write(piece)
|
115 | self.pos = pos
|
116 |
|
117 | def PrintTheRest(self):
|
118 | """Print until the end of the string."""
|
119 | self.PrintUntil(self.right_pos)
|
120 |
|
121 | def Print(self, s):
|
122 | """Print text to the underlying buffer."""
|
123 | self.f.write(s)
|
124 |
|
125 |
|
126 | # HTML Tokens
|
127 | # CommentBegin, ProcessingBegin, CDataBegin are "pseudo-tokens", not visible
|
128 | TOKENS = 'Decl Comment CommentBegin Processing ProcessingBegin CData CDataBegin StartTag StartEndTag EndTag DecChar HexChar CharEntity RawData HtmlCData Invalid EndOfStream'.split(
|
129 | )
|
130 |
|
131 |
|
132 | class Tok(object):
|
133 | """
|
134 | Avoid lint errors by using these aliases
|
135 | """
|
136 | pass
|
137 |
|
138 |
|
139 | TOKEN_NAMES = [None] * len(TOKENS) # type: List[str]
|
140 |
|
141 | this_module = sys.modules[__name__]
|
142 | for i, tok_str in enumerate(TOKENS):
|
143 | setattr(this_module, tok_str, i)
|
144 | setattr(Tok, tok_str, i)
|
145 | TOKEN_NAMES[i] = tok_str
|
146 |
|
147 |
|
148 | def TokenName(tok_id):
|
149 | return TOKEN_NAMES[tok_id]
|
150 |
|
151 |
|
152 | def MakeLexer(rules):
|
153 | return [(re.compile(pat, re.VERBOSE), i) for (pat, i) in rules]
|
154 |
|
155 |
|
156 | #
|
157 | # Eggex
|
158 | #
|
159 | # Tag = / ~['>']+ /
|
160 |
|
161 | # Is this valid? A single character?
|
162 | # Tag = / ~'>'* /
|
163 |
|
164 | # Maybe better: / [NOT '>']+/
|
165 | # capital letters not allowed there?
|
166 | #
|
167 | # But then this is confusing:
|
168 | # / [NOT ~digit]+/
|
169 | #
|
170 | # / [NOT digit] / is [^\d]
|
171 | # / ~digit / is \D
|
172 | #
|
173 | # Or maybe:
|
174 | #
|
175 | # / [~ digit]+ /
|
176 | # / [~ '>']+ /
|
177 | # / [NOT '>']+ /
|
178 |
|
179 | # End = / '</' Tag '>' /
|
180 | # StartEnd = / '<' Tag '/>' /
|
181 | # Start = / '<' Tag '>' /
|
182 | #
|
183 | # EntityRef = / '&' dot{* N} ';' /
|
184 |
|
185 | # Tag name, or attribute name
|
186 | # colon is used in XML
|
187 |
|
188 | # https://www.w3.org/TR/xml/#NT-Name
|
189 | # Hm there is a lot of unicode stuff. We are simplifying parsing
|
190 |
|
191 | _NAME = r'[a-zA-Z][a-zA-Z0-9:_\-]*' # must start with letter
|
192 |
|
193 | LEXER = [
|
194 | (r'<!--', Tok.CommentBegin),
|
195 |
|
196 | # Processing instruction are used for the XML header:
|
197 | # <?xml version="1.0" encoding="UTF-8"?>
|
198 | # They are technically XML-only, but in HTML5, they are another kind of
|
199 | # comment:
|
200 | #
|
201 | # https://developer.mozilla.org/en-US/docs/Web/API/ProcessingInstruction
|
202 | #
|
203 | (r'<\?', Tok.ProcessingBegin),
|
204 | # Not necessary in HTML5, but occurs in XML
|
205 | (r'<!\[CDATA\[', Tok.CDataBegin), # <![CDATA[
|
206 |
|
207 | # Markup declarations
|
208 | # - In HTML5, there is only <!DOCTYPE html>
|
209 | # - XML has 4 more declarations: <!ELEMENT ...> ATTLIST ENTITY NOTATION
|
210 | # - these seem to be part of DTD
|
211 | # - it's useful to skip these, and be able to parse the rest of the document
|
212 | # - Note: < is allowed?
|
213 | (r'<! [^>]+ >', Tok.Decl),
|
214 |
|
215 | # Tags
|
216 | # Notes:
|
217 | # - We look for a valid tag name, but we don't validate attributes.
|
218 | # That's done in the tag lexer.
|
219 | # - We don't allow leading whitespace
|
220 | (r'</ (%s) >' % _NAME, Tok.EndTag),
|
221 | # self-closing <br/> comes before StartTag
|
222 | # could/should these be collapsed into one rule?
|
223 | (r'< (%s) [^>]* />' % _NAME, Tok.StartEndTag), # end </a>
|
224 | (r'< (%s) [^>]* >' % _NAME, Tok.StartTag), # start <a>
|
225 |
|
226 | # Characters
|
227 | # https://www.w3.org/TR/xml/#sec-references
|
228 | (r'&\# [0-9]+ ;', Tok.DecChar),
|
229 | (r'&\# x[0-9a-fA-F]+ ;', Tok.HexChar),
|
230 | (r'& %s ;' % _NAME, Tok.CharEntity),
|
231 |
|
232 | # HTML5 allows unescaped > in raw data, but < is not allowed.
|
233 | # https://stackoverflow.com/questions/10462348/right-angle-bracket-in-html
|
234 | #
|
235 | # - My early blog has THREE errors when disallowing >
|
236 | # - So do some .wwz files
|
237 | (r'[^&<]+', Tok.RawData),
|
238 | (r'.', Tok.Invalid), # error!
|
239 | ]
|
240 |
|
241 | # Old notes:
|
242 | #
|
243 | # Non-greedy matches are regular and can be matched in linear time
|
244 | # with RE2.
|
245 | #
|
246 | # https://news.ycombinator.com/item?id=27099798
|
247 | #
|
248 | # Maybe try combining all of these for speed.
|
249 |
|
250 | # . is any char except newline
|
251 | # https://re2c.org/manual/manual_c.html
|
252 |
|
253 | # Discarded options
|
254 | #(r'<!-- .*? -->', Tok.Comment),
|
255 |
|
256 | # Hack from Claude: \s\S instead of re.DOTALL. I don't like this
|
257 | #(r'<!-- [\s\S]*? -->', Tok.Comment),
|
258 | #(r'<!-- (?:.|[\n])*? -->', Tok.Comment),
|
259 |
|
260 | LEXER = MakeLexer(LEXER)
|
261 |
|
262 |
|
263 | class Lexer(object):
|
264 |
|
265 | def __init__(self, s, left_pos=0, right_pos=-1, no_special_tags=False):
|
266 | self.s = s
|
267 | self.pos = left_pos
|
268 | self.right_pos = len(s) if right_pos == -1 else right_pos
|
269 | self.no_special_tags = no_special_tags
|
270 |
|
271 | self.cache = {} # string -> compiled regex pattern object
|
272 |
|
273 | # either </script> or </style> - we search until we see that
|
274 | self.search_state = None # type: Optional[str]
|
275 |
|
276 | # Position of tag name, if applicable
|
277 | # - Set after you get a StartTag, EndTag, or StartEndTag
|
278 | # - Unset on other tags
|
279 | self.tag_pos_left = -1
|
280 | self.tag_pos_right = -1
|
281 |
|
282 | def _Peek(self):
|
283 | # type: () -> Tuple[int, int]
|
284 | """
|
285 | Note: not using _Peek() now
|
286 | """
|
287 | if self.pos == self.right_pos:
|
288 | return Tok.EndOfStream, self.pos
|
289 |
|
290 | assert self.pos < self.right_pos, self.pos
|
291 |
|
292 | if self.search_state is not None and not self.no_special_tags:
|
293 | pos = self.s.find(self.search_state, self.pos)
|
294 | if pos == -1:
|
295 | # unterminated <script> or <style>
|
296 | raise LexError(self.s, self.pos)
|
297 | self.search_state = None
|
298 | # beginning
|
299 | return Tok.HtmlCData, pos
|
300 |
|
301 | # Find the first match.
|
302 | # Note: frontend/match.py uses _LongestMatch(), which is different!
|
303 | # TODO: reconcile them. This lexer should be expressible in re2c.
|
304 |
|
305 | for pat, tok_id in LEXER:
|
306 | m = pat.match(self.s, self.pos)
|
307 | if m:
|
308 | if tok_id in (Tok.StartTag, Tok.EndTag, Tok.StartEndTag):
|
309 | self.tag_pos_left = m.start(1)
|
310 | self.tag_pos_right = m.end(1)
|
311 | else:
|
312 | # Reset state
|
313 | self.tag_pos_left = -1
|
314 | self.tag_pos_right = -1
|
315 |
|
316 | if tok_id == Tok.CommentBegin:
|
317 | pos = self.s.find('-->', self.pos)
|
318 | if pos == -1:
|
319 | # unterminated <!--
|
320 | raise LexError(self.s, self.pos)
|
321 | return Tok.Comment, pos + 3 # -->
|
322 |
|
323 | if tok_id == Tok.ProcessingBegin:
|
324 | pos = self.s.find('?>', self.pos)
|
325 | if pos == -1:
|
326 | # unterminated <?
|
327 | raise LexError(self.s, self.pos)
|
328 | return Tok.Processing, pos + 2 # ?>
|
329 |
|
330 | if tok_id == Tok.CDataBegin:
|
331 | pos = self.s.find(']]>', self.pos)
|
332 | if pos == -1:
|
333 | # unterminated <![CDATA[
|
334 | raise LexError(self.s, self.pos)
|
335 | return Tok.CData, pos + 3 # ]]>
|
336 |
|
337 | if tok_id == Tok.StartTag:
|
338 | if self.TagNameEquals('script'):
|
339 | self.search_state = '</script>'
|
340 | elif self.TagNameEquals('style'):
|
341 | self.search_state = '</style>'
|
342 |
|
343 | return tok_id, m.end()
|
344 | else:
|
345 | raise AssertionError('Tok.Invalid rule should have matched')
|
346 |
|
347 | def TagNameEquals(self, expected):
|
348 | # type: (str) -> bool
|
349 | assert self.tag_pos_left != -1, self.tag_pos_left
|
350 | assert self.tag_pos_right != -1, self.tag_pos_right
|
351 |
|
352 | # TODO: In C++, this does not need an allocation
|
353 | # TODO: conditionally lower() case here (maybe not in XML mode)
|
354 | return expected == self.s[self.tag_pos_left:self.tag_pos_right]
|
355 |
|
356 | def TagName(self):
|
357 | # type: () -> None
|
358 | assert self.tag_pos_left != -1, self.tag_pos_left
|
359 | assert self.tag_pos_right != -1, self.tag_pos_right
|
360 |
|
361 | # TODO: conditionally lower() case here (maybe not in XML mode)
|
362 | return self.s[self.tag_pos_left:self.tag_pos_right]
|
363 |
|
364 | def Read(self):
|
365 | # type: () -> Tuple[int, int]
|
366 | tok_id, end_pos = self._Peek()
|
367 | self.pos = end_pos # advance
|
368 | return tok_id, end_pos
|
369 |
|
370 | def LookAhead(self, regex):
|
371 | # Cache the regex compilation. This could also be LookAheadFor(THEAD)
|
372 | # or something.
|
373 | pat = self.cache.get(regex)
|
374 | if pat is None:
|
375 | pat = re.compile(regex)
|
376 | self.cache[regex] = pat
|
377 |
|
378 | m = pat.match(self.s, self.pos)
|
379 | return m is not None
|
380 |
|
381 |
|
382 | def _Tokens(s, left_pos, right_pos):
|
383 | """
|
384 | Args:
|
385 | s: string to parse
|
386 | left_pos, right_pos: Optional span boundaries.
|
387 | """
|
388 | lx = Lexer(s, left_pos, right_pos)
|
389 | while True:
|
390 | tok_id, pos = lx.Read()
|
391 | yield tok_id, pos
|
392 | if tok_id == Tok.EndOfStream:
|
393 | break
|
394 |
|
395 |
|
396 | def ValidTokens(s, left_pos=0, right_pos=-1):
|
397 | """Wrapper around _Tokens to prevent callers from having to handle Invalid.
|
398 |
|
399 | I'm not combining the two functions because I might want to do a
|
400 | 'yield' transformation on Tokens()? Exceptions might complicate the
|
401 | issue?
|
402 | """
|
403 | pos = left_pos
|
404 | for tok_id, end_pos in _Tokens(s, left_pos, right_pos):
|
405 | if tok_id == Tok.Invalid:
|
406 | raise LexError(s, pos)
|
407 | yield tok_id, end_pos
|
408 | pos = end_pos
|
409 |
|
410 |
|
411 | def ValidTokenList(s, no_special_tags=False):
|
412 | """A wrapper that can be more easily translated to C++. Doesn't use iterators."""
|
413 |
|
414 | start_pos = 0
|
415 | tokens = []
|
416 | lx = Lexer(s, no_special_tags=no_special_tags)
|
417 | while True:
|
418 | tok_id, end_pos = lx.Read()
|
419 | tokens.append((tok_id, end_pos))
|
420 | if tok_id == Tok.EndOfStream:
|
421 | break
|
422 | if tok_id == Tok.Invalid:
|
423 | raise LexError(s, start_pos)
|
424 | start_pos = end_pos
|
425 | return tokens
|
426 |
|
427 |
|
428 | # Tag names:
|
429 | # Match <a or </a
|
430 | # Match <h2, but not <2h
|
431 | #
|
432 | # HTML 5 doesn't restrict tag names at all
|
433 | # https://html.spec.whatwg.org/#toc-syntax
|
434 | #
|
435 | # XML allows : - .
|
436 | # https://www.w3.org/TR/xml/#NT-NameChar
|
437 |
|
438 | # Namespaces for MathML, SVG
|
439 | # XLink, XML, XMLNS
|
440 | #
|
441 | # https://infra.spec.whatwg.org/#namespaces
|
442 | #
|
443 | # Allow - for td-attrs
|
444 |
|
445 | # allow underscore/hyphen. what about colons, like _NAME?
|
446 | _UNQUOTED_VALUE = r'[a-zA-Z0-9_\-]+'
|
447 |
|
448 | # TODO: we don't need to capture the tag name here? That's done at the top
|
449 | # level
|
450 | _TAG_RE = re.compile(r'/? \s* (%s)' % _NAME, re.VERBOSE)
|
451 |
|
452 | _TAG_LAST_RE = re.compile(r'\s* /? >', re.VERBOSE)
|
453 |
|
454 | # To match href="foo"
|
455 | # Note: in HTML5 and XML, single quoted attributes are also valid
|
456 |
|
457 | # <button disabled> is standard usage
|
458 |
|
459 | _ATTR_RE = re.compile(
|
460 | r'''
|
461 | \s+ # Leading whitespace is required
|
462 | (%s) # Attribute name
|
463 | (?: # Optional attribute value
|
464 | \s* = \s*
|
465 | (?:
|
466 | " ([^>"]*) " # double quoted value
|
467 | | (%s) # Attribute value
|
468 | )
|
469 | )?
|
470 | ''' % (_NAME, _UNQUOTED_VALUE), re.VERBOSE)
|
471 |
|
472 | TagName, AttrName, UnquotedValue, QuotedValue = range(4)
|
473 |
|
474 |
|
475 | class TagLexer(object):
|
476 | """
|
477 | Given a tag like <a href="..."> or <link type="..." />, the TagLexer
|
478 | provides a few operations:
|
479 |
|
480 | - What is the tag?
|
481 | - Iterate through the attributes, giving (name, value_start_pos, value_end_pos)
|
482 | """
|
483 |
|
484 | def __init__(self, s):
|
485 | self.s = s
|
486 | self.start_pos = -1 # Invalid
|
487 | self.end_pos = -1
|
488 |
|
489 | def Reset(self, start_pos, end_pos):
|
490 | """Reuse instances of this object."""
|
491 | self.start_pos = start_pos
|
492 | self.end_pos = end_pos
|
493 |
|
494 | def TagString(self):
|
495 | return self.s[self.start_pos:self.end_pos]
|
496 |
|
497 | def TagName(self):
|
498 | # First event
|
499 | tok_id, start, end = next(self.Tokens())
|
500 | return self.s[start:end]
|
501 |
|
502 | def GetSpanForAttrValue(self, attr_name):
|
503 | """
|
504 | Used by oils_doc.py, for href shortcuts
|
505 | """
|
506 | # Algorithm: search for QuotedValue or UnquotedValue after AttrName
|
507 | # TODO: Could also cache these
|
508 |
|
509 | events = self.Tokens()
|
510 | val = (-1, -1)
|
511 | try:
|
512 | while True:
|
513 | tok_id, start, end = next(events)
|
514 | if tok_id == AttrName:
|
515 | name = self.s[start:end]
|
516 | if name == attr_name:
|
517 | # The value should come next
|
518 | tok_id, start, end = next(events)
|
519 | if tok_id in (QuotedValue, UnquotedValue):
|
520 | # Note: quoted values may have &
|
521 | # We would need ANOTHER lexer to unescape them.
|
522 | # Right now help_gen.py and oils_doc.py
|
523 | val = start, end
|
524 | break
|
525 |
|
526 | except StopIteration:
|
527 | pass
|
528 | return val
|
529 |
|
530 | def GetAttrRaw(self, attr_name):
|
531 | """
|
532 | Return the value, which may be UNESCAPED.
|
533 | """
|
534 | start, end = self.GetSpanForAttrValue(attr_name)
|
535 | if start == -1:
|
536 | return None
|
537 | return self.s[start:end]
|
538 |
|
539 | def AllAttrsRaw(self):
|
540 | """
|
541 | Get a list of pairs [('class', 'foo'), ('href', '?foo=1&bar=2')]
|
542 |
|
543 | The quoted values may be escaped. We would need another lexer to
|
544 | unescape them.
|
545 | """
|
546 | pairs = []
|
547 | events = self.Tokens()
|
548 | try:
|
549 | while True:
|
550 | tok_id, start, end = next(events)
|
551 | if tok_id == AttrName:
|
552 | name = self.s[start:end]
|
553 |
|
554 | # The value should come next
|
555 | tok_id, start, end = next(events)
|
556 | if tok_id in (QuotedValue, UnquotedValue):
|
557 | # Note: quoted values may have &
|
558 | # We would need ANOTHER lexer to unescape them, but we
|
559 | # don't need that for ul-table
|
560 |
|
561 | val = self.s[start:end]
|
562 | pairs.append((name, val))
|
563 | except StopIteration:
|
564 | pass
|
565 | return pairs
|
566 |
|
567 | def Tokens(self):
|
568 | """
|
569 | Yields a sequence of tokens: Tag (AttrName AttrValue?)*
|
570 |
|
571 | Where each Token is (Type, start_pos, end_pos)
|
572 |
|
573 | Note that start and end are NOT redundant! We skip over some unwanted
|
574 | characters.
|
575 | """
|
576 | m = _TAG_RE.match(self.s, self.start_pos + 1)
|
577 | if not m:
|
578 | raise RuntimeError("Couldn't find HTML tag in %r" %
|
579 | self.TagString())
|
580 | yield TagName, m.start(1), m.end(1)
|
581 |
|
582 | pos = m.end(0)
|
583 | #log('POS %d', pos)
|
584 |
|
585 | while True:
|
586 | # don't search past the end
|
587 | m = _ATTR_RE.match(self.s, pos, self.end_pos)
|
588 | if not m:
|
589 | #log('BREAK pos %d', pos)
|
590 | break
|
591 | #log('AttrName %r', m.group(1))
|
592 |
|
593 | yield AttrName, m.start(1), m.end(1)
|
594 |
|
595 | # Quoted is group 2, unquoted is group 3.
|
596 | if m.group(2) is not None:
|
597 | yield QuotedValue, m.start(2), m.end(2)
|
598 | elif m.group(3) is not None:
|
599 | yield UnquotedValue, m.start(3), m.end(3)
|
600 |
|
601 | # Skip past the "
|
602 | pos = m.end(0)
|
603 |
|
604 | #log('TOK %r', self.s)
|
605 |
|
606 | m = _TAG_LAST_RE.match(self.s, pos)
|
607 | #log('_TAG_LAST_RE match %r', self.s[pos:])
|
608 | if not m:
|
609 | # Extra data at end of tag. TODO: add messages for all these.
|
610 | raise LexError(self.s, pos)
|
611 |
|
612 |
|
613 | def ReadUntilStartTag(it, tag_lexer, tag_name):
|
614 | """Find the next <foo>, returning its (start, end) positions
|
615 |
|
616 | Raise ParseError if it's not found.
|
617 |
|
618 | tag_lexer is RESET.
|
619 | """
|
620 | pos = 0
|
621 | while True:
|
622 | try:
|
623 | tok_id, end_pos = next(it)
|
624 | except StopIteration:
|
625 | break
|
626 | tag_lexer.Reset(pos, end_pos)
|
627 | if tok_id == Tok.StartTag and tag_lexer.TagName() == tag_name:
|
628 | return pos, end_pos
|
629 |
|
630 | pos = end_pos
|
631 |
|
632 | raise ParseError('No start tag %r' % tag_name)
|
633 |
|
634 |
|
635 | def ReadUntilEndTag(it, tag_lexer, tag_name):
|
636 | """Find the next </foo>, returning its (start, end) position
|
637 |
|
638 | Raise ParseError if it's not found.
|
639 |
|
640 | tag_lexer is RESET.
|
641 | """
|
642 | pos = 0
|
643 | while True:
|
644 | try:
|
645 | tok_id, end_pos = next(it)
|
646 | except StopIteration:
|
647 | break
|
648 | tag_lexer.Reset(pos, end_pos)
|
649 | if tok_id == Tok.EndTag and tag_lexer.TagName() == tag_name:
|
650 | return pos, end_pos
|
651 |
|
652 | pos = end_pos
|
653 |
|
654 | raise ParseError('No end tag %r' % tag_name)
|
655 |
|
656 |
|
657 | CHAR_ENTITY = {
|
658 | 'amp': '&',
|
659 | 'lt': '<',
|
660 | 'gt': '>',
|
661 | 'quot': '"',
|
662 | }
|
663 |
|
664 |
|
665 | def ToText(s, left_pos=0, right_pos=-1):
|
666 | """Given HTML, return text by unquoting > and < etc.
|
667 |
|
668 | Used by:
|
669 | doctools/oils_doc.py: PygmentsPlugin
|
670 | doctools/help_gen.py: HelpIndexCards
|
671 |
|
672 | In the latter case, we cold process some tags, like:
|
673 |
|
674 | - Blue Link (not clickable, but still useful)
|
675 | - Red X
|
676 |
|
677 | That should be html.ToAnsi.
|
678 | """
|
679 | f = StringIO()
|
680 | out = Output(s, f, left_pos, right_pos)
|
681 |
|
682 | pos = left_pos
|
683 | for tok_id, end_pos in ValidTokens(s, left_pos, right_pos):
|
684 | if tok_id == Tok.RawData:
|
685 | out.SkipTo(pos)
|
686 | out.PrintUntil(end_pos)
|
687 |
|
688 | elif tok_id == Tok.CharEntity: # &
|
689 |
|
690 | entity = s[pos + 1:end_pos - 1]
|
691 |
|
692 | out.SkipTo(pos)
|
693 | out.Print(CHAR_ENTITY[entity])
|
694 | out.SkipTo(end_pos)
|
695 |
|
696 | # Not handling these yet
|
697 | elif tok_id == Tok.HexChar:
|
698 | raise AssertionError('Hex Char %r' % s[pos:pos + 20])
|
699 |
|
700 | elif tok_id == Tok.DecChar:
|
701 | raise AssertionError('Dec Char %r' % s[pos:pos + 20])
|
702 |
|
703 | pos = end_pos
|
704 |
|
705 | out.PrintTheRest()
|
706 | return f.getvalue()
|
707 |
|
708 |
|
709 | # https://developer.mozilla.org/en-US/docs/Glossary/Void_element
|
710 | VOID_ELEMENTS = [
|
711 | 'area',
|
712 | 'base',
|
713 | 'br',
|
714 | 'col',
|
715 | 'embed',
|
716 | 'hr',
|
717 | 'img',
|
718 | 'input',
|
719 | 'link',
|
720 | 'meta',
|
721 | 'param',
|
722 | 'source',
|
723 | 'track',
|
724 | 'wbr',
|
725 | ]
|
726 |
|
727 | LEX_ATTRS = 1 << 1
|
728 | LEX_QUOTED_VALUES = 1 << 2 # href="?x=42&y=99"
|
729 | NO_SPECIAL_TAGS = 1 << 3 # <script> <style>, VOID tags, etc.
|
730 | BALANCED_TAGS = 1 << 4 # are tags balanced?
|
731 |
|
732 |
|
733 | def Validate(contents, flags, counters):
|
734 | # type: (str, int, Counters) -> None
|
735 |
|
736 | tag_lexer = TagLexer(contents)
|
737 | no_special_tags = bool(flags & NO_SPECIAL_TAGS)
|
738 | lx = Lexer(contents, no_special_tags=no_special_tags)
|
739 | tokens = []
|
740 | start_pos = 0
|
741 | tag_stack = []
|
742 | while True:
|
743 | tok_id, end_pos = lx.Read()
|
744 |
|
745 | if tok_id == Tok.Invalid:
|
746 | raise LexError(contents, start_pos)
|
747 | if tok_id == Tok.EndOfStream:
|
748 | break
|
749 |
|
750 | tokens.append((tok_id, end_pos))
|
751 |
|
752 | if tok_id == Tok.StartEndTag:
|
753 | counters.num_start_end_tags += 1
|
754 |
|
755 | tag_lexer.Reset(start_pos, end_pos)
|
756 | all_attrs = tag_lexer.AllAttrsRaw()
|
757 | counters.num_attrs += len(all_attrs)
|
758 | counters.debug_attrs.extend(all_attrs)
|
759 |
|
760 | elif tok_id == Tok.StartTag:
|
761 | counters.num_start_tags += 1
|
762 |
|
763 | tag_lexer.Reset(start_pos, end_pos)
|
764 | all_attrs = tag_lexer.AllAttrsRaw()
|
765 | counters.num_attrs += len(all_attrs)
|
766 | counters.debug_attrs.extend(all_attrs)
|
767 |
|
768 | if flags & BALANCED_TAGS:
|
769 | tag_name = lx.TagName()
|
770 | if flags & NO_SPECIAL_TAGS:
|
771 | tag_stack.append(tag_name)
|
772 | else:
|
773 | # e.g. <meta> is considered self-closing, like <meta/>
|
774 | if tag_name not in VOID_ELEMENTS:
|
775 | tag_stack.append(tag_name)
|
776 |
|
777 | counters.max_tag_stack = max(counters.max_tag_stack,
|
778 | len(tag_stack))
|
779 | elif tok_id == Tok.EndTag:
|
780 | if flags & BALANCED_TAGS:
|
781 | try:
|
782 | expected = tag_stack.pop()
|
783 | except IndexError:
|
784 | raise ParseError('Tag stack empty',
|
785 | s=contents,
|
786 | start_pos=start_pos)
|
787 |
|
788 | actual = lx.TagName()
|
789 | if expected != actual:
|
790 | raise ParseError(
|
791 | 'Got unexpected closing tag %r; opening tag was %r' %
|
792 | (contents[start_pos:end_pos], expected),
|
793 | s=contents,
|
794 | start_pos=start_pos)
|
795 |
|
796 | start_pos = end_pos
|
797 |
|
798 | if len(tag_stack) != 0:
|
799 | raise ParseError('Missing closing tags at end of doc: %s' %
|
800 | ' '.join(tag_stack),
|
801 | s=contents,
|
802 | start_pos=start_pos)
|
803 |
|
804 | counters.num_tokens += len(tokens)
|
805 |
|
806 |
|
807 | class Counters(object):
|
808 |
|
809 | def __init__(self):
|
810 | self.num_tokens = 0
|
811 | self.num_start_tags = 0
|
812 | self.num_start_end_tags = 0
|
813 | self.num_attrs = 0
|
814 | self.max_tag_stack = 0
|
815 |
|
816 | self.debug_attrs = []
|
817 |
|
818 |
|
819 | def main(argv):
|
820 | action = argv[1]
|
821 |
|
822 | if action == 'tokens':
|
823 | contents = sys.stdin.read()
|
824 |
|
825 | lx = Lexer(contents)
|
826 | start_pos = 0
|
827 | while True:
|
828 | tok_id, end_pos = lx.Read()
|
829 | if tok_id == Tok.Invalid:
|
830 | raise LexError(contents, start_pos)
|
831 | if tok_id == Tok.EndOfStream:
|
832 | break
|
833 |
|
834 | frag = contents[start_pos:end_pos]
|
835 | log('%d %s %r', end_pos, TokenName(tok_id), frag)
|
836 | start_pos = end_pos
|
837 |
|
838 | return 0
|
839 |
|
840 | elif action in ('lex-htm8', 'parse-htm8', 'parse-xml'):
|
841 |
|
842 | errors = []
|
843 | counters = Counters()
|
844 |
|
845 | flags = LEX_ATTRS | LEX_QUOTED_VALUES
|
846 | if action.startswith('parse-'):
|
847 | flags |= BALANCED_TAGS
|
848 | if action == 'parse-xml':
|
849 | flags |= NO_SPECIAL_TAGS
|
850 |
|
851 | i = 0
|
852 | for line in sys.stdin:
|
853 | filename = line.strip()
|
854 | with open(filename) as f:
|
855 | contents = f.read()
|
856 |
|
857 | try:
|
858 | Validate(contents, flags, counters)
|
859 | except LexError as e:
|
860 | log('Lex error in %r: %s', filename, e)
|
861 | errors.append((filename, e))
|
862 | except ParseError as e:
|
863 | log('Parse error in %r: %s', filename, e)
|
864 | errors.append((filename, e))
|
865 | i += 1
|
866 |
|
867 | log('')
|
868 | log(
|
869 | ' %d tokens, %d start/end tags, %d start tags, %d attrs, %d max tag stack depth in %d files',
|
870 | counters.num_tokens, counters.num_start_end_tags,
|
871 | counters.num_start_tags, counters.num_attrs,
|
872 | counters.max_tag_stack, i)
|
873 | log(' %d errors', len(errors))
|
874 | if len(errors):
|
875 | return 1
|
876 | return 0
|
877 |
|
878 | elif action == 'todo':
|
879 | # Other algorithms:
|
880 | #
|
881 | # - select first subtree with given ID
|
882 | # - this requires understanding the void tags I suppose
|
883 | # - select all subtrees that have a class
|
884 | # - materialize DOM
|
885 |
|
886 | # Safe-HTM8? This is a filter
|
887 | return 0
|
888 |
|
889 | else:
|
890 | raise RuntimeError('Invalid action %r' % action)
|
891 |
|
892 |
|
893 | if __name__ == '__main__':
|
894 | sys.exit(main(sys.argv))
|