File size: 12,015 Bytes
d1ceb73 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 |
import re
from typing import Optional, List, Dict, Any, Match
from .core import Parser, InlineState
from .util import (
escape,
escape_url,
unikey,
)
from .helpers import (
PREVENT_BACKSLASH,
PUNCTUATION,
HTML_TAGNAME,
HTML_ATTRIBUTES,
unescape_char,
parse_link,
parse_link_label,
parse_link_text,
)
PAREN_END_RE = re.compile(r'\s*\)')
AUTO_EMAIL = (
r'''<[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9]'''
r'(?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?'
r'(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*>'
)
INLINE_HTML = (
r'<' + HTML_TAGNAME + HTML_ATTRIBUTES + r'\s*/?>|' # open tag
r'</' + HTML_TAGNAME + r'\s*>|' # close tag
r'<!--(?!>|->)(?:(?!--)[\s\S])+?(?<!-)-->|' # comment
r'<\?[\s\S]+?\?>|' # script like <?php?>
r'<![A-Z][\s\S]+?>|' # doctype
r'<!\[CDATA[\s\S]+?\]\]>' # cdata
)
EMPHASIS_END_RE = {
'*': re.compile(r'(?:' + PREVENT_BACKSLASH + r'\\\*|[^\s*])\*(?!\*)'),
'_': re.compile(r'(?:' + PREVENT_BACKSLASH + r'\\_|[^\s_])_(?!_)\b'),
'**': re.compile(r'(?:' + PREVENT_BACKSLASH + r'\\\*|[^\s*])\*\*(?!\*)'),
'__': re.compile(r'(?:' + PREVENT_BACKSLASH + r'\\_|[^\s_])__(?!_)\b'),
'***': re.compile(r'(?:' + PREVENT_BACKSLASH + r'\\\*|[^\s*])\*\*\*(?!\*)'),
'___': re.compile(r'(?:' + PREVENT_BACKSLASH + r'\\_|[^\s_])___(?!_)\b'),
}
class InlineParser(Parser):
sc_flag = 0
state_cls = InlineState
#: linebreak leaves two spaces at the end of line
STD_LINEBREAK = r'(?:\\| {2,})\n\s*'
#: every new line becomes <br>
HARD_LINEBREAK = r' *\n\s*'
# we only need to find the start pattern of an inline token
SPECIFICATION = {
# e.g. \`, \$
'escape': r'(?:\\' + PUNCTUATION + ')+',
# `code, ```code
'codespan': r'`{1,}',
# *w, **w, _w, __w
'emphasis': r'\*{1,3}(?=[^\s*])|\b_{1,3}(?=[^\s_])',
# [link], ![img]
'link': r'!?\[',
# <https://example.com>. regex copied from commonmark.js
'auto_link': r'<[A-Za-z][A-Za-z0-9.+-]{1,31}:[^<>\x00-\x20]*>',
'auto_email': AUTO_EMAIL,
'inline_html': INLINE_HTML,
'linebreak': STD_LINEBREAK,
'softbreak': HARD_LINEBREAK,
'prec_auto_link': r'<[A-Za-z][A-Za-z\d.+-]{1,31}:',
'prec_inline_html': r'</?' + HTML_TAGNAME + r'|<!|<\?',
}
DEFAULT_RULES = (
'escape',
'codespan',
'emphasis',
'link',
'auto_link',
'auto_email',
'inline_html',
'linebreak',
)
def __init__(self, hard_wrap: bool=False):
super(InlineParser, self).__init__()
self.hard_wrap = hard_wrap
# lazy add linebreak
if hard_wrap:
self.specification['linebreak'] = self.HARD_LINEBREAK
else:
self.rules.append('softbreak')
self._methods = {
name: getattr(self, 'parse_' + name) for name in self.rules
}
def parse_escape(self, m: Match, state: InlineState) -> int:
text = m.group(0)
text = unescape_char(text)
state.append_token({
'type': 'text',
'raw': text,
})
return m.end()
def parse_link(self, m: Match, state: InlineState) -> Optional[int]:
pos = m.end()
marker = m.group(0)
is_image = marker[0] == '!'
if is_image and state.in_image:
state.append_token({'type': 'text', 'raw': marker})
return pos
elif not is_image and state.in_link:
state.append_token({'type': 'text', 'raw': marker})
return pos
text = None
label, end_pos = parse_link_label(state.src, pos)
if label is None:
text, end_pos = parse_link_text(state.src, pos)
if text is None:
return
if text is None:
text = label
if end_pos >= len(state.src) and label is None:
return
rules = ['codespan', 'prec_auto_link', 'prec_inline_html']
prec_pos = self.precedence_scan(m, state, end_pos, rules)
if prec_pos:
return prec_pos
if end_pos < len(state.src):
c = state.src[end_pos]
if c == '(':
# standard link [text](<url> "title")
attrs, pos2 = parse_link(state.src, end_pos + 1)
if pos2:
token = self.__parse_link_token(is_image, text, attrs, state)
state.append_token(token)
return pos2
elif c == '[':
# standard ref link [text][label]
label2, pos2 = parse_link_label(state.src, end_pos + 1)
if pos2:
end_pos = pos2
if label2:
label = label2
if label is None:
return
ref_links = state.env.get('ref_links')
if not ref_links:
return
key = unikey(label)
env = ref_links.get(key)
if env:
attrs = {'url': env['url'], 'title': env.get('title')}
token = self.__parse_link_token(is_image, text, attrs, state)
token['ref'] = key
token['label'] = label
state.append_token(token)
return end_pos
def __parse_link_token(self, is_image, text, attrs, state):
new_state = state.copy()
new_state.src = text
if is_image:
new_state.in_image = True
token = {
'type': 'image',
'children': self.render(new_state),
'attrs': attrs,
}
else:
new_state.in_link = True
token = {
'type': 'link',
'children': self.render(new_state),
'attrs': attrs,
}
return token
def parse_auto_link(self, m: Match, state: InlineState) -> int:
text = m.group(0)
pos = m.end()
if state.in_link:
self.process_text(text, state)
return pos
text = text[1:-1]
self._add_auto_link(text, text, state)
return pos
def parse_auto_email(self, m: Match, state: InlineState) -> int:
text = m.group(0)
pos = m.end()
if state.in_link:
self.process_text(text, state)
return pos
text = text[1:-1]
url = 'mailto:' + text
self._add_auto_link(url, text, state)
return pos
def _add_auto_link(self, url, text, state):
state.append_token({
'type': 'link',
'children': [{'type': 'text', 'raw': text}],
'attrs': {'url': escape_url(url)},
})
def parse_emphasis(self, m: Match, state: InlineState) -> int:
pos = m.end()
marker = m.group(0)
mlen = len(marker)
if mlen == 1 and state.in_emphasis:
state.append_token({'type': 'text', 'raw': marker})
return pos
elif mlen == 2 and state.in_strong:
state.append_token({'type': 'text', 'raw': marker})
return pos
_end_re = EMPHASIS_END_RE[marker]
m1 = _end_re.search(state.src, pos)
if not m1:
state.append_token({'type': 'text', 'raw': marker})
return pos
end_pos = m1.end()
text = state.src[pos:end_pos-mlen]
prec_pos = self.precedence_scan(m, state, end_pos)
if prec_pos:
return prec_pos
new_state = state.copy()
new_state.src = text
if mlen == 1:
new_state.in_emphasis = True
children = self.render(new_state)
state.append_token({'type': 'emphasis', 'children': children})
elif mlen == 2:
new_state.in_strong = True
children = self.render(new_state)
state.append_token({'type': 'strong', 'children': children})
else:
new_state.in_emphasis = True
new_state.in_strong = True
children = [{
'type': 'strong',
'children': self.render(new_state)
}]
state.append_token({
'type': 'emphasis',
'children': children,
})
return end_pos
def parse_codespan(self, m: Match, state: InlineState) -> int:
marker = m.group(0)
# require same marker with same length at end
pattern = re.compile(r'(.*?[^`])' + marker + r'(?!`)', re.S)
pos = m.end()
m = pattern.match(state.src, pos)
if m:
end_pos = m.end()
code = m.group(1)
# Line endings are treated like spaces
code = code.replace('\n', ' ')
if len(code.strip()):
if code.startswith(' ') and code.endswith(' '):
code = code[1:-1]
state.append_token({'type': 'codespan', 'raw': escape(code)})
return end_pos
else:
state.append_token({'type': 'text', 'raw': marker})
return pos
def parse_linebreak(self, m: Match, state: InlineState) -> int:
state.append_token({'type': 'linebreak'})
return m.end()
def parse_softbreak(self, m: Match, state: InlineState) -> int:
state.append_token({'type': 'softbreak'})
return m.end()
def parse_inline_html(self, m: Match, state: InlineState) -> int:
end_pos = m.end()
html = m.group(0)
state.append_token({'type': 'inline_html', 'raw': html})
if html.startswith(('<a ', '<a>', '<A ', '<A>')):
state.in_link = True
elif html.startswith(('</a ', '</a>', '</A ', '</A>')):
state.in_link = False
return end_pos
def process_text(self, text: str, state: InlineState):
state.append_token({'type': 'text', 'raw': text})
def parse(self, state: InlineState) -> List[Dict[str, Any]]:
pos = 0
sc = self.compile_sc()
while pos < len(state.src):
m = sc.search(state.src, pos)
if not m:
break
end_pos = m.start()
if end_pos > pos:
hole = state.src[pos:end_pos]
self.process_text(hole, state)
new_pos = self.parse_method(m, state)
if not new_pos:
# move cursor 1 character forward
pos = end_pos + 1
hole = state.src[end_pos:pos]
self.process_text(hole, state)
else:
pos = new_pos
if pos == 0:
# special case, just pure text
self.process_text(state.src, state)
elif pos < len(state.src):
self.process_text(state.src[pos:], state)
return state.tokens
def precedence_scan(self, m: Match, state: InlineState, end_pos: int, rules=None):
if rules is None:
rules = ['codespan', 'link', 'prec_auto_link', 'prec_inline_html']
mark_pos = m.end()
sc = self.compile_sc(rules)
m1 = sc.search(state.src, mark_pos, end_pos)
if not m1:
return
rule_name = m1.lastgroup.replace('prec_', '')
sc = self.compile_sc([rule_name])
m2 = sc.match(state.src, m1.start())
if not m2:
return
func = self._methods[rule_name]
new_state = state.copy()
new_state.src = state.src
m2_pos = func(m2, new_state)
if not m2_pos or m2_pos < end_pos:
return
raw_text = state.src[m.start():m2.start()]
state.append_token({'type': 'text', 'raw': raw_text})
for token in new_state.tokens:
state.append_token(token)
return m2_pos
def render(self, state: InlineState):
self.parse(state)
return state.tokens
def __call__(self, s, env):
state = self.state_cls(env)
state.src = s
return self.render(state)
|