def markupIDL(doc): highlightingOccurred = False idlEls = findAll("pre.idl:not([data-no-idl]), xmp.idl:not([data-no-idl])", doc) for el in findAll("script[type=idl]", doc): # To help with syntax-highlighting, <script type=idl> is also allowed here. idlEls.append(el) el.tag = "pre" removeAttr(el, "type") addClass(el, "idl") # One pass with a silent parser to collect the symbol table. symbolTable = None for el in idlEls: p = parser.Parser(textContent(el), ui=IDLSilent(), symbol_table=symbolTable) symbolTable = p.symbol_table # Then a real pass to actually mark up the IDL, # and collect it for the index. for el in idlEls: if isNormative(el, doc): text = textContent(el) # Parse once with a fresh parser, so I can spit out just this <pre>'s markup. widl = parser.Parser(text, ui=IDLUI(), symbol_table=symbolTable) marker = DebugMarker() if doc.debug else IDLMarker() replaceContents(el, parseHTML(str(widl.markup(marker)))) # Parse a second time with the global one, which collects all data in the doc. doc.widl.parse(text) addClass(el, "highlight") highlightingOccurred = True if doc.md.slimBuildArtifact: # Remove the highlight-only spans for el in idlEls: for span in findAll("span", el): contents = childNodes(span, clear=True) replaceNode(span, *contents) return if highlightingOccurred: doc.extraStyles[ 'style-syntax-highlighting'] += "pre.idl.highlight { color: #708090; }"
def highlightWithWebIDL(text, el): from widlparser import parser """ Trick the widlparser emitter, which wants to output HTML via wrapping with start/end tags, into instead outputting a stack-based text format. A \1 indicates a new stack push; the text between the \1 and the \2 is the attr to be pushed. A \3 indicates a stack pop. All other text is colored with the attr currently on top of the stack. """ class IDLUI: def warn(self, msg): die("{0}", msg.rstrip()) class HighlightMarker: # Just applies highlighting classes to IDL stuff. def markup_type_name(self, text, construct): return ("\1n\2", "\3") def markup_name(self, text, construct): return ("\1g\2", "\3") def markup_keyword(self, text, construct): return ("\1b\2", "\3") def markup_enum_value(self, text, construct): return ("\1s\2", "\3") if "\1" in text or "\2" in text or "\3" in text: die( "WebIDL text contains some U+0001-0003 characters, which are used by the highlighter. This block can't be highlighted. :(", el=el, ) return widl = parser.Parser(text, IDLUI()) return coloredTextFromWidlStack(str(widl.markup(HighlightMarker())))
input_lines = input.split('\n') output_lines = output.split('\n') for input_line, output_line in itertools.izip_longest(input_lines, output_lines, fillvalue=''): if (input_line != output_line): print("<" + input_line) print(">" + output_line) print() if __name__ == "__main__": # called from the command line sys.excepthook = debugHook sys.stdout = codecs.getwriter('utf8')(sys.stdout) parser = parser.Parser(ui=ui()) if (1 < len(sys.argv)): for fileName in sys.argv[1:]: print("Parsing: " + fileName) file = open(fileName) parser.reset() text = file.read() parser.parse(text) assert (text == unicode(parser)) quit() idl = u"""dictionary CSSFontFaceLoadEventInit : EventInit { sequence<CSSFontFaceRule> fontfaces = [ ]; }; interface Simple{ serializer; serializer = { foo };
def getParser(): return parser.Parser(ui=IDLSilent())