From 671f59d9d3e2613913fcd7396fbd72df588a1b83 Mon Sep 17 00:00:00 2001 From: Daniel Fancsali Date: Mon, 23 Mar 2020 22:14:11 +0000 Subject: [PATCH 1/3] Refactor to Postprocessor Other processors/extensions may generate headings, that might need to be downgraded. A prime example is the built-in raw HTML processing, that removes inline HTML processes the actual markdown, and then -- in a preprocessor -- puts the removed raw HTML back. To avoid this, the header downgrade logic has been moved to a postprocessor with a low priority (5) to make sure it's the last thing running. --- mdx_headdown/__init__.py | 44 ++++++++++++++++++---------------------- 1 file changed, 20 insertions(+), 24 deletions(-) diff --git a/mdx_headdown/__init__.py b/mdx_headdown/__init__.py index 56b3dd6..2fdd94e 100644 --- a/mdx_headdown/__init__.py +++ b/mdx_headdown/__init__.py @@ -5,34 +5,30 @@ heading levels to offset by. This will automatically be absolutised and integerised. -(c) 2018, Sascha Cowley under the MIT Licence. +(c) 2018-2020, Sascha Cowley and contributors under the MIT Licence. """ # Import relevant modules. from markdown.extensions import Extension -from markdown.treeprocessors import Treeprocessor +from markdown.postprocessors import Postprocessor import re name = "mdx_headdown" -class DowngradeHeadingsTreeprocessor(Treeprocessor): - """ Downgrade headings via the Treeprocessor interface. """ - def run(self, root): +class DowngradeHeadingsPostprocessor(Postprocessor): + """ Downgrade headings via the Preprocessor interface. """ + def run(self, text): # Ensure the offset value is a positive (or zero) integer. - offset = abs(int(self.config['offset'])) - - # Match headings 1-6 case insensitively if they're the entirety of the - # string, and capture the heading number. - heading_pattern = re.compile('^h([1-6])$', re.I) - - for element in root: - # Attempt matching each tag against the heading pattern. - match = heading_pattern.match(element.tag) - if match: - # For all headings, increase their heading number by `offset`. - # If the new heading number is > 6, use 6 instead. - element.tag = 'h%d' % min(6, int(match.group(1))+offset) + self.offset = abs(int(self.config['offset'])) + # Match headings 1-6 case insensitively, and capture the heading number. + heading_pattern = re.compile(r'([^<]*)', re.I) + + return re.sub(heading_pattern, self.downgrade, text) + + def downgrade(self, mo): + return '%(content)s' % \ + {'level': min(6, int(mo.group(1)) + self.offset), 'content': mo.group(2) } class DowngradeHeadingsExtension(Extension): """ Setup the extension for usage. """ @@ -45,15 +41,15 @@ def __init__(self, **kwargs): super(DowngradeHeadingsExtension, self).__init__(**kwargs) def extendMarkdown(self, md): - # Register the plugin as a Treeprocessor. """ - md.treeprocessors.register( - DowngradeHeadingsTreeprocessor(md), - 'downgradeheadings', 200 + # Register the plugin as a Postprocessor. """ + md.postprocessors.register( + DowngradeHeadingsPostprocessor(md), + 'downgradeheadings', 5 ) # And give the actual processor access to the configuration. - DowngradeHeadingsTreeprocessor.config = self.getConfigs() + DowngradeHeadingsPostprocessor.config = self.getConfigs() def makeExtension(*args, **kwargs): # Tell Markdown to make this an extension. - return DowngradeHeadingsExtension(*args, **kwargs) \ No newline at end of file + return DowngradeHeadingsExtension(*args, **kwargs) From d22847eee025897c2de88c1f58cec4091f791508 Mon Sep 17 00:00:00 2001 From: Daniel Fancsali Date: Mon, 23 Mar 2020 22:30:05 +0000 Subject: [PATCH 2/3] Update license & copyright texts to mention contributors --- LICENSE | 4 ++-- README.md | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/LICENSE b/LICENSE index 94ba609..b5f7408 100644 --- a/LICENSE +++ b/LICENSE @@ -1,4 +1,4 @@ -Copyright 2018, Sascha Cowley. +Copyright 2018-2020, Sascha Cowley and contributors Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to @@ -16,4 +16,4 @@ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR -OTHER DEALINGS IN THE SOFTWARE. \ No newline at end of file +OTHER DEALINGS IN THE SOFTWARE. diff --git a/README.md b/README.md index 586939d..6f24ec6 100644 --- a/README.md +++ b/README.md @@ -44,7 +44,7 @@ MARKDOWN = { Thanks are owed to the author of [mdx_downheader][p2], whose code I examined for inspiration; and the contributers to the [default python-markdown extensions][pmdx], whose code I examined to get a better idea of what the [manual][pmdapi] was talking about. -This project is copyright 2018 by Sascha Cowley under the [MIT License](LICENSE). +This project is copyright 2018-2020 by Sascha Cowley and contributors [MIT License](LICENSE). [pmd]: https://python-markdown.github.io/ @@ -54,4 +54,4 @@ This project is copyright 2018 by Sascha Cowley under the [MIT License](LICENSE) [pmdx]: https://python-markdown.github.io/extensions/ [pmdapi]: https://python-markdown.github.io/extensions/ [pelicanconf]: http://docs.getpelican.com/en/stable/settings.html -[3ppmdx]: https://github.com/Python-Markdown/markdown/wiki/third-party-extensions \ No newline at end of file +[3ppmdx]: https://github.com/Python-Markdown/markdown/wiki/third-party-extensions From bd21cbc0ee5132f0b6e1bb76451581c82b8e944a Mon Sep 17 00:00:00 2001 From: Daniel Fancsali Date: Tue, 24 Mar 2020 22:30:27 +0000 Subject: [PATCH 3/3] Selective disable for one heading Enable the user to add 'headdown="0"' to the tag, and make the extension skip this heading. --- README.md | 13 +++++++++++++ mdx_headdown/__init__.py | 16 ++++++++++++---- 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 6f24ec6..c5b4865 100644 --- a/README.md +++ b/README.md @@ -40,6 +40,18 @@ MARKDOWN = { ... ``` +You can selectively disable processing of a particular heading by adding the `headdown="0"` attribute to it: + +```html +

This will remain level 1!

+``` + +... or using the `attr_list` extension: +```markdown +# This one stays at level 1 { headdown='0' } +``` +**Note**: This is analogous to how the [Markdown in HTML][pmd_mdinhtml] extension – within Python-Markdown Extra, that comes with Python-Markdown itself – would allow you to enable markdown processing inside raw HTML blocks by inlcuding `markdown="1"`. + ## Credit Thanks are owed to the author of [mdx_downheader][p2], whose code I examined for inspiration; and the contributers to the [default python-markdown extensions][pmdx], whose code I examined to get a better idea of what the [manual][pmdapi] was talking about. @@ -48,6 +60,7 @@ This project is copyright 2018-2020 by Sascha Cowley and contributors [MIT Licen [pmd]: https://python-markdown.github.io/ +[pmd_mdinhtml]: https://python-markdown.github.io/extensions/md_in_html/ [pelican]: https://getpelican.com/ [p1]: https://code.google.com/archive/p/markdown-downheader/ [p2]: https://github.com/cprieto/mdx_downheader diff --git a/mdx_headdown/__init__.py b/mdx_headdown/__init__.py index 2fdd94e..5c9b75f 100644 --- a/mdx_headdown/__init__.py +++ b/mdx_headdown/__init__.py @@ -11,6 +11,7 @@ # Import relevant modules. from markdown.extensions import Extension from markdown.postprocessors import Postprocessor +from xml.etree import ElementTree as ET import re name = "mdx_headdown" @@ -22,13 +23,20 @@ def run(self, text): self.offset = abs(int(self.config['offset'])) # Match headings 1-6 case insensitively, and capture the heading number. - heading_pattern = re.compile(r'([^<]*)', re.I) + heading_pattern = re.compile(r']*>([^<]*)', re.I) return re.sub(heading_pattern, self.downgrade, text) - def downgrade(self, mo): - return '%(content)s' % \ - {'level': min(6, int(mo.group(1)) + self.offset), 'content': mo.group(2) } + def downgrade(self, match): + element = ET.fromstring(match.group(0)) + + # Only process this heading if 'headdown="1"' (or missing...) + if element.attrib.get('headdown', 1) == 1: + # For all headings, increase their heading number by `offset`. + # If the new heading number is > 6, use 6 instead. + element.tag = 'h%d' % min(6, int(match.group(1))+self.offset) + + return ET.tostring(element, encoding="unicode") class DowngradeHeadingsExtension(Extension): """ Setup the extension for usage. """