| @@ 94-170 (lines=77) @@ | ||
| 91 | return pattern.sub('', text) |
|
| 92 | ||
| 93 | ||
| 94 | class BlockGrammar(object): |
|
| 95 | """Grammars for block level tokens.""" |
|
| 96 | ||
| 97 | def_links = re.compile( |
|
| 98 | r'^ *\[([^^\]]+)\]: *' # [key]: |
|
| 99 | r'<?([^\s>]+)>?' # <link> or link |
|
| 100 | r'(?: +["(]([^\n]+)[")])? *(?:\n+|$)' |
|
| 101 | ) |
|
| 102 | def_footnotes = re.compile( |
|
| 103 | r'^\[\^([^\]]+)\]: *(' |
|
| 104 | r'[^\n]*(?:\n+|$)' # [^key]: |
|
| 105 | r'(?: {1,}[^\n]*(?:\n+|$))*' |
|
| 106 | r')' |
|
| 107 | ) |
|
| 108 | ||
| 109 | newline = re.compile(r'^\n+') |
|
| 110 | block_code = re.compile(r'^( {4}[^\n]+\n*)+') |
|
| 111 | fences = re.compile( |
|
| 112 | r'^ *(`{3,}|~{3,}) *(\S+)? *\n' # ```lang |
|
| 113 | r'([\s\S]+?)\s*' |
|
| 114 | r'\1 *(?:\n+|$)' # ``` |
|
| 115 | ) |
|
| 116 | hrule = re.compile(r'^ {0,3}[-*_](?: *[-*_]){2,} *(?:\n+|$)') |
|
| 117 | heading = re.compile(r'^ *(#{1,6}) *([^\n]+?) *#* *(?:\n+|$)') |
|
| 118 | lheading = re.compile(r'^([^\n]+)\n *(=|-)+ *(?:\n+|$)') |
|
| 119 | block_quote = re.compile(r'^( *>[^\n]+(\n[^\n]+)*\n*)+') |
|
| 120 | list_block = re.compile( |
|
| 121 | r'^( *)(?=[*+-]|\d+\.)(([*+-])?(?:\d+\.)?) [\s\S]+?' |
|
| 122 | r'(?:' |
|
| 123 | r'\n+(?=\1?(?:[-*_] *){3,}(?:\n+|$))' # hrule |
|
| 124 | r'|\n+(?=%s)' # def links |
|
| 125 | r'|\n+(?=%s)' # def footnotes\ |
|
| 126 | r'|\n+(?=\1(?(3)\d+\.|[*+-]) )' # heterogeneous bullet |
|
| 127 | r'|\n{2,}' |
|
| 128 | r'(?! )' |
|
| 129 | r'(?!\1(?:[*+-]|\d+\.) )\n*' |
|
| 130 | r'|' |
|
| 131 | r'\s*$)' % ( |
|
| 132 | _pure_pattern(def_links), |
|
| 133 | _pure_pattern(def_footnotes), |
|
| 134 | ) |
|
| 135 | ) |
|
| 136 | list_item = re.compile( |
|
| 137 | r'^(( *)(?:[*+-]|\d+\.) [^\n]*' |
|
| 138 | r'(?:\n(?!\2(?:[*+-]|\d+\.) )[^\n]*)*)', |
|
| 139 | flags=re.M |
|
| 140 | ) |
|
| 141 | list_bullet = re.compile(r'^ *(?:[*+-]|\d+\.) +') |
|
| 142 | paragraph = re.compile( |
|
| 143 | r'^((?:[^\n]+\n?(?!' |
|
| 144 | r'%s|%s|%s|%s|%s|%s|%s|%s|%s' |
|
| 145 | r'))+)\n*' % ( |
|
| 146 | _pure_pattern(fences).replace(r'\1', r'\2'), |
|
| 147 | _pure_pattern(list_block).replace(r'\1', r'\3'), |
|
| 148 | _pure_pattern(hrule), |
|
| 149 | _pure_pattern(heading), |
|
| 150 | _pure_pattern(lheading), |
|
| 151 | _pure_pattern(block_quote), |
|
| 152 | _pure_pattern(def_links), |
|
| 153 | _pure_pattern(def_footnotes), |
|
| 154 | '<' + _block_tag, |
|
| 155 | ) |
|
| 156 | ) |
|
| 157 | block_html = re.compile( |
|
| 158 | r'^ *(?:%s|%s|%s) *(?:\n{2,}|\s*$)' % ( |
|
| 159 | r'<!--[\s\S]*?-->', |
|
| 160 | r'<(%s)((?:%s)*?)>([\s\S]*?)<\/\1>' % (_block_tag, _valid_attr), |
|
| 161 | r'<%s(?:%s)*?\s*\/?>' % (_block_tag, _valid_attr), |
|
| 162 | ) |
|
| 163 | ) |
|
| 164 | table = re.compile( |
|
| 165 | r'^ *\|(.+)\n *\|( *[-:]+[-| :]*)\n((?: *\|.*(?:\n|$))*)\n*' |
|
| 166 | ) |
|
| 167 | nptable = re.compile( |
|
| 168 | r'^ *(\S.*\|.*)\n *([-:]+ *\|[-| :]*)\n((?:.*\|.*(?:\n|$))*)\n*' |
|
| 169 | ) |
|
| 170 | text = re.compile(r'^[^\n]+') |
|
| 171 | ||
| 172 | ||
| 173 | class BlockLexer(object): |
|
| @@ 82-157 (lines=76) @@ | ||
| 79 | return pattern.sub('', text) |
|
| 80 | ||
| 81 | ||
| 82 | class BlockGrammar(object): |
|
| 83 | """Grammars for block level tokens.""" |
|
| 84 | ||
| 85 | def_links = re.compile( |
|
| 86 | r'^ *\[([^^\]]+)\]: *' # [key]: |
|
| 87 | r'<?([^\s>]+)>?' # <link> or link |
|
| 88 | r'(?: +["(]([^\n]+)[")])? *(?:\n+|$)' |
|
| 89 | ) |
|
| 90 | def_footnotes = re.compile( |
|
| 91 | r'^\[\^([^\]]+)\]: *(' |
|
| 92 | r'[^\n]*(?:\n+|$)' # [^key]: |
|
| 93 | r'(?: {1,}[^\n]*(?:\n+|$))*' |
|
| 94 | r')' |
|
| 95 | ) |
|
| 96 | ||
| 97 | newline = re.compile(r'^\n+') |
|
| 98 | block_code = re.compile(r'^( {4}[^\n]+\n*)+') |
|
| 99 | fences = re.compile( |
|
| 100 | r'^ *(`{3,}|~{3,}) *(\S+)? *\n' # ```lang |
|
| 101 | r'([\s\S]+?)\s*' |
|
| 102 | r'\1 *(?:\n+|$)' # ``` |
|
| 103 | ) |
|
| 104 | hrule = re.compile(r'^ {0,3}[-*_](?: *[-*_]){2,} *(?:\n+|$)') |
|
| 105 | heading = re.compile(r'^ *(#{1,6}) *([^\n]+?) *#* *(?:\n+|$)') |
|
| 106 | lheading = re.compile(r'^([^\n]+)\n *(=|-)+ *(?:\n+|$)') |
|
| 107 | block_quote = re.compile(r'^( *>[^\n]+(\n[^\n]+)*\n*)+') |
|
| 108 | list_block = re.compile( |
|
| 109 | r'^( *)([*+-]|\d+\.) [\s\S]+?' |
|
| 110 | r'(?:' |
|
| 111 | r'\n+(?=\1?(?:[-*_] *){3,}(?:\n+|$))' # hrule |
|
| 112 | r'|\n+(?=%s)' # def links |
|
| 113 | r'|\n+(?=%s)' # def footnotes |
|
| 114 | r'|\n{2,}' |
|
| 115 | r'(?! )' |
|
| 116 | r'(?!\1(?:[*+-]|\d+\.) )\n*' |
|
| 117 | r'|' |
|
| 118 | r'\s*$)' % ( |
|
| 119 | _pure_pattern(def_links), |
|
| 120 | _pure_pattern(def_footnotes), |
|
| 121 | ) |
|
| 122 | ) |
|
| 123 | list_item = re.compile( |
|
| 124 | r'^(( *)(?:[*+-]|\d+\.) [^\n]*' |
|
| 125 | r'(?:\n(?!\2(?:[*+-]|\d+\.) )[^\n]*)*)', |
|
| 126 | flags=re.M |
|
| 127 | ) |
|
| 128 | list_bullet = re.compile(r'^ *(?:[*+-]|\d+\.) +') |
|
| 129 | paragraph = re.compile( |
|
| 130 | r'^((?:[^\n]+\n?(?!' |
|
| 131 | r'%s|%s|%s|%s|%s|%s|%s|%s|%s' |
|
| 132 | r'))+)\n*' % ( |
|
| 133 | _pure_pattern(fences).replace(r'\1', r'\2'), |
|
| 134 | _pure_pattern(list_block).replace(r'\1', r'\3'), |
|
| 135 | _pure_pattern(hrule), |
|
| 136 | _pure_pattern(heading), |
|
| 137 | _pure_pattern(lheading), |
|
| 138 | _pure_pattern(block_quote), |
|
| 139 | _pure_pattern(def_links), |
|
| 140 | _pure_pattern(def_footnotes), |
|
| 141 | '<' + _block_tag, |
|
| 142 | ) |
|
| 143 | ) |
|
| 144 | block_html = re.compile( |
|
| 145 | r'^ *(?:%s|%s|%s) *(?:\n{2,}|\s*$)' % ( |
|
| 146 | r'<!--[\s\S]*?-->', |
|
| 147 | r'<(%s)((?:%s)*?)>([\s\S]+?)<\/\1>' % (_block_tag, _valid_attr), |
|
| 148 | r'<%s(?:%s)*?>' % (_block_tag, _valid_attr), |
|
| 149 | ) |
|
| 150 | ) |
|
| 151 | table = re.compile( |
|
| 152 | r'^ *\|(.+)\n *\|( *[-:]+[-| :]*)\n((?: *\|.*(?:\n|$))*)\n*' |
|
| 153 | ) |
|
| 154 | nptable = re.compile( |
|
| 155 | r'^ *(\S.*\|.*)\n *([-:]+ *\|[-| :]*)\n((?:.*\|.*(?:\n|$))*)\n*' |
|
| 156 | ) |
|
| 157 | text = re.compile(r'^[^\n]+') |
|
| 158 | ||
| 159 | ||
| 160 | class BlockLexer(object): |
|