diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 05b2e387594c72e67766f93779b37470603886c7..7f2493494310dfec3c49d7200eedfe7b259b6d44 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -4,10 +4,6 @@ stages: - test - publish -# Upgrade python and install dependencies to avoid compiling from sources. -before_script: -- apt-get update && apt-get -qq install asciidoc - test:py27: stage: test script: diff --git a/.settings/org.eclipse.core.resources.prefs b/.settings/org.eclipse.core.resources.prefs index e4796c64abbe976215ca5c9ddc1331db1ae510f1..ad6d94ad52749fd3d3a43d794e154492e2a3df90 100644 --- a/.settings/org.eclipse.core.resources.prefs +++ b/.settings/org.eclipse.core.resources.prefs @@ -1,2 +1,2 @@ eclipse.preferences.version=1 -encoding/lektor_asciidoc.py=utf-8 +encoding/lektor_pythonmarkdown.py=utf-8 diff --git a/README.md b/README.md index 5aba1cbec1b928a99cb4b28da4800930be462234..922fb436cc585aacde466314a5fa447ece813668 100644 --- a/README.md +++ b/README.md @@ -1,11 +1,29 @@ -# Lektor AsciiDoc Plugin +# Lektor Python-Markdown Plugin -A [Lektor](https://www.getlektor.com/) plugin to add an AsciiDoc field type. +A [Lektor](https://www.getlektor.com/) plugin to parse markdown using +[Python-Markdown](https://python-markdown.github.io/). By default, lektor +uses [mistune](http://mistune.readthedocs.io/en/latest/) to parse markdown +field. +With this plugin, you can chose which parser is to be used by setting a +different type on the field. Either: `markdown` or `pythonmarkdown` ## Installation -Add lektor-asciidoc to your project from command line: +Add lektor-pythonmarkdown to your project from command line: ``` -lektor plugins add lektor-asciidoc +lektor plugins add lektor-pythonmarkdown ``` + +## Usage + +In your model, you need to define the type of field as follow: +``` +[model] +name = Page + +[fields.body] +label = Body +type = pythonmarkdown + +``` \ No newline at end of file diff --git a/lektor_asciidoc.py b/lektor_asciidoc.py deleted file mode 100644 index b650bd26e0a6d8b83ad0e24aeeee81e86c58d6ae..0000000000000000000000000000000000000000 --- a/lektor_asciidoc.py +++ /dev/null @@ -1,40 +0,0 @@ -# -*- coding: utf-8 -*- -from subprocess import PIPE, Popen - -from lektor.pluginsystem import Plugin -from lektor.types import Type - - -def asciidoc_to_html(text): - p = Popen(['asciidoc', '--no-header-footer', '--backend=html5', '-'], - stdin=PIPE, stdout=PIPE, stderr=PIPE) - - out, err = p.communicate(text) - if p.returncode != 0: - raise RuntimeError('asciidoc: "%s"' % err) - - return out - - -# Wrapper with an __html__ method prevents Lektor from escaping HTML tags. -class HTML(object): - def __init__(self, html): - self.html = html - - def __html__(self): - return self.html - - -class AsciiDocType(Type): - widget = 'multiline-text' - - def value_from_raw(self, raw): - return HTML(asciidoc_to_html(raw.value or u'')) - - -class AsciiDocPlugin(Plugin): - name = u'AsciiDoc' - description = u'Adds AsciiDoc field type to Lektor.' - - def on_setup_env(self, **extra): - self.env.add_type(AsciiDocType) diff --git a/lektor_pythonmarkdown.py b/lektor_pythonmarkdown.py new file mode 100644 index 0000000000000000000000000000000000000000..a011b00384b8523ee66f9ce689ffbfed35b8fb1d --- /dev/null +++ b/lektor_pythonmarkdown.py @@ -0,0 +1,52 @@ +# -*- coding: utf-8 -*- +''' +Created on Jun 8, 2018 + +@author: Patrik Dufresne +''' +from lektor.pluginsystem import Plugin +from lektor.types import Type +import markdown + +EXTENTIONS = [ + "markdown.extensions.extra", + "markdown.extensions.admonition", + "markdown.extensions.codehilite", + "markdown.extensions.headerid", + "markdown.extensions.meta", + "markdown.extensions.nl2br", + "markdown.extensions.sane_lists", + "markdown.extensions.smarty", + "markdown.extensions.toc", + "markdown.extensions.wikilinks", +] + + +def pythonmarkdown_to_html(text): + return markdown.markdown(text, EXTENTIONS) + + +# Wrapper with an __html__ method prevents Lektor from escaping HTML tags. +class HTML(object): + + def __init__(self, html): + self.html = html + + def __html__(self): + return self.html + + +# The name of the class is used a key for the fields type. +class PythonMarkdownType(Type): + widget = 'multiline-text' + + def value_from_raw(self, raw): + return HTML(pythonmarkdown_to_html(raw.value or u'')) + + +class PythonMarkdownPlugin(Plugin): + name = u'pythonmarkdown' + description = u'Adds AsciiDoc field type to Lektor.' + + def on_setup_env(self, **extra): + self.env.add_type(PythonMarkdownType) diff --git a/setup.py b/setup.py index 6b975021ed54e089fc0ce744287ab792600807ea..34a0c3588aad9f959bd8aa4ec68bacc3e4ce0b42 100644 --- a/setup.py +++ b/setup.py @@ -9,23 +9,24 @@ with io.open('README.md', 'rt', encoding="utf8") as f: _description_re = re.compile(r'description\s+=\s+(?P.*)') -with open('lektor_asciidoc.py', 'rb') as f: +with open('lektor_pythonmarkdown.py', 'rb') as f: description = str(ast.literal_eval(_description_re.search( f.read().decode('utf-8')).group(1))) setup( - author=u'A. Jesse Jiryu Davis', - author_email='jesse@emptysquare.net', + author='Patrik Dufresne Service Logiciel inc.', + author_email='info@patrikdufresne.com', description=description, - keywords='Lektor plugin static-site blog asciidoc', + keywords='Lektor plugin static-site blog Python-Markdown', license='MIT', long_description=readme, long_description_content_type='text/markdown', - name='lektor-asciidoc', - py_modules=['lektor_asciidoc'], + name='lektor-pythonmarkdown', + py_modules=['lektor_pythonmarkdown'], + install_requires=['markdown'], tests_require=['lektor'], version='0.3', - url='https://github.com/nixjdm/lektor-asciidoc', + url='https://github.com/ikus060/lektor-python-markdown', classifiers=[ 'Environment :: Plugins', 'Environment :: Web Environment', @@ -35,7 +36,7 @@ setup( ], entry_points={ 'lektor.plugins': [ - 'asciidoc = lektor_asciidoc:AsciiDocPlugin', + 'pythonmarkdown = lektor_pythonmarkdown:PythonMarkdownPlugin', ] } ) diff --git a/tests/demo-project/content/contents.lr b/tests/demo-project/content/contents.lr index f8223144aea0dd8753b3b5e584c9e6b2ed99ad08..570ab7b55d0f6aa5c0de3c379cc7cd8c08f21329 100644 --- a/tests/demo-project/content/contents.lr +++ b/tests/demo-project/content/contents.lr @@ -1,9 +1,9 @@ body: -== Header 1 +# Header 1 -Some text. +## Header 2 {: .customclass} ------ +``` code here ------ +``` diff --git a/tests/demo-project/models/page.ini b/tests/demo-project/models/page.ini index 42b9068165579c0278bd155d1ef14defe1b74831..e77517ecfbe5689a354493a7af46363af1b13f54 100644 --- a/tests/demo-project/models/page.ini +++ b/tests/demo-project/models/page.ini @@ -3,4 +3,4 @@ name = Page [fields.body] label = Body -type = asciidoc +type = pythonmarkdown diff --git a/tests/test_lektor_asciidoc.py b/tests/test_lektor_pythonmarkdown.py similarity index 72% rename from tests/test_lektor_asciidoc.py rename to tests/test_lektor_pythonmarkdown.py index a58f8c8e545bdebccd6ee7cbd23d562509f3e683..91c884418883dcf082895f04353d23c25982c366 100644 --- a/tests/test_lektor_asciidoc.py +++ b/tests/test_lektor_pythonmarkdown.py @@ -1,8 +1,10 @@ + ''' Created on Jun 8, 2018 -@author: ikus060 +@author: Patrik Dufresne ''' + from lektor.builder import Builder from lektor.db import Database from lektor.environment import Environment @@ -33,8 +35,11 @@ class TestLektorAsciidoc(unittest.TestCase): assert not failures page_path = os.path.join(self.builder.destination_path, 'index.html') html = open(page_path).read() - assert '

Header 1

' in html - assert '
code here
' in html + print(html) + assert '

Header 1

' in html + assert '

Header 2

' in html + # The output changes depending on the version of python-markdown uses. + assert '
code here
' in html if __name__ == "__main__":