Exemplo n.º 1
0
    def is_pycode(node: nodes.literal_block) -> bool:
        """Checks if the node is literal block of python"""
        if node.rawsource != node.astext():
            return False  # skip parsed-literal node

        language = node.get("language")
        if language in ("py", "py3", "python", "python3", "default"):
            return True
        elif language == "guess":
            try:
                lexer = guess_lexer(node.rawsource)
                return isinstance(lexer, (PythonLexer, Python3Lexer))
            except Exception:
                pass

        return False
Exemplo n.º 2
0
 def visit_literal_block(self, node: nodes.literal_block) -> None:
     setting = self.settings[-1]
     if 'language' not in node:
         node['language'] = setting.language
         node['force'] = setting.force
     if 'linenos' not in node:
         lines = node.astext().count('\n')
         node['linenos'] = (lines >= setting.lineno_threshold - 1)
Exemplo n.º 3
0
    def is_pyconsole(node: nodes.literal_block) -> bool:
        if node.rawsource != node.astext():
            return False  # skip parsed-literal node

        language = node.get('language')
        if language in ('pycon', 'pycon3'):
            return True
        elif language in ('py', 'py3', 'python', 'python3', 'default'):
            return node.rawsource.startswith('>>>')
        elif language == 'guess':
            try:
                lexer = guess_lexer(node.rawsource)
                return isinstance(lexer, PythonConsoleLexer)
            except Exception:
                pass

        return False
Exemplo n.º 4
0
    def visit_literal_block(self, node: nodes.literal_block):
        """Begin ``literal_block``"""
        if node.rawsource != node.astext():
            # most probably a parsed-literal block -- don't highlight
            return super().visit_literal_block(node)

        lang = node.get('language', 'default')
        linenos = node.get('linenos', False)
        highlight_args = node.get('highlight_args', {})

        hl_lines = highlight_args.get('hl_lines')
        if node.attributes.get('revealjs-hl-lines'):
            hl_lines = node.attributes['revealjs-hl-lines']

        opts = self.config.highlight_options.get(lang, {})

        highlighted = self.highlighter.highlight_block(node.rawsource,
                                                       lang,
                                                       opts=opts,
                                                       linenos=bool(linenos),
                                                       location=node)

        pre_attrs = {
            'CLASS': 'highlight highlight-%s notranslate"' % lang,
        }
        code_attrs = {
            'CLASS': 'hljs',
        }

        if node.attributes.get('revealjs-id'):
            pre_attrs['DATA-ID'] = node.attributes['revealjs-id']
        if hl_lines:
            code_attrs['DATA-LINE-NUMBERS'] = hl_lines
        if node.attributes.get('revealjs-index'):
            code_attrs['DATA-FRAGMENT-INDEX'] = node.attributes[
                'revealjs-index']

        starttag = self.starttag(node, 'pre', suffix='', **pre_attrs)
        code_tag = self.starttag({}, 'code', suffix='', **code_attrs)

        self.body.append(starttag + code_tag + highlighted + '</code></pre>\n')
        raise nodes.SkipNode