From 96ed6a0da2eecaefd178e85c85ec17a33338306a Mon Sep 17 00:00:00 2001 From: Patrik Dufresne Date: Wed, 7 Nov 2018 07:49:39 -0500 Subject: [PATCH] Fix link resolution for image to use PRIMARY_ALT Images/Attachments are only generated into the primary alt folder and not into the secondary alt. So image url need to get resolved using the primary alt. --- lektor_pythonmarkdown.py | 35 ++++++++++++++++++----------- tests/test_lektor_pythonmarkdown.py | 13 ++++++++--- 2 files changed, 32 insertions(+), 16 deletions(-) diff --git a/lektor_pythonmarkdown.py b/lektor_pythonmarkdown.py index 7e78031..3e64500 100644 --- a/lektor_pythonmarkdown.py +++ b/lektor_pythonmarkdown.py @@ -7,17 +7,16 @@ Created on Jun 8, 2018 import markdown from markdown.extensions import Extension from markdown.inlinepatterns import LinkPattern +from markupsafe import Markup import types from weakref import ref as weakref +from werkzeug.urls import url_parse from lektor.context import get_ctx +from lektor.environment import PRIMARY_ALT from lektor.pluginsystem import Plugin from lektor.types import Type from lektor.utils import bool_from_string -from markupsafe import Markup -from werkzeug.urls import url_parse - - SECTION_EXTENSIONS = "extensions" SECTION_MARKDOWN = "markdown" @@ -26,15 +25,25 @@ DEFAULT_EXTENTIONS = { } -def sanitize_url(self, link): +def sanitize_link(self, 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 LinkPattern.sanitize_url(self, link) + + +def sanitize_image(self, 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) + link = get_ctx().record.url_to(link, alt=PRIMARY_ALT, base_url=get_ctx().base_url) return LinkPattern.sanitize_url(self, link) @@ -45,17 +54,17 @@ class LektorMarkdownExtension(Extension): markdown rendered (mistune). """ - def _patch(self, p): + def _patch(self, p, func): """ Monkey patch the sanitize_url method. """ - p.sanitize_url = types.MethodType(sanitize_url, p) + p.sanitize_url = types.MethodType(func, p) def extendMarkdown(self, md, md_globals): - self._patch(md.inlinePatterns['link']) - self._patch(md.inlinePatterns['image_link']) - self._patch(md.inlinePatterns['image_reference']) - self._patch(md.inlinePatterns['reference']) + 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) class PythonMarkdownConfig(object): diff --git a/tests/test_lektor_pythonmarkdown.py b/tests/test_lektor_pythonmarkdown.py index 221ecd2..67b8514 100644 --- a/tests/test_lektor_pythonmarkdown.py +++ b/tests/test_lektor_pythonmarkdown.py @@ -42,14 +42,21 @@ class TestLektorPythonMarkdown(unittest.TestCase): # The output changes depending on the version of python-markdown uses. # assert '
code here
' in html # Check url & image substitution - assert 'Link to Sub Page' in html - assert 'Link to Slug' in html - assert 'Link to Invalid Page' in html + assert 'Link to Sub Page' in html + assert 'Link to Slug' in html + assert 'Link to Invalid Page' in html assert 'alttxt' in html # Check references assert 'Sub Page' in html assert 'Yahoo' in html + def test_basic_alt_en(self): + failures = self.builder.build_all() + assert not failures + page_path = os.path.join(self.builder.destination_path, 'en/index.html') + html = open(page_path).read() + print(html) + assert 'alttxt' in html if __name__ == "__main__": -- GitLab