From eb8fdb1a467db1323d1cdc62813d2d84ec148cb6 Mon Sep 17 00:00:00 2001 From: randogoth Date: Fri, 19 May 2023 11:46:15 +0300 Subject: [PATCH] added formatting settings --- README.md | 41 ++++++++++++++++++++++++++++++++++++++++- plugin/micron/micron.py | 35 ++++++++++++++++++++--------------- 2 files changed, 60 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index adbb635..44a21a0 100644 --- a/README.md +++ b/README.md @@ -62,4 +62,43 @@ Copy the generated .mu files from the output folder to the NN node's `pages` fol $ cp -R output/* ~/.nomadnetwork/storage/pages ``` -Run your node and serve the pages.... \ No newline at end of file +Run your node and serve the pages.... + +## Additional Formatting + +Since micron templates do not support styling like CSS and Markdown is a strict content-only markup language all the color styling needs to be done using micron tags in the template files themselves. + +Micron-blog offers a few Settings that allow to set enclosing tags for some of the inline and block elements that are rendered by Pelican: + +```Python +MICRON_EMPHASIS_FORMAT = [] +MICRON_STRONG_FORMAT = [] +MICRON_LINK_FORMAT = [] +MICRON_CODE_FORMAT = [] +MICRON_HEADER_FORMAT = [] +MICRON_QUOTE_FORMAT = [] +MICRON_LIST_FORMAT = [] +``` + +Each of them taks a list of exactly two tag definitions, one for the left and one for the right enclosing tag. For example, if you want to define all rendered links to be blue and underlined and all emphasized text to be yellow you could define the following settings in `pelicanconfig.py`: + +```Python +MICRON_LINK_FORMAT = ['`F00f`_', '`_`f'] +MICRON_EMPHASIS_FORMAT = ['`Fff0', '`f'] +``` + +Now links created as: +```Markdown +[title](url) +*emphasized text* +``` +will be rendered as: +``` +`F00f`_`[title`url]`_`f +`Fff0`*emphasized text`*`f +``` + +If you want the same settings to be honoured by the templates, they need to be implemented respecively. The settings are accessible by the templates: +```jinja +{{ MICRON_LINK_FORMAT[0] }}`[{{ p.title }}`:/{{ p.url }}]{{ MICRON_LINK_FORMAT[1] }} +``` \ No newline at end of file diff --git a/plugin/micron/micron.py b/plugin/micron/micron.py index 71bdb62..e6fd54e 100644 --- a/plugin/micron/micron.py +++ b/plugin/micron/micron.py @@ -63,6 +63,7 @@ def render_underlined_html(self, token, state) -> str: class MicronRenderer(MarkdownRenderer): """A renderer to format Micron text.""" NAME = 'micron' + settings = {} def __call__(self, tokens, state: BlockState): out = self.render_tokens(tokens, state) @@ -78,31 +79,30 @@ class MicronRenderer(MarkdownRenderer): return token['raw'] def emphasis(self, token: Dict[str, Any], state: BlockState) -> str: - return '`*' + self.render_children(token, state) + '`*' + fpre, fpost = self.settings.get('MICRON_EMPHASIS_FORMAT', ['', '']) + return fpre + '`*' + self.render_children(token, state) + '`*' + fpost def strong(self, token: Dict[str, Any], state: BlockState) -> str: - return '`!' + self.render_children(token, state) + '`!' + fpre, fpost = self.settings.get('MICRON_STRONG_FORMAT', ['', '']) + return fpre + '`!' + self.render_children(token, state) + '`!' + fpost def link(self, token: Dict[str, Any], state: BlockState) -> str: + fpre, fpost = self.settings.get('MICRON_LINK_FORMAT', ['', '']) label = token.get('label') text = self.render_children(token, state) - out = '`[' + text + '`' - if label: - return out + '`[' + label + '`' + out = fpre + out += '`[' + text + '`' attrs = token['attrs'] url = attrs['url'] - if text == url: - return '`[' + text + '`' - elif 'mailto:' + text == url: - return '`[' + text + '`' out += url - return out + ']' + return out + ']' + fpost def image(self, token: Dict[str, Any], state: BlockState) -> str: return self.link(token, state) def codespan(self, token: Dict[str, Any], state: BlockState) -> str: - return '`=' + token['raw'] + '`=' + fpre, fpost = self.settings.get('MICRON_CODE_FORMAT', ['', '']) + return fpre + '`=' + token['raw'] + '`=' + fpost def linebreak(self, token: Dict[str, Any], state: BlockState) -> str: return ' \n' @@ -121,12 +121,14 @@ class MicronRenderer(MarkdownRenderer): return text + '\n\n' def heading(self, token: Dict[str, Any], state: BlockState) -> str: + fpre, fpost = self.settings.get('MICRON_HEADER_FORMAT', ['', '']) level = token['attrs']['level'] if level > 3: level = 3 marker = '>' * level text = self.render_children(token, state) - return marker + ' ' + text + '\n\n' + return fpre + marker + ' ' + text + fpost + '\n\n' + def thematic_break(self, token: Dict[str, Any], state: BlockState) -> str: return '-\n\n' @@ -134,15 +136,17 @@ class MicronRenderer(MarkdownRenderer): return self.render_children(token, state) + '\n' def block_code(self, token: Dict[str, Any], state: BlockState) -> str: + fpre, fpost = self.settings.get('MICRON_CODE_FORMAT', ['', '']) code = token['raw'] if code and code[-1] != '\n': code += '\n' marker = '`=' - return marker + '\n' + code + marker + '\n\n' + return fpre + marker + '\n' + code + marker + fpost + '\n\n' def block_quote(self, token: Dict[str, Any], state: BlockState) -> str: + fpre, fpost = self.settings.get('MICRON_QUOTE_FORMAT', ['', '']) text = indent(self.render_children(token, state), '>>>>') - return text + '\n\n' + return fpre + text + fpost + '\n\n' def block_html(self, token: Dict[str, Any], state: BlockState) -> str: return '' @@ -151,7 +155,8 @@ class MicronRenderer(MarkdownRenderer): return '' def list(self, token: Dict[str, Any], state: BlockState) -> str: - return render_list(self, token, state) + fpre, fpost = self.settings.get('MICRON_LIST_FORMAT', ['', '']) + return fpre + render_list(self, token, state) + fpost # === M A R K D O W N T O M I C R O N R E A D E R ===