def to_html(self, docstring_linker, **options): """ Translate this docstring to HTML. @param docstring_linker: An HTML translator for crossreference links into and out of the docstring. @type docstring_linker: L{DocstringLinker} @param options: Any extra options for the output. Unknown options are ignored. @return: An HTML fragment that encodes this docstring. @rtype: C{string} """ # Default behavior: plaintext = plaintext_to_html(self.to_plaintext(docstring_linker)) return '<pre class="literalblock">\n%s\n</pre>\n' % plaintext
def to_html(self, docstring_linker, **options): """ Translate this docstring to HTML. @param docstring_linker: An HTML translator for crossreference links into and out of the docstring. @type docstring_linker: L{DocstringLinker} @param options: Any extra options for the output. Unknown options are ignored. @return: An HTML fragment that encodes this docstring. @rtype: C{string} """ # Default behavior: plaintext = plaintext_to_html(self.to_plaintext(docstring_linker)) return '<pre class="literalblock">\n%s\n</pre>\n' % plaintext
def to_html(self, docstring_linker, **options): if options.get('verbatim', self._verbatim) == 0: return plaintext_to_html(self.to_plaintext(docstring_linker)) else: return ParsedDocstring.to_html(self, docstring_linker, **options)
def markup(self, s, tag): if tag == 'other': return plaintext_to_html(s) else: return ('<span class="py-%s">%s</span>' % (tag, plaintext_to_html(s)))
def markup(self, s, tag): if tag == 'other': return plaintext_to_html(s) else: return ('<span class="py-%s">%s</span>' % (tag, plaintext_to_html(s)))
def to_html(self, docstring_linker, **options): if options.get('verbatim', self._verbatim) == 0: return plaintext_to_html(self.to_plaintext(docstring_linker)) else: return ParsedDocstring.to_html(self, docstring_linker, **options)
def _colorize_re(tree, noparen=0): """ Recursively descend the given regexp parse tree to produce the HTML code for a colorized version of the regexp. @param tree: The regexp parse tree for the regexp that should be colorized. @type tree: L{sre_parse.SubPattern} @param noparen: If true, then don't include parenthases around the expression in C{tree}, even if it contains multiple elements. @type noparen: C{boolean} @return: The HTML code for a colorized version of C{tree} @rtype: C{string} """ result = [] out = result.append if len(tree) > 1 and not noparen: out('<span class="%s">(</span>' % PAREN_TAG) for elt in tree: op = elt[0] args = elt[1] if op == sre_constants.LITERAL: c = unichr(args) if c == '\t': out(r'<span class="%s">\t</span>' % ESCAPE_TAG) elif c == '\n': out(r'<span class="%s">\n</span>' % ESCAPE_TAG) elif c == '\r': out(r'<span class="%s">\r</span>' % ESCAPE_TAG) elif c == '\f': out(r'<span class="%s">\f</span>' % ESCAPE_TAG) elif c == '\v': out(r'<span class="%s">\v</span>' % ESCAPE_TAG) elif ord(c)<32 or ord(c)>=127: if c < 256: template = r'<span class="%s">\x%02x</span>' else: template = r'<span class="%s">\u%04x</span>' out(template % (ESCAPE_TAG,ord(c))) elif c in '.^$\\*+?{}[]|()': out(r'<span class="%s">\%c</span>' % (ESCAPE_TAG, c)) else: out(plaintext_to_html(unichr(args))) continue elif op == sre_constants.ANY: out('<span class="%s">.</span>' % ANY_TAG) elif op == sre_constants.BRANCH: if args[0] is not None: raise ValueError('Branch expected None arg but got %s' % args[0]) VBAR = '<span class="%s">|</span>' % BRANCH_TAG out(VBAR.join([_colorize_re(item,1) for item in args[1]])) elif op == sre_constants.IN: if (len(args) == 1 and args[0][0] == sre_constants.CATEGORY): out(_colorize_re(args)) else: out('<span class="%s">[</span>' % CHOICE_TAG) out(_colorize_re(args, 1)) out('<span class="%s">]</span>' % CHOICE_TAG) elif op == sre_constants.CATEGORY: out('<span class="%s">' % CATEGORY_TAG) if args == sre_constants.CATEGORY_DIGIT: out(r'\d') elif args == sre_constants.CATEGORY_NOT_DIGIT: out(r'\D') elif args == sre_constants.CATEGORY_SPACE: out(r'\s') elif args == sre_constants.CATEGORY_NOT_SPACE: out(r'\S') elif args == sre_constants.CATEGORY_WORD: out(r'\w') elif args == sre_constants.CATEGORY_NOT_WORD: out(r'\W') else: raise ValueError('Unknown category %s' % args) out('</span>') elif op == sre_constants.AT: out('<span class="%s">' % AT_TAG) if args == sre_constants.AT_BEGINNING_STRING: out(r'\A') elif args == sre_constants.AT_BEGINNING: out(r'^') elif args == sre_constants.AT_END: out(r'$') elif args == sre_constants.AT_BOUNDARY: out(r'\b') elif args == sre_constants.AT_NON_BOUNDARY: out(r'\B') elif args == sre_constants.AT_END_STRING: out(r'\Z') else: raise ValueError('Unknown position %s' % args) out('</span>') elif op == sre_constants.MAX_REPEAT: min = args[0] max = args[1] if max == sre_constants.MAXREPEAT: if min == 0: out(_colorize_re(args[2])) out('<span class="%s">*</span>' % STAR_TAG) elif min == 1: out(_colorize_re(args[2])) out('<span class="%s">+</span>' % PLUS_TAG) else: out(_colorize_re(args[2])) out('<span class="%s">{%d,}</span>' % (RNG_TAG, min)) elif min == 0: if max == 1: out(_colorize_re(args[2])) out('<span class="%s">?</span>' % QMRK_TAG) else: out(_colorize_re(args[2])) out('<span class="%s">{,%d}</span>' % (RNG_TAG, max)) elif min == max: out(_colorize_re(args[2])) out('<span class="%s">{%d}</span>' % (RNG_TAG, max)) else: out(_colorize_re(args[2])) out('<span class="%s">{%d,%d}</span>' % (RNG_TAG, min, max)) elif op == sre_constants.MIN_REPEAT: min = args[0] max = args[1] if max == sre_constants.MAXREPEAT: if min == 0: out(_colorize_re(args[2])) out('<span class="%s">*?</span>' % STAR_TAG) elif min == 1: out(_colorize_re(args[2])) out('<span class="%s">+?</span>' % PLUS_TAG) else: out(_colorize_re(args[2])) out('<span class="%s">{%d,}?</span>' % (RNG_TAG, min)) elif min == 0: if max == 1: out(_colorize_re(args[2])) out('<span class="%s">??</span>' % QMRK_TAG) else: out(_colorize_re(args[2])) out('<span class="%s">{,%d}?</span>' % (RNG_TAG, max)) elif min == max: out(_colorize_re(args[2])) out('<span class="%s">{%d}?</span>' % (RNG_TAG, max)) else: out(_colorize_re(args[2])) out('<span class="%s">{%d,%d}?</span>'%(RNG_TAG, min, max)) elif op == sre_constants.SUBPATTERN: if args[0] is None: out('<span class="%s">(?:</span>' % PAREN_TAG) elif isinstance(args[0], (int, long)): # This is cheating: out('<span class="%s">(</span>' % PAREN_TAG) else: out('<span class="%s">(?P<</span>' % PAREN_TAG) out('<span class="%s">%s</span>' % (REF_TAG, plaintext_to_html(args[0]))) out('<span class="%s">></span>' % PAREN_TAG) out(_colorize_re(args[1], 1)) out('<span class="%s">)</span>' % PAREN_TAG) elif op == sre_constants.GROUPREF: out('<span class="%s">\\%d</span>' % (REF_TAG, args)) elif op == sre_constants.RANGE: start = _colorize_re( ((sre_constants.LITERAL, args[0]),) ) end = _colorize_re( ((sre_constants.LITERAL, args[1]),) ) out('%s<span class="%s">-</span>%s' % (start, CHOICE_TAG, end)) elif op == sre_constants.NEGATE: out('<span class="%s">^</span>' % CHOICE_TAG) elif op == sre_constants.ASSERT: if args[0]: out('<span class="%s">(?=</span>' % ASSERT_TAG) else: out('<span class="%s">(?<=</span>' % ASSERT_TAG) out(''.join(_colorize_re(args[1], 1))) out('<span class="%s">)</span>' % ASSERT_TAG) elif op == sre_constants.ASSERT_NOT: if args[0]: out('<span class="%s">(?!</span>' % ASSERT_TAG) else: out('<span class="%s">(?<!</span>' % ASSERT_TAG) out(''.join(_colorize_re(args[1], 1))) out('<span class="%s">)</span>' % ASSERT_TAG) elif op == sre_constants.NOT_LITERAL: lit = _colorize_re( ((sre_constants.LITERAL, args),) ) out('<span class="%s">[^</span>%s<span class="%s">]</span>' % (CHOICE_TAG, lit, CHOICE_TAG)) else: log.error("Error colorizing regexp: unknown elt %r" % elt) if len(tree) > 1 and not noparen: out('<span class="%s">)</span>' % PAREN_TAG) return u''.join(result)