From 186e4d4ddd9f92823c8231eb19d69f115dcd7ae2 Mon Sep 17 00:00:00 2001 From: Don Bowman Date: Sat, 20 Oct 2018 18:39:25 -0400 Subject: [PATCH] Add support for markdown >= 3.0 [python-markdown](https://github.com/Python-Markdown/markdown) is now at version 3.0.1. Some interfaces that were formerly deprecated are now removed. Make corresponding changes here, notably: - remove 'smart_strong' - remove 'safe_mode' HTML (was not implemented) - remove 'md_globals' - fix positional argumenst (e.g. extensions=[]) - support InlineProcessor (instead of Pattern) in WikiLinks --- requirements_readthedocs.txt | 2 +- setup.py | 2 +- src/wiki/conf/settings.py | 1 - src/wiki/core/markdown/mdx/codehilite.py | 6 ++-- src/wiki/core/markdown/mdx/previewlinks.py | 2 +- src/wiki/core/markdown/mdx/responsivetable.py | 2 +- .../attachments/markdown_extensions.py | 6 ++-- .../plugins/images/markdown_extensions.py | 6 ++-- src/wiki/plugins/links/mdx/djangowikilinks.py | 10 +++---- src/wiki/plugins/links/mdx/urlize.py | 14 +++++----- src/wiki/plugins/macros/mdx/macro.py | 4 +-- src/wiki/plugins/macros/mdx/toc.py | 4 +-- src/wiki/plugins/macros/mdx/wikilinks.py | 28 ++++++++----------- src/wiki/plugins/redlinks/mdx/redlinks.py | 2 +- tests/plugins/links/test_urlize.py | 8 ------ 15 files changed, 42 insertions(+), 55 deletions(-) diff --git a/requirements_readthedocs.txt b/requirements_readthedocs.txt index de48bc46..903dfc6d 100644 --- a/requirements_readthedocs.txt +++ b/requirements_readthedocs.txt @@ -5,5 +5,5 @@ Django>=1.8,<1.12 Pillow django-sekizai>=0.10 sorl-thumbnail>=12,<13 -Markdown>=2.6,<2.7 +Markdown>=3.0.0,<3.1.0 bleach>=2.1,<2.2 diff --git a/setup.py b/setup.py index db7a84f7..aa1a5dfd 100755 --- a/setup.py +++ b/setup.py @@ -31,7 +31,7 @@ install_requirements = [ "django-mptt>=0.9,<0.10", "django-sekizai>=0.10", "sorl-thumbnail>=12,<13", - "Markdown>=2.6,<2.7", + "Markdown>=3.0.0,<3.1.0" ] test_requirements = [ diff --git a/src/wiki/conf/settings.py b/src/wiki/conf/settings.py index c558bf7f..c2f35ddd 100644 --- a/src/wiki/conf/settings.py +++ b/src/wiki/conf/settings.py @@ -51,7 +51,6 @@ MARKDOWN_KWARGS = { 'extensions': [ 'markdown.extensions.footnotes', 'markdown.extensions.attr_list', - 'markdown.extensions.smart_strong', 'markdown.extensions.footnotes', 'markdown.extensions.attr_list', 'markdown.extensions.def_list', diff --git a/src/wiki/core/markdown/mdx/codehilite.py b/src/wiki/core/markdown/mdx/codehilite.py index b8c2ef6f..cd62325d 100644 --- a/src/wiki/core/markdown/mdx/codehilite.py +++ b/src/wiki/core/markdown/mdx/codehilite.py @@ -58,7 +58,7 @@ class WikiFencedBlockPreprocessor(Preprocessor): if m.group('lang'): lang = m.group('lang') html = highlight(m.group('code'), self.config, self.markdown.tab_length, lang=lang) - placeholder = self.markdown.htmlStash.store(html, safe=True) + placeholder = self.markdown.htmlStash.store(html) text = '%s\n%s\n%s' % (text[:m.start()], placeholder, text[m.end():]) @@ -76,7 +76,7 @@ class HiliteTreeprocessor(Treeprocessor): for block in blocks: if len(block) == 1 and block[0].tag == 'code': html = highlight(block[0].text, self.config, self.markdown.tab_length) - placeholder = self.markdown.htmlStash.store(html, safe=True) + placeholder = self.markdown.htmlStash.store(html) # Clear codeblock in etree instance block.clear() # Change to p element which will later @@ -92,7 +92,7 @@ class WikiCodeHiliteExtension(CodeHiliteExtension): because it's hard to extend... """ - def extendMarkdown(self, md, md_globals): + def extendMarkdown(self, md): """ Add HilitePostprocessor to Markdown instance. """ hiliter = HiliteTreeprocessor(md) hiliter.config = self.getConfigs() diff --git a/src/wiki/core/markdown/mdx/previewlinks.py b/src/wiki/core/markdown/mdx/previewlinks.py index f679af3d..31ca8285 100644 --- a/src/wiki/core/markdown/mdx/previewlinks.py +++ b/src/wiki/core/markdown/mdx/previewlinks.py @@ -6,7 +6,7 @@ class PreviewLinksExtension(markdown.Extension): """Markdown Extension that sets all anchor targets to _blank when in preview mode""" - def extendMarkdown(self, md, md_globals): + def extendMarkdown(self, md): md.treeprocessors.add('previewlinks', PreviewLinksTree(md), "_end") diff --git a/src/wiki/core/markdown/mdx/responsivetable.py b/src/wiki/core/markdown/mdx/responsivetable.py index d0e0e03a..8ba493ae 100644 --- a/src/wiki/core/markdown/mdx/responsivetable.py +++ b/src/wiki/core/markdown/mdx/responsivetable.py @@ -6,7 +6,7 @@ from markdown.util import etree class ResponsiveTableExtension(markdown.Extension): """Wraps all tables with Bootstrap's table-responsive class""" - def extendMarkdown(self, md, md_globals): + def extendMarkdown(self, md): md.treeprocessors.add('responsivetable', ResponsiveTableTree(md), "_end") diff --git a/src/wiki/plugins/attachments/markdown_extensions.py b/src/wiki/plugins/attachments/markdown_extensions.py index 88851e44..5438a1a1 100644 --- a/src/wiki/plugins/attachments/markdown_extensions.py +++ b/src/wiki/plugins/attachments/markdown_extensions.py @@ -16,7 +16,7 @@ class AttachmentExtension(markdown.Extension): """ Abbreviation Extension for Python-Markdown. """ - def extendMarkdown(self, md, md_globals): + def extendMarkdown(self, md): """ Insert AbbrPreprocessor before ReferencePreprocessor. """ md.preprocessors.add( 'dw-attachments', @@ -80,7 +80,7 @@ class AttachmentPreprocessor(markdown.preprocessors.Preprocessor): 'attachment_can_read': attachment_can_read, } ) - line = self.markdown.htmlStash.store(html, safe=True) + line = self.markdown.htmlStash.store(html) except models.Attachment.DoesNotExist: html = ( """Attachment with ID """ @@ -88,7 +88,7 @@ class AttachmentPreprocessor(markdown.preprocessors.Preprocessor): ).format(attachment_id) line = line.replace( '[' + m.group(2) + ']', - self.markdown.htmlStash.store(html, safe=True) + self.markdown.htmlStash.store(html) ) new_text.append(before + line + after) return new_text diff --git a/src/wiki/plugins/images/markdown_extensions.py b/src/wiki/plugins/images/markdown_extensions.py index 6819f264..b17e3957 100644 --- a/src/wiki/plugins/images/markdown_extensions.py +++ b/src/wiki/plugins/images/markdown_extensions.py @@ -23,7 +23,7 @@ class ImageExtension(markdown.Extension): """ Images plugin markdown extension for django-wiki. """ - def extendMarkdown(self, md, md_globals): + def extendMarkdown(self, md): md.inlinePatterns.add('dw-images', ImagePattern(IMAGE_RE, md), '>link') md.postprocessors.add('dw-images-cleanup', ImagePostprocessor(md), '>raw_html') @@ -76,8 +76,8 @@ class ImagePattern(markdown.inlinepatterns.Pattern): }, ) html_before, html_after = html.split(caption_placeholder) - placeholder_before = self.markdown.htmlStash.store(html_before, safe=True) - placeholder_after = self.markdown.htmlStash.store(html_after, safe=True) + placeholder_before = self.markdown.htmlStash.store(html_before) + placeholder_after = self.markdown.htmlStash.store(html_after) return placeholder_before + caption + placeholder_after + trailer diff --git a/src/wiki/plugins/links/mdx/djangowikilinks.py b/src/wiki/plugins/links/mdx/djangowikilinks.py index 11364d37..b211f22a 100755 --- a/src/wiki/plugins/links/mdx/djangowikilinks.py +++ b/src/wiki/plugins/links/mdx/djangowikilinks.py @@ -23,7 +23,7 @@ from markdown.util import etree from wiki import models -class WikiPathExtension(markdown.Extension): +class WikiPathExtension(markdown.extensions.Extension): def __init__(self, configs): # set extension defaults @@ -42,12 +42,12 @@ class WikiPathExtension(markdown.Extension): for key, value in configs: self.setConfig(key, value) - def extendMarkdown(self, md, md_globals): + def extendMarkdown(self, md): self.md = md # append to end of inline patterns WIKI_RE = r'\[(?P