<s> from __future__ import generators <EOL> r"""<STR_LIT>""" <EOL> cmdln_desc = """<STR_LIT>""" <EOL> __version_info__ = ( <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:1> ) <EOL> __version__ = '<STR_LIT:.>' . join ( map ( str , __version_info__ ) ) <EOL> __author__ = "<STR_LIT>" <EOL> import os <EOL> import sys <EOL> from pprint import pprint , pformat <EOL> import re <EOL> import logging <EOL> try : <EOL> from hashlib import md5 <EOL> except ImportError : <EOL> from md5 import md5 <EOL> import optparse <EOL> from random import random , randint <EOL> import codecs <EOL> try : <EOL> from urllib . parse import quote <EOL> except ImportError : <EOL> from urllib import quote <EOL> if sys . version_info [ : <NUM_LIT:2> ] < ( <NUM_LIT:2> , <NUM_LIT:4> ) : <EOL> from sets import Set as set <EOL> def reversed ( sequence ) : <EOL> for i in sequence [ : : - <NUM_LIT:1> ] : <EOL> yield i <EOL> if sys . version_info [ <NUM_LIT:0> ] <= <NUM_LIT:2> : <EOL> py3 = False <EOL> try : <EOL> bytes <EOL> except NameError : <EOL> bytes = str <EOL> base_string_type = basestring <EOL> elif sys . version_info [ <NUM_LIT:0> ] >= <NUM_LIT:3> : <EOL> py3 = True <EOL> unicode = str <EOL> base_string_type = str <EOL> DEBUG = False <EOL> log = logging . getLogger ( "<STR_LIT>" ) <EOL> DEFAULT_TAB_WIDTH = <NUM_LIT:4> <EOL> SECRET_SALT = bytes ( randint ( <NUM_LIT:0> , <NUM_LIT> ) ) <EOL> def _hash_text ( s ) : <EOL> return '<STR_LIT>' + md5 ( SECRET_SALT + s . encode ( "<STR_LIT:utf-8>" ) ) . hexdigest ( ) <EOL> g_escape_table = dict ( [ ( ch , _hash_text ( ch ) ) <EOL> for ch in '<STR_LIT>' ] ) <EOL> class MarkdownError ( Exception ) : <EOL> pass <EOL> def markdown_path ( path , encoding = "<STR_LIT:utf-8>" , <EOL> html4tags = False , tab_width = DEFAULT_TAB_WIDTH , <EOL> safe_mode = None , extras = None , link_patterns = None , <EOL> use_file_vars = False ) : <EOL> fp = codecs . open ( path , '<STR_LIT:r>' , encoding ) <EOL> text = fp . read ( ) <EOL> fp . close ( ) <EOL> return Markdown ( html4tags = html4tags , tab_width = tab_width , <EOL> safe_mode = safe_mode , extras = extras , <EOL> link_patterns = link_patterns , <EOL> use_file_vars = use_file_vars ) . convert ( text ) <EOL> def markdown ( text , html4tags = False , tab_width = DEFAULT_TAB_WIDTH , <EOL> safe_mode = None , extras = None , link_patterns = None , <EOL> use_file_vars = False ) : <EOL> return Markdown ( html4tags = html4tags , tab_width = tab_width , <EOL> safe_mode = safe_mode , extras = extras , <EOL> link_patterns = link_patterns , <EOL> use_file_vars = use_file_vars ) . convert ( text ) <EOL> class Markdown ( object ) : <EOL> extras = None <EOL> urls = None <EOL> titles = None <EOL> html_blocks = None <EOL> html_spans = None <EOL> math_spans = None <EOL> html_removed_text = "<STR_LIT>" <EOL> list_level = <NUM_LIT:0> <EOL> _ws_only_line_re = re . compile ( r"<STR_LIT>" , re . M ) <EOL> def __init__ ( self , html4tags = False , tab_width = <NUM_LIT:4> , safe_mode = None , <EOL> extras = None , link_patterns = None , use_file_vars = False ) : <EOL> if html4tags : <EOL> self . empty_element_suffix = "<STR_LIT:>>" <EOL> else : <EOL> self . empty_element_suffix = "<STR_LIT>" <EOL> self . tab_width = tab_width <EOL> if safe_mode is True : <EOL> self . safe_mode = "<STR_LIT:replace>" <EOL> else : <EOL> self . safe_mode = safe_mode <EOL> if self . extras is None : <EOL> self . extras = { } <EOL> elif not isinstance ( self . extras , dict ) : <EOL> self . extras = dict ( [ ( e , None ) for e in self . extras ] ) <EOL> if extras : <EOL> if not isinstance ( extras , dict ) : <EOL> extras = dict ( [ ( e , None ) for e in extras ] ) <EOL> self . extras . update ( extras ) <EOL> assert isinstance ( self . extras , dict ) <EOL> if "<STR_LIT>" in self . extras and not "<STR_LIT>" in self . extras : <EOL> self . extras [ "<STR_LIT>" ] = None <EOL> self . _instance_extras = self . extras . copy ( ) <EOL> self . link_patterns = link_patterns <EOL> self . use_file_vars = use_file_vars <EOL> self . _outdent_re = re . compile ( r'<STR_LIT>' % tab_width , re . M ) <EOL> self . _escape_table = g_escape_table . copy ( ) <EOL> if "<STR_LIT>" in self . extras : <EOL> self . _escape_table [ '<STR_LIT:">' ] = _hash_text ( '<STR_LIT:">' ) <EOL> self . _escape_table [ "<STR_LIT:'>" ] = _hash_text ( "<STR_LIT:'>" ) <EOL> def reset ( self ) : <EOL> self . urls = { } <EOL> self . titles = { } <EOL> self . html_blocks = { } <EOL> self . html_spans = { } <EOL> self . math_spans = { } <EOL> self . list_level = <NUM_LIT:0> <EOL> self . extras = self . _instance_extras . copy ( ) <EOL> if "<STR_LIT>" in self . extras : <EOL> self . footnotes = { } <EOL> self . footnote_ids = [ ] <EOL> self . inline_footnote_id = <NUM_LIT:0> <EOL> if "<STR_LIT>" in self . extras : <EOL> self . _count_from_header_id = { } <EOL> if "<STR_LIT>" in self . extras : <EOL> self . metadata = { } <EOL> _a_nofollow = re . compile ( r"<STR_LIT>" , re . IGNORECASE ) <EOL> def convert ( self , text ) : <EOL> """<STR_LIT>""" <EOL> self . reset ( ) <EOL> if not isinstance ( text , unicode ) : <EOL> text = unicode ( text , '<STR_LIT:utf-8>' ) <EOL> if self . use_file_vars : <EOL> emacs_vars = self . _get_emacs_vars ( text ) <EOL> if "<STR_LIT>" in emacs_vars : <EOL> splitter = re . compile ( "<STR_LIT>" ) <EOL> for e in splitter . split ( emacs_vars [ "<STR_LIT>" ] ) : <EOL> if '<STR_LIT:=>' in e : <EOL> ename , earg = e . split ( '<STR_LIT:=>' , <NUM_LIT:1> ) <EOL> try : <EOL> earg = int ( earg ) <EOL> except ValueError : <EOL> pass <EOL> else : <EOL> ename , earg = e , None <EOL> self . extras [ ename ] = earg <EOL> text = re . sub ( "<STR_LIT>" , "<STR_LIT:\n>" , text ) <EOL> text += "<STR_LIT>" <EOL> text = self . _detab ( text ) <EOL> text = self . _ws_only_line_re . sub ( "<STR_LIT>" , text ) <EOL> if "<STR_LIT>" in self . extras : <EOL> text = self . _extract_metadata ( text ) <EOL> text = self . preprocess ( text ) <EOL> if self . extras . get ( "<STR_LIT>" ) : <EOL> text = self . _hash_math_spans ( text ) <EOL> if "<STR_LIT>" in self . extras and not self . safe_mode : <EOL> text = self . _do_fenced_code_blocks ( text ) <EOL> if self . safe_mode : <EOL> text = self . _hash_html_spans ( text ) <EOL> text = self . _hash_html_blocks ( text , raw = True ) <EOL> if "<STR_LIT>" in self . extras and self . safe_mode : <EOL> text = self . _do_fenced_code_blocks ( text ) <EOL> if "<STR_LIT>" in self . extras : <EOL> text = self . _strip_footnote_definitions ( text ) <EOL> text = self . _strip_inline_footnotes ( text ) <EOL> text = self . _strip_link_definitions ( text ) <EOL> text = self . _run_block_gamut ( text ) <EOL> if "<STR_LIT>" in self . extras : <EOL> text = self . _sort_footnotes ( text ) <EOL> text = self . _add_footnotes ( text ) <EOL> text = self . postprocess ( text ) <EOL> text = self . _unescape_special_chars ( text ) <EOL> if self . safe_mode : <EOL> text = self . _unhash_html_spans ( text ) <EOL> if self . extras . get ( "<STR_LIT>" ) : <EOL> text = self . _unhash_math_spans ( text ) <EOL> if "<STR_LIT>" in self . extras : <EOL> text = self . _a_nofollow . sub ( r'<STR_LIT>' , text ) <EOL> text += "<STR_LIT:\n>" <EOL> rv = UnicodeWithAttrs ( text ) <EOL> if "<STR_LIT>" in self . extras : <EOL> rv . _toc = self . _toc <EOL> if "<STR_LIT>" in self . extras : <EOL> rv . metadata = self . metadata <EOL> return rv <EOL> def postprocess ( self , text ) : <EOL> """<STR_LIT>""" <EOL> return text <EOL> def preprocess ( self , text ) : <EOL> """<STR_LIT>""" <EOL> return text <EOL> _metadata_pat = re . compile ( """<STR_LIT>""" ) <EOL> def _extract_metadata ( self , text ) : <EOL> if not text . startswith ( "<STR_LIT>" ) : <EOL> return text <EOL> match = self . _metadata_pat . match ( text ) <EOL> if not match : <EOL> return text <EOL> tail = text [ len ( match . group ( <NUM_LIT:0> ) ) : ] <EOL> metadata_str = match . group ( <NUM_LIT:1> ) . strip ( ) <EOL> for line in metadata_str . split ( '<STR_LIT:\n>' ) : <EOL> key , value = line . split ( '<STR_LIT::>' , <NUM_LIT:1> ) <EOL> self . metadata [ key . strip ( ) ] = value . strip ( ) <EOL> return tail <EOL> _emacs_oneliner_vars_pat = re . compile ( r"<STR_LIT>" , re . UNICODE ) <EOL> _emacs_local_vars_pat = re . compile ( r"""<STR_LIT>""" , re . IGNORECASE | re . MULTILINE | re . DOTALL | re . VERBOSE ) <EOL> def _get_emacs_vars ( self , text ) : <EOL> """<STR_LIT>""" <EOL> emacs_vars = { } <EOL> SIZE = pow ( <NUM_LIT:2> , <NUM_LIT> ) <EOL> head = text [ : SIZE ] <EOL> if "<STR_LIT>" in head : <EOL> match = self . _emacs_oneliner_vars_pat . search ( head ) <EOL> if match : <EOL> emacs_vars_str = match . group ( <NUM_LIT:1> ) <EOL> assert '<STR_LIT:\n>' not in emacs_vars_str <EOL> emacs_var_strs = [ s . strip ( ) for s in emacs_vars_str . split ( '<STR_LIT:;>' ) <EOL> if s . strip ( ) ] <EOL> if len ( emacs_var_strs ) == <NUM_LIT:1> and '<STR_LIT::>' not in emacs_var_strs [ <NUM_LIT:0> ] : <EOL> emacs_vars [ "<STR_LIT>" ] = emacs_var_strs [ <NUM_LIT:0> ] . strip ( ) <EOL> else : <EOL> for emacs_var_str in emacs_var_strs : <EOL> try : <EOL> variable , value = emacs_var_str . strip ( ) . split ( '<STR_LIT::>' , <NUM_LIT:1> ) <EOL> except ValueError : <EOL> log . debug ( "<STR_LIT>" <EOL> "<STR_LIT>" , emacs_var_str ) <EOL> continue <EOL> emacs_vars [ variable . lower ( ) ] = value . strip ( ) <EOL> tail = text [ - SIZE : ] <EOL> if "<STR_LIT>" in tail : <EOL> match = self . _emacs_local_vars_pat . search ( tail ) <EOL> if match : <EOL> prefix = match . group ( "<STR_LIT>" ) <EOL> suffix = match . group ( "<STR_LIT>" ) <EOL> lines = match . group ( "<STR_LIT:content>" ) . splitlines ( <NUM_LIT:0> ) <EOL> for i , line in enumerate ( lines ) : <EOL> if not line . startswith ( prefix ) : <EOL> log . debug ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> % ( line , prefix ) ) <EOL> return { } <EOL> if i != len ( lines ) - <NUM_LIT:1> and not line . endswith ( suffix ) : <EOL> log . debug ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> % ( line , suffix ) ) <EOL> return { } <EOL> continued_for = None <EOL> for line in lines [ : - <NUM_LIT:1> ] : <EOL> if prefix : line = line [ len ( prefix ) : ] <EOL> if suffix : line = line [ : - len ( suffix ) ] <EOL> line = line . strip ( ) <EOL> if continued_for : <EOL> variable = continued_for <EOL> if line . endswith ( '<STR_LIT:\\>' ) : <EOL> line = line [ : - <NUM_LIT:1> ] . rstrip ( ) <EOL> else : <EOL> continued_for = None <EOL> emacs_vars [ variable ] += '<STR_LIT:U+0020>' + line <EOL> else : <EOL> try : <EOL> variable , value = line . split ( '<STR_LIT::>' , <NUM_LIT:1> ) <EOL> except ValueError : <EOL> log . debug ( "<STR_LIT>" <EOL> "<STR_LIT>" % line ) <EOL> continue <EOL> value = value . strip ( ) <EOL> if value . endswith ( '<STR_LIT:\\>' ) : <EOL> value = value [ : - <NUM_LIT:1> ] . rstrip ( ) <EOL> continued_for = variable <EOL> else : <EOL> continued_for = None <EOL> emacs_vars [ variable ] = value <EOL> for var , val in list ( emacs_vars . items ( ) ) : <EOL> if len ( val ) > <NUM_LIT:1> and ( val . startswith ( '<STR_LIT:">' ) and val . endswith ( '<STR_LIT:">' ) <EOL> or val . startswith ( '<STR_LIT:">' ) and val . endswith ( '<STR_LIT:">' ) ) : <EOL> emacs_vars [ var ] = val [ <NUM_LIT:1> : - <NUM_LIT:1> ] <EOL> return emacs_vars <EOL> _detab_re = re . compile ( r'<STR_LIT>' , re . M ) <EOL> def _detab_sub ( self , match ) : <EOL> g1 = match . group ( <NUM_LIT:1> ) <EOL> return g1 + ( '<STR_LIT:U+0020>' * ( self . tab_width - len ( g1 ) % self . tab_width ) ) <EOL> def _detab ( self , text ) : <EOL> r"""<STR_LIT>""" <EOL> if '<STR_LIT:\t>' not in text : <EOL> return text <EOL> return self . _detab_re . subn ( self . _detab_sub , text ) [ <NUM_LIT:0> ] <EOL> _html5tags = '<STR_LIT>' <EOL> _block_tags_a = '<STR_LIT>' <EOL> _block_tags_a += _html5tags <EOL> _strict_tag_block_re = re . compile ( r"""<STR_LIT>""" % _block_tags_a , <EOL> re . X | re . M ) <EOL> _block_tags_b = '<STR_LIT>' <EOL> _block_tags_b += _html5tags <EOL> _liberal_tag_block_re = re . compile ( r"""<STR_LIT>""" % _block_tags_b , <EOL> re . X | re . M ) <EOL> _html_markdown_attr_re = re . compile ( <EOL> r'''<STR_LIT>''' ) <EOL> def _hash_html_block_sub ( self , match , raw = False ) : <EOL> html = match . group ( <NUM_LIT:1> ) <EOL> if raw and self . safe_mode : <EOL> html = self . _sanitize_html ( html ) <EOL> elif '<STR_LIT>' in self . extras and '<STR_LIT>' in html : <EOL> first_line = html . split ( '<STR_LIT:\n>' , <NUM_LIT:1> ) [ <NUM_LIT:0> ] <EOL> m = self . _html_markdown_attr_re . search ( first_line ) <EOL> if m : <EOL> lines = html . split ( '<STR_LIT:\n>' ) <EOL> middle = '<STR_LIT:\n>' . join ( lines [ <NUM_LIT:1> : - <NUM_LIT:1> ] ) <EOL> last_line = lines [ - <NUM_LIT:1> ] <EOL> first_line = first_line [ : m . start ( ) ] + first_line [ m . end ( ) : ] <EOL> f_key = _hash_text ( first_line ) <EOL> self . html_blocks [ f_key ] = first_line <EOL> l_key = _hash_text ( last_line ) <EOL> self . html_blocks [ l_key ] = last_line <EOL> return '<STR_LIT>' . join ( [ "<STR_LIT>" , f_key , <EOL> "<STR_LIT>" , middle , "<STR_LIT>" , <EOL> l_key , "<STR_LIT>" ] ) <EOL> key = _hash_text ( html ) <EOL> self . html_blocks [ key ] = html <EOL> return "<STR_LIT>" + key + "<STR_LIT>" <EOL> def _hash_html_blocks ( self , text , raw = False ) : <EOL> """<STR_LIT>""" <EOL> if '<STR_LIT:<>' not in text : <EOL> return text <EOL> hash_html_block_sub = _curry ( self . _hash_html_block_sub , raw = raw ) <EOL> text = self . _strict_tag_block_re . sub ( hash_html_block_sub , text ) <EOL> text = self . _liberal_tag_block_re . sub ( hash_html_block_sub , text ) <EOL> if "<STR_LIT>" in text : <EOL> _hr_tag_re = _hr_tag_re_from_tab_width ( self . tab_width ) <EOL> text = _hr_tag_re . sub ( hash_html_block_sub , text ) <EOL> if "<STR_LIT>" in text : <EOL> start = <NUM_LIT:0> <EOL> while True : <EOL> try : <EOL> start_idx = text . index ( "<STR_LIT>" , start ) <EOL> except ValueError : <EOL> break <EOL> try : <EOL> end_idx = text . index ( "<STR_LIT>" , start_idx ) + <NUM_LIT:3> <EOL> except ValueError : <EOL> break <EOL> start = end_idx <EOL> if start_idx : <EOL> for i in range ( self . tab_width - <NUM_LIT:1> ) : <EOL> if text [ start_idx - <NUM_LIT:1> ] != '<STR_LIT:U+0020>' : <EOL> break <EOL> start_idx -= <NUM_LIT:1> <EOL> if start_idx == <NUM_LIT:0> : <EOL> break <EOL> if start_idx == <NUM_LIT:0> : <EOL> pass <EOL> elif start_idx == <NUM_LIT:1> and text [ <NUM_LIT:0> ] == '<STR_LIT:\n>' : <EOL> start_idx = <NUM_LIT:0> <EOL> elif text [ start_idx - <NUM_LIT:2> : start_idx ] == '<STR_LIT>' : <EOL> pass <EOL> else : <EOL> break <EOL> while end_idx < len ( text ) : <EOL> if text [ end_idx ] not in '<STR_LIT>' : <EOL> break <EOL> end_idx += <NUM_LIT:1> <EOL> if text [ end_idx : end_idx + <NUM_LIT:2> ] not in ( '<STR_LIT>' , '<STR_LIT:\n>' , '<STR_LIT>' ) : <EOL> continue <EOL> html = text [ start_idx : end_idx ] <EOL> if raw and self . safe_mode : <EOL> html = self . _sanitize_html ( html ) <EOL> key = _hash_text ( html ) <EOL> self . html_blocks [ key ] = html <EOL> text = text [ : start_idx ] + "<STR_LIT>" + key + "<STR_LIT>" + text [ end_idx : ] <EOL> if "<STR_LIT>" in self . extras : <EOL> _xml_oneliner_re = _xml_oneliner_re_from_tab_width ( self . tab_width ) <EOL> text = _xml_oneliner_re . sub ( hash_html_block_sub , text ) <EOL> return text <EOL> def _strip_link_definitions ( self , text ) : <EOL> less_than_tab = self . tab_width - <NUM_LIT:1> <EOL> _link_def_re = re . compile ( r"""<STR_LIT>""" % less_than_tab , re . X | re . M | re . U ) <EOL> return _link_def_re . sub ( self . _extract_link_def_sub , text ) <EOL> def _extract_link_def_sub ( self , match ) : <EOL> id , url , title = match . groups ( ) <EOL> key = id . lower ( ) <EOL> self . urls [ key ] = self . _encode_amps_and_angles ( url ) <EOL> if title : <EOL> self . titles [ key ] = title <EOL> return "<STR_LIT>" <EOL> def _extract_footnote_def_sub ( self , match ) : <EOL> id , text = match . groups ( ) <EOL> text = _dedent ( text , skip_first_line = not text . startswith ( '<STR_LIT:\n>' ) ) . strip ( ) <EOL> normed_id = re . sub ( r'<STR_LIT>' , '<STR_LIT:->' , id ) <EOL> self . footnotes [ normed_id ] = text + "<STR_LIT>" <EOL> return "<STR_LIT>" <EOL> def _strip_footnote_definitions ( self , text ) : <EOL> """<STR_LIT>""" <EOL> less_than_tab = self . tab_width - <NUM_LIT:1> <EOL> footnote_def_re = re . compile ( r'''<STR_LIT>''' % ( less_than_tab , self . tab_width , self . tab_width ) , <EOL> re . X | re . M ) <EOL> return footnote_def_re . sub ( self . _extract_footnote_def_sub , text ) <EOL> def _extract_inline_footnote_sub ( self , match ) : <EOL> text = match . group ( <NUM_LIT:1> ) . strip ( ) <EOL> self . inline_footnote_id += <NUM_LIT:1> <EOL> self . footnotes [ str ( self . inline_footnote_id ) ] = text + "<STR_LIT>" <EOL> return '<STR_LIT>' % self . inline_footnote_id <EOL> def _strip_inline_footnotes ( self , text ) : <EOL> """<STR_LIT>""" <EOL> _inline_footnote_re = re . compile ( r'<STR_LIT>' ) <EOL> return _inline_footnote_re . sub ( self . _extract_inline_footnote_sub , text ) <EOL> _hr_re = re . compile ( r'<STR_LIT>' , re . M ) <EOL> def _run_block_gamut ( self , text ) : <EOL> if "<STR_LIT>" in self . extras : <EOL> text = self . _do_fenced_code_blocks ( text ) <EOL> text = self . _do_headers ( text ) <EOL> hr = "<STR_LIT>" + self . empty_element_suffix + "<STR_LIT:\n>" <EOL> text = re . sub ( self . _hr_re , hr , text ) <EOL> text = self . _do_lists ( text ) <EOL> if "<STR_LIT>" in self . extras : <EOL> text = self . _prepare_pyshell_blocks ( text ) <EOL> if "<STR_LIT>" in self . extras : <EOL> text = self . _do_wiki_tables ( text ) <EOL> if "<STR_LIT>" in self . extras : <EOL> text = self . _do_tables ( text ) <EOL> text = self . _do_code_blocks ( text ) <EOL> text = self . _do_block_quotes ( text ) <EOL> text = self . _hash_html_blocks ( text ) <EOL> text = self . _form_paragraphs ( text ) <EOL> return text <EOL> def _pyshell_block_sub ( self , match ) : <EOL> lines = match . group ( <NUM_LIT:0> ) . splitlines ( <NUM_LIT:0> ) <EOL> _dedentlines ( lines ) <EOL> indent = '<STR_LIT:U+0020>' * self . tab_width <EOL> s = ( '<STR_LIT:\n>' <EOL> + indent + ( '<STR_LIT:\n>' + indent ) . join ( lines ) <EOL> + '<STR_LIT>' ) <EOL> return s <EOL> def _prepare_pyshell_blocks ( self , text ) : <EOL> """<STR_LIT>""" <EOL> if "<STR_LIT>" not in text : <EOL> return text <EOL> less_than_tab = self . tab_width - <NUM_LIT:1> <EOL> _pyshell_block_re = re . compile ( r"""<STR_LIT>""" % less_than_tab , re . M | re . X ) <EOL> return _pyshell_block_re . sub ( self . _pyshell_block_sub , text ) <EOL> def _table_sub ( self , match ) : <EOL> head , underline , body = match . groups ( ) <EOL> cols = [ cell . strip ( ) for cell in underline . strip ( '<STR_LIT>' ) . split ( '<STR_LIT:|>' ) ] <EOL> align_from_col_idx = { } <EOL> for col_idx , col in enumerate ( cols ) : <EOL> if col [ <NUM_LIT:0> ] == '<STR_LIT::>' and col [ - <NUM_LIT:1> ] == '<STR_LIT::>' : <EOL> align_from_col_idx [ col_idx ] = '<STR_LIT>' <EOL> elif col [ <NUM_LIT:0> ] == '<STR_LIT::>' : <EOL> align_from_col_idx [ col_idx ] = '<STR_LIT>' <EOL> elif col [ - <NUM_LIT:1> ] == '<STR_LIT::>' : <EOL> align_from_col_idx [ col_idx ] = '<STR_LIT>' <EOL> hlines = [ '<STR_LIT>' % self . _html_class_str_from_tag ( '<STR_LIT>' ) , '<STR_LIT>' , '<STR_LIT>' ] <EOL> cols = [ cell . strip ( ) for cell in head . strip ( '<STR_LIT>' ) . split ( '<STR_LIT:|>' ) ] <EOL> for col_idx , col in enumerate ( cols ) : <EOL> hlines . append ( '<STR_LIT>' % ( <EOL> align_from_col_idx . get ( col_idx , '<STR_LIT>' ) , <EOL> self . _run_span_gamut ( col ) <EOL> ) ) <EOL> hlines . append ( '<STR_LIT>' ) <EOL> hlines . append ( '<STR_LIT>' ) <EOL> hlines . append ( '<STR_LIT>' ) <EOL> for line in body . strip ( '<STR_LIT:\n>' ) . split ( '<STR_LIT:\n>' ) : <EOL> hlines . append ( '<STR_LIT>' ) <EOL> cols = [ cell . strip ( ) for cell in line . strip ( '<STR_LIT>' ) . split ( '<STR_LIT:|>' ) ] <EOL> for col_idx , col in enumerate ( cols ) : <EOL> hlines . append ( '<STR_LIT>' % ( <EOL> align_from_col_idx . get ( col_idx , '<STR_LIT>' ) , <EOL> self . _run_span_gamut ( col ) <EOL> ) ) <EOL> hlines . append ( '<STR_LIT>' ) <EOL> hlines . append ( '<STR_LIT>' ) <EOL> hlines . append ( '<STR_LIT>' ) <EOL> return '<STR_LIT:\n>' . join ( hlines ) + '<STR_LIT:\n>' <EOL> def _do_tables ( self , text ) : <EOL> """<STR_LIT>""" <EOL> less_than_tab = self . tab_width - <NUM_LIT:1> <EOL> table_re = re . compile ( r'''<STR_LIT>''' % ( less_than_tab , less_than_tab , less_than_tab ) , re . M | re . X ) <EOL> return table_re . sub ( self . _table_sub , text ) <EOL> def _wiki_table_sub ( self , match ) : <EOL> ttext = match . group ( <NUM_LIT:0> ) . strip ( ) <EOL> rows = [ ] <EOL> for line in ttext . splitlines ( <NUM_LIT:0> ) : <EOL> line = line . strip ( ) [ <NUM_LIT:2> : - <NUM_LIT:2> ] . strip ( ) <EOL> row = [ c . strip ( ) for c in re . split ( r'<STR_LIT>' , line ) ] <EOL> rows . append ( row ) <EOL> hlines = [ '<STR_LIT>' % self . _html_class_str_from_tag ( '<STR_LIT>' ) , '<STR_LIT>' ] <EOL> for row in rows : <EOL> hrow = [ '<STR_LIT>' ] <EOL> for cell in row : <EOL> hrow . append ( '<STR_LIT>' ) <EOL> hrow . append ( self . _run_span_gamut ( cell ) ) <EOL> hrow . append ( '<STR_LIT>' ) <EOL> hrow . append ( '<STR_LIT>' ) <EOL> hlines . append ( '<STR_LIT>' . join ( hrow ) ) <EOL> hlines += [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> return '<STR_LIT:\n>' . join ( hlines ) + '<STR_LIT:\n>' <EOL> def _do_wiki_tables ( self , text ) : <EOL> if "<STR_LIT>" not in text : <EOL> return text <EOL> less_than_tab = self . tab_width - <NUM_LIT:1> <EOL> wiki_table_re = re . compile ( r'''<STR_LIT>''' % less_than_tab , re . M | re . X ) <EOL> return wiki_table_re . sub ( self . _wiki_table_sub , text ) <EOL> def _run_span_gamut ( self , text ) : <EOL> text = self . _do_code_spans ( text ) <EOL> text = self . _escape_special_chars ( text ) <EOL> text = self . _do_links ( text ) <EOL> text = self . _do_auto_links ( text ) <EOL> if "<STR_LIT>" in self . extras : <EOL> text = self . _do_link_patterns ( text ) <EOL> text = self . _encode_amps_and_angles ( text ) <EOL> text = self . _do_italics_and_bold ( text ) <EOL> if "<STR_LIT>" in self . extras : <EOL> text = self . _do_smart_punctuation ( text ) <EOL> if "<STR_LIT>" in self . extras : <EOL> text = re . sub ( r"<STR_LIT>" , "<STR_LIT>" % self . empty_element_suffix , text ) <EOL> else : <EOL> text = re . sub ( r"<STR_LIT>" , "<STR_LIT>" % self . empty_element_suffix , text ) <EOL> return text <EOL> _sorta_html_tokenize_re = re . compile ( r"""<STR_LIT>""" , re . X ) <EOL> def _escape_special_chars ( self , text ) : <EOL> escaped = [ ] <EOL> is_html_markup = False <EOL> for token in self . _sorta_html_tokenize_re . split ( text ) : <EOL> if is_html_markup : <EOL> escaped . append ( token . replace ( '<STR_LIT:*>' , self . _escape_table [ '<STR_LIT:*>' ] ) <EOL> . replace ( '<STR_LIT:_>' , self . _escape_table [ '<STR_LIT:_>' ] ) ) <EOL> else : <EOL> escaped . append ( self . _encode_backslash_escapes ( token ) ) <EOL> is_html_markup = not is_html_markup <EOL> return '<STR_LIT>' . join ( escaped ) <EOL> def _hash_html_spans ( self , text ) : <EOL> def _is_auto_link ( s ) : <EOL> if '<STR_LIT::>' in s and self . _auto_link_re . match ( s ) : <EOL> return True <EOL> elif '<STR_LIT:@>' in s and self . _auto_email_link_re . match ( s ) : <EOL> return True <EOL> return False <EOL> tokens = [ ] <EOL> is_html_markup = False <EOL> for token in self . _sorta_html_tokenize_re . split ( text ) : <EOL> if is_html_markup and not _is_auto_link ( token ) : <EOL> sanitized = self . _sanitize_html ( token ) <EOL> key = _hash_text ( sanitized ) <EOL> self . html_spans [ key ] = sanitized <EOL> tokens . append ( key ) <EOL> else : <EOL> tokens . append ( token ) <EOL> is_html_markup = not is_html_markup <EOL> return '<STR_LIT>' . join ( tokens ) <EOL> def _unhash_html_spans ( self , text ) : <EOL> for key , sanitized in list ( self . html_spans . items ( ) ) : <EOL> text = text . replace ( key , sanitized ) <EOL> return text <EOL> def _math_span_sub ( self , match ) : <EOL> m = match . string [ match . start ( ) : match . start ( <NUM_LIT:2> ) ] + self . _encode_code ( match . group ( <NUM_LIT:2> ) ) + match . string [ match . end ( <NUM_LIT:2> ) : match . end ( ) ] <EOL> key = _hash_text ( m ) <EOL> self . math_spans [ key ] = m <EOL> return key <EOL> def _hash_math_spans ( self , text ) : <EOL> _math_span_re = re . compile ( r'''<STR_LIT>''' % re . escape ( self . extras [ '<STR_LIT>' ] ) , re . X ) <EOL> return _math_span_re . sub ( self . _math_span_sub , text ) <EOL> def _unhash_math_spans ( self , text ) : <EOL> for key , sanitized in list ( self . math_spans . items ( ) ) : <EOL> text = text . replace ( key , sanitized ) <EOL> return text <EOL> def _sanitize_html ( self , s ) : <EOL> if self . safe_mode == "<STR_LIT:replace>" : <EOL> return self . html_removed_text <EOL> elif self . safe_mode == "<STR_LIT>" : <EOL> replacements = [ <EOL> ( '<STR_LIT:&>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT:<>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT:>>' , '<STR_LIT>' ) , <EOL> ] <EOL> for before , after in replacements : <EOL> s = s . replace ( before , after ) <EOL> return s <EOL> else : <EOL> raise MarkdownError ( "<STR_LIT>" <EOL> "<STR_LIT>" % self . safe_mode ) <EOL> _inline_link_title = re . compile ( r'''<STR_LIT>''' , re . X | re . S ) <EOL> _tail_of_reference_link_re = re . compile ( r'''<STR_LIT>''' , re . X | re . S ) <EOL> _whitespace = re . compile ( r'<STR_LIT>' ) <EOL> _strip_anglebrackets = re . compile ( r'<STR_LIT>' ) <EOL> def _find_non_whitespace ( self , text , start ) : <EOL> """<STR_LIT>""" <EOL> match = self . _whitespace . match ( text , start ) <EOL> return match . end ( ) <EOL> def _find_balanced ( self , text , start , open_c , close_c ) : <EOL> """<STR_LIT>""" <EOL> i = start <EOL> l = len ( text ) <EOL> count = <NUM_LIT:1> <EOL> while count > <NUM_LIT:0> and i < l : <EOL> if text [ i ] == open_c : <EOL> count += <NUM_LIT:1> <EOL> elif text [ i ] == close_c : <EOL> count -= <NUM_LIT:1> <EOL> i += <NUM_LIT:1> <EOL> return i <EOL> def _extract_url_and_title ( self , text , start ) : <EOL> """<STR_LIT>""" <EOL> idx = self . _find_non_whitespace ( text , start + <NUM_LIT:1> ) <EOL> if idx == len ( text ) : <EOL> return None , None , None <EOL> end_idx = idx <EOL> has_anglebrackets = text [ idx ] == "<STR_LIT:<>" <EOL> if has_anglebrackets : <EOL> end_idx = self . _find_balanced ( text , end_idx + <NUM_LIT:1> , "<STR_LIT:<>" , "<STR_LIT:>>" ) <EOL> end_idx = self . _find_balanced ( text , end_idx , "<STR_LIT:(>" , "<STR_LIT:)>" ) <EOL> match = self . _inline_link_title . search ( text , idx , end_idx ) <EOL> if not match : <EOL> return None , None , None <EOL> url , title = text [ idx : match . start ( ) ] , match . group ( "<STR_LIT:title>" ) <EOL> if has_anglebrackets : <EOL> url = self . _strip_anglebrackets . sub ( r'<STR_LIT>' , url ) <EOL> return url , title , end_idx <EOL> def _do_links ( self , text ) : <EOL> """<STR_LIT>""" <EOL> MAX_LINK_TEXT_SENTINEL = <NUM_LIT> <EOL> anchor_allowed_pos = <NUM_LIT:0> <EOL> curr_pos = <NUM_LIT:0> <EOL> while True : <EOL> try : <EOL> start_idx = text . index ( '<STR_LIT:[>' , curr_pos ) <EOL> except ValueError : <EOL> break <EOL> text_length = len ( text ) <EOL> bracket_depth = <NUM_LIT:0> <EOL> for p in range ( start_idx + <NUM_LIT:1> , min ( start_idx + MAX_LINK_TEXT_SENTINEL , <EOL> text_length ) ) : <EOL> ch = text [ p ] <EOL> if ch == '<STR_LIT:]>' : <EOL> bracket_depth -= <NUM_LIT:1> <EOL> if bracket_depth < <NUM_LIT:0> : <EOL> break <EOL> elif ch == '<STR_LIT:[>' : <EOL> bracket_depth += <NUM_LIT:1> <EOL> else : <EOL> curr_pos = start_idx + <NUM_LIT:1> <EOL> continue <EOL> link_text = text [ start_idx + <NUM_LIT:1> : p ] <EOL> if "<STR_LIT>" in self . extras and link_text . startswith ( "<STR_LIT>" ) : <EOL> normed_id = re . sub ( r'<STR_LIT>' , '<STR_LIT:->' , link_text [ <NUM_LIT:1> : ] ) <EOL> if normed_id in self . footnotes : <EOL> self . footnote_ids . append ( normed_id ) <EOL> result = '<STR_LIT>' '<STR_LIT>' % ( normed_id , normed_id , len ( self . footnote_ids ) ) <EOL> text = text [ : start_idx ] + result + text [ p + <NUM_LIT:1> : ] <EOL> else : <EOL> curr_pos = p + <NUM_LIT:1> <EOL> continue <EOL> p += <NUM_LIT:1> <EOL> if p == text_length : <EOL> return text <EOL> if text [ p ] == '<STR_LIT:(>' : <EOL> url , title , url_end_idx = self . _extract_url_and_title ( text , p ) <EOL> if url is not None : <EOL> is_img = start_idx > <NUM_LIT:0> and text [ start_idx - <NUM_LIT:1> ] == "<STR_LIT:!>" <EOL> if is_img : <EOL> start_idx -= <NUM_LIT:1> <EOL> url = url . replace ( '<STR_LIT:*>' , self . _escape_table [ '<STR_LIT:*>' ] ) . replace ( '<STR_LIT:_>' , self . _escape_table [ '<STR_LIT:_>' ] ) <EOL> if title : <EOL> title_str = '<STR_LIT>' % ( <EOL> _xml_escape_attr ( title ) <EOL> . replace ( '<STR_LIT:*>' , self . _escape_table [ '<STR_LIT:*>' ] ) <EOL> . replace ( '<STR_LIT:_>' , self . _escape_table [ '<STR_LIT:_>' ] ) ) <EOL> else : <EOL> title_str = '<STR_LIT>' <EOL> if is_img : <EOL> img_class_str = self . _html_class_str_from_tag ( "<STR_LIT>" ) <EOL> result = '<STR_LIT>' % ( url . replace ( '<STR_LIT:">' , '<STR_LIT>' ) , <EOL> _xml_escape_attr ( link_text ) , <EOL> title_str , img_class_str , self . empty_element_suffix ) <EOL> if "<STR_LIT>" in self . extras : <EOL> result = result . replace ( '<STR_LIT:">' , self . _escape_table [ '<STR_LIT:">' ] ) <EOL> curr_pos = start_idx + len ( result ) <EOL> text = text [ : start_idx ] + result + text [ url_end_idx : ] <EOL> elif start_idx >= anchor_allowed_pos : <EOL> result_head = '<STR_LIT>' % ( url , title_str ) <EOL> result = '<STR_LIT>' % ( result_head , link_text ) <EOL> if "<STR_LIT>" in self . extras : <EOL> result = result . replace ( '<STR_LIT:">' , self . _escape_table [ '<STR_LIT:">' ] ) <EOL> curr_pos = start_idx + len ( result_head ) <EOL> anchor_allowed_pos = start_idx + len ( result ) <EOL> text = text [ : start_idx ] + result + text [ url_end_idx : ] <EOL> else : <EOL> curr_pos = start_idx + <NUM_LIT:1> <EOL> continue <EOL> else : <EOL> match = self . _tail_of_reference_link_re . match ( text , p ) <EOL> if match : <EOL> is_img = start_idx > <NUM_LIT:0> and text [ start_idx - <NUM_LIT:1> ] == "<STR_LIT:!>" <EOL> if is_img : <EOL> start_idx -= <NUM_LIT:1> <EOL> link_id = match . group ( "<STR_LIT:id>" ) . lower ( ) <EOL> if not link_id : <EOL> link_id = link_text . lower ( ) <EOL> if link_id in self . urls : <EOL> url = self . urls [ link_id ] <EOL> url = url . replace ( '<STR_LIT:*>' , self . _escape_table [ '<STR_LIT:*>' ] ) . replace ( '<STR_LIT:_>' , self . _escape_table [ '<STR_LIT:_>' ] ) <EOL> title = self . titles . get ( link_id ) <EOL> if title : <EOL> before = title <EOL> title = _xml_escape_attr ( title ) . replace ( '<STR_LIT:*>' , self . _escape_table [ '<STR_LIT:*>' ] ) . replace ( '<STR_LIT:_>' , self . _escape_table [ '<STR_LIT:_>' ] ) <EOL> title_str = '<STR_LIT>' % title <EOL> else : <EOL> title_str = '<STR_LIT>' <EOL> if is_img : <EOL> img_class_str = self . _html_class_str_from_tag ( "<STR_LIT>" ) <EOL> result = '<STR_LIT>' % ( url . replace ( '<STR_LIT:">' , '<STR_LIT>' ) , <EOL> link_text . replace ( '<STR_LIT:">' , '<STR_LIT>' ) , <EOL> title_str , img_class_str , self . empty_element_suffix ) <EOL> if "<STR_LIT>" in self . extras : <EOL> result = result . replace ( '<STR_LIT:">' , self . _escape_table [ '<STR_LIT:">' ] ) <EOL> curr_pos = start_idx + len ( result ) <EOL> text = text [ : start_idx ] + result + text [ match . end ( ) : ] <EOL> elif start_idx >= anchor_allowed_pos : <EOL> result = '<STR_LIT>' % ( url , title_str , link_text ) <EOL> result_head = '<STR_LIT>' % ( url , title_str ) <EOL> result = '<STR_LIT>' % ( result_head , link_text ) <EOL> if "<STR_LIT>" in self . extras : <EOL> result = result . replace ( '<STR_LIT:">' , self . _escape_table [ '<STR_LIT:">' ] ) <EOL> curr_pos = start_idx + len ( result_head ) <EOL> anchor_allowed_pos = start_idx + len ( result ) <EOL> text = text [ : start_idx ] + result + text [ match . end ( ) : ] <EOL> else : <EOL> curr_pos = start_idx + <NUM_LIT:1> <EOL> else : <EOL> curr_pos = match . end ( ) <EOL> continue <EOL> curr_pos = start_idx + <NUM_LIT:1> <EOL> return text <EOL> def header_id_from_text ( self , text , prefix , n ) : <EOL> """<STR_LIT>""" <EOL> header_id = _slugify ( text ) <EOL> if prefix and isinstance ( prefix , base_string_type ) : <EOL> header_id = prefix + '<STR_LIT:->' + header_id <EOL> if header_id in self . _count_from_header_id : <EOL> self . _count_from_header_id [ header_id ] += <NUM_LIT:1> <EOL> header_id += '<STR_LIT>' % self . _count_from_header_id [ header_id ] <EOL> else : <EOL> self . _count_from_header_id [ header_id ] = <NUM_LIT:1> <EOL> return header_id <EOL> _toc = None <EOL> def _toc_add_entry ( self , level , id , name ) : <EOL> if self . _toc is None : <EOL> self . _toc = [ ] <EOL> self . _toc . append ( ( level , id , self . _unescape_special_chars ( name ) ) ) <EOL> _h_re_base = r'''<STR_LIT>''' <EOL> _h_re = re . compile ( _h_re_base % '<STR_LIT:*>' , re . X | re . M ) <EOL> _h_re_tag_friendly = re . compile ( _h_re_base % '<STR_LIT:+>' , re . X | re . M ) <EOL> def _h_sub ( self , match ) : <EOL> if match . group ( <NUM_LIT:1> ) is not None : <EOL> n = { "<STR_LIT:=>" : <NUM_LIT:1> , "<STR_LIT:->" : <NUM_LIT:2> } [ match . group ( <NUM_LIT:3> ) [ <NUM_LIT:0> ] ] <EOL> header_group = match . group ( <NUM_LIT:2> ) <EOL> else : <EOL> n = len ( match . group ( <NUM_LIT:5> ) ) <EOL> header_group = match . group ( <NUM_LIT:6> ) <EOL> demote_headers = self . extras . get ( "<STR_LIT>" ) <EOL> if demote_headers : <EOL> n = min ( n + demote_headers , <NUM_LIT:6> ) <EOL> header_id_attr = "<STR_LIT>" <EOL> if "<STR_LIT>" in self . extras : <EOL> header_id = self . header_id_from_text ( header_group , <EOL> self . extras [ "<STR_LIT>" ] , n ) <EOL> if header_id : <EOL> header_id_attr = '<STR_LIT>' % header_id <EOL> html = self . _run_span_gamut ( header_group ) <EOL> if "<STR_LIT>" in self . extras and header_id : <EOL> self . _toc_add_entry ( n , header_id , html ) <EOL> return "<STR_LIT>" % ( n , header_id_attr , html , n ) <EOL> def _do_headers ( self , text ) : <EOL> if '<STR_LIT>' in self . extras : <EOL> return self . _h_re_tag_friendly . sub ( self . _h_sub , text ) <EOL> return self . _h_re . sub ( self . _h_sub , text ) <EOL> _marker_ul_chars = '<STR_LIT>' <EOL> _marker_any = r'<STR_LIT>' % _marker_ul_chars <EOL> _marker_ul = '<STR_LIT>' % _marker_ul_chars <EOL> _marker_ol = r'<STR_LIT>' <EOL> def _list_sub ( self , match ) : <EOL> lst = match . group ( <NUM_LIT:1> ) <EOL> lst_type = match . group ( <NUM_LIT:3> ) in self . _marker_ul_chars and "<STR_LIT>" or "<STR_LIT>" <EOL> result = self . _process_list_items ( lst ) <EOL> if self . list_level : <EOL> return "<STR_LIT>" % ( lst_type , result , lst_type ) <EOL> else : <EOL> return "<STR_LIT>" % ( lst_type , result , lst_type ) <EOL> def _do_lists ( self , text ) : <EOL> pos = <NUM_LIT:0> <EOL> while True : <EOL> hits = [ ] <EOL> for marker_pat in ( self . _marker_ul , self . _marker_ol ) : <EOL> less_than_tab = self . tab_width - <NUM_LIT:1> <EOL> whole_list = r'''<STR_LIT>''' % ( less_than_tab , marker_pat , marker_pat ) <EOL> if self . list_level : <EOL> list_re = re . compile ( "<STR_LIT>" + whole_list , re . X | re . M | re . S ) <EOL> else : <EOL> list_re = re . compile ( r"<STR_LIT>" + whole_list , <EOL> re . X | re . M | re . S ) <EOL> match = list_re . search ( text , pos ) <EOL> if match : <EOL> hits . append ( ( match . start ( ) , match ) ) <EOL> if not hits : <EOL> break <EOL> hits . sort ( ) <EOL> match = hits [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] <EOL> start , end = match . span ( ) <EOL> middle = self . _list_sub ( match ) <EOL> text = text [ : start ] + middle + text [ end : ] <EOL> pos = start + len ( middle ) <EOL> return text <EOL> _list_item_re = re . compile ( r'''<STR_LIT>''' % ( _marker_any , _marker_any ) , <EOL> re . M | re . X | re . S ) <EOL> _last_li_endswith_two_eols = False <EOL> def _list_item_sub ( self , match ) : <EOL> item = match . group ( <NUM_LIT:4> ) <EOL> leading_line = match . group ( <NUM_LIT:1> ) <EOL> leading_space = match . group ( <NUM_LIT:2> ) <EOL> if leading_line or "<STR_LIT>" in item or self . _last_li_endswith_two_eols : <EOL> item = self . _run_block_gamut ( self . _outdent ( item ) ) <EOL> else : <EOL> item = self . _do_lists ( self . _outdent ( item ) ) <EOL> if item . endswith ( '<STR_LIT:\n>' ) : <EOL> item = item [ : - <NUM_LIT:1> ] <EOL> item = self . _run_span_gamut ( item ) <EOL> self . _last_li_endswith_two_eols = ( len ( match . group ( <NUM_LIT:5> ) ) == <NUM_LIT:2> ) <EOL> return "<STR_LIT>" % item <EOL> def _process_list_items ( self , list_str ) : <EOL> self . list_level += <NUM_LIT:1> <EOL> self . _last_li_endswith_two_eols = False <EOL> list_str = list_str . rstrip ( '<STR_LIT:\n>' ) + '<STR_LIT:\n>' <EOL> list_str = self . _list_item_re . sub ( self . _list_item_sub , list_str ) <EOL> self . list_level -= <NUM_LIT:1> <EOL> return list_str <EOL> def _get_pygments_lexer ( self , lexer_name ) : <EOL> try : <EOL> from pygments import lexers , util <EOL> except ImportError : <EOL> return None <EOL> try : <EOL> return lexers . get_lexer_by_name ( lexer_name ) <EOL> except util . ClassNotFound : <EOL> return None <EOL> def _color_with_pygments ( self , codeblock , lexer , ** formatter_opts ) : <EOL> import pygments <EOL> import pygments . formatters <EOL> class HtmlCodeFormatter ( pygments . formatters . HtmlFormatter ) : <EOL> def _wrap_code ( self , inner ) : <EOL> """<STR_LIT>""" <EOL> yield <NUM_LIT:0> , "<STR_LIT>" <EOL> for tup in inner : <EOL> yield tup <EOL> yield <NUM_LIT:0> , "<STR_LIT>" <EOL> def wrap ( self , source , outfile ) : <EOL> """<STR_LIT>""" <EOL> return self . _wrap_div ( self . _wrap_pre ( self . _wrap_code ( source ) ) ) <EOL> formatter_opts . setdefault ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> formatter = HtmlCodeFormatter ( ** formatter_opts ) <EOL> return pygments . highlight ( codeblock , lexer , formatter ) <EOL> def _code_block_sub ( self , match , is_fenced_code_block = False ) : <EOL> lexer_name = None <EOL> if is_fenced_code_block : <EOL> lexer_name = match . group ( <NUM_LIT:1> ) <EOL> if lexer_name : <EOL> formatter_opts = self . extras [ '<STR_LIT>' ] or { } <EOL> codeblock = match . group ( <NUM_LIT:2> ) <EOL> codeblock = codeblock [ : - <NUM_LIT:1> ] <EOL> else : <EOL> codeblock = match . group ( <NUM_LIT:1> ) <EOL> codeblock = self . _outdent ( codeblock ) <EOL> codeblock = self . _detab ( codeblock ) <EOL> codeblock = codeblock . lstrip ( '<STR_LIT:\n>' ) <EOL> codeblock = codeblock . rstrip ( ) <EOL> if "<STR_LIT>" in self . extras and codeblock . startswith ( "<STR_LIT>" ) : <EOL> lexer_name , rest = codeblock . split ( '<STR_LIT:\n>' , <NUM_LIT:1> ) <EOL> lexer_name = lexer_name [ <NUM_LIT:3> : ] . strip ( ) <EOL> codeblock = rest . lstrip ( "<STR_LIT:\n>" ) <EOL> formatter_opts = self . extras [ '<STR_LIT>' ] or { } <EOL> if lexer_name : <EOL> def unhash_code ( codeblock ) : <EOL> for key , sanitized in list ( self . html_spans . items ( ) ) : <EOL> codeblock = codeblock . replace ( key , sanitized ) <EOL> replacements = [ <EOL> ( "<STR_LIT>" , "<STR_LIT:&>" ) , <EOL> ( "<STR_LIT>" , "<STR_LIT:<>" ) , <EOL> ( "<STR_LIT>" , "<STR_LIT:>>" ) <EOL> ] <EOL> for old , new in replacements : <EOL> codeblock = codeblock . replace ( old , new ) <EOL> return codeblock <EOL> lexer = self . _get_pygments_lexer ( lexer_name ) <EOL> if lexer : <EOL> codeblock = unhash_code ( codeblock ) <EOL> colored = self . _color_with_pygments ( codeblock , lexer , <EOL> ** formatter_opts ) <EOL> return "<STR_LIT>" % colored <EOL> codeblock = self . _encode_code ( codeblock ) <EOL> pre_class_str = self . _html_class_str_from_tag ( "<STR_LIT>" ) <EOL> code_class_str = self . _html_class_str_from_tag ( "<STR_LIT:code>" ) <EOL> return "<STR_LIT>" % ( <EOL> pre_class_str , code_class_str , codeblock ) <EOL> def _html_class_str_from_tag ( self , tag ) : <EOL> """<STR_LIT>""" <EOL> if "<STR_LIT>" not in self . extras : <EOL> return "<STR_LIT>" <EOL> try : <EOL> html_classes_from_tag = self . extras [ "<STR_LIT>" ] <EOL> except TypeError : <EOL> return "<STR_LIT>" <EOL> else : <EOL> if tag in html_classes_from_tag : <EOL> return '<STR_LIT>' % html_classes_from_tag [ tag ] <EOL> return "<STR_LIT>" <EOL> def _do_code_blocks ( self , text ) : <EOL> """<STR_LIT>""" <EOL> code_block_re = re . compile ( r'''<STR_LIT>''' % ( self . tab_width , self . tab_width ) , <EOL> re . M | re . X ) <EOL> return code_block_re . sub ( self . _code_block_sub , text ) <EOL> _fenced_code_block_re = re . compile ( r'''<STR_LIT>''' , re . M | re . X | re . S ) <EOL> def _fenced_code_block_sub ( self , match ) : <EOL> return self . _code_block_sub ( match , is_fenced_code_block = True ) ; <EOL> def _do_fenced_code_blocks ( self , text ) : <EOL> """<STR_LIT>""" <EOL> return self . _fenced_code_block_re . sub ( self . _fenced_code_block_sub , text ) <EOL> _code_span_re = re . compile ( r'''<STR_LIT>''' , re . X | re . S ) <EOL> def _code_span_sub ( self , match ) : <EOL> c = match . group ( <NUM_LIT:2> ) . strip ( "<STR_LIT>" ) <EOL> c = self . _encode_code ( c ) <EOL> return "<STR_LIT>" % c <EOL> def _do_code_spans ( self , text ) : <EOL> return self . _code_span_re . sub ( self . _code_span_sub , text ) <EOL> def _encode_code ( self , text ) : <EOL> """<STR_LIT>""" <EOL> replacements = [ <EOL> ( '<STR_LIT:&>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT:<>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT:>>' , '<STR_LIT>' ) , <EOL> ] <EOL> for before , after in replacements : <EOL> text = text . replace ( before , after ) <EOL> return text <EOL> _strong_re = re . compile ( r"<STR_LIT>" , re . S ) <EOL> _em_re = re . compile ( r"<STR_LIT>" , re . S ) <EOL> _code_friendly_strong_re = re . compile ( r"<STR_LIT>" , re . S ) <EOL> _code_friendly_em_re = re . compile ( r"<STR_LIT>" , re . S ) <EOL> def _do_italics_and_bold ( self , text ) : <EOL> if "<STR_LIT>" in self . extras : <EOL> text = self . _code_friendly_strong_re . sub ( r"<STR_LIT>" , text ) <EOL> text = self . _code_friendly_em_re . sub ( r"<STR_LIT>" , text ) <EOL> else : <EOL> text = self . _strong_re . sub ( r"<STR_LIT>" , text ) <EOL> text = self . _em_re . sub ( r"<STR_LIT>" , text ) <EOL> return text <EOL> _apostrophe_year_re = re . compile ( r"<STR_LIT>" ) <EOL> _contractions = [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT:o>" , "<STR_LIT:n>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] <EOL> def _do_smart_contractions ( self , text ) : <EOL> text = self . _apostrophe_year_re . sub ( r"<STR_LIT>" , text ) <EOL> for c in self . _contractions : <EOL> text = text . replace ( "<STR_LIT>" % c , "<STR_LIT>" % c ) <EOL> text = text . replace ( "<STR_LIT>" % c . capitalize ( ) , <EOL> "<STR_LIT>" % c . capitalize ( ) ) <EOL> return text <EOL> _opening_single_quote_re = re . compile ( r"<STR_LIT>" ) <EOL> _opening_double_quote_re = re . compile ( r'<STR_LIT>' ) <EOL> _closing_single_quote_re = re . compile ( r"<STR_LIT>" ) <EOL> _closing_double_quote_re = re . compile ( r'<STR_LIT>' ) <EOL> def _do_smart_punctuation ( self , text ) : <EOL> """<STR_LIT>""" <EOL> if "<STR_LIT:'>" in text : <EOL> text = self . _do_smart_contractions ( text ) <EOL> text = self . _opening_single_quote_re . sub ( "<STR_LIT>" , text ) <EOL> text = self . _closing_single_quote_re . sub ( "<STR_LIT>" , text ) <EOL> if '<STR_LIT:">' in text : <EOL> text = self . _opening_double_quote_re . sub ( "<STR_LIT>" , text ) <EOL> text = self . _closing_double_quote_re . sub ( "<STR_LIT>" , text ) <EOL> text = text . replace ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> text = text . replace ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> text = text . replace ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> text = text . replace ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> text = text . replace ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> return text <EOL> _block_quote_base = r'''<STR_LIT>''' <EOL> _block_quote_re = re . compile ( _block_quote_base % '<STR_LIT>' , re . M | re . X ) <EOL> _block_quote_re_spoiler = re . compile ( _block_quote_base % '<STR_LIT>' , re . M | re . X ) <EOL> _bq_one_level_re = re . compile ( '<STR_LIT>' , re . M ) ; <EOL> _bq_one_level_re_spoiler = re . compile ( '<STR_LIT>' , re . M ) ; <EOL> _bq_all_lines_spoilers = re . compile ( r'<STR_LIT>' , re . M ) <EOL> _html_pre_block_re = re . compile ( r'<STR_LIT>' , re . S ) <EOL> def _dedent_two_spaces_sub ( self , match ) : <EOL> return re . sub ( r'<STR_LIT>' , '<STR_LIT>' , match . group ( <NUM_LIT:1> ) ) <EOL> def _block_quote_sub ( self , match ) : <EOL> bq = match . group ( <NUM_LIT:1> ) <EOL> is_spoiler = '<STR_LIT>' in self . extras and self . _bq_all_lines_spoilers . match ( bq ) <EOL> if is_spoiler : <EOL> bq = self . _bq_one_level_re_spoiler . sub ( '<STR_LIT>' , bq ) <EOL> else : <EOL> bq = self . _bq_one_level_re . sub ( '<STR_LIT>' , bq ) <EOL> bq = self . _ws_only_line_re . sub ( '<STR_LIT>' , bq ) <EOL> bq = self . _run_block_gamut ( bq ) <EOL> bq = re . sub ( '<STR_LIT>' , '<STR_LIT:U+0020>' , bq ) <EOL> bq = self . _html_pre_block_re . sub ( self . _dedent_two_spaces_sub , bq ) <EOL> if is_spoiler : <EOL> return '<STR_LIT>' % bq <EOL> else : <EOL> return '<STR_LIT>' % bq <EOL> def _do_block_quotes ( self , text ) : <EOL> if '<STR_LIT:>>' not in text : <EOL> return text <EOL> if '<STR_LIT>' in self . extras : <EOL> return self . _block_quote_re_spoiler . sub ( self . _block_quote_sub , text ) <EOL> else : <EOL> return self . _block_quote_re . sub ( self . _block_quote_sub , text ) <EOL> def _form_paragraphs ( self , text ) : <EOL> text = text . strip ( '<STR_LIT:\n>' ) <EOL> grafs = [ ] <EOL> for i , graf in enumerate ( re . split ( r"<STR_LIT>" , text ) ) : <EOL> if graf in self . html_blocks : <EOL> grafs . append ( self . html_blocks [ graf ] ) <EOL> else : <EOL> cuddled_list = None <EOL> if "<STR_LIT>" in self . extras : <EOL> li = self . _list_item_re . search ( graf + '<STR_LIT:\n>' ) <EOL> if ( li and len ( li . group ( <NUM_LIT:2> ) ) <= <NUM_LIT:3> and li . group ( "<STR_LIT>" ) <EOL> and li . group ( "<STR_LIT>" ) [ - <NUM_LIT:1> ] == li . group ( "<STR_LIT>" ) [ - <NUM_LIT:1> ] ) : <EOL> start = li . start ( ) <EOL> cuddled_list = self . _do_lists ( graf [ start : ] ) . rstrip ( "<STR_LIT:\n>" ) <EOL> assert cuddled_list . startswith ( "<STR_LIT>" ) or cuddled_list . startswith ( "<STR_LIT>" ) <EOL> graf = graf [ : start ] <EOL> graf = self . _run_span_gamut ( graf ) <EOL> grafs . append ( "<STR_LIT>" + graf . lstrip ( "<STR_LIT>" ) + "<STR_LIT>" ) <EOL> if cuddled_list : <EOL> grafs . append ( cuddled_list ) <EOL> return "<STR_LIT>" . join ( grafs ) <EOL> def _sort_footnotes ( self , text ) : <EOL> """<STR_LIT>""" <EOL> _footnote_tag_re = re . compile ( r'''<STR_LIT>''' ) <EOL> self . footnote_ids = [ ] <EOL> def _repl ( match ) : <EOL> id = match . group ( <NUM_LIT:1> ) <EOL> num = match . group ( <NUM_LIT:2> ) <EOL> if id in self . footnotes : <EOL> self . footnote_ids . append ( id ) <EOL> return match . string [ match . start ( <NUM_LIT:0> ) : match . start ( <NUM_LIT:2> ) ] + str ( len ( self . footnote_ids ) ) + match . string [ match . end ( <NUM_LIT:2> ) : match . end ( <NUM_LIT:0> ) ] <EOL> else : <EOL> return match . string [ match . start ( ) : match . end ( ) ] <EOL> return _footnote_tag_re . sub ( _repl , text ) <EOL> def _add_footnotes ( self , text ) : <EOL> if self . footnotes : <EOL> footer = [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' + self . empty_element_suffix , <EOL> '<STR_LIT>' , <EOL> ] <EOL> for i , id in enumerate ( self . footnote_ids ) : <EOL> if i != <NUM_LIT:0> : <EOL> footer . append ( '<STR_LIT>' ) <EOL> footer . append ( '<STR_LIT>' % id ) <EOL> footer . append ( self . _run_block_gamut ( self . footnotes [ id ] ) ) <EOL> backlink = ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' % ( id , i + <NUM_LIT:1> ) ) <EOL> if footer [ - <NUM_LIT:1> ] . endswith ( "<STR_LIT>" ) : <EOL> footer [ - <NUM_LIT:1> ] = footer [ - <NUM_LIT:1> ] [ : - len ( "<STR_LIT>" ) ] + '<STR_LIT>' + backlink + "<STR_LIT>" <EOL> else : <EOL> footer . append ( "<STR_LIT>" % backlink ) <EOL> footer . append ( '<STR_LIT>' ) <EOL> footer . append ( '<STR_LIT>' ) <EOL> footer . append ( '<STR_LIT>' ) <EOL> return text + '<STR_LIT>' + '<STR_LIT:\n>' . join ( footer ) <EOL> else : <EOL> return text <EOL> _ampersand_re = re . compile ( r'<STR_LIT>' ) <EOL> _naked_lt_re = re . compile ( r'<STR_LIT>' , re . I ) <EOL> _naked_gt_re = re . compile ( r'''<STR_LIT>''' , re . I ) <EOL> def _encode_amps_and_angles ( self , text ) : <EOL> text = self . _ampersand_re . sub ( '<STR_LIT>' , text ) <EOL> text = self . _naked_lt_re . sub ( '<STR_LIT>' , text ) <EOL> text = self . _naked_gt_re . sub ( '<STR_LIT>' , text ) <EOL> return text <EOL> def _encode_backslash_escapes ( self , text ) : <EOL> for ch , escape in list ( self . _escape_table . items ( ) ) : <EOL> text = text . replace ( "<STR_LIT:\\>" + ch , escape ) <EOL> return text <EOL> _auto_link_re = re . compile ( r'<STR_LIT>' , re . I ) <EOL> def _auto_link_sub ( self , match ) : <EOL> g1 = match . group ( <NUM_LIT:1> ) <EOL> return '<STR_LIT>' % ( g1 , g1 ) <EOL> _auto_email_link_re = re . compile ( r"""<STR_LIT>""" , re . I | re . X | re . U ) <EOL> def _auto_email_link_sub ( self , match ) : <EOL> return self . _encode_email_address ( <EOL> self . _unescape_special_chars ( match . group ( <NUM_LIT:1> ) ) ) <EOL> def _do_auto_links ( self , text ) : <EOL> text = self . _auto_link_re . sub ( self . _auto_link_sub , text ) <EOL> text = self . _auto_email_link_re . sub ( self . _auto_email_link_sub , text ) <EOL> return text <EOL> def _encode_email_address ( self , addr ) : <EOL> chars = [ _xml_encode_email_char_at_random ( ch ) <EOL> for ch in "<STR_LIT>" + addr ] <EOL> addr = '<STR_LIT>' % ( '<STR_LIT>' . join ( chars ) , '<STR_LIT>' . join ( chars [ <NUM_LIT:7> : ] ) ) <EOL> return addr <EOL> def _do_link_patterns ( self , text ) : <EOL> """<STR_LIT>""" <EOL> link_from_hash = { } <EOL> for regex , repl in self . link_patterns : <EOL> replacements = [ ] <EOL> for match in regex . finditer ( text ) : <EOL> if hasattr ( repl , "<STR_LIT>" ) : <EOL> href = repl ( match ) <EOL> else : <EOL> href = match . expand ( repl ) <EOL> replacements . append ( ( match . span ( ) , href ) ) <EOL> for ( start , end ) , href in reversed ( replacements ) : <EOL> escaped_href = ( <EOL> href . replace ( '<STR_LIT:">' , '<STR_LIT>' ) <EOL> . replace ( '<STR_LIT:*>' , self . _escape_table [ '<STR_LIT:*>' ] ) <EOL> . replace ( '<STR_LIT:_>' , self . _escape_table [ '<STR_LIT:_>' ] ) ) <EOL> link = '<STR_LIT>' % ( escaped_href , text [ start : end ] ) <EOL> hash = _hash_text ( link ) <EOL> link_from_hash [ hash ] = link <EOL> text = text [ : start ] + hash + text [ end : ] <EOL> for hash , link in list ( link_from_hash . items ( ) ) : <EOL> text = text . replace ( hash , link ) <EOL> return text <EOL> def _unescape_special_chars ( self , text ) : <EOL> for ch , hash in list ( self . _escape_table . items ( ) ) : <EOL> text = text . replace ( hash , ch ) <EOL> return text <EOL> def _outdent ( self , text ) : <EOL> return self . _outdent_re . sub ( '<STR_LIT>' , text ) <EOL> class MarkdownWithExtras ( Markdown ) : <EOL> """<STR_LIT>""" <EOL> extras = [ "<STR_LIT>" , "<STR_LIT>" ] <EOL> class UnicodeWithAttrs ( unicode ) : <EOL> """<STR_LIT>""" <EOL> metadata = None <EOL> _toc = None <EOL> def toc_html ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . _toc is None : <EOL> return None <EOL> def indent ( ) : <EOL> return '<STR_LIT:U+0020>' * ( len ( h_stack ) - <NUM_LIT:1> ) <EOL> lines = [ ] <EOL> h_stack = [ <NUM_LIT:0> ] <EOL> for level , id , name in self . _toc : <EOL> if level > h_stack [ - <NUM_LIT:1> ] : <EOL> lines . append ( "<STR_LIT>" % indent ( ) ) <EOL> h_stack . append ( level ) <EOL> elif level == h_stack [ - <NUM_LIT:1> ] : <EOL> lines [ - <NUM_LIT:1> ] += "<STR_LIT>" <EOL> else : <EOL> while level < h_stack [ - <NUM_LIT:1> ] : <EOL> h_stack . pop ( ) <EOL> if not lines [ - <NUM_LIT:1> ] . endswith ( "<STR_LIT>" ) : <EOL> lines [ - <NUM_LIT:1> ] += "<STR_LIT>" <EOL> lines . append ( "<STR_LIT>" % indent ( ) ) <EOL> lines . append ( '<STR_LIT>' % ( <EOL> indent ( ) , id , name ) ) <EOL> while len ( h_stack ) > <NUM_LIT:1> : <EOL> h_stack . pop ( ) <EOL> if not lines [ - <NUM_LIT:1> ] . endswith ( "<STR_LIT>" ) : <EOL> lines [ - <NUM_LIT:1> ] += "<STR_LIT>" <EOL> lines . append ( "<STR_LIT>" % indent ( ) ) <EOL> return '<STR_LIT:\n>' . join ( lines ) + '<STR_LIT:\n>' <EOL> toc_html = property ( toc_html ) <EOL> _slugify_strip_re = re . compile ( r'<STR_LIT>' ) <EOL> _slugify_hyphenate_re = re . compile ( r'<STR_LIT>' ) <EOL> def _slugify ( value ) : <EOL> """<STR_LIT>""" <EOL> import unicodedata <EOL> value = unicodedata . normalize ( '<STR_LIT>' , value ) . encode ( '<STR_LIT:ascii>' , '<STR_LIT:ignore>' ) . decode ( ) <EOL> value = _slugify_strip_re . sub ( '<STR_LIT>' , value ) . strip ( ) . lower ( ) <EOL> return _slugify_hyphenate_re . sub ( '<STR_LIT:->' , value ) <EOL> def _curry ( * args , ** kwargs ) : <EOL> function , args = args [ <NUM_LIT:0> ] , args [ <NUM_LIT:1> : ] <EOL> def result ( * rest , ** kwrest ) : <EOL> combined = kwargs . copy ( ) <EOL> combined . update ( kwrest ) <EOL> return function ( * args + rest , ** combined ) <EOL> return result <EOL> def _regex_from_encoded_pattern ( s ) : <EOL> """<STR_LIT>""" <EOL> if s . startswith ( '<STR_LIT:/>' ) and s . rfind ( '<STR_LIT:/>' ) != <NUM_LIT:0> : <EOL> idx = s . rfind ( '<STR_LIT:/>' ) <EOL> pattern , flags_str = s [ <NUM_LIT:1> : idx ] , s [ idx + <NUM_LIT:1> : ] <EOL> flag_from_char = { <EOL> "<STR_LIT:i>" : re . IGNORECASE , <EOL> "<STR_LIT:l>" : re . LOCALE , <EOL> "<STR_LIT:s>" : re . DOTALL , <EOL> "<STR_LIT:m>" : re . MULTILINE , <EOL> "<STR_LIT:u>" : re . UNICODE , <EOL> } <EOL> flags = <NUM_LIT:0> <EOL> for char in flags_str : <EOL> try : <EOL> flags |= flag_from_char [ char ] <EOL> except KeyError : <EOL> raise ValueError ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> % ( char , s , '<STR_LIT>' . join ( list ( flag_from_char . keys ( ) ) ) ) ) <EOL> return re . compile ( s [ <NUM_LIT:1> : idx ] , flags ) <EOL> else : <EOL> return re . compile ( re . escape ( s ) ) <EOL> def _dedentlines ( lines , tabsize = <NUM_LIT:8> , skip_first_line = False ) : <EOL> """<STR_LIT>""" <EOL> DEBUG = False <EOL> if DEBUG : <EOL> print ( "<STR_LIT>" % ( tabsize , skip_first_line ) ) <EOL> indents = [ ] <EOL> margin = None <EOL> for i , line in enumerate ( lines ) : <EOL> if i == <NUM_LIT:0> and skip_first_line : continue <EOL> indent = <NUM_LIT:0> <EOL> for ch in line : <EOL> if ch == '<STR_LIT:U+0020>' : <EOL> indent += <NUM_LIT:1> <EOL> elif ch == '<STR_LIT:\t>' : <EOL> indent += tabsize - ( indent % tabsize ) <EOL> elif ch in '<STR_LIT:\r\n>' : <EOL> continue <EOL> else : <EOL> break <EOL> else : <EOL> continue <EOL> if DEBUG : print ( "<STR_LIT>" % ( indent , line ) ) <EOL> if margin is None : <EOL> margin = indent <EOL> else : <EOL> margin = min ( margin , indent ) <EOL> if DEBUG : print ( "<STR_LIT>" % margin ) <EOL> if margin is not None and margin > <NUM_LIT:0> : <EOL> for i , line in enumerate ( lines ) : <EOL> if i == <NUM_LIT:0> and skip_first_line : continue <EOL> removed = <NUM_LIT:0> <EOL> for j , ch in enumerate ( line ) : <EOL> if ch == '<STR_LIT:U+0020>' : <EOL> removed += <NUM_LIT:1> <EOL> elif ch == '<STR_LIT:\t>' : <EOL> removed += tabsize - ( removed % tabsize ) <EOL> elif ch in '<STR_LIT:\r\n>' : <EOL> if DEBUG : print ( "<STR_LIT>" % line ) <EOL> lines [ i ] = lines [ i ] [ j : ] <EOL> break <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> % ( ch , line , margin ) ) <EOL> if DEBUG : <EOL> print ( "<STR_LIT>" % ( line , ch , removed , margin ) ) <EOL> if removed == margin : <EOL> lines [ i ] = lines [ i ] [ j + <NUM_LIT:1> : ] <EOL> break <EOL> elif removed > margin : <EOL> lines [ i ] = '<STR_LIT:U+0020>' * ( removed - margin ) + lines [ i ] [ j + <NUM_LIT:1> : ] <EOL> break <EOL> else : <EOL> if removed : <EOL> lines [ i ] = lines [ i ] [ removed : ] <EOL> return lines <EOL> def _dedent ( text , tabsize = <NUM_LIT:8> , skip_first_line = False ) : <EOL> """<STR_LIT>""" <EOL> lines = text . splitlines ( <NUM_LIT:1> ) <EOL> _dedentlines ( lines , tabsize = tabsize , skip_first_line = skip_first_line ) <EOL> return '<STR_LIT>' . join ( lines ) <EOL> class _memoized ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , func ) : <EOL> self . func = func <EOL> self . cache = { } <EOL> def __call__ ( self , * args ) : <EOL> try : <EOL> return self . cache [ args ] <EOL> except KeyError : <EOL> self . cache [ args ] = value = self . func ( * args ) <EOL> return value <EOL> except TypeError : <EOL> return self . func ( * args ) <EOL> def __repr__ ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . func . __doc__ <EOL> def _xml_oneliner_re_from_tab_width ( tab_width ) : <EOL> """<STR_LIT>""" <EOL> return re . compile ( r"""<STR_LIT>""" % ( tab_width - <NUM_LIT:1> ) , re . X ) <EOL> _xml_oneliner_re_from_tab_width = _memoized ( _xml_oneliner_re_from_tab_width ) <EOL> def _hr_tag_re_from_tab_width ( tab_width ) : <EOL> return re . compile ( r"""<STR_LIT>""" % ( tab_width - <NUM_LIT:1> ) , re . X ) <EOL> _hr_tag_re_from_tab_width = _memoized ( _hr_tag_re_from_tab_width ) <EOL> def _xml_escape_attr ( attr , skip_single_quote = True ) : <EOL> """<STR_LIT>""" <EOL> escaped = ( attr <EOL> . replace ( '<STR_LIT:&>' , '<STR_LIT>' ) <EOL> . replace ( '<STR_LIT:">' , '<STR_LIT>' ) <EOL> . replace ( '<STR_LIT:<>' , '<STR_LIT>' ) <EOL> . replace ( '<STR_LIT:>>' , '<STR_LIT>' ) ) <EOL> if not skip_single_quote : <EOL> escaped = escaped . replace ( "<STR_LIT:'>" , "<STR_LIT>" ) <EOL> return escaped <EOL> def _xml_encode_email_char_at_random ( ch ) : <EOL> r = random ( ) <EOL> if r > <NUM_LIT> and ch not in "<STR_LIT>" : <EOL> return ch <EOL> elif r < <NUM_LIT> : <EOL> return '<STR_LIT>' % hex ( ord ( ch ) ) [ <NUM_LIT:1> : ] <EOL> else : <EOL> return '<STR_LIT>' % ord ( ch ) <EOL> class _NoReflowFormatter ( optparse . IndentedHelpFormatter ) : <EOL> """<STR_LIT>""" <EOL> def format_description ( self , description ) : <EOL> return description or "<STR_LIT>" <EOL> def _test ( ) : <EOL> import doctest <EOL> doctest . testmod ( ) <EOL> def main ( argv = None ) : <EOL> if argv is None : <EOL> argv = sys . argv <EOL> if not logging . root . handlers : <EOL> logging . basicConfig ( ) <EOL> usage = "<STR_LIT>" <EOL> version = "<STR_LIT>" + __version__ <EOL> parser = optparse . OptionParser ( prog = "<STR_LIT>" , usage = usage , <EOL> version = version , description = cmdln_desc , <EOL> formatter = _NoReflowFormatter ( ) ) <EOL> parser . add_option ( "<STR_LIT>" , "<STR_LIT>" , dest = "<STR_LIT>" , <EOL> action = "<STR_LIT>" , const = logging . DEBUG , <EOL> help = "<STR_LIT>" ) <EOL> parser . add_option ( "<STR_LIT>" , <EOL> help = "<STR_LIT>" ) <EOL> parser . add_option ( "<STR_LIT>" , action = "<STR_LIT:store_true>" , default = False , <EOL> help = "<STR_LIT>" ) <EOL> parser . add_option ( "<STR_LIT>" , "<STR_LIT>" , metavar = "<STR_LIT>" , dest = "<STR_LIT>" , <EOL> help = "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> parser . add_option ( "<STR_LIT>" , "<STR_LIT>" , action = "<STR_LIT>" , <EOL> help = "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> parser . add_option ( "<STR_LIT>" , <EOL> help = "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> parser . add_option ( "<STR_LIT>" , <EOL> help = "<STR_LIT>" ) <EOL> parser . add_option ( "<STR_LIT>" , action = "<STR_LIT:store_true>" , <EOL> help = "<STR_LIT>" ) <EOL> parser . add_option ( "<STR_LIT>" , action = "<STR_LIT:store_true>" , <EOL> help = "<STR_LIT>" ) <EOL> parser . set_defaults ( log_level = logging . INFO , compare = False , <EOL> encoding = "<STR_LIT:utf-8>" , safe_mode = None , use_file_vars = False ) <EOL> opts , paths = parser . parse_args ( ) <EOL> log . setLevel ( opts . log_level ) <EOL> if opts . self_test : <EOL> return _test ( ) <EOL> if opts . extras : <EOL> extras = { } <EOL> for s in opts . extras : <EOL> splitter = re . compile ( "<STR_LIT>" ) <EOL> for e in splitter . split ( s ) : <EOL> if '<STR_LIT:=>' in e : <EOL> ename , earg = e . split ( '<STR_LIT:=>' , <NUM_LIT:1> ) <EOL> try : <EOL> earg = int ( earg ) <EOL> except ValueError : <EOL> pass <EOL> else : <EOL> ename , earg = e , None <EOL> extras [ ename ] = earg <EOL> else : <EOL> extras = None <EOL> if opts . link_patterns_file : <EOL> link_patterns = [ ] <EOL> f = open ( opts . link_patterns_file ) <EOL> try : <EOL> for i , line in enumerate ( f . readlines ( ) ) : <EOL> if not line . strip ( ) : continue <EOL> if line . lstrip ( ) . startswith ( "<STR_LIT:#>" ) : continue <EOL> try : <EOL> pat , href = line . rstrip ( ) . rsplit ( None , <NUM_LIT:1> ) <EOL> except ValueError : <EOL> raise MarkdownError ( "<STR_LIT>" <EOL> % ( opts . link_patterns_file , i + <NUM_LIT:1> , line ) ) <EOL> link_patterns . append ( <EOL> ( _regex_from_encoded_pattern ( pat ) , href ) ) <EOL> finally : <EOL> f . close ( ) <EOL> else : <EOL> link_patterns = None <EOL> from os . path import join , dirname , abspath , exists <EOL> markdown_pl = join ( dirname ( dirname ( abspath ( __file__ ) ) ) , "<STR_LIT:test>" , <EOL> "<STR_LIT>" ) <EOL> if not paths : <EOL> paths = [ '<STR_LIT:->' ] <EOL> for path in paths : <EOL> if path == '<STR_LIT:->' : <EOL> text = sys . stdin . read ( ) <EOL> else : <EOL> fp = codecs . open ( path , '<STR_LIT:r>' , opts . encoding ) <EOL> text = fp . read ( ) <EOL> fp . close ( ) <EOL> if opts . compare : <EOL> from subprocess import Popen , PIPE <EOL> print ( "<STR_LIT>" ) <EOL> p = Popen ( '<STR_LIT>' % markdown_pl , shell = True , stdin = PIPE , stdout = PIPE , close_fds = True ) <EOL> p . stdin . write ( text . encode ( '<STR_LIT:utf-8>' ) ) <EOL> p . stdin . close ( ) <EOL> perl_html = p . stdout . read ( ) . decode ( '<STR_LIT:utf-8>' ) <EOL> if py3 : <EOL> sys . stdout . write ( perl_html ) <EOL> else : <EOL> sys . stdout . write ( perl_html . encode ( <EOL> sys . stdout . encoding or "<STR_LIT:utf-8>" , '<STR_LIT>' ) ) <EOL> print ( "<STR_LIT>" ) <EOL> html = markdown ( text , <EOL> html4tags = opts . html4tags , <EOL> safe_mode = opts . safe_mode , <EOL> extras = extras , link_patterns = link_patterns , <EOL> use_file_vars = opts . use_file_vars ) <EOL> if py3 : <EOL> sys . stdout . write ( html ) <EOL> else : <EOL> sys . stdout . write ( html . encode ( <EOL> sys . stdout . encoding or "<STR_LIT:utf-8>" , '<STR_LIT>' ) ) <EOL> if extras and "<STR_LIT>" in extras : <EOL> log . debug ( "<STR_LIT>" + <EOL> html . toc_html . encode ( sys . stdout . encoding or "<STR_LIT:utf-8>" , '<STR_LIT>' ) ) <EOL> if opts . compare : <EOL> test_dir = join ( dirname ( dirname ( abspath ( __file__ ) ) ) , "<STR_LIT:test>" ) <EOL> if exists ( join ( test_dir , "<STR_LIT>" ) ) : <EOL> sys . path . insert ( <NUM_LIT:0> , test_dir ) <EOL> from test_markdown2 import norm_html_from_html <EOL> norm_html = norm_html_from_html ( html ) <EOL> norm_perl_html = norm_html_from_html ( perl_html ) <EOL> else : <EOL> norm_html = html <EOL> norm_perl_html = perl_html <EOL> print ( "<STR_LIT>" % ( norm_perl_html == norm_html ) ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> sys . exit ( main ( sys . argv ) ) </s>
