python类highlight()的实例源码

ddquery.py 文件源码 项目:ddquery 作者: elinaldosoft 项目源码 文件源码 阅读 18 收藏 0 点赞 0 评论 0
def format(self, record):
        super(SqlFormatter, self).format(record)
        sql = record.sql.strip()

        if self.parse:
            sql = sqlparse.format(sql, reindent=self.reindent, keyword_case=self.keyword_case)
            if hasattr(record, 'duration'):
                sql = "({0:.3f}ms) {1}".format(record.duration, sql)

        if self.highlight:
            sql = highlight(
                    sql,
                    self._lexer,
                    self._formatter
                )

        return sql
superfences.py 文件源码 项目:sublime-text-3-packages 作者: nickjj 项目源码 文件源码 阅读 17 收藏 0 点赞 0 评论 0
def patch_fenced_rule(self):
        """
        Patch Python Markdown with our own fenced block extension.

        if the Python Markdown's 'fenced_code' extension was already configured,
        we will replace it.
        """

        config = self.getConfigs()
        fenced = SuperFencesBlockPreprocessor(self.markdown)
        indented_code = SuperFencesCodeBlockProcessor(self)
        fenced.config = config
        indented_code.config = config
        indented_code.markdown = self.markdown
        hiliter = SuperFencesHiliteTreeprocessor(self.markdown)
        hiliter.config = self.getConfigs()
        self.markdown.treeprocessors["hilite"] = hiliter
        self.markdown.superfences[0]["formatter"] = fenced.highlight
        self.markdown.parser.blockprocessors['code'] = indented_code
        self.markdown.preprocessors.add('fenced_code_block', fenced, ">normalize_whitespace")
util.py 文件源码 项目:blog-a 作者: richardchien 项目源码 文件源码 阅读 18 收藏 0 点赞 0 评论 0
def render_md(md_str):
    """
    Render a markdown string

    :param md_str: markdown string to render
    :return: html text
    """
    return misaka.Markdown(
        renderer,
        extensions=('fenced-code',
                    'tables',
                    'footnotes',
                    'autolink',
                    'strikethrough',
                    'underline',
                    'highlight',
                    'quote',
                    'superscript')
    )(md_str)


# regex to match the post file with correct format
parsers.py 文件源码 项目:quokka_ng 作者: rochacbruno 项目源码 文件源码 阅读 31 收藏 0 点赞 0 评论 0
def block_code(text, lang, inlinestyles=False, linenos=False):
    if not lang:
        text = text.strip()
        return u'<pre><code>%s</code></pre>\n' % mistune.escape(text)

    try:
        lexer = get_lexer_by_name(lang, stripall=True)
        formatter = html.HtmlFormatter(
            noclasses=inlinestyles, linenos=linenos
        )
        code = highlight(text, lexer, formatter)
        if linenos:
            return '<div class="highlight-wrapper">%s</div>\n' % code
        return code
    except BaseException:
        return '<pre class="%s"><code>%s</code></pre>\n' % (
            lang, mistune.escape(text)
        )
markdown.py 文件源码 项目:SiteFab 作者: ebursztein 项目源码 文件源码 阅读 25 收藏 0 点赞 0 评论 0
def block_code(self, code, lang):
        "Block code highlighter and formater"
        try:
            if not lang:
                lexer = guess_lexer(code, stripall=True)
            else:
                lexer = get_lexer_by_name(lang, stripall=True)
            detected = True
            code = highlight(code, lexer, self.code_formatter)
        except:
            code = escape(code)
            lang = None

        self.info.code.append(code)

        template = self.jinja2.get_template('code')
        rv = template.render(code=code, lang=lang, site=self.site, meta=self.meta)
        rv = rv.encode('utf-8')
        return rv
postprocessing.py 文件源码 项目:geekbook 作者: mmagnus 项目源码 文件源码 阅读 32 收藏 0 点赞 0 评论 0
def unhighlight(text):
    hits = re.findall(
        '<div class="highlight"><pre><span></span>(?P<text>.+?)</pre></div>', text, re.M | re.S)
    for h in hits:
        # print 'h',h.strip()
        if h.strip():
            if h.find('<span') == -1:  # it's note
                # print 'no span'
                h_and_context = re.findall(
                    r'<div class="highlight"><pre><span></span>' + re.escape(h) + '</pre></div>', text, re.M | re.S)
                if h_and_context:
                    h_and_context = h_and_context[0]
                    h_and_context_unhigh = h_and_context.replace(
                        '<div class="highlight">', '').replace('</pre></div>', '</pre>')
                    text = text.replace(h_and_context, h_and_context_unhigh)
            else:
                h_and_context = re.findall(
                    r'<div class="highlight"><pre><span></span>' + re.escape(h) + '</pre></div>', text, re.M | re.S)
                # print h_and_context
    return text
markdown2.py 文件源码 项目:awesome-python3-webapp 作者: syusonn 项目源码 文件源码 阅读 25 收藏 0 点赞 0 评论 0
def _color_with_pygments(self, codeblock, lexer, **formatter_opts):
        import pygments
        import pygments.formatters

        class HtmlCodeFormatter(pygments.formatters.HtmlFormatter):
            def _wrap_code(self, inner):
                """A function for use in a Pygments Formatter which
                wraps in <code> tags.
                """
                yield 0, "<code>"
                for tup in inner:
                    yield tup
                yield 0, "</code>"

            def wrap(self, source, outfile):
                """Return the source with a code, pre, and div."""
                return self._wrap_div(self._wrap_pre(self._wrap_code(source)))

        formatter_opts.setdefault("cssclass", "codehilite")
        formatter = HtmlCodeFormatter(**formatter_opts)
        return pygments.highlight(codeblock, lexer, formatter)
mail_formater.py 文件源码 项目:DualisWatcher 作者: LucaVazz 项目源码 文件源码 阅读 19 收藏 0 点赞 0 评论 0
def _format_code(html_code):
    # syntax highlighting:
    code_highlighted = highlight(html_code, HtmlLexer(), HtmlFormatter(style='monokai', noclasses=True))
    # add formatting for diff-markers:
    common_diff_style = ' margin-right: 3px; padding-right: 7px; padding-left: 7px;'
    code_formatted = code_highlighted \
        .replace('[-', '<span style="background: #71332c;' + common_diff_style + '">') \
        .replace('-]', '</span>') \
        .replace('{+', '<span style="background: #2e4d28;' + common_diff_style + '">') \
        .replace('+}', '</span>')

    # wrap in general layout:
    code_wrapped = '<div style="color: #efefef; margin: 3px; margin-left: 17px;line-height: 150%%;">%s</div>'\
                   %(code_formatted)

    return code_wrapped
markdown2.py 文件源码 项目:touch-pay-client 作者: HackPucBemobi 项目源码 文件源码 阅读 20 收藏 0 点赞 0 评论 0
def _color_with_pygments(self, codeblock, lexer, **formatter_opts):
        import pygments
        import pygments.formatters

        class HtmlCodeFormatter(pygments.formatters.HtmlFormatter):
            def _wrap_code(self, inner):
                """A function for use in a Pygments Formatter which
                wraps in <code> tags.
                """
                yield 0, "<code>"
                for tup in inner:
                    yield tup
                yield 0, "</code>"

            def wrap(self, source, outfile):
                """Return the source with a code, pre, and div."""
                return self._wrap_div(self._wrap_pre(self._wrap_code(source)))

        formatter_opts.setdefault("cssclass", "codehilite")
        formatter = HtmlCodeFormatter(**formatter_opts)
        return pygments.highlight(codeblock, lexer, formatter)
markdown2.py 文件源码 项目:python-awesome-web 作者: tianzhenyun 项目源码 文件源码 阅读 23 收藏 0 点赞 0 评论 0
def _color_with_pygments(self, codeblock, lexer, **formatter_opts):
        import pygments
        import pygments.formatters

        class HtmlCodeFormatter(pygments.formatters.HtmlFormatter):
            def _wrap_code(self, inner):
                """A function for use in a Pygments Formatter which
                wraps in <code> tags.
                """
                yield 0, "<code>"
                for tup in inner:
                    yield tup
                yield 0, "</code>"

            def wrap(self, source, outfile):
                """Return the source with a code, pre, and div."""
                return self._wrap_div(self._wrap_pre(self._wrap_code(source)))

        formatter_opts.setdefault("cssclass", "codehilite")
        formatter = HtmlCodeFormatter(**formatter_opts)
        return pygments.highlight(codeblock, lexer, formatter)
superfences.py 文件源码 项目:macos-st-packages 作者: zce 项目源码 文件源码 阅读 19 收藏 0 点赞 0 评论 0
def patch_fenced_rule(self):
        """
        Patch Python Markdown with our own fenced block extension.

        if the Python Markdown's 'fenced_code' extension was already configured,
        we will replace it.
        """

        config = self.getConfigs()
        fenced = SuperFencesBlockPreprocessor(self.markdown)
        indented_code = SuperFencesCodeBlockProcessor(self)
        fenced.config = config
        indented_code.config = config
        indented_code.markdown = self.markdown
        hiliter = SuperFencesHiliteTreeprocessor(self.markdown)
        hiliter.config = self.getConfigs()
        self.markdown.treeprocessors["hilite"] = hiliter
        self.markdown.superfences[0]["formatter"] = fenced.highlight
        self.markdown.parser.blockprocessors['code'] = indented_code
        self.markdown.preprocessors.add('fenced_code_block', fenced, ">normalize_whitespace")
util.py 文件源码 项目:exchangelib 作者: ecederstrand 项目源码 文件源码 阅读 19 收藏 0 点赞 0 评论 0
def emit(self, record):
        """Pretty-print and syntax highlight a log statement if all these conditions are met:
           * This is a DEBUG message
           * We're outputting to a terminal
           * The log message args is a dict containing keys starting with 'xml_' and values as bytes
        """
        if record.levelno == logging.DEBUG and self.is_tty() and isinstance(record.args, dict):
            for key, value in record.args.items():
                if not key.startswith('xml_'):
                    continue
                if not isinstance(value, bytes):
                    continue
                if not is_xml(value[:10].decode('utf-8', errors='ignore')):
                    continue
                try:
                    if PY2:
                        record.args[key] = self.highlight_xml(self.prettify_xml(value)).encode('utf-8')
                    else:
                        record.args[key] = self.highlight_xml(self.prettify_xml(value))
                except Exception:
                    # Something bad happened, but we don't want to crash the program just because logging failed
                    pass
        return super(PrettyXmlHandler, self).emit(record)
pyq.py 文件源码 项目:pyq 作者: caioariede 项目源码 文件源码 阅读 25 收藏 0 点赞 0 评论 0
def display_matches(m, selector, filename, opts):
    matches = matching_lines(m.match(selector, filename), filename)

    if opts.get('l'):
        files = {}
        for line, no, _ in matches:
            if opts.get('l'):
                if filename not in files:
                    click.echo(filename)
                    # do not repeat files
                    files[filename] = True

    else:
        lines = {}
        for line, no, col in matches:
            text = highlight(line.strip(), PythonLexer(), TerminalFormatter())
            if not opts['e']:
                if no not in lines:
                    lines[no] = True
                    click.echo('{}:{}  {}'.format(filename, no, text),
                               nl=False)
            else:
                click.echo('{}:{}:{}  {}'.format(filename, no, col, text),
                           nl=False)
highlight.py 文件源码 项目:sahriswiki 作者: prologic 项目源码 文件源码 阅读 19 收藏 0 点赞 0 评论 0
def highlight(text, mime=None, lang=None, linenos=False, title=""):
    formatter = HTMLFormatter(
        cssclass="code",
        linenos=linenos,
        full=True,
        title=title
    )

    try:
        if mime:
            lexer = pygments.lexers.get_lexer_for_mimetype(mime)
        elif lang:
            lexer = pygments.lexers.get_lexer_by_name(lang)
        else:
            lexer = pygments.lexers.guess_lexer(text)
    except pygments.util.ClassNotFound:
        return tag.pre(text)

    return Markup(pygments.highlight(text, lexer, formatter))
markdown2.py 文件源码 项目:true_review_web2py 作者: lucadealfaro 项目源码 文件源码 阅读 29 收藏 0 点赞 0 评论 0
def _color_with_pygments(self, codeblock, lexer, **formatter_opts):
        import pygments
        import pygments.formatters

        class HtmlCodeFormatter(pygments.formatters.HtmlFormatter):
            def _wrap_code(self, inner):
                """A function for use in a Pygments Formatter which
                wraps in <code> tags.
                """
                yield 0, "<code>"
                for tup in inner:
                    yield tup
                yield 0, "</code>"

            def wrap(self, source, outfile):
                """Return the source with a code, pre, and div."""
                return self._wrap_div(self._wrap_pre(self._wrap_code(source)))

        formatter_opts.setdefault("cssclass", "codehilite")
        formatter = HtmlCodeFormatter(**formatter_opts)
        return pygments.highlight(codeblock, lexer, formatter)
markdown2.py 文件源码 项目:python-webapp-blog 作者: tzshlyt 项目源码 文件源码 阅读 25 收藏 0 点赞 0 评论 0
def _color_with_pygments(self, codeblock, lexer, **formatter_opts):
        import pygments
        import pygments.formatters

        class HtmlCodeFormatter(pygments.formatters.HtmlFormatter):

            def _wrap_code(self, inner):
                """A function for use in a Pygments Formatter which
                wraps in <code> tags.
                """
                yield 0, "<code>"
                for tup in inner:
                    yield tup
                yield 0, "</code>"

            def wrap(self, source, outfile):
                """Return the source with a code, pre, and div."""
                return self._wrap_div(self._wrap_pre(self._wrap_code(source)))

        formatter_opts.setdefault("cssclass", "codehilite")
        formatter = HtmlCodeFormatter(**formatter_opts)
        return pygments.highlight(codeblock, lexer, formatter)
markdown2.py 文件源码 项目:spc 作者: whbrewer 项目源码 文件源码 阅读 27 收藏 0 点赞 0 评论 0
def _color_with_pygments(self, codeblock, lexer, **formatter_opts):
        import pygments
        import pygments.formatters

        class HtmlCodeFormatter(pygments.formatters.HtmlFormatter):
            def _wrap_code(self, inner):
                """A function for use in a Pygments Formatter which
                wraps in <code> tags.
                """
                yield 0, "<code>"
                for tup in inner:
                    yield tup
                yield 0, "</code>"

            def wrap(self, source, outfile):
                """Return the source with a code, pre, and div."""
                return self._wrap_div(self._wrap_pre(self._wrap_code(source)))

        formatter_opts.setdefault("cssclass", "codehilite")
        formatter = HtmlCodeFormatter(**formatter_opts)
        return pygments.highlight(codeblock, lexer, formatter)
views.py 文件源码 项目:incubator-airflow-old 作者: apache 项目源码 文件源码 阅读 21 收藏 0 点赞 0 评论 0
def code(self):
        dag_id = request.args.get('dag_id')
        dag = dagbag.get_dag(dag_id)
        title = dag_id
        try:
            with open(dag.fileloc, 'r') as f:
                code = f.read()
            html_code = highlight(
                code, lexers.PythonLexer(), HtmlFormatter(linenos=True))
        except IOError as e:
            html_code = str(e)

        return self.render(
            'airflow/dag_code.html', html_code=html_code, dag=dag, title=title,
            root=request.args.get('root'),
            demo_mode=conf.getboolean('webserver', 'demo_mode'))
shell.py 文件源码 项目:os-http 作者: openstack-dev 项目源码 文件源码 阅读 28 收藏 0 点赞 0 评论 0
def format_resp(resp):
    # I can see no way to get the HTTP version
    headers = ["HTTP/1.1 %d %s" % (resp.status_code, resp.reason or '')]
    headers.extend('%s: %s' % k for k in resp.headers.items())
    headers = '\n'.join(headers)

    if 'json' in resp.headers.get('Content-Type', '').lower():
        body = json.dumps(resp.json(), sort_keys=True, indent=4)
    else:
        body = resp.content

    if pygments:
        mime = resp.headers.get('Content-Type')
        http_lexer = pygments.lexers.get_lexer_by_name('http')
        formatter = pygments.formatters.get_formatter_by_name(formatter_name)

        try:
            body_lexer = pygments.lexers.get_lexer_for_mimetype(mime)
        except pygments.util.ClassNotFound:
            body_lexer = pygments.lexers.get_lexer_by_name('text')

        headers = pygments.highlight(headers, http_lexer, formatter)
        body = pygments.highlight(body, body_lexer, formatter)

    return '\n'.join([headers, '', body])
qt.py 文件源码 项目:fandango 作者: tango-controls 项目源码 文件源码 阅读 25 收藏 0 点赞 0 评论 0
def printf(self,txt,append=True,highlight=False,curr=''):
        if append: 
            try: 
                if hasattr(self.result,'page'):
                    curr = self.result.page().currentFrame().toHtml().replace('</body></html>','')
                    plain = self.result.page().currentFrame().toPlainText()
                else:
                    curr = self.result.toHtml().replace('</body></html>','')
                    plain = self.result.toPlainText()
            except: 
                traceback.print_exc()
                curr = ''
        txt = self.highlight(txt) if (highlight is True) else txt #highlight=1 can still be used to force highlighting
        if (self.css and '<style>' not in curr) or not curr or not append:
            txt = ('<html><head><style>%s</style></head><body>%s</body></html>'%(self.css,txt))
        else:
            txt = (str(curr)+'<br>'+txt+'</body></html>')
        self.result.setHtml(txt)
        try:
            self.result.keyPressEvent(Qt.QKeyEvent(Qt.QEvent.KeyPress,Qt.Qt.Key_End,Qt.Qt.NoModifier))
            #self.result.page().mainFrame().scroll(0,self.result.page().mainFrame().contentsSize().height())
        except:
            traceback.print_exc()
markdown2.py 文件源码 项目:Problematica-public 作者: TechMaz 项目源码 文件源码 阅读 22 收藏 0 点赞 0 评论 0
def _color_with_pygments(self, codeblock, lexer, **formatter_opts):
        import pygments
        import pygments.formatters

        class HtmlCodeFormatter(pygments.formatters.HtmlFormatter):
            def _wrap_code(self, inner):
                """A function for use in a Pygments Formatter which
                wraps in <code> tags.
                """
                yield 0, "<code>"
                for tup in inner:
                    yield tup
                yield 0, "</code>"

            def wrap(self, source, outfile):
                """Return the source with a code, pre, and div."""
                return self._wrap_div(self._wrap_pre(self._wrap_code(source)))

        formatter_opts.setdefault("cssclass", "codehilite")
        formatter = HtmlCodeFormatter(**formatter_opts)
        return pygments.highlight(codeblock, lexer, formatter)
markdown2.py 文件源码 项目:FBlog-python3-webapp 作者: fuyangzhen 项目源码 文件源码 阅读 22 收藏 0 点赞 0 评论 0
def _color_with_pygments(self, codeblock, lexer, **formatter_opts):
        import pygments
        import pygments.formatters

        class HtmlCodeFormatter(pygments.formatters.HtmlFormatter):
            def _wrap_code(self, inner):
                """A function for use in a Pygments Formatter which
                wraps in <code> tags.
                """
                yield 0, "<code>"
                for tup in inner:
                    yield tup
                yield 0, "</code>"

            def wrap(self, source, outfile):
                """Return the source with a code, pre, and div."""
                return self._wrap_div(self._wrap_pre(self._wrap_code(source)))

        formatter_opts.setdefault("cssclass", "codehilite")
        formatter = HtmlCodeFormatter(**formatter_opts)
        return pygments.highlight(codeblock, lexer, formatter)
markdown2.py 文件源码 项目:SublimeConfluence 作者: mlf4aiur 项目源码 文件源码 阅读 24 收藏 0 点赞 0 评论 0
def _color_with_pygments(self, codeblock, lexer, **formatter_opts):
        import pygments
        import pygments.formatters

        class HtmlCodeFormatter(pygments.formatters.HtmlFormatter):
            def _wrap_code(self, inner):
                """A function for use in a Pygments Formatter which
                wraps in <code> tags.
                """
                yield 0, "<code>"
                for tup in inner:
                    yield tup
                yield 0, "</code>"

            def wrap(self, source, outfile):
                """Return the source with a code, pre, and div."""
                return self._wrap_div(self._wrap_pre(self._wrap_code(source)))

        formatter_opts.setdefault("cssclass", "codehilite")
        formatter = HtmlCodeFormatter(**formatter_opts)
        return pygments.highlight(codeblock, lexer, formatter)
style_transfer.py 文件源码 项目:style_transfer 作者: crowsonkb 项目源码 文件源码 阅读 28 收藏 0 点赞 0 评论 0
def print_args():
    """Prints out all command-line parameters."""
    bg = terminal_bg()
    if bg is True:
        style = 'xcode'
    elif bg is False:
        style = 'monokai'

    pprint = print_
    try:
        if bg is not None:
            import pygments
            from pygments.lexers import Python3Lexer
            from pygments.formatters import Terminal256Formatter
            pprint = partial(pygments.highlight, lexer=Python3Lexer(),
                             formatter=Terminal256Formatter(style=style), outfile=sys.stdout)
    except ImportError:
        pass
    print_('Parameters:')
    for key in sorted(ARGS):
        v = repr(getattr(ARGS, key))
        print_('% 14s: ' % key, end='')
        pprint(v)
    print_()
formatters.py 文件源码 项目:djangolab 作者: DhiaTN 项目源码 文件源码 阅读 26 收藏 0 点赞 0 评论 0
def format(self, record):

        sql = record.sql.strip()

        if sqlparse:
            # Indent the SQL query
            sql = sqlparse.format(sql, reindent=True)

        if pygments:
            # Highlight the SQL query
            sql = pygments.highlight(
                sql,
                SqlLexer(),
                Terminal256Formatter(style='monokai')
            )

        record.statement = sql
        return super(SQLFormatter, self).format(record)
markdown2.py 文件源码 项目:AnkiHub 作者: dayjaby 项目源码 文件源码 阅读 25 收藏 0 点赞 0 评论 0
def _color_with_pygments(self, codeblock, lexer, **formatter_opts):
        import pygments
        import pygments.formatters

        class HtmlCodeFormatter(pygments.formatters.HtmlFormatter):
            def _wrap_code(self, inner):
                """A function for use in a Pygments Formatter which
                wraps in <code> tags.
                """
                yield 0, "<code>"
                for tup in inner:
                    yield tup
                yield 0, "</code>"

            def wrap(self, source, outfile):
                """Return the source with a code, pre, and div."""
                return self._wrap_div(self._wrap_pre(self._wrap_code(source)))

        formatter_opts.setdefault("cssclass", "codehilite")
        formatter = HtmlCodeFormatter(**formatter_opts)
        return pygments.highlight(codeblock, lexer, formatter)
markdown2.py 文件源码 项目:MarkdownLivePreview 作者: math2001 项目源码 文件源码 阅读 19 收藏 0 点赞 0 评论 0
def _color_with_pygments(self, codeblock, lexer, **formatter_opts):
        import pygments
        import pygments.formatters

        class HtmlCodeFormatter(pygments.formatters.HtmlFormatter):
            def _wrap_code(self, inner):
                """A function for use in a Pygments Formatter which
                wraps in <code> tags.
                """
                yield 0, "<code>"
                for tup in inner:
                    yield tup
                yield 0, "</code>"

            def wrap(self, source, outfile):
                """Return the source with a code, pre, and div."""
                return self._wrap_div(self._wrap_pre(self._wrap_code(source)))

        formatter_opts.setdefault("cssclass", "codehilite")
        formatter = HtmlCodeFormatter(**formatter_opts)
        return pygments.highlight(codeblock, lexer, formatter)
markdown2.py 文件源码 项目:rekall-agent-server 作者: rekall-innovations 项目源码 文件源码 阅读 25 收藏 0 点赞 0 评论 0
def _color_with_pygments(self, codeblock, lexer, **formatter_opts):
        import pygments
        import pygments.formatters

        class HtmlCodeFormatter(pygments.formatters.HtmlFormatter):
            def _wrap_code(self, inner):
                """A function for use in a Pygments Formatter which
                wraps in <code> tags.
                """
                yield 0, "<code>"
                for tup in inner:
                    yield tup
                yield 0, "</code>"

            def wrap(self, source, outfile):
                """Return the source with a code, pre, and div."""
                return self._wrap_div(self._wrap_pre(self._wrap_code(source)))

        formatter_opts.setdefault("cssclass", "codehilite")
        formatter = HtmlCodeFormatter(**formatter_opts)
        return pygments.highlight(codeblock, lexer, formatter)
markdown2.py 文件源码 项目:Bigglesworth 作者: MaurizioB 项目源码 文件源码 阅读 24 收藏 0 点赞 0 评论 0
def _color_with_pygments(self, codeblock, lexer, **formatter_opts):
        import pygments
        import pygments.formatters

        class HtmlCodeFormatter(pygments.formatters.HtmlFormatter):
            def _wrap_code(self, inner):
                """A function for use in a Pygments Formatter which
                wraps in <code> tags.
                """
                yield 0, "<code>"
                for tup in inner:
                    yield tup
                yield 0, "</code>"

            def wrap(self, source, outfile):
                """Return the source with a code, pre, and div."""
                return self._wrap_div(self._wrap_pre(self._wrap_code(source)))

        formatter_opts.setdefault("cssclass", "codehilite")
        formatter = HtmlCodeFormatter(**formatter_opts)
        return pygments.highlight(codeblock, lexer, formatter)
wiki.py 文件源码 项目:pygameweb 作者: pygame 项目源码 文件源码 阅读 31 收藏 0 点赞 0 评论 0
def _wiki_code(content):
    """
    >>> u = _wiki_code('<code class="python">my_code()</code>')
    >>> expected = '<div class="highlight"><pre><span class="n">my_code</span><span class="p">()</span>\\n</pre></div>\\n'
    >>> expected in u
    True

    """
    css_class = "python"
    if css_class:
        content = content.replace('<code>', '<code class=\"%s\">' % css_class);

    #syntax_css = u"<style>%s</style>" % HtmlFormatter().get_style_defs('.highlight')
    #content = syntax_css + content

    return re.sub(r'\<code\s+class\s*\=\s*\"([^\"]+)\"\>(.*?)\<\/code>',
                  _wiki_code_callback, content, flags=re.I | re.S)


问题


面经


文章

微信
公众号

扫码关注公众号