U 5_I@sdZddlmZddlmZddlmZmZddlm Z m Z ddl m Z m Z dgZdd Zd Zd Zd d ZGdddeZGdddeZdS)z pygments.formatters.latex ~~~~~~~~~~~~~~~~~~~~~~~~~ Formatter for LaTeX fancyvrb output. :copyright: Copyright 2006-2020 by the Pygments team, see AUTHORS. :license: BSD, see LICENSE for details. )StringIO) Formatter)Lexer do_insertions)TokenSTANDARD_TYPES) get_bool_opt get_int_optLatexFormattercCs|dddddddd|dd|dd |d d |d d |dd|dd|dd|dd|dd|dd|dd|dd|dd|d d!|S)"N\{}z\%sZbs{}z\%sZob{}z\%sZcb{}^z\%sZca{}_z\%sZus{}&z\%sZam{}z\%sZgt{}#z\%sZsh{}%z\%sZpc{}$z\%sZdl{}-z\%sZhy{}'z\%sZsq{}"z\%sZdq{}~z\%sZti{})replace)text commandprefixr C/usr/local/lib/python3.8/site-packages/pygments/formatters/latex.py escape_texsh      r"z \documentclass{%(docclass)s} \usepackage{fancyvrb} \usepackage{color} \usepackage[%(encoding)s]{inputenc} %(preamble)s %(styledefs)s \begin{document} \section*{%(title)s} %(code)s \end{document} a \makeatletter \def\%(cp)s@reset{\let\%(cp)s@it=\relax \let\%(cp)s@bf=\relax%% \let\%(cp)s@ul=\relax \let\%(cp)s@tc=\relax%% \let\%(cp)s@bc=\relax \let\%(cp)s@ff=\relax} \def\%(cp)s@tok#1{\csname %(cp)s@tok@#1\endcsname} \def\%(cp)s@toks#1+{\ifx\relax#1\empty\else%% \%(cp)s@tok{#1}\expandafter\%(cp)s@toks\fi} \def\%(cp)s@do#1{\%(cp)s@bc{\%(cp)s@tc{\%(cp)s@ul{%% \%(cp)s@it{\%(cp)s@bf{\%(cp)s@ff{#1}}}}}}} \def\%(cp)s#1#2{\%(cp)s@reset\%(cp)s@toks#1+\relax+\%(cp)s@do{#2}} %(styles)s \def\%(cp)sZbs{\char`\\} \def\%(cp)sZus{\char`\_} \def\%(cp)sZob{\char`\{} \def\%(cp)sZcb{\char`\}} \def\%(cp)sZca{\char`\^} \def\%(cp)sZam{\char`\&} \def\%(cp)sZlt{\char`\<} \def\%(cp)sZgt{\char`\>} \def\%(cp)sZsh{\char`\#} \def\%(cp)sZpc{\char`\%%} \def\%(cp)sZdl{\char`\$} \def\%(cp)sZhy{\char`\-} \def\%(cp)sZsq{\char`\'} \def\%(cp)sZdq{\char`\"} \def\%(cp)sZti{\char`\~} %% for compatibility with earlier versions \def\%(cp)sZat{@} \def\%(cp)sZlb{[} \def\%(cp)sZrb{]} \makeatother cCsDt|}|r|Sd}|dkr<|d|}|j}t|}q||S)N)rgetparent)ttypeZfnameZanamer r r!_get_ttype_names   r(c@sDeZdZdZdZddgZdgZddZdd Zdd d Z d dZ dS)r a Format tokens as LaTeX code. This needs the `fancyvrb` and `color` standard packages. Without the `full` option, code is formatted as one ``Verbatim`` environment, like this: .. sourcecode:: latex \begin{Verbatim}[commandchars=\\\{\}] \PY{k}{def }\PY{n+nf}{foo}(\PY{n}{bar}): \PY{k}{pass} \end{Verbatim} The special command used here (``\PY``) and all the other macros it needs are output by the `get_style_defs` method. With the `full` option, a complete LaTeX document is output, including the command definitions in the preamble. The `get_style_defs()` method of a `LatexFormatter` returns a string containing ``\def`` commands defining the macros needed inside the ``Verbatim`` environments. Additional options accepted: `style` The style to use, can be a string or a Style subclass (default: ``'default'``). `full` Tells the formatter to output a "full" document, i.e. a complete self-contained document (default: ``False``). `title` If `full` is true, the title that should be used to caption the document (default: ``''``). `docclass` If the `full` option is enabled, this is the document class to use (default: ``'article'``). `preamble` If the `full` option is enabled, this can be further preamble commands, e.g. ``\usepackage`` (default: ``''``). `linenos` If set to ``True``, output line numbers (default: ``False``). `linenostart` The line number for the first line (default: ``1``). `linenostep` If set to a number n > 1, only every nth line number is printed. `verboptions` Additional options given to the Verbatim environment (see the *fancyvrb* docs for possible values) (default: ``''``). `commandprefix` The LaTeX commands used to produce colored output are constructed using this prefix and some letters (default: ``'PY'``). .. versionadded:: 0.7 .. versionchanged:: 0.10 The default is now ``'PY'`` instead of ``'C'``. `texcomments` If set to ``True``, enables LaTeX comment lines. That is, LaTex markup in comment tokens is not escaped so that LaTeX can render it (default: ``False``). .. versionadded:: 1.2 `mathescape` If set to ``True``, enables LaTeX math mode escape in comments. That is, ``'$...$'`` inside a comment will trigger math mode (default: ``False``). .. versionadded:: 1.2 `escapeinside` If set to a string of length 2, enables escaping to LaTeX. Text delimited by these 2 characters is read as LaTeX code and typeset accordingly. It has no effect in string literals. It has no effect in comments if `texcomments` or `mathescape` is set. (default: ``''``). .. versionadded:: 2.0 `envname` Allows you to pick an alternative environment name replacing Verbatim. The alternate environment still has to support Verbatim's option syntax. (default: ``'Verbatim'``). .. versionadded:: 2.0 ZLaTeXZlatexZtexz*.texcKstj|f||dd|_|dd|_t|dd|_tt|dd|_ tt|d d|_ |d d|_ t|d d|_ |d d |_ t|dd|_t|dd|_|dd|_t|jdkr|jd|_|jd|_nd|_|dd|_|dS)NdocclassZarticlepreambler#linenosF linenostart linenostep verboptions nobackgroundrZPY texcomments mathescape escapeinsiderenvnameZVerbatim)r__init__r%r)r*rr+absr r,r.r/r0rr1r2r3lenleftrightr5_create_stylesheet)selfoptionsr r r!r6s$ zLatexFormatter.__init__c Cs4tdi}|_i}|_|j}dd}|jD]\}}t|}d}|drR|d7}|drb|d7}|drr|d 7}|d r|d 7}|d r|d 7}|dr|d 7}|dr|d||d7}|dr|d||d||df7}n|dr|d||d7}|dkrq,|d|}|||<|||<q,dS)Nr#cs$rdfdddDSdSdS)N,cs.g|]&}dt||dddqS)z%.2fr-go@)int).0icolr r! szGLatexFormatter._create_stylesheet..rgbcolor..)rr4z1,1,1)joinrCr rCr!rgbcolors z3LatexFormatter._create_stylesheet..rgbcolorZboldz\let\$$@bf=\textbfZitalicz\let\$$@it=\textitZ underlinez\let\$$@ul=\underlineZromanz\let\$$@ff=\textrmZsansz\let\$$@ff=\textsfZmonoZcolorz'\def\$$@tc##1{\textcolor[rgb]{%s}{##1}}ZborderzK\def\$$@bc##1{\setlength{\fboxsep}{0pt}\fcolorbox[rgb]{%s}{%s}{\strut ##1}}ZbgcolorzF\def\$$@bc##1{\setlength{\fboxsep}{0pt}\colorbox[rgb]{%s}{\strut ##1}}z$$)r ttype2namecmd2defrZstyler(r) r<t2nZc2dcprHr'ZndefnameZcmndefr r r!r;sL        z!LatexFormatter._create_stylesheetr#cCsH|j}g}|jD]\}}|d|||fqt|jd|dS)z Return the command sequences needed to define the commands used to format text in the verbatim environment. ``arg`` is ignored. z/\expandafter\def\csname %s@tok@%s\endcsname{%s} )rLstyles)rrJitemsappendSTYLE_TEMPLATErG)r<argrLrOrMZ definitionr r r!get_style_defs=szLatexFormatter.get_style_defsc Csv|j}|j}|jr|}t}|d|jd|jrn|j|j}}|d|rVd|pXd|rfd|phd|j s|j s|j r|d|j r|d|j |d |D]P\}} |t jkr|j r4| d d }td t| D](} |d | | krq|| | 7}q| t|d} t||}|| } n|j r| d } d } t| D]$\} } | snt| || | <| } qRd | } n|j r | }d} |r||j\}}}|r||j\}}}|r| t|||7} n| t||||7} n| t||7} qn t| |} n|t jkr,t| |} g}|t k r|z|||Wn$tk rp|t|YnX|j}q0dt|}|r| d}|ddD]*}|r|d|||f|dq|dr|d|||dfq|| q|d|jd|jrr|jp(d}dddd| dd|}|t!t"|j#|j$|j%||&|'ddS)Nz\begin{z}[commandchars=\\\{\}z ,numbers=leftz,firstnumber=%dr#z,stepnumber=%dz0,codes={\catcode`\$=3\catcode`\^=7\catcode`\_=8}r>z] rr-rF+rNr$z \%s{%s}{%s}z\end{z} utf8latin1)utf_8latin_1 iso_8859_1rr)r)r*titleencodingZ styledefscode)(rIrZfullrwriter5r+r,r.r2r1r3r/rCommentranger8r"split enumeraterG partitionr9r:EscaperQKeyErrorr(r&reversedr\r%r DOC_TEMPLATEdictr)r*r[rTgetvalue)r<Z tokensourceZoutfilerKrLZ realoutfilestartstepr'valuerBpartsZin_mathpartrasep1bsep2rOZstylevalZsplliner\r r r!format_unencodedJs                   zLatexFormatter.format_unencodedN)r#) __name__ __module__ __qualname____doc__rMaliases filenamesr6r;rTrtr r r r!r sa- c@s8eZdZdZddZddZddZdd Zd d Zd S) LatexEmbeddedLexera This lexer takes one lexer as argument, the lexer for the language being formatted, and the left and right delimiters for escaped text. First everything is scanned using the language lexer to obtain strings and comments. All other consecutive tokens are merged and the resulting text is scanned for escaped segments, which are given the Token.Escape type. Finally text that is not escaped is scanned again with the language lexer. cKs$||_||_||_tj|f|dSN)r9r:langrr6)r<r9r:r}r=r r r!r6szLatexEmbeddedLexer.__init__cCsd}g}g}||D]F\}}}|dkrL|rB|t||fg}||7}q||||fq|rt|t||ft||j|S)Nr#)_find_safe_escape_tokensrQr8rr}get_tokens_unprocessed)r<rZbufferedZ insertionsZ insertion_bufrBtvr r r!rs  z)LatexEmbeddedLexer.get_tokens_unprocessedccsd||j|ddD]F\}}}|dkrR||D]\}}}||||fVq4q|d|fVqdS)z8 find escape tokens that are not in strings or comments cSs|tjkp|tjkSr|)rr_String)rr r r!z=LatexEmbeddedLexer._find_safe_escape_tokens..N) _filter_tor}r_find_escape_tokens)r<rrBrrZi2t2Zv2r r r!r~s z+LatexEmbeddedLexer._find_safe_escape_tokensccsfd}d}|D]D\}}}||r@|r2|d|fVd}|||fVq |sH|}||7}q |rb|d|fVdS)zC Keep only the tokens that match `pred`, merge the others together r#rNr )r<itZpredZbufidxrBrrr r r!rs  zLatexEmbeddedLexer._filter_toccsd}|r||j\}}}|r6|d|fV|t|7}|r||j\}}}|r|t|tj|fV|t|t|t|7}q|tj|fV|t|7}|}qdS)z; Find escape tokens within text, give token=None otherwise rN)rcr9r8r:rrdError)r<rindexrorprqrrr r r!rs   z&LatexEmbeddedLexer._find_escape_tokensN) rurvrwrxr6rr~rrr r r r!r{s   r{N)rxiorZpygments.formatterrZpygments.lexerrrZpygments.tokenrrZ pygments.utilrr __all__r"rgrRr(r r{r r r r!s  7%