-
Notifications
You must be signed in to change notification settings - Fork 3
/
lektor_asciidoc.py
40 lines (26 loc) · 956 Bytes
/
lektor_asciidoc.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# -*- 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)