コード例 #1
0
    def highlight(self, source, language):
        """
        Syntax highlight the code block.

        If config is not empty, then the codehlite extension
        is enabled, so we call into it to highlight the code.
        """
        if CodeHilite and self.codehilite_conf:
            sublime_hl_enabled, sublime_hl = self.config.get(
                "sublime_hl", None)
            if sublime_hl_enabled:
                code = sublime_hl.syntax_highlight(source,
                                                   language,
                                                   hl_lines=parse_hl_lines(
                                                       self.hl_lines))
            else:
                code = SublimeHighlight(
                    source,
                    linenums=self.codehilite_conf['linenums'][0],
                    guess_lang=self.codehilite_conf['guess_lang'][0],
                    css_class=self.codehilite_conf['css_class'][0],
                    style=self.codehilite_conf['pygments_style'][0],
                    lang=language,
                    noclasses=self.codehilite_conf['noclasses'][0],
                    hl_lines=parse_hl_lines(self.hl_lines),
                    use_pygments=self.codehilite_conf['use_pygments']
                    [0]).hilite()
        else:
            lang = self.CLASS_ATTR % language if language else ''
            code = self.CODE_WRAP % (lang, _escape(source))
        return code
コード例 #2
0
ファイル: superfences.py プロジェクト: 1901/sublime_sync
    def highlight(self, source, language):
        """
        Syntax highlight the code block.

        If config is not empty, then the codehlite extension
        is enabled, so we call into it to highlight the code.
        """
        if CodeHilite and self.codehilite_conf:
            sublime_hl_enabled, sublime_hl = self.config.get("sublime_hl", None)
            if sublime_hl_enabled:
                code = sublime_hl.syntax_highlight(source, language, hl_lines=parse_hl_lines(self.hl_lines))
            else:
                code = SublimeHighlight(
                    source,
                    linenums=self.codehilite_conf['linenums'][0],
                    guess_lang=self.codehilite_conf['guess_lang'][0],
                    css_class=self.codehilite_conf['css_class'][0],
                    style=self.codehilite_conf['pygments_style'][0],
                    lang=language,
                    noclasses=self.codehilite_conf['noclasses'][0],
                    hl_lines=parse_hl_lines(self.hl_lines),
                    use_pygments=self.codehilite_conf['use_pygments'][0]
                ).hilite()
        else:
            lang = self.CLASS_ATTR % language if language else ''
            code = self.CODE_WRAP % (lang, _escape(source))
        return code
コード例 #3
0
    def run(self, lines):
        """ Match and store Fenced Code Blocks in the HtmlStash. """

        # Check for code hilite extension
        if not self.checked_for_codehilite:
            for ext in self.md.registeredExtensions:
                if isinstance(ext, CodeHiliteExtension):
                    self.codehilite_conf = ext.config
                    break

            self.checked_for_codehilite = True

        text = "\n".join(lines)
        while 1:
            m = self.FENCED_BLOCK_RE.search(text)

            if m:
                lang = ''
                wrap_tag = ''
                wrap_class = ''
                if m.group('lang'):
                    lang = self.LANG_TAG % m.group('lang')

                if m.group('wrap_tag'):
                    wrap_tag = m.group('wrap_tag')

                if m.group('wrap_class'):
                    wrap_class = m.group('wrap_class')

                # If config is not empty, then the codehighlite extension
                # is enabled, so we call it to highlight the code
                if self.codehilite_conf:
                    highliter = CodeHilite(
                        m.group('code'),
                        linenums=self.codehilite_conf['linenums'][0],
                        guess_lang=self.codehilite_conf['guess_lang'][0],
                        css_class=self.codehilite_conf['css_class'][0],
                        style=self.codehilite_conf['pygments_style'][0],
                        use_pygments=self.codehilite_conf['use_pygments'][0],
                        lang=(m.group('lang') or None),
                        noclasses=self.codehilite_conf['noclasses'][0],
                        hl_lines=parse_hl_lines(m.group('hl_lines')))

                    code = highliter.hilite()
                else:
                    code = self.CODE_WRAP % (lang, self._escape(
                        m.group('code')))

                placeholder = self.md.htmlStash.store(code)

                text = '{}\n<div class="ez-code-example">{}</div> \n {}\n{}'\
                    .format(text[:m.start()], m.group('code'), placeholder, text[m.end():])

                if wrap_tag and wrap_class:
                    text = '<%s class="%s">\n%s\n</%s>' % (
                        wrap_tag, wrap_class, text, wrap_tag)

            else:
                break
        return text.split("\n")
コード例 #4
0
    def _identify_code_tabs(self, block_str):

        text = FencedCodeTabsPreprocessor._filter_content(block_str)

        while True:
            m = self.FENCE_BLOCK_REGEX.search(text)
            if m:

                first_line = text[m.start():].split('\n')[0]

                kwargs = {}
                for param, regex in PARAM_REGEXES.items():
                    param_m = regex.search(first_line)
                    if param_m:
                        if param_m.group(param):
                            kwargs[param] = param_m.group(param)
                        elif (not param_m.group(param) is None) and param in PARAM_DEFAULTS:
                            kwargs[param] = PARAM_DEFAULTS[param]
                        else:
                            raise Exception("{} needs an argument within \n{}".format(param, first_line))

                lang = ''
                label = ''

                if m.group('lang') and m.group('lang') not in PARAM_REGEXES:
                    lang = m.group('lang')
                    label = lang[0:1].capitalize() + lang[1:]

                if kwargs.get('fct_label'):
                    label =  kwargs.get('fct_label')

                if self.codehilite_conf:
                    highliter = CodeHilite(
                        m.group('code'),
                        linenums=self.codehilite_conf['linenums'][0],
                        guess_lang=self.codehilite_conf['guess_lang'][0],
                        css_class=self.codehilite_conf['css_class'][0],
                        style=self.codehilite_conf['pygments_style'][0],
                        lang=(m.group('lang') or None),
                        noclasses=self.codehilite_conf['noclasses'][0],
                        hl_lines=parse_hl_lines(kwargs.get('hl_lines'))
                    )

                    code = highliter.hilite()
                else:
                    code = self.TAB_ITEM_BODY_WRAP_ESCAPE % (lang,
                                                             self._escape(m.group('code')))

                self.tab_items.append(FencedCodeTabs(label, lang, code))

                placeholder = self.tab_item_placeholder.format(len(self.tab_items) - 1)

                text = "{}\n{}\n{}".format(text[:m.start()],
                                           placeholder,
                                           text[m.end():])

            else:
                break

        return text
コード例 #5
0
ファイル: fencer.py プロジェクト: phoikoi/apix
    def run(self, lines):
        """ Match and store Fenced Code Blocks in the HtmlStash. """

        # Check for code hilite extension
        if not self.checked_for_codehilite:
            for ext in self.md.registeredExtensions:
                if isinstance(ext, CodeHiliteExtension):
                    self.codehilite_conf = ext.config
                    break

            self.checked_for_codehilite = True

        text = "\n".join(lines)
        while 1:
            m = self.FENCED_BLOCK_RE.search(text)
            if m:
                lang = ''
                if m.group('lang'):
                    lang = self.LANG_TAG % m.group('lang')
                print(f"lang is {lang}")
                if 'mermaid' in lang:
                    code = f"<div class=\"mermaid\">{m.group('code')}</div>"
                    print(code)
                else:
                    # If config is not empty, then the codehighlite extension
                    # is enabled, so we call it to highlight the code
                    if self.codehilite_conf:
                        highliter = CodeHilite(
                            m.group('code'),
                            linenums=self.codehilite_conf['linenums'][0],
                            guess_lang=self.codehilite_conf['guess_lang'][0],
                            css_class=self.codehilite_conf['css_class'][0],
                            style=self.codehilite_conf['pygments_style'][0],
                            use_pygments=self.codehilite_conf['use_pygments']
                            [0],
                            lang=(m.group('lang') or None),
                            noclasses=self.codehilite_conf['noclasses'][0],
                            hl_lines=parse_hl_lines(m.group('hl_lines')))

                        code = highliter.hilite()
                    else:
                        code = self.CODE_WRAP % (lang,
                                                 self._escape(m.group('code')))

                placeholder = self.md.htmlStash.store(code)
                text = '%s\n%s\n%s' % (text[:m.start()], placeholder,
                                       text[m.end():])
            else:
                break
        return text.split("\n")
コード例 #6
0
    def _highlite_code(self, lang, code, options):
        if self.codehilite_config:
            highliter = CodeHilite(
                code,
                linenums=self.codehilite_config['linenums'][0],
                guess_lang=self.codehilite_config['guess_lang'][0],
                css_class=self.codehilite_config['css_class'][0],
                style=self.codehilite_config['pygments_style'][0],
                lang=(lang or None),
                noclasses=self.codehilite_config['noclasses'][0],
                hl_lines=parse_hl_lines(options['hl_lines'])
            )
            return highliter.hilite()

        return '<pre><code class="{}">{}</code></pre>'.format(lang, util.escape(code))
コード例 #7
0
ファイル: fenced_code.py プロジェクト: suryakantpandey/zulip
    def _parseHeader(self) -> None:
        # Python-Markdown has a feature to parse-and-hide shebang
        # lines present in code blocks:
        #
        # https://python-markdown.github.io/extensions/code_hilite/#shebang-no-path
        #
        # While using shebang lines for language detection is
        # reasonable, we don't want this feature because it can be
        # really confusing when doing anything else in a one-line code
        # block that starts with `!` (which would then render as an
        # empty code block!).  So we disable the feature, by
        # overriding this function, which implements it in CodeHilite
        # upstream.

        # split text into lines
        lines = self.src.split("\n")
        # Python-Markdown pops out the first line which we are avoiding here.
        # Examine first line
        fl = lines[0]

        c = re.compile(
            r"""
            (?:(?:^::+)|(?P<shebang>^[#]!)) # Shebang or 2 or more colons
            (?P<path>(?:/\w+)*[/ ])?        # Zero or 1 path
            (?P<lang>[\w#.+-]*)             # The language
            \s*                             # Arbitrary whitespace
            # Optional highlight lines, single- or double-quote-delimited
            (hl_lines=(?P<quot>"|')(?P<hl_lines>.*?)(?P=quot))?
            """,
            re.VERBOSE,
        )
        # Search first line for shebang
        m = c.search(fl)
        if m:
            # We have a match
            try:
                self.lang = m.group("lang").lower()
            except IndexError:  # nocoverage
                self.lang = None

            if self.options["linenos"] is None and m.group("shebang"):
                # Overridable and Shebang exists - use line numbers
                self.options["linenos"] = True

            self.options["hl_lines"] = parse_hl_lines(m.group("hl_lines"))

        self.src = "\n".join(lines).strip("\n")