inlinehilite.py 文件源码

python
阅读 15 收藏 0 点赞 0 评论 0

项目:macos-st-packages 作者: zce 项目源码 文件源码
def codehilite(self, lang, src):
        """Syntax highlite the inline code block."""

        process_text = self.style_plain_text or lang or self.guess_lang
        if not lang and self.style_plain_text and not self.guess_lang:
            lang = 'text'
        sublime_hl_enabled, sublime_hl = self.config.get("sublime_hl", None)
        if sublime_hl_enabled:
            code = sublime_hl.syntax_highlight(src, lang, inline=True)
        elif pygments and self.use_pygments and process_text:
            try:
                lexer = get_lexer_by_name(lang)
            except ValueError:
                try:
                    if self.guess_lang:
                        lexer = guess_lexer(src)
                    else:
                        lexer = get_lexer_by_name('text')
                except ValueError:
                    lexer = get_lexer_by_name('text')

            formatter = SublimeInlineHtmlFormatter(
                style=self.style,
                cssclass=self.css_class,
                noclasses=self.noclasses,
                classprefix=self.css_class + ' '  # Insert class prefix for styling Sublime
            )
            code = highlight(src, lexer, formatter)
        else:
            # Just escape and build markup usable by JS highlighting libs
            txt = src.replace('&', '&')
            txt = txt.replace('<', '&lt;')
            txt = txt.replace('>', '&gt;')
            txt = txt.replace('"', '&quot;')
            txt = multi_space.sub(replace_nbsp, txt.replace('\t', ' ' * 4))  # Special format for sublime.

            classes = [self.css_class] if self.css_class and process_text else []
            if lang and process_text:
                classes.append('language-%s' % lang)
            class_str = ''
            if len(classes):
                class_str = ' class="%s"' % ' '.join(classes)
            code = '<code%s>%s</code>' % (class_str, txt)
        placeholder = self.markdown.htmlStash.store(code, safe=True)
        return placeholder
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号