Commit 4bf54882 authored by Patrik Dufresne's avatar Patrik Dufresne

Merge branch 'develop/gitlab' into 'master'

Develop/gitlab

See merge request !1
parents 1b126475 9d8ab48c
Pipeline #247 passed with stages
in 1 minute and 14 seconds
image: ikus060/docker-debian-py2-py3:stretch
stages:
- test
- publish
test:py27:
stage: test
script:
# Register the plugin entry point and run the test
- python2 setup.py develop
- python2 setup.py nosetests
test:py3:
stage: test
script:
# Register the plugin entry point and run the test
- python3 setup.py develop
- python3 setup.py nosetests
publish_pypi:
stage: publish
only:
- tags
script:
- pip install wheel twine --upgrade
- python setup.py sdist bdist_wheel
- twine upload dist/* -u $PYPI_USR -p $PYPI_PWD
github_push:
stage: publish
only:
- branches
script:
- git push --force https://${GITHUB_USR}:${GITHUB_PWD}@github.com/ikus060/lektor-python-markdown.git refs/remotes/origin/${CI_COMMIT_REF_NAME}:refs/heads/${CI_COMMIT_REF_NAME}
- git push https://${GITHUB_USR}:${GITHUB_PWD}@github.com/ikus060/lektor-python-markdown.git --tags
eclipse.preferences.version=1
encoding/lektor_asciidoc.py=utf-8
encoding/lektor_pythonmarkdown.py=utf-8
# Lektor AsciiDoc Plugin
# Lektor Python-Markdown Plugin
A [Lektor](https://www.getlektor.com/) plugin to add an AsciiDoc field type.
[![pipeline status](http://git.patrikdufresne.com/pdsl/lektor-python-markdown/badges/master/pipeline.svg)](http://git.patrikdufresne.com/pdsl/lektor-python-markdown/commits/master)
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-pythonmarkdown
```
## Usage
In your model, you need to define the type of field as follow:
```
[model]
name = Page
[fields.body]
label = Body
type = pythonmarkdown
```
# Warning !
This plugins is is obviously incompatible with all of the mistune-specific events and plugins. Namely, all of `markdown-*` events and the plugins built around them.
# Advance configuration
This lektor plugins provide a nice way to configure python-markdown. For instance, it's possible to explicitly define the extentions to be enabled and to configure each of them seperatly.
For an advance configuration, you need to create a file named `pythonmarkdown.ini` in the `configs` folder.
In that file you may write something similar to the following:
```
lektor plugins add lektor-asciidoc
[markdown]
# Define the configuration of python-markdown.
# Reference: https://python-markdown.github.io/reference/#markdown
#output_format = xhtml1
#tab_length = 4
#safe_mode = False
#enable_attributes = False
#smart_emphasis = True
#lazy_ol = True
[extensions]
# List extensions to be enabled.
markdown.extensions.extra = 1
markdown.extensions.admonition = 1
markdown.extensions.codehilite = 1
markdown.extensions.headerid = 1
markdown.extensions.meta = 1
markdown.extensions.nl2br = 1
markdown.extensions.sane_lists = 1
markdown.extensions.smarty = 1
markdown.extensions.toc = 1
markdown.extensions.wikilinks = 1
[markdown.extensions.codehilite]
# Specific configuration for an extension.
# Reference: https://python-markdown.github.io/extensions/code_hilite/#usage
linenums = True
#guess_lang = True
#css_class = codehilite
#pygments_style = default
#noclasses = False
#use_pygments = True
```
# -*- 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)
# -*- coding: utf-8 -*-
'''
Created on Jun 8, 2018
@author: Patrik Dufresne
'''
from lektor.pluginsystem import Plugin
from lektor.types import Type
import markdown
SECTION_EXTENSIONS = "extensions"
SECTION_MARKDOWN = "markdown"
DEFAULT_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, cfg):
# TODO Call events.
return markdown.markdown(text, **cfg.options)
# 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
def _value(v):
"This function tries to convert the configuration value to a sane type."
if v.lower() in ['true', 'false']:
return v.lower() == 'true'
try:
return int(v)
except:
return v
class PythonMarkdownConfig(object):
"""
Define configuration of python-markdown.
"""
def _section_as_dict(self, name):
return {k: _value(v) for k, v in self.plugin_config.section_as_dict(name).items()}
def _extensions(self):
return [
e
for e, v in (self._section_as_dict(SECTION_EXTENSIONS) or DEFAULT_EXTENTIONS).items()
if v]
def __init__(self, plugin_config):
self.plugin_config = plugin_config
self.options = self._section_as_dict(SECTION_MARKDOWN)
self.options.update({
"extensions": self._extensions(),
"extension_configs": {e: self._section_as_dict(e) for e in self._extensions()},
})
class PythonMarkdownPlugin(Plugin):
name = u'pythonmarkdown'
description = u'Adds AsciiDoc field type to Lektor.'
def on_setup_env(self, **extra):
plugin_config = self.get_config()
# We declare this type as an internal class inside the plugin to get
# access to the get_config() function from the Type.
#
# 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):
"""
Called to convert the raw value (markdown) into html.
"""
cfg = PythonMarkdownConfig(plugin_config)
return HTML(pythonmarkdown_to_html(raw.value or u'', cfg))
self.env.add_type(PythonMarkdownType)
......@@ -9,23 +9,25 @@ with io.open('README.md', 'rt', encoding="utf8") as f:
_description_re = re.compile(r'description\s+=\s+(?P<description>.*)')
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'],
tests_require=['pytest'],
version='0.3',
url='https://github.com/nixjdm/lektor-asciidoc',
name='lektor-pythonmarkdown',
py_modules=['lektor_pythonmarkdown'],
install_requires=['markdown'],
tests_require=['lektor'],
setup_requires=['setuptools_scm'],
use_scm_version=True,
url='https://github.com/ikus060/lektor-python-markdown',
classifiers=[
'Environment :: Plugins',
'Environment :: Web Environment',
......@@ -35,7 +37,7 @@ setup(
],
entry_points={
'lektor.plugins': [
'asciidoc = lektor_asciidoc:AsciiDocPlugin',
'pythonmarkdown = lektor_pythonmarkdown:PythonMarkdownPlugin',
]
}
)
[markdown]
#output_format = xhtml1
#tab_length = 4
#safe_mode = False
#enable_attributes = False
#smart_emphasis = True
#lazy_ol = True
[extensions]
markdown.extensions.extra = 1
markdown.extensions.admonition = 1
markdown.extensions.codehilite = 1
markdown.extensions.headerid = 1
markdown.extensions.meta = 1
markdown.extensions.nl2br = 1
markdown.extensions.sane_lists = 1
markdown.extensions.smarty = 1
markdown.extensions.toc = 1
markdown.extensions.wikilinks = 1
[markdown.extensions.codehilite]
linenums = True
#guess_lang = True
#css_class = codehilite
#pygments_style = default
#noclasses = False
#use_pygments = True
body:
== Header 1
# Header 1
Some text.
## Header 2 {: .customclass}
-----
```
code here
-----
```
......@@ -3,4 +3,4 @@ name = Page
[fields.body]
label = Body
type = asciidoc
type = pythonmarkdown
'''
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 '<h2 id="_header_1">Header 1</h2>' in html
assert '<pre>code here</pre>' in html
print(html)
assert '<h1 id="header-1">Header 1</h1>' in html
assert '<h2 class="customclass" id="header-2">Header 2</h2>' in html
# The output changes depending on the version of python-markdown uses.
assert '<pre class="codehilite"><code class="linenums">code here</code></pre>' in html
if __name__ == "__main__":
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment