B ññëU6W'ã @s´dZdZddlZddlZddlZddlZddlZddlZdd„Z dd„Z dd „Z d d „Z d d d ddddddœZ e fdd„ZdNdd„Zddddddddd œZd!Zd"eefd#d$„Zd%d&d'd(d)d(d*d+dœZd,Zd-d.„Zd"eefd/d0„Zed1kr°ddlZddlZddlZddlZddlZejdeje  d2¡d3Z!e!j"d4d5d6d7e!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-WdQRXe e-ƒZ.e$j/rúe e.ƒZ0nNe$j's e$j&ree.e)dJZ0n0e$j%r*ee.ƒZ0ne$j1r@ee.e)dJZ0nee.ƒZ0e$j&r¤ej2 3ej2 4e)¡¡ddKZ5e*e5dLƒZ+e+ 6e0¡WdQRXe *dMej2 7e5¡¡n ej8 6e0¡dS)Oz-Add syntax highlighting to Python source codezRaymond HettingeréNcCs tt|ƒS)z)Return True if s is the name of a builtin)ÚhasattrÚbuiltins)Ús©rúg}x.|D]&\}}| |d¡\}}||||g7}q Wd |¡S)zBAdd syntax highlighting to source code using ANSI escape sequences)r r r )Úgetr )r5Zcolorsr6r0r3ÚopenerÚcloserrrrÚansi_highlightXs r;ú
ú
cCs\|g}x@|D]8\}}|r&| d|¡| t |¡¡|r | d¡q W| |¡d |¡S)z+Convert classified text to an HTML fragmentzzr )r!Ú html_moduleÚescaper )r5r9r:r6r0r3rrrÚhtml_highlightcs r@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 {title} {body} ÚpythoncCs:d dd„| ¡Dƒ¡}t|ƒ}t |¡}|j|||dS)z6Create a complete HTML page with colorized source codeÚ cSsg|] }d|‘qS)z%s %sr)Ú.0Úitemrrrú Žsz#build_html_page..)ÚtitleÚcssÚbody)r Úitemsr@r>r?Úformat)r5rFrGÚhtmlZcss_strr6rrrÚbuild_html_page‹s rLz{\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} cs"ddddœ‰t d‡fdd„|¡S)z;Replace backslash and braces with their escaped equivalentsz\{z\}z\textbackslash{})Ú{Ú}ú\z[\\{}]cs ˆ| ¡S)N)Úgroup)Zmo)ÚxlatrrÚ³ózalltt_escape..)ÚreÚsub)rr)rQrÚ alltt_escape°s rVcCstd dd„| ¡Dƒ¡}g}x>|D]6\}}|r<| d|¡| t|ƒ¡|r"| d¡q"Wtt||d |¡dS)z;Create a complete LaTeX document with colorized source coderBcss|]}d|VqdS)z\newcommand{\py%s}[1]{%s}Nr)rCÚcrrrú ¹sz"latex_highlight..z\py%s{rNr )rFÚmacrosrH)r rIr!rVÚdefault_latex_documentÚdict)r5rFZcommandsZdocumentrYr6r0r3rrrÚlatex_highlightµsr\Ú__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 )Z descriptionZformatter_classZepilogÚ sourcefileZ SOURCEFILEz!file containing Python sourcecode)ÚmetavarÚhelpz-bz --browserÚ store_truez launch a browser to show results)Úactionr`z-cz --completezbuild a complete html webpagez-lz--latexzbuild a LaTeX documentz-rz--rawzraw 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)rFz.htmlÚwzfile://)r<r=)9Ú__doc__Ú __author__rr"rKr>rrTr&rrr4r7Z default_ansir;r@Z default_cssZ default_htmlrLZdefault_latex_commandsrZrVr\Ú__name__ÚargparseZos.pathÚosÚsysÚtextwrapZ webbrowserÚArgumentParserZRawDescriptionHelpFormatterÚdedentÚparserÚ add_argumentÚ parse_argsÚargsZsectionZbrowserZcompleteÚerrorr^ÚopenÚfÚreadr.r5ÚrawZencodedZlatexÚpathÚsplitextÚbasenameZhtmlfileÚwriteÚabspathÚstdoutrrrrÚs´)