\2?@s6dZddlmZddlmZddlmZmZddlZddlZddl Z ddl m Z ddl m Z dd lmZdd lmZdd lmZdd lmZdd lmZddlmZmZdddgZe jdZGdddeZddZ ddZ!dS)a Python Markdown =============== Python Markdown converts Markdown to HTML and can be used as a library or called from the command line. ## Basic usage as a module: import markdown html = markdown.markdown(your_text_string) See for more information and instructions on how to extend the functionality of Python Markdown. Read that before you try modifying this file. ## Authors and License Started by [Manfred Stienstra](http://www.dwerg.net/). Continued and maintained by [Yuri Takhteyev](http://www.freewisdom.org), [Waylan Limberg](http://achinghead.com/) and [Artem Yunusov](http://blog.splyer.com). Contact: markdown@freewisdom.org Copyright 2007-2013 The Python Markdown Project (v. 1.7 and later) Copyright 200? Django Software Foundation (OrderedDict implementation) Copyright 2004, 2005, 2006 Yuri Takhteyev (v. 0.2-1.6b) Copyright 2004 Manfred Stienstra (the original version) License: BSD (see LICENSE for details). )absolute_import)unicode_literals)version version_infoN)util)build_preprocessors)build_block_parser)build_treeprocessors)build_inlinepatterns)build_postprocessors) Extension)to_html_stringto_xhtml_stringMarkdownmarkdownmarkdownFromFileZMARKDOWNc@s#eZdZdZdZidd6dd6dd6dd 6dd 6Zied 6ed 6ed 6ed6ed6ed6Zdddddddddddddddd gZ d!d"Z d#d$Z d%d&Z gd'd(Z d)d*Zd+d,Zd-d.Zd/d0Zd1d1d1d2d3Zd1S)4rzConvert Markdown to HTML.Zdivz[HTML_REMOVED]Zhtml_replacement_textZ tab_lengthTenable_attributesZsmart_emphasisZlazy_olhtmlZhtml4Zhtml5Zxhtmlxhtml1Zxhtml5\`*_{}[]()>#+-.!cOsnddddg}d}xO|D]G}|||krF||||s z,Markdown.build_extension..,cSs.g|]$\}}|j|jfqSr@)strip)rRrSyr@r@rArTs r%zmarkdown.extensionsrZmdxz/Failed loading extension '%s' from '%s' or '%s'z%Failed to initiate extension '%s': %s) dictfindrQupdatejoin __import__ rpartition ImportErrorr9Z makeExtensionr-AttributeError) r8Zext_namer*r;Zext_argsZpairs module_namemoduleZmodule_name_old_styleemessager@r@rArIs:   )   zMarkdown.build_extensioncCs|jj||S)z# This gets called by the extension )r0append)r8 extensionr@r@rAregisterExtensionszMarkdown.registerExtensioncCsN|jj|jjx-|jD]"}t|dr$|jq$q$W|S)zR Resets all state variables so that we can start with a new text. r7)r4r7r3clearr0hasattr)r8rfr@r@rAr7s   zMarkdown.resetcCs|j|_y|j|j|_Wntk r}zht|jj}|jd|jddj|df}|f|j dd|_ WYdd}~XnX|S)z/ Set the output format for the class instance. z+Invalid Output Format: "%s". Use one of %s."z", "rN) lowerr(output_formats serializerKeyErrorlistkeyssortr\r9)r8formatrcZ valid_formatsrdr@r@rAr6s zMarkdown.set_output_formatc Cs|jsdSytj|}Wn7tk r\}z|jd7_WYdd}~XnX|jd|_x,|jjD]}|j |j|_qW|j j |jj }x;|j jD]*}|j |}|dk r|}qqW|j|}|jryW|jd|jt|jd}|jd|j} ||| j}Wqtk r|jjd|jrd}ntd |jYqXnx&|jjD]} | j |}qW|jS) a Convert markdown to serialized XHTML or HTML. Keyword arguments: * source: Source text as a Unicode string. Markdown processing takes place in five steps: 1. A bunch of "preprocessors" munge the input text. 2. BlockParser() parses the high-level structural elements of the pre-processed text into an ElementTree. 3. A bunch of "treeprocessors" are run against the ElementTree. One such treeprocessor runs InlinePatterns against the ElementTree, detecting inline markup. 4. Some post-processors are run against the text after the ElementTree has been serialized into text. 5. The output is written to a string. r)z/. -- Note: Markdown only accepts unicode input!N z<%s>zz<%s />z4Markdown failed to strip top-level tags. Document=%r)rVr text_typeUnicodeDecodeErrorreasonrQlinesrCvaluesrunrDZ parseDocumentZgetrootrErmr1indexdoc_tagr+rindex ValueErrorendswithrF) r8sourcercZpreprootZ treeprocessorZnewRootoutputstartendppr@r@rAconverts8    '  zMarkdown.convertNc Cs|p d}|rpt|tjrBtj|ddd|}ntj||}|j}|jn3tj j}t|tj s|j |}n|j d}|j |}|rBt|tjrtj|dd|dd}|j||jqtj|}||dd}|j|nN|j|d}ytjjj|Wn"tk rtjj|YnX|S) a?Converts a markdown file and returns the HTML as a unicode string. Decodes the file using the provided encoding (defaults to utf-8), passes the file content to markdown, and outputs the html to either the provided stream or the file with provided name, using the same encoding as the source file. The 'xmlcharrefreplace' error handler is used when encoding the output. **Note:** This is the only place that decoding and encoding of unicode takes place in Python-Markdown. (All other code is unicode-in / unicode-out.) Keyword arguments: * input: File object or path. Reads from stdin if `None`. * output: File object or path. Writes to stdout if `None`. * encoding: Encoding of input and output files. Defaults to utf-8. zutf-8moderencodinguwerrorsxmlcharrefreplace)rGrrHcodecsopen getreaderreadclosesysstdinrudecodelstriprwrite getwriterencodestdoutbufferr`) r8inputrrZ input_filetextrZ output_filewriterr@r@rA convertFile@s8       zMarkdown.convertFile)rNrM __qualname____doc__r|r,rrrlZ ESCAPED_CHARSrBr2r5rIrgr7r6rrr@r@r@rAr5s4    ? .   DcOst||}|j|S)aConvert a markdown string to HTML and return HTML as a unicode string. This is a shortcut function for `Markdown` class to cover the most basic use case. It initializes an instance of Markdown, loads the necessary extensions and runs the parser on the given text. Keyword arguments: * text: Markdown formatted text as Unicode or ASCII string. * Any arguments accepted by the Markdown class. Returns: An HTML document as a string. )rr)rr9r:mdr@r@rArscOsddddg}d}xO|D]G}|||krF||||s(   W