Commit 2f5b92d5 authored by Patrik Dufresne's avatar Patrik Dufresne

Use Python-Markdown to convert document.

Replace all the call to asciidoc by a call to python-markdown.
parent 024020f9
......@@ -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:
......
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.
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
# -*- coding: utf-8 -*-
from subprocess import PIPE, Popen
'''
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 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
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
......@@ -25,16 +36,17 @@ class HTML(object):
return self.html
class AsciiDocType(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):
return HTML(asciidoc_to_html(raw.value or u''))
return HTML(pythonmarkdown_to_html(raw.value or u''))
class AsciiDocPlugin(Plugin):
name = u'AsciiDoc'
class PythonMarkdownPlugin(Plugin):
name = u'pythonmarkdown'
description = u'Adds AsciiDoc field type to Lektor.'
def on_setup_env(self, **extra):
self.env.add_type(AsciiDocType)
self.env.add_type(PythonMarkdownType)
......@@ -9,23 +9,24 @@ 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'],
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',
]
}
)
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>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