B
    18™\Ê#  ã            	   @   s´  d Z dZddlZddlZddlZddlZddlZddlZdd„ Z	dd„ Z
dd	„ Zd
d„ ZdddddddddœZefdd„ZdNdd„Zddddddddd œZd!Zd"eefd#d$„Zd%d&d'd(d)d(d*d+dœZd,Zd-d.„ Zd"eefd/d0„Zed1kr°ddlZddlZddlZddlZddlZejd eje  d2¡d3Z!e!j"d4d5d6d7 e!j"d8d9d:d;d< e!j"d=d>d:d?d< e!j"d@dAd:dBd< e!j"dCdDd:dEd< e!j"dFdGd:dHd< e! #¡ Z$e$j%r¾e$j&s´e$j'r¾e! (dI¡ e$j)Z)e*e)ƒZ+e+ ,¡ Z-W dQ R X ee-ƒZ.e$j/rúee.ƒZ0nNe$j's
e$j&ree.e)dJZ0n0e$j%r*ee.ƒZ0ne$j1r@ee.e)dJZ0nee.ƒZ0e$j&r¤ej2 3ej2 4e)¡¡d dK Z5e*e5dLƒZ+e+ 6e0¡ W dQ R X e *dMej2 7e5¡ ¡ nej8 6e0¡ dS )Oz-Add syntax highlighting to Python source codezRaymond Hettingeré    Nc             C   s
   t t| ƒS )z)Return True if s is the name of a builtin)ÚhasattrÚbuiltins)Ús© r   úW/oak/stanford/groups/akundaje/marinovg/programs/Python-3.7.3/Tools/scripts/highlight.pyÚ
is_builtin   s    r   c             C   s|   || \}}\}}||kr2| |d  ||… |fS | |d  |d… g| ||d …  | |d  d|… g }d  |¡|fS )z8Join content from a range of lines between start and endé   NÚ )Újoin)ÚlinesÚstartÚendÚsrowÚscolÚerowÚecolZrowsr   r   r   Úcombine_range   s
    <r   c             c   sx  |   d¡}| d¡ t tt|ƒd¡}d }}tj}d}xt |¡D ]
}|| }}	|\}}\}
}\}}}d}|tjkr€d}n˜|tj	kr |dd… dkr d}nx|tj
krÆd	}|tjksÀ|d
krÄd}nR|tjkr|dkràd}n8|	dkrîd}n*t |¡r d}nt|ƒr|	dkrd}|rFt|||
|fƒ\}}d|fV  |||f }}||fV  qFW t||||fƒ\}}d|fV  dS )z„Generate and classify chunks of Python for syntax highlighting.
       Yields tuples in the form: (category, categorized_text).
    Tr	   )r   r   ÚcommentNr   z{}[](),.:;@ÚoperatorÚstringr   Ú	docstring)ÚdefÚclassÚimportÚfromÚ
definition)r   r   ÚdefnameÚkeywordÚ.Úbuiltin)Ú
splitlinesÚappendÚ	functoolsÚpartialÚnextÚiterÚtokenizeÚCOMMENTÚgenerate_tokensÚOPÚSTRINGÚINDENTÚNAMEr   Ú	iskeywordr   r   )Úsourcer   ÚreadlineÚkindZtok_strÚtok_typeZwrittenÚtokZprev_tok_typeZprev_tok_strr   r   r   r   Zlogical_linenoÚtextZline_upto_tokenr   r   r   Úanalyze_python   sD    





r4   c             C   s6   g }x&| D ]\}}|  d|pd|f ¡ q
W d |¡S )z-Straight text display of text classificationsz
%15s:  %r
Úplainr	   )r!   r
   )Úclassified_textÚresultr0   r3   r   r   r   Úraw_highlightD   s    r8   )z[0;31mz[0m)z[0;32mz[0m)z[0;33mz[0m)z[0;35mz[0m)z[0;34mz[0m)r   r   r   r   r   r   r   r   c             C   s>   g }x.| D ]&\}}|  |d¡\}}||||g7 }q
W d |¡S )zBAdd syntax highlighting to source code using ANSI escape sequences)r	   r	   r	   )Úgetr
   )r6   Zcolorsr7   r0   r3   ÚopenerÚcloserr   r   r   Úansi_highlightX   s
    r<   ú<pre class="python">
ú</pre>
c             C   s\   |g}x@| D ]8\}}|r&|  d| ¡ |  t |¡¡ |r|  d¡ qW |  |¡ d |¡S )z+Convert classified text to an HTML fragmentz<span class="%s">z</span>r	   )r!   Úhtml_moduleÚescaper
   )r6   r:   r;   r7   r0   r3   r   r   r   Úhtml_highlightc   s    
rA   z{color: crimson;}z{color: forestgreen;}z({color: forestgreen; font-style:italic;}z{color: darkorange;}z{color: purple;}z&{color: darkorange; font-weight:bold;}z{color: blue;}z{color: brown;})z.commentz.stringz
.docstringz.keywordz.builtinz.definitionz.defnamez	.operatora  <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
          "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-type" content="text/html;charset=UTF-8">
<title> {title} </title>
<style type="text/css">
{css}
</style>
</head>
<body>
{body}
</body>
</html>
Úpythonc             C   s:   d  dd„ | ¡ D ƒ¡}t| ƒ}t |¡}|j|||dS )z6Create a complete HTML page with colorized source codeÚ
c             S   s   g | ]}d | ‘qS )z%s %sr   )Ú.0Úitemr   r   r   ú
<listcomp>Ž   s    z#build_html_page.<locals>.<listcomp>)ÚtitleÚcssÚbody)r
   ÚitemsrA   r?   r@   Úformat)r6   rG   rH   ÚhtmlZcss_strr7   r   r   r   Úbuild_html_page‹   s    
rM   z{\color{red}#1}z{\color{ForestGreen}#1}z{\emph{\color{ForestGreen}#1}}z{\color{orange}#1}z{\color{purple}#1}z{\color{blue}#1}z{\color{brown}#1}a   
\documentclass{article}
\usepackage{alltt}
\usepackage{upquote}
\usepackage{color}
\usepackage[usenames,dvipsnames]{xcolor}
\usepackage[cm]{fullpage}
%(macros)s
\begin{document}
\center{\LARGE{%(title)s}}
\begin{alltt}
%(body)s
\end{alltt}
\end{document}
c                s"   ddddœ‰ t  d‡ fdd„| ¡S )z;Replace backslash and braces with their escaped equivalentsz\{z\}z\textbackslash{})ú{ú}ú\z[\\{}]c                s   ˆ |   ¡  S )N)Úgroup)Úmo)Úxlatr   r   Ú<lambda>³   ó    zalltt_escape.<locals>.<lambda>)ÚreÚsub)r   r   )rS   r   Úalltt_escape°   s    rX   c             C   st   d  dd„ | ¡ D ƒ¡}g }x>| D ]6\}}|r<| d| ¡ | t|ƒ¡ |r"| d¡ q"W tt||d  |¡d S )z;Create a complete LaTeX document with colorized source coderC   c             s   s   | ]}d | V  qdS )z\newcommand{\py%s}[1]{%s}Nr   )rD   Úcr   r   r   ú	<genexpr>¹   s    z"latex_highlight.<locals>.<genexpr>z\py%s{rO   r	   )rG   ÚmacrosrI   )r
   rJ   r!   rX   Údefault_latex_documentÚdict)r6   rG   ÚcommandsZdocumentr[   r7   r0   r3   r   r   r   Úlatex_highlightµ   s    r_   Ú__main__aa  
                examples:

                  # Show syntax highlighted code in the terminal window
                  $ ./highlight.py myfile.py

                  # Colorize myfile.py and display in a browser
                  $ ./highlight.py -b myfile.py

                  # Create an HTML section to embed in an existing webpage
                  ./highlight.py -s myfile.py

                  # Create a complete HTML file
                  $ ./highlight.py -c myfile.py > myfile.html

                  # Create a PDF using LaTeX
                  $ ./highlight.py -l myfile.py | pdflatex

            )ÚdescriptionÚformatter_classÚepilogÚ
sourcefileZ
SOURCEFILEz!file containing Python sourcecode)ÚmetavarÚhelpz-bz	--browserÚ
store_truez launch a browser to show results)Úactionrf   z-cz
--completezbuild a complete html webpagez-lz--latexzbuild a LaTeX documentz-rz--rawzraw parse of categorized textz-sz	--sectionz3show an HTML section rather than a complete webpagezVThe -s/--section option is incompatible with the -b/--browser or -c/--complete options)rG   z.htmlÚwzfile://)r=   r>   )9Ú__doc__Ú
__author__r   r"   rL   r?   r   rV   r&   r   r   r4   r8   Zdefault_ansir<   rA   Zdefault_cssZdefault_htmlrM   Zdefault_latex_commandsr\   rX   r_   Ú__name__ÚargparseÚos.pathÚosÚsysÚtextwrapÚ
webbrowserÚArgumentParserÚRawDescriptionHelpFormatterÚdedentÚparserÚadd_argumentÚ
parse_argsÚargsÚsectionÚbrowserÚcompleteÚerrorrd   ÚopenÚfÚreadr.   r6   ÚrawÚencodedZlatexÚpathÚsplitextÚbasenameZhtmlfileÚwriteÚabspathÚstdoutr   r   r   r   Ú<module>   s´   )













