Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/mistune/plugins/extra.py: 100%

24 statements  

« prev     ^ index     » next       coverage.py v7.2.7, created at 2023-06-03 06:10 +0000

1from ..util import escape_url, ESCAPE_TEXT 

2 

3__all__ = ['plugin_url', 'plugin_strikethrough'] 

4 

5 

6#: url link like: ``https://lepture.com/`` 

7URL_LINK_PATTERN = r'''(https?:\/\/[^\s<]+[^<.,:;"')\]\s])''' 

8 

9 

10def parse_url_link(inline, m, state): 

11 url = m.group(0) 

12 if state.get('_in_link'): 

13 return 'text', url 

14 return 'link', escape_url(url) 

15 

16 

17def plugin_url(md): 

18 md.inline.register_rule('url_link', URL_LINK_PATTERN, parse_url_link) 

19 md.inline.rules.append('url_link') 

20 

21 

22#: strike through syntax looks like: ``~~word~~`` 

23STRIKETHROUGH_PATTERN = ( 

24 r'~~(?=[^\s~])(' 

25 r'(?:\\~|[^~])*' 

26 r'(?:' + ESCAPE_TEXT + r'|[^\s~]))~~' 

27) 

28 

29 

30def parse_strikethrough(inline, m, state): 

31 text = m.group(1) 

32 return 'strikethrough', inline.render(text, state) 

33 

34 

35def render_html_strikethrough(text): 

36 return '<del>' + text + '</del>' 

37 

38 

39def plugin_strikethrough(md): 

40 md.inline.register_rule( 

41 'strikethrough', STRIKETHROUGH_PATTERN, parse_strikethrough) 

42 

43 index = md.inline.rules.index('codespan') 

44 if index != -1: 

45 md.inline.rules.insert(index + 1, 'strikethrough') 

46 else: # pragma: no cover 

47 md.inline.rules.append('strikethrough') 

48 

49 if md.renderer.NAME == 'html': 

50 md.renderer.register('strikethrough', render_html_strikethrough)