[tor-commits] [lego/master] Add plugin for embedding markdown in HTML templates

gus at torproject.org gus at torproject.org
Mon Jul 26 13:26:21 UTC 2021


commit dd51825c4aa4d15c50ec27245aa734f1df840f5c
Author: kez <kez at torproject.org>
Date:   Fri Jul 9 14:32:26 2021 -0400

    Add plugin for embedding markdown in HTML templates
---
 packages/lektor-md-tag/.gitignore             |  5 ++++
 packages/lektor-md-tag/LICENSE                |  3 +++
 packages/lektor-md-tag/README.md              | 35 ++++++++++++++++++++++++
 packages/lektor-md-tag/lektor_markdown_tag.py | 20 ++++++++++++++
 packages/lektor-md-tag/setup.cfg              |  2 ++
 packages/lektor-md-tag/setup.py               | 38 +++++++++++++++++++++++++++
 6 files changed, 103 insertions(+)

diff --git a/packages/lektor-md-tag/.gitignore b/packages/lektor-md-tag/.gitignore
new file mode 100644
index 0000000..463960b
--- /dev/null
+++ b/packages/lektor-md-tag/.gitignore
@@ -0,0 +1,5 @@
+dist
+build
+*.pyc
+*.pyo
+*.egg-info
diff --git a/packages/lektor-md-tag/LICENSE b/packages/lektor-md-tag/LICENSE
new file mode 100644
index 0000000..14fb13d
--- /dev/null
+++ b/packages/lektor-md-tag/LICENSE
@@ -0,0 +1,3 @@
+Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted.
+
+THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
diff --git a/packages/lektor-md-tag/README.md b/packages/lektor-md-tag/README.md
new file mode 100644
index 0000000..6e58c92
--- /dev/null
+++ b/packages/lektor-md-tag/README.md
@@ -0,0 +1,35 @@
+# Lektor markdown tag
+
+Embed markdown in a lektor template
+
+## Installation
+
+If you're using this, you're probably working on a [Tor project lektor site](https://gitlab.torproject.org/tpo/web) that uses [lego](https://gitlab.torproject.org/tpo/web/lego/). In that case, you don't need to do anything special after running
+```sh
+git submodule update --init --recursive
+```
+
+If you're *not* working on a TPO site, you can clone the [lego repo](https://gitlab.torproject.org/tpo/web/lego/) and copy the `packages/lektor-md-tag` directory to your lektor site's `packages`. Here's a small script to do that for you:
+```sh
+# run this in your lektor site directory
+git clone --depth 1 https://gitlab.torproject.org/tpo/web/lego
+mkdir -p packages
+mv lego/packages/lektor-md-tag packages
+rm -rf lego
+```
+
+## Usage
+
+From within your Lektor HTML template:
+```jinja
+{{ md('**This is bold!**') }}
+```
+
+This jinja function *will* wrap paragraphs in a set of `<p></p>` tags; don't add use this tag inside an HTML paragraph. The above snippet renders like this:
+```html
+<p><strong>This is bold!</strong></p>
+```
+
+## License
+
+[0BSD](https://opensource.org/licenses/0BSD). I cannot realistically be bothered to deal with copyright and licensing, have fun!
diff --git a/packages/lektor-md-tag/lektor_markdown_tag.py b/packages/lektor-md-tag/lektor_markdown_tag.py
new file mode 100644
index 0000000..206c1dd
--- /dev/null
+++ b/packages/lektor-md-tag/lektor_markdown_tag.py
@@ -0,0 +1,20 @@
+# -*- coding: utf-8 -*-
+
+#Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted.
+#
+#THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+
+from lektor.markdown import markdown_to_html
+from lektor.pluginsystem import Plugin
+
+import markupsafe
+
+
+class MarkdownTagPlugin(Plugin):
+    name = 'lektor markdown tag'
+    description = 'Embed markdown in a lektor template'
+
+    def on_setup_env(self, **extra):
+        def md(markdown_str: str):
+            return markupsafe.Markup(markdown_to_html(markdown_str)[0])
+        self.env.jinja_env.globals.update(md=md)
diff --git a/packages/lektor-md-tag/setup.cfg b/packages/lektor-md-tag/setup.cfg
new file mode 100644
index 0000000..3c6e79c
--- /dev/null
+++ b/packages/lektor-md-tag/setup.cfg
@@ -0,0 +1,2 @@
+[bdist_wheel]
+universal=1
diff --git a/packages/lektor-md-tag/setup.py b/packages/lektor-md-tag/setup.py
new file mode 100644
index 0000000..175582b
--- /dev/null
+++ b/packages/lektor-md-tag/setup.py
@@ -0,0 +1,38 @@
+import ast
+import io
+import re
+
+from setuptools import setup, find_packages
+
+with io.open('README.md', 'rt', encoding="utf8") as f:
+    readme = f.read()
+
+_description_re = re.compile(r'description\s+=\s+(?P<description>.*)')
+
+with open('lektor_markdown_tag.py', 'rb') as f:
+    description = str(ast.literal_eval(_description_re.search(
+        f.read().decode('utf-8')).group(1)))
+
+setup(
+    author='',
+    author_email='',
+    description=description,
+    keywords='Lektor plugin',
+    license='0BSD',
+    long_description=readme,
+    long_description_content_type='text/markdown',
+    name='lektor-markdown-tag',
+    packages=find_packages(),
+    py_modules=['lektor_markdown_tag'],
+    # url='[link to your repository]',
+    version='0.1',
+    classifiers=[
+        'Framework :: Lektor',
+        'Environment :: Plugins',
+    ],
+    entry_points={
+        'lektor.plugins': [
+            'markdown-tag = lektor_markdown_tag:MarkdownTagPlugin',
+        ]
+    }
+)





More information about the tor-commits mailing list