diff --git a/CHANGES.md b/CHANGES.md index 00b7924a11473164ee57a6601a709adf9351f960..cabbe1cb870807063c2fd79099ddf4538a463916 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,8 +1,8 @@ # Change logs -## v1.1 +## v1.2 -Release date 26th of January, 2019 +Release date 27th of January, 2019 - Support Markdown v2.x and v3.x diff --git a/README.md b/README.md index 983201d9349e92f9e55d93fdc20e09bc7f000b2a..bc59b8e2df05992740ffb106e5c716d9296b5da8 100644 --- a/README.md +++ b/README.md @@ -59,7 +59,6 @@ In that file you may write something similar to the following: markdown.extensions.extra = 1 markdown.extensions.admonition = 1 markdown.extensions.codehilite = 1 -markdown.extensions.headerid = 1 markdown.extensions.meta = 1 markdown.extensions.nl2br = 1 markdown.extensions.sane_lists = 1 diff --git a/lektor_pythonmarkdown.py b/lektor_pythonmarkdown.py index 8acf8704dacb1f0ba85703feaa7d9e1cd9269360..587c92306dc13855181eabab29ff7e9d8a70af34 100644 --- a/lektor_pythonmarkdown.py +++ b/lektor_pythonmarkdown.py @@ -6,12 +6,9 @@ Created on Jun 8, 2018 ''' import markdown from markdown.extensions import Extension - -# Markdown v3.x -from markdown.inlinepatterns import Pattern - from markupsafe import Markup import types +import traceback from weakref import ref as weakref from werkzeug.urls import url_parse @@ -21,6 +18,7 @@ from lektor.pluginsystem import Plugin from lektor.types import Type from lektor.utils import bool_from_string + SECTION_EXTENSIONS = "extensions" SECTION_MARKDOWN = "markdown" DEFAULT_EXTENTIONS = { @@ -28,26 +26,48 @@ DEFAULT_EXTENTIONS = { } -def sanitize_link(self, link): +def sanitize_link( link): """ Patched function to resolve the url using Lektor. """ if get_ctx() and get_ctx().record is not None: url = url_parse(link) if not url.scheme: - link = get_ctx().record.url_to(link, base_url=get_ctx().base_url) - return Pattern.unescape(self, link) + return get_ctx().record.url_to(link, base_url=get_ctx().base_url) + return link -def sanitize_image(self, link): +def sanitize_image(link): """ Patched function to resolve the url using Lektor. """ if get_ctx() and get_ctx().record is not None: url = url_parse(link) if not url.scheme: - link = get_ctx().record.url_to(link, alt=PRIMARY_ALT, base_url=get_ctx().base_url) - return Pattern.unescape(self, link) + return get_ctx().record.url_to(link, alt=PRIMARY_ALT, base_url=get_ctx().base_url) + return link + + +def handleMatch(self, *args, **kwargs): + """ + Patched function to resolve the href/src using Lektor. + """ + # Call the original function. + t = self._handleMatch(*args, **kwargs) + if isinstance(t, tuple): + el = t[0] + else: + el = t + if el is None: + return t + + # Resolv link using lektor + if el.get('href'): + el.set('href', sanitize_link(el.get('href'))) + if el.get('src'): + el.set('src', sanitize_image(el.get('src'))) + + return t class LektorMarkdownExtension(Extension): @@ -61,13 +81,14 @@ class LektorMarkdownExtension(Extension): """ Monkey patch the sanitize_url method. """ - p.sanitize_url = types.MethodType(func, p) + p._handleMatch = p.handleMatch + p.handleMatch = types.MethodType(func, p) - def extendMarkdown(self, md, md_globals): - self._patch(md.inlinePatterns['link'], sanitize_link) - self._patch(md.inlinePatterns['reference'], sanitize_link) - self._patch(md.inlinePatterns['image_link'], sanitize_image) - self._patch(md.inlinePatterns['image_reference'], sanitize_image) + def extendMarkdown(self, md, md_globals={}): + self._patch(md.inlinePatterns['link'], handleMatch) + self._patch(md.inlinePatterns['reference'], handleMatch) + self._patch(md.inlinePatterns['image_link'], handleMatch) + self._patch(md.inlinePatterns['image_reference'], handleMatch) class PythonMarkdownConfig(object): @@ -121,7 +142,10 @@ def pythonmarkdown_to_html(text, record=None): raise RuntimeError('PythonMarkdownPLugin is required for python-markdown rendering') cfg = PythonMarkdownConfig(plugin.get_config()) # TODO May need to emit event to let other plugin hook into this one. - return markdown.markdown(text, **cfg.options) + try: + return markdown.markdown(text, **cfg.options) + except: + return "pythonmarkdown error: " + traceback.format_exc() class PythonMarkdown(object): diff --git a/setup.py b/setup.py index 4a2d527811c49b4e98af853a1a0eec947744742d..0440a3d42ca44861d0991a67be3da8d84523a674 100644 --- a/setup.py +++ b/setup.py @@ -23,7 +23,7 @@ setup( long_description_content_type='text/markdown', name='lektor-pythonmarkdown', py_modules=['lektor_pythonmarkdown'], - install_requires=['markdown<3.0.0'], + install_requires=['markdown'], tests_require=['lektor'], setup_requires=['setuptools_scm'], use_scm_version=True, diff --git a/tests/demo-project/configs/pythonmarkdown.ini b/tests/demo-project/configs/pythonmarkdown.ini index 0f52af417aba0bf4e8f07dc81539cf1143baf511..c662b5ebc3ec5f95bd3687dfae51d4ff172acf06 100644 --- a/tests/demo-project/configs/pythonmarkdown.ini +++ b/tests/demo-project/configs/pythonmarkdown.ini @@ -10,7 +10,6 @@ markdown.extensions.extra = 1 markdown.extensions.admonition = 1 markdown.extensions.codehilite = 1 -markdown.extensions.headerid = 1 markdown.extensions.meta = 1 markdown.extensions.nl2br = 1 markdown.extensions.sane_lists = 1