Mercurial > p > roundup > code
comparison roundup/cgi/templating.py @ 7561:91725f12b239
Support markdown2 2.4.10, 2.4.8- and exclude 2.4.9
Handle these changes to markdown2
version 2.4.9 broke links like (issue1)[issue1]: raise error if used
Version 2.4.10 changed how filtering of schemes is done: adapt to new
method
Mail url's in markdown are formatted
[label](mailto:user@something.com). The markdown format wrapper uses
the plain text formatter to turn issue1 and user@something.com into
markdown formatted strings to be htmlized by the markdown formatters.
However when the plain text formatter saw (mailto:user@something.com)
it made it (mailto:<user@something.com>). This is broken as the enamil
address shouldn't have the angle brackets. By modifying the email
pattern to include an optional mailto:, all three markdown formatters
do the right thing and I don't end up with href="<user@something.com>"
in the link.
| author | John Rouillard <rouilj@ieee.org> |
|---|---|
| date | Sun, 23 Jul 2023 16:50:35 -0400 |
| parents | d267b0454500 |
| children | 448dad050c9e |
comparison
equal
deleted
inserted
replaced
| 7560:5cadcaa13bed | 7561:91725f12b239 |
|---|---|
| 59 def _import_markdown2(): | 59 def _import_markdown2(): |
| 60 try: | 60 try: |
| 61 import markdown2 | 61 import markdown2 |
| 62 import re | 62 import re |
| 63 | 63 |
| 64 class Markdown(markdown2.Markdown): | 64 # Note: version 2.4.9 does not work with Roundup as it breaks |
| 65 # don't allow disabled protocols in links | 65 # [issue1](issue1) formatted links. |
| 66 _safe_protocols = re.compile('(?!' + ':|'.join([ | 66 |
| 67 re.escape(s) for s in _disable_url_schemes]) | 67 # Versions 2.4.8 and 2.4.10 use different methods to filter |
| 68 # allowed schemes. 2.4.8 uses a pre-compiled regexp while | |
| 69 # 2.4.10 uses a regexp string that it compiles. | |
| 70 | |
| 71 markdown2_vi = markdown2.__version_info__ | |
| 72 if markdown2_vi > (2, 4, 9): | |
| 73 # Create the filtering regexp. | |
| 74 # Allowed default is same as what hyper_re supports. | |
| 75 | |
| 76 # pathed_schemes are terminated with :// | |
| 77 pathed_schemes = [ 'http', 'https', 'ftp', 'ftps' ] | |
| 78 # non_pathed are terminated with a : | |
| 79 non_pathed_schemes = [ "mailto" ] | |
| 80 | |
| 81 for disabled in _disable_url_schemes: | |
| 82 try: | |
| 83 pathed_schemes.remove(disabled) | |
| 84 except ValueError: # if disabled not in list | |
| 85 pass | |
| 86 try: | |
| 87 non_pathed_schemes.remove(disabled) | |
| 88 except ValueError: | |
| 89 pass | |
| 90 | |
| 91 re_list = [] | |
| 92 for scheme in pathed_schemes: | |
| 93 re_list.append(r'(?:%s)://' % scheme) | |
| 94 for scheme in non_pathed_schemes: | |
| 95 re_list.append(r'(?:%s):' % scheme) | |
| 96 | |
| 97 enabled_schemes = r"|".join(re_list) | |
| 98 class Markdown(markdown2.Markdown): | |
| 99 _safe_protocols = enabled_schemes | |
| 100 elif markdown2_vi == (2, 4, 9): | |
| 101 raise RuntimeError("Unsupported version - markdown2 v2.4.9\n") | |
| 102 else: | |
| 103 class Markdown(markdown2.Markdown): | |
| 104 # don't allow disabled protocols in links | |
| 105 _safe_protocols = re.compile('(?!' + ':|'.join([ | |
| 106 re.escape(s) for s in _disable_url_schemes]) | |
| 68 + ':)', re.IGNORECASE) | 107 + ':)', re.IGNORECASE) |
| 69 | 108 |
| 70 def _extras(config): | 109 def _extras(config): |
| 71 extras = {'fenced-code-blocks': {}, 'nofollow': None} | 110 extras = {'fenced-code-blocks': {}, 'nofollow': None} |
| 72 if config['MARKDOWN_BREAK_ON_NEWLINE']: | 111 if config['MARKDOWN_BREAK_ON_NEWLINE']: |
| 1637 [\w]{2,5} # TLD | 1676 [\w]{2,5} # TLD |
| 1638 ) | 1677 ) |
| 1639 (:[\d]{1,5})? # port | 1678 (:[\d]{1,5})? # port |
| 1640 (/[\w\-$.+!*(),;:@&=?/~\\#%]*)? # path etc. | 1679 (/[\w\-$.+!*(),;:@&=?/~\\#%]*)? # path etc. |
| 1641 )| | 1680 )| |
| 1642 (?P<email>[-+=%/\w\.]+@[\w\.\-]+)| | 1681 (?P<email>(?:mailto:)?[-+=%/\w\.]+@[\w\.\-]+)| |
| 1643 (?P<item>(?P<class>[A-Za-z_]+)(\s*)(?P<id>\d+)(?P<fragment>\#[^][\#%^{}"<>\s]+)?) | 1682 (?P<item>(?P<class>[A-Za-z_]+)(\s*)(?P<id>\d+)(?P<fragment>\#[^][\#%^{}"<>\s]+)?) |
| 1644 )''', re.X | re.I) | 1683 )''', re.X | re.I) |
| 1645 protocol_re = re.compile('^(ht|f)tp(s?)://', re.I) | 1684 protocol_re = re.compile('^(ht|f)tp(s?)://', re.I) |
| 1646 | 1685 |
| 1647 # disable rst directives that have security implications | 1686 # disable rst directives that have security implications |
