forked from nixjdm/lektor-asciidoc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lektor_asciidoctor.py
49 lines (35 loc) · 1.27 KB
/
lektor_asciidoctor.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
41
42
43
44
45
46
47
48
49
# -*- coding: utf-8 -*-
import sys, os
from subprocess import PIPE, Popen
from lektor.pluginsystem import Plugin
from lektor.types import Type
def asciidoctor_to_html(text):
if os.name == 'nt':
p = Popen(['asciidoctor.bat', '-s','-'], stdin=PIPE, stdout=PIPE, stderr=PIPE)
else:
p = Popen(['asciidoctor', '-s','-'], stdin=PIPE, stdout=PIPE, stderr=PIPE)
if sys.version_info[0] < 3:
out, err = p.communicate(text)
else:
out, err = p.communicate(text.encode('utf-8'))
if p.returncode != 0:
raise RuntimeError('asciidoctor: "%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):
if sys.version_info[0] < 3:
return HTML(asciidoctor_to_html(raw.value or u''))
else:
return HTML(asciidoctor_to_html(raw.value or u'').decode('utf-8'))
class AsciiDoctorPlugin(Plugin):
name = u'AsciiDoctor'
description = u'Adds AsciiDoc field type to Lektor.'
def on_setup_env(self, **extra):
self.env.add_type(AsciiDocType)