Beispiel #1
0
    def format_codechunks(self, chunk):
        from pygments import highlight
        from pygments.lexers import PythonLexer, TextLexer, PythonConsoleLexer
        from pygments.formatters import LatexFormatter

        chunk['content'] = highlight(chunk['content'], PythonLexer(),
                                     LatexFormatter(verboptions="frame=single,fontsize=\small, xleftmargin=0.5em"))
        if len(chunk['result'].strip()) > 0 and chunk['results'] == 'verbatim':
            if chunk['term']:
                chunk['result'] = highlight(chunk['result'], PythonLexer(), LatexFormatter(
                    verboptions="frame=single,fontsize=\small, xleftmargin=0.5em"))
            else:
                chunk['result'] = highlight(chunk['result'], TextLexer(), LatexFormatter(
                    verboptions="frame=leftline,fontsize=\small, xleftmargin=0.5em"))
        return PwebFormatter.format_codechunks(self, chunk)
Beispiel #2
0
    def __init__(self, source=None):
        PwebTexPygmentsFormatter.__init__(self, source)
        from pygments.formatters import LatexFormatter

        x = LatexFormatter()
        self.header = ("""\\documentclass[a4paper,11pt,final]{article}
        \\usepackage{fancyvrb, color, graphicx, hyperref, ,amsmath, url}
        \\usepackage{palatino}
        \\usepackage[a4paper,text={16.5cm,25.2cm},centering]{geometry}

        \\hypersetup
        {   pdfauthor = {Pweave},
            pdftitle={Published from %s},
            colorlinks=TRUE,
            linkcolor=black,
            citecolor=blue,
            urlcolor=blue
        }
        \\setlength{\parindent}{0pt}
        \\setlength{\parskip}{1.2ex}
        %% fix for pandoc 1.14
        \\providecommand{\\tightlist}{%%
            \\setlength{\\itemsep}{0pt}\\setlength{\\parskip}{0pt}}
        %s
        """) % (self.source, x.get_style_defs())
        self.footer = r"\end{document}"
Beispiel #3
0
def test_valid_output():
    with open(TESTFILE) as fp:
        tokensource = list(PythonLexer().get_tokens(fp.read()))
    fmt = LatexFormatter(full=True, encoding='latin1')

    handle, pathname = tempfile.mkstemp('.tex')
    # place all output files in /tmp too
    old_wd = os.getcwd()
    os.chdir(os.path.dirname(pathname))
    tfile = os.fdopen(handle, 'wb')
    fmt.format(tokensource, tfile)
    tfile.close()
    try:
        import subprocess
        po = subprocess.Popen(['latex', '-interaction=nonstopmode',
                               pathname], stdout=subprocess.PIPE)
        ret = po.wait()
        output = po.stdout.read()
        po.stdout.close()
    except OSError as e:
        # latex not available
        pytest.skip(str(e))
    else:
        if ret:
            print(output)
        assert not ret, 'latex run reported errors'

    os.unlink(pathname)
    os.chdir(old_wd)
Beispiel #4
0
def highlight2latex(source,
                    language='ipython',
                    metadata=None,
                    strip_verbatim=False):
    """
    Return a syntax-highlighted version of the input source as latex output.

    Parameters
    ----------
    source : str
        source of the cell to highlight
    language : str
        language to highlight the syntax of
    metadata : NotebookNode cell metadata
        metadata of the cell to highlight
    strip_verbatim : bool
        remove the Verbatim environment that pygments provides by default
    """
    latex = _pygment_highlight(source, LatexFormatter(), language, metadata)
    if strip_verbatim:
        latex = latex.replace(r'\begin{Verbatim}[commandchars=\\\{\}]' + '\n',
                              '')
        return latex.replace('\n\\end{Verbatim}\n', '')
    else:
        return latex
Beispiel #5
0
 def __init__(self, dest='html', stylename='sphinx'):
     self.dest = dest
     if not pygments:
         return
     if stylename == 'sphinx':
         style = SphinxStyle
     elif '.' in stylename:
         module, stylename = stylename.rsplit('.', 1)
         style = getattr(__import__(module, None, None, ['']), stylename)
     else:
         style = get_style_by_name(stylename)
     self.hfmter = {False: HtmlFormatter(style=style),
                    True: HtmlFormatter(style=style, linenos=True)}
     self.lfmter = {False: LatexFormatter(style=style, commandprefix='PYG'),
                    True: LatexFormatter(style=style, linenos=True,
                                         commandprefix='PYG')}
Beispiel #6
0
    def __init__(self, document):
        LaTeXTranslator.__init__(self, document)
        self.head_prefix = [
            x for x in self.head_prefix if ('{typearea}' not in x)
        ]
        hyperref_posn = [
            i for i in range(len(self.head_prefix))
            if ('{hyperref}' in self.head_prefix[i])
        ]
        if not hyperref_posn:
            self.head_prefix.append(None)
            hyperref_posn = [-1]  # XXX hack
        self.head_prefix[hyperref_posn[0]] = (
            '\\usepackage{hyperref}\n' + '\\usepackage{fancyvrb}\n' +
            LatexFormatter(style="manni").get_style_defs() + "\n")

        self.head_prefix.extend([
            '\\definecolor{rrblitbackground}{rgb}{0.55, 0.3, 0.1}\n',
            '\\newenvironment{rtbliteral}{\n',
            '\\begin{ttfamily}\n',
            '\\color{rrblitbackground}\n',
            '}{\n',
            '\\end{ttfamily}\n',
            '}\n',
        ])
        # this fixes the hardcoded section titles in docutils 0.4
        self.d_class = DocumentClass('article')
Beispiel #7
0
def rst2tex(in_path, out_path):

    options.mkdir_p(out_path)
    for file in glob.glob(os.path.join(in_path, '*')):
        shutil.copy(file, out_path)

    base_dir = os.path.dirname(__file__)
    scipy_status = os.path.join(base_dir, '_static/status.sty')
    shutil.copy(scipy_status, out_path)
    scipy_style = os.path.join(base_dir, '_static/scipy.sty')
    shutil.copy(scipy_style, out_path)
    preamble = r'''\usepackage{scipy}'''

    # Add the LaTeX commands required by Pygments to do syntax highlighting

    pygments = None

    try:
        import pygments
    except ImportError:
        import warnings
        warnings.warn(
            RuntimeWarning('Could not import Pygments. '
                           'Syntax highlighting will fail.'))

    if pygments:
        from pygments.formatters import LatexFormatter
        from writer.sphinx_highlight import SphinxStyle

        preamble += LatexFormatter(style=SphinxStyle).get_style_defs()

    settings = {
        'documentclass': 'IEEEtran',
        'use_verbatim_when_possible': True,
        'use_latex_citations': True,
        'latex_preamble': preamble,
        'documentoptions': 'letterpaper,compsoc,twoside',
        'halt_level': 3,  # 2: warn; 3: error; 4: severe
    }

    try:
        rst, = glob.glob(os.path.join(in_path, '*.rst'))
    except ValueError:
        raise RuntimeError("Found more than one input .rst--not sure which "
                           "one to use.")

    content = header + open(rst, 'r').read()

    tex = dc.publish_string(source=content,
                            writer=writer,
                            settings_overrides=settings)

    stats_file = os.path.join(out_path, 'paper_stats.json')
    d = options.cfg2dict(stats_file)
    d.update(writer.document.stats)
    options.dict2cfg(d, stats_file)

    tex_file = os.path.join(out_path, 'paper.tex')
    with open(tex_file, 'w') as f:
        f.write(tex)
Beispiel #8
0
    def _generate_pygments_latex_def(self):
        """
        Generate the pygments latex definitions that allows pygments
        to work in latex.
        """

        return LatexFormatter().get_style_defs()
Beispiel #9
0
def build_pygments_macros(filename):
    from pygments.formatters import LatexFormatter
    text = LatexFormatter().get_style_defs()
    f = file(filename, "w")
    f.write(text)
    f.write('\n')
    f.close()
Beispiel #10
0
 def __init__(self, dest='html', stylename='sphinx'):
     self.dest = dest
     if not pygments:
         return
     if stylename == 'sphinx':
         style = SphinxStyle
     else:
         style = get_style_by_name(stylename)
     self.hfmter = {
         False: HtmlFormatter(style=style),
         True: HtmlFormatter(style=style, linenos=True)
     }
     self.lfmter = {
         False: LatexFormatter(style=style),
         True: LatexFormatter(style=style, linenos=True)
     }
Beispiel #11
0
def code(title):
    """
    Return syntax highlighted LaTeX.
    """
    filename = title.split(' ')[1]

    # open the code file relative from the yml file path
    f = open(
        os.path.join(os.path.dirname(os.path.abspath(source_file)), filename))

    out = "\n\\begin{frame}[fragile,t]"
    out += "\n\t\\frametitle{Code: \"%s\"}" % filename

    try:
        from pygments import highlight
        from pygments.lexers import get_lexer_for_filename, get_lexer_by_name
        from pygments.formatters import LatexFormatter

        try:
            lexer = get_lexer_for_filename(filename)
        except:
            lexer = get_lexer_by_name('text')
        out += "\n%s\n" % highlight(f.read(), lexer,
                                    LatexFormatter(linenos=True))
    except ImportError:
        out += "\n\t\\begin{lstlisting}\n"
        out += f.read()
        out += "\n\t\end{lstlisting}"

    f.close()
    out += "\n\end{frame}"
    return out
Beispiel #12
0
    def __call__(self, source, language=None, metadata=None, strip_verbatim=False):
        """
        Return a syntax-highlighted version of the input source as latex output.

        Parameters
        ----------
        source : str
            source of the cell to highlight
        language : str
            language to highlight the syntax of
        metadata : NotebookNode cell metadata
            metadata of the cell to highlight
        strip_verbatim : bool
            remove the Verbatim environment that pygments provides by default
        """
        from pygments.formatters import LatexFormatter
        if not language:
            language=self.pygments_lexer

        latex = _pygments_highlight(source, LatexFormatter(**self.extra_formatter_options), language, metadata)
        if strip_verbatim:
            latex = latex.replace(r'\begin{Verbatim}[commandchars=\\\{\}]' + '\n', '')
            return latex.replace('\n\\end{Verbatim}\n', '')
        else:
            return latex
Beispiel #13
0
def render_error(etype=None, evalue=None, tb=None, as_document=False):
    r'''Renders the last traceback as LaTeX code. The resulting code uses 
    pygments for syntax highlighting. If no traceback object is given, the last
    traceback is used.
    
    Example
    -------
    
    >>> try:
    ...     1/0
    ... except:
    ...     print(render_error())                        #doctest: +ELLIPSIS
    \makeatletter...
    \begin{Verbatim}[commandchars=\\\{\}]
    \PY{n}{Traceback}...
    \end{Verbatim}
    '''

    if evalue is None:
        etype, evalue, tb = sys.exc_info()

    with print_capture(capture_errors=True) as tbcode:
        traceback.print_exception(etype, evalue, tb)

    return highlight(tbcode, PythonLexer(), LatexFormatter(full=as_document))
Beispiel #14
0
    def visit_literal_block(self, node):
        self.non_breaking_paragraph = True

        if 'language' in node.attributes:
            # do highlighting
            from pygments import highlight
            from pygments.lexers import PythonLexer, get_lexer_by_name
            from pygments.formatters import LatexFormatter

            extra_opts = 'fontsize=\\footnotesize'

            linenos = node.attributes.get('linenos', False)
            linenostart = node.attributes.get('linenostart', 1)
            if linenos:
                extra_opts += ',xleftmargin=2.25mm,numbersep=3pt'

            lexer = get_lexer_by_name(node.attributes['language'])
            tex = highlight(
                node.astext(), lexer,
                LatexFormatter(linenos=linenos,
                               linenostart=linenostart,
                               verboptions=extra_opts))

            self.out.append("\\vspace{1mm}\n" + tex + "\\vspace{1mm}\n")
            raise nodes.SkipNode
        else:
            LaTeXTranslator.visit_literal_block(self, node)
Beispiel #15
0
def styledefs(outfile):
    """
    Generate the LaTeX style definitions to be used in building the code.

    The `--outfile` option tells where to save it.
    """
    with open(outfile, 'w') as styledefsfile:
        styledefsfile.write(LatexFormatter().get_style_defs())
    click.echo('Wrote the latex style definitions to ' + outfile + '.')
Beispiel #16
0
def test_correct_output():
    with open(TESTFILE) as fp:
        tokensource = list(PythonLexer().get_tokens(fp.read()))
    hfmt = LatexFormatter(nowrap=True)
    houtfile = StringIO()
    hfmt.format(tokensource, houtfile)

    assert r'\begin{Verbatim}' not in houtfile.getvalue()
    assert r'\end{Verbatim}' not in houtfile.getvalue()
Beispiel #17
0
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        from pygments.formatters import LatexFormatter

        self.header = (
            (
                """\\documentclass[a4paper,11pt,final]{article}
        \\usepackage{fancyvrb, color, graphicx, hyperref, amsmath, url, textcomp}
        \\usepackage{palatino}
        \\usepackage[a4paper,text={16.5cm,25.2cm},centering]{geometry}

        %%Set different options for xetex and luatex
        \\usepackage{iftex}
        \\ifxetex\\usepackage{fontspec}\\fi

        \\ifluatex\\usepackage{fontspec}\\fi

        \\usepackage{xcolor}
        %% ANSI colors from nbconvert
        \\definecolor{ansi-black}{HTML}{3E424D}
        \\definecolor{ansi-black-intense}{HTML}{282C36}
        \\definecolor{ansi-red}{HTML}{E75C58}
        \\definecolor{ansi-red-intense}{HTML}{B22B31}
        \\definecolor{ansi-green}{HTML}{00A250}
        \\definecolor{ansi-green-intense}{HTML}{007427}
        \\definecolor{ansi-yellow}{HTML}{DDB62B}
        \\definecolor{ansi-yellow-intense}{HTML}{B27D12}
        \\definecolor{ansi-blue}{HTML}{208FFB}
        \\definecolor{ansi-blue-intense}{HTML}{0065CA}
        \\definecolor{ansi-magenta}{HTML}{D160C4}
        \\definecolor{ansi-magenta-intense}{HTML}{A03196}
        \\definecolor{ansi-cyan}{HTML}{60C6C8}
        \\definecolor{ansi-cyan-intense}{HTML}{258F8F}
        \\definecolor{ansi-white}{HTML}{C5C1B4}
         \\definecolor{ansi-white-intense}{HTML}{A1A6B2}

        \\hypersetup
        {   pdfauthor = {Pweave},
            pdftitle={Published from %s},
            colorlinks=TRUE,
            linkcolor=black,
            citecolor=blue,
            urlcolor=blue
        }
        \\setlength{\parindent}{0pt}
        \\setlength{\parskip}{1.2ex}
        %% fix for pandoc 1.14
        \\providecommand{\\tightlist}{%%
            \\setlength{\\itemsep}{0pt}\\setlength{\\parskip}{0pt}}
        %s
        """
            )
            % (self.source, LatexFormatter().get_style_defs())
        )
        self.footer = r"\end{document}"
        self.subheader = "\n\\begin{document}\n"
        self.fig_mimetypes = ["application/pdf", "image/png", "image/jpg"]
Beispiel #18
0
    def highlight_code(self):
        line_numbers = []
        html_snippets = []
        if self.matched_line_number():
            snippet_cluster_lns = self.compute_lines_to_highlight(
                self.adjacent_line_numbers())

            snippets = []
            for snippet_cluster_ln in snippet_cluster_lns:
                snippet = []

                for n in snippet_cluster_ln:

                    snippet.append(self.file_content_lines[n])
                start_line = min(snippet_cluster_ln)
                highlight_lines = map(lambda x: x - start_line + 1,
                                      self.matching_line_numbers)
                snippets.append(
                    ("\n".join(snippet), start_line, highlight_lines))
                #self.code_snippets.append( GitSearchItemSnippet("\n".join(snippet), start_line) )

            # lineostart is independent from hl_lines, so we need to take care of shifting the matching line numbers

            #print "Highlight Lines:" + str(highlight_lines)
            #html_snippets =  ['<a href="%s#foo-%s">%s</a>' % (self.file_path, snippet[1], highlight(snippet[0], JavaLexer(), HtmlFormatter(linenos=True, anchorlinenos=True, linenostart=snippet[1]) )) for snippet in snippets] #hl_lines=snippet[2],
            html_snippets = [
                highlight(snippet[0], JavaLexer(),
                          LatexFormatter(linenos=True, linenostart=snippet[1]))
                for snippet in snippets
            ]
            self.code_snippets = [
                GitSearchItemSnippet(self.hl_snippet(snippet[0], snippet[1]),
                                     snippet[1]) for snippet in snippets
            ]  #hl_lines=snippet[2],

        # Lexical Search does not store line number, so we are currently not able to highlight the correct location of matched term
        if not html_snippets:
            html_snippets.append(
                highlight(self.file_content, JavaLexer(),
                          HtmlFormatter(linenos=True, anchorlinenos=True)))
            self.code_snippets.append(
                GitSearchItemSnippet(self.hl_snippet(self.file_content, 0), 0))
            #line_numbers = list(self.matched_line_number())

        # unescape html and wrap snippets with anchor
        #html_snippets = [unescape_html(html_snippet) for html_snippet in html_snippets]

        # import uuid
        # filename = str(uuid.uuid4())
        # with open("/tmp/%s" % filename, "w") as f:
        # 	f.write("".join(html_snippets))
        #print "".join(html_snippets)

        return "".join(html_snippets)
    def handle_data(self, data):
        if self.mystack:
            tag = self.mystack[-1]
            if tag == 'code.lang-python':
                data = (_codecomment(data) +
                        highlight(data, PythonLexer(), LatexFormatter()))
            elif tag == 'code':
                data = _latexescape(data)
            elif tag == 'code.lang-verbatim':
                data = _unescape(data)

        self.latex.append(data)
Beispiel #20
0
def header(metas):
    """
    Return the LaTeX Beamer document header declarations.
    """

    out = "\documentclass[slidestop,red]{beamer}"
    out += "\n\usepackage[utf8]{inputenc}"
    if metas.get('tex_babel'):
        out += "\n\usepackage[%s]{babel}" % metas['tex_babel']
    if metas.get('tex_fontenc'):
        out += "\n\usepackage[%s]{fontenc}" % metas['tex_fontenc']
    out += "\n\usepackage{fancyvrb,color}\n\n"

    # generate style definitions for pygments syntax highlighting
    try:
        from pygments.formatters import LatexFormatter
        out += LatexFormatter(
            style=metas.get('highlight_style', 'default')).get_style_defs()
    except ImportError:
        out += "\usepackage{listings}\n"
        out += "\lstset{numbers=left}"

    out += "\n\n\usetheme{Antibes}"
    out += "\n\setbeamertemplate{footline}[frame number]"
    out += "\n\usecolortheme{lily}"
    out += "\n\\beamertemplateshadingbackground{blue!5}{yellow!10}"

    if metas.has_key('short_title'):
        short_title = "[%s]" % metas.get('short_title')
    else:
        short_title = ""
    out += "\n\n\\title%s{%s}" % (short_title,
                                  metas.get('title', 'Example Presentation'))
    out += "\n\\author{%s}" % metas.get('author', 'Arthur Koziel')
    out += "\n\\institute{%s}" % metas.get('institute', '')
    out += "\n\date{%s}" % metas.get('date', '\\today')
    out += "\n\n\\begin{document}"
    out += "\n\n\\frame{\\titlepage}"

    if metas.get('outline', True):
        out += "\n\n\section*{%s}" % metas.get('outline_name', 'Outline')
        out += "\n\\frame {"
        out += "\n\t\\frametitle{%s}" % metas.get('outline_name', 'Outline')
        out += "\n\t\\tableofcontents"
        out += "\n}"

        out += "\n\n\AtBeginSection[] {"
        out += "\n\t\\frame{"
        out += "\n\t\t\\frametitle{%s}" % metas.get('outline_name', 'Outline')
        out += "\n\t\t\\tableofcontents[currentsection]"
        out += "\n\t}"
        out += "\n}"
    return out
Beispiel #21
0
def _as(format, node):
    PRE = Var('PRE')
    assert node == ('pre', {}, [PRE])
    s = PRE.val
    lang = _guess_lang(s)
    if format == 'html':
        formatter = HtmlFormatter()
    elif format == 'latex':
        formatter = LatexFormatter()
    else:
        raise RuntimeError('Not a valid output format: %r' % format)
    return highlight(s, get_lexer_by_name(lang), formatter)
Beispiel #22
0
def highlight2latex(source, language='ipython'):
    """
    Return a syntax-highlighted version of the input source as latex output.
    
    Parameters
    ----------
    source : str
        Source code to highlight the syntax of.
    language : str
        Language to highlight the syntax of.
    """
    return _pygment_highlight(source, LatexFormatter(), language)
Beispiel #23
0
def extract_algorithms(ps_infile, env):
    """
    Вытаскиваем части кода-алгоритмы
    """
    import pygments
    from pygments.lexers import get_lexer_by_name
    from pygments.formatters import LatexFormatter
    
    algorithm_regexp = re.compile(
        r"(?ms)\#ALGORITHM\s+(?P<name>[a-zA-z-0-9]+)\s*(?P<code>.+?)\s*\#ENDALGORITHM")
    hideline_regexps = [re.compile(r"(?m)^.*\#hide *\n"), re.compile(r"(?m)\n.*\#hide *") ]
    ls = ut.file2string(ps_infile) 
    for algorithm in algorithm_regexp.finditer(ls):
        algfilename = lib.get_target(ps_infile, algorithm.group('name')+".py")
        texfilename = lib.get_target(ps_infile, algorithm.group('name')+".tex")
        #lf = open(algfilename, 'w')
        code = algorithm.group('code')
        for r in hideline_regexps:
            code = re.sub(r, "", code)
        #code = lib.utf8anyway(code)    
        #lf.write(code)
        #lf.close()
        
        #tempblock = os.path.join(tempfile.gettempdir(), tempfile.gettempprefix())
        #ls = ''.join([env.project_db['paths']['python'],
        #             r'\scripts\pygmentize -f latex -l python ',
        #             ' -o "%(tempblock)s" "%(algfilename)s" ' % vars() ])
        #os.system(ls)
        
        lexer = get_lexer_by_name('python')
        code = ut.unicodeanyway(code)
        latex_tokens = pygments.lex(code, lexer)
        
#        sblock = ut.file2string(tempblock)
#        from pygments.formatters import LatexFormatter
        latex_formatter = LatexFormatter(texcomments = True)
        latex = pygments.format(latex_tokens, latex_formatter)
        stexblock = r"""
\documentclass{minimal}
\usepackage{xecyr}
\XeTeXdefaultencoding "utf-8"
\XeTeXinputencoding "utf-8"
\defaultfontfeatures{Mapping=tex-text}
\setmonofont{Consolas}
\usepackage{color}
\usepackage{fancyvrb}
\usepackage[russian,english]{babel} 
        """ + latex_formatter.get_style_defs() + r"""
\begin{document}
        """ + latex + r"""
\end{document}
        """
        ut.string2file(stexblock, texfilename, encoding='utf-8')
    def __call__(self, obj):

        used_title_styles = self.context.state.memo.title_styles
        section_level = self.context.state.memo.section_level + 1
        title_style = get_title_style(used_title_styles, section_level)
        title = obj.name + '\n' + title_style * len(obj.name) + '\n\n'
        documentation = obj.doc.value.replace('\\n', '\n')  # fix linebreaks
        documentation = documentation.replace('\\t', '    ')  # fix tabs

        temp = nodes.Element()
        lines = statemachine.string2lines(title + documentation)
        self.context.content.data = lines
        self.context.state.nested_parse(
            self.context.content,
            self.context.content_offset,
            temp, match_titles=True
        )

        node = temp.children.pop()

        if self.context.options.get('style', 'default') == 'minimal':
            return [node]

        tags_info = get_tags_information(obj,
                                         UserKeywordNode.TAGS_LIST) if self.context.options.get(
            'style', 'default') == "expanded" else ''

        all_steps = [x for x in obj.steps if not x.is_comment()]

        steps = '***Keywords***\n\n%s\n' % obj.name
        steps += tags_info
        for step in flatten(list(map(Adapter(self.context), all_steps))):
            steps += ' ' * 4 + step.astext() + '\n'

        lexer = get_lexer_by_name('robotframework')
        formatter = HtmlFormatter(noclasses=False)
        parsed = highlight(steps, lexer, formatter)
        if self.context.options.get('style', 'default') == 'default':
            parsed = re.sub('<span class="gh">[^\n]+\n\n', '', parsed)
            parsed = re.sub('<span class="gu">[^<]+</span>', '', parsed)
            parsed = re.sub('<pre><span class="p"></span>', '<pre>', parsed)
            parsed = re.sub('<span class="p">    ', '<span class="p">', parsed)
        node.append(nodes.raw('', parsed, format='html'))

        formatter = LatexFormatter()
        parsed = highlight(steps, lexer, formatter)
        if self.context.options.get('style', 'default') == 'default':
            parsed = re.sub('\\\PY{g\+gh}{[^}]+}\n\n', '', parsed)
            parsed = re.sub('\\\PY{g\+gu}{[^}]+}\n', '', parsed)
        node.append(nodes.raw('', parsed, format='latex'))

        return [node]
Beispiel #25
0
def highlight2latex(source, language='ipython', metadata=None):
    """
    Return a syntax-highlighted version of the input source as latex output.

    Parameters
    ----------
    source : str
        source of the cell to highlight.
    language : str
        Language to highlight the syntax of.
    metadata : NotebookNode cell metadata
        metadata of the cell to highlight.
    """
    return _pygment_highlight(source, LatexFormatter(), language, metadata)
Beispiel #26
0
    def format_codechunks(self, chunk):
        from pygments import highlight
        from IPython.lib.lexers import IPyLexer

        # from pygments.lexers import PythonLexer, TextLexer, PythonConsoleLexer
        from pygments.formatters import LatexFormatter

        chunk["content"] = highlight(
            chunk["content"],
            IPyLexer(),
            LatexFormatter(
                verboptions="frame=single,fontsize=\small, xleftmargin=0.5em"),
        )
        return PwebFormatter.format_codechunks(self, chunk)
Beispiel #27
0
 def to_latex(self, builder: Builder) -> str:
     lexer = None
     if self.lang is not None:
         try:
             lexer = get_lexer_by_name(self.lang, stripall=True)
         except Exception as e:
             print(e)
     output: List[str] = []
     if lexer is not None:
         formatter = LatexFormatter(linenos=False, verboptions="breaklines")
         output.append(highlight(self.code, lexer, formatter))
     else:
         output.append(r"\begin{Verbatim}[breaklines]")
         output.append(self.code)
         output.append(r"\end{Verbatim}")
     return "\n".join(output)
Beispiel #28
0
    def test_highlight(self):
        """Check that highlighting style can be changed"""
        nb = self.build_notebook()
        res = self.build_resources()
        preprocessor = self.build_preprocessor()

        # Set the style to a known builtin that's not the default
        preprocessor.style = 'colorful'
        nb, res = preprocessor(nb, res)
        style_defs = res['latex']['pygments_definitions']

        # Get the default
        from pygments.formatters import LatexFormatter
        default_defs = LatexFormatter(style='default').get_style_defs()

        # Verify that the style was in fact changed
        assert style_defs != default_defs
Beispiel #29
0
    def preprocess(self, nb, resources):
        """Preprocessing to apply on each notebook.
        
        Parameters
        ----------
        nb : NotebookNode
            Notebook being converted
        resources : dictionary
            Additional resources used in the conversion process.  Allows
            preprocessors to pass variables into the Jinja engine.
        """
        # Generate Pygments definitions for Latex
        from pygments.formatters import LatexFormatter

        resources.setdefault("latex", {})
        resources["latex"].setdefault("pygments_definitions",
                                      LatexFormatter().get_style_defs())
        return nb, resources
    def run(self):
        path = resolve_path(
            self.options.get('source', self.options.get('suite')),
            os.path.dirname(self.state.document.current_source)
        )

        lexer = get_lexer_by_name('robotframework')

        with open(path, 'r') as source:
            formatter = HtmlFormatter(noclasses=False)
            parsed = highlight(source.read(), lexer, formatter)
        html_node = nodes.raw('', parsed, format='html')

        with open(path, 'r') as source:
            formatter = LatexFormatter()
            parsed = highlight(source.read(), lexer, formatter)
        latex_node = nodes.raw('', parsed, format='latex')

        return [html_node, latex_node]