From mboxrd@z Thu Jan 1 00:00:00 1970 From: andy at warmcat.com (Andy Green) Date: Wed, 20 Jun 2018 18:12:54 +0800 Subject: [PATCH v4 11/16] md2html: add asset mapping In-Reply-To: <152948941145.29466.10223016890282865269.stgit@mail.warmcat.com> References: <152948941145.29466.10223016890282865269.stgit@mail.warmcat.com> Message-ID: <152948957470.29466.14608988436277033922.stgit@mail.warmcat.com> From: John Keeping This remaps the "src" attribute on elements according to a second command line argument, you can try it out with: md2html --- filters/html-converters/md2html | 50 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 49 insertions(+), 1 deletion(-) diff --git a/filters/html-converters/md2html b/filters/html-converters/md2html index ebf3856..eb5d977 100755 --- a/filters/html-converters/md2html +++ b/filters/html-converters/md2html @@ -2,7 +2,41 @@ import markdown import sys import io +from markdown.util import etree from pygments.formatters import HtmlFormatter +from urllib.parse import urljoin + + +class AssetMappingProcessor(markdown.treeprocessors.Treeprocessor): + + def __init__(self, asset_prefix): + self.asset_prefix = asset_prefix + + def run(self, root): + asset_prefix = self.asset_prefix + for img in root.iter('img'): + src = img.get('src') + if src is None: + continue + img.set('src', urljoin(asset_prefix, src)) + + +class AssetMappingExtension(markdown.extensions.Extension): + + def __init__(self, **kwargs): + self.config = {'asset_prefix': ['', 'prefix for relative asset URLs']} + super(AssetMappingExtension, self).__init__(**kwargs) + + def extendMarkdown(self, md, md_globals): + asset_prefix = self.getConfig('asset_prefix') + if not asset_prefix: + return + + md.treeprocessors.add('asset_mapping', + AssetMappingProcessor(asset_prefix), + '_end') + + sys.stdin = io.TextIOWrapper(sys.stdin.buffer, encoding='utf-8') sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8') sys.stdout.write(''' @@ -289,6 +323,20 @@ sys.stdout.write(''' ''') sys.stdout.write("
") sys.stdout.flush() + +extensions = [ + "markdown.extensions.fenced_code", + "markdown.extensions.codehilite", + "markdown.extensions.tables" +] +extension_configs = { + "markdown.extensions.codehilite":{"css_class":"highlight"} +} + +if len(sys.argv) > 2: + extensions.append(AssetMappingExtension(asset_prefix=sys.argv[2])) + # Note: you may want to run this through bleach for sanitization -markdown.markdownFromFile(output_format="html5", extensions=["markdown.extensions.fenced_code", "markdown.extensions.codehilite", "markdown.extensions.tables"], extension_configs={"markdown.extensions.codehilite":{"css_class":"highlight"}}) +markdown.markdownFromFile(output_format="html5", extensions=extensions, extension_configs=extension_configs) sys.stdout.write("
") +