-
Notifications
You must be signed in to change notification settings - Fork 23
/
flatpandoc.py
115 lines (93 loc) · 3.01 KB
/
flatpandoc.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
"""
flask_flatpages_pandoc
~~~~~~~~~~~~~~~~~~~~~~
Flask-FlatPages-Pandoc is an HTML renderer for Flask-FlatPages that uses
pandoc as backend.
:copyright: (c) 2014 Fabian Hirschmann <fabian@hirschmann.email>
:license: MIT, see LICENSE.txt for more details.
With some changes by @apas:
- Invoke pandoc via pypandoc instead subprocess
- Indentation changes
- Support of Pandoc 2.0 by @ThoseGrapefruits
- Support of Python 3 by @frstp64
License: MIT
"""
import pkg_resources
import pypandoc
from flask import render_template_string, Markup
try:
__version__ = pkg_resources.require("Flask-FlatPages-Pandoc")[0]
except pkg_resources.DistributionNotFound:
__version__ = "0.0-dev"
class FlatPagesPandoc(object):
"""
Class that, when applied to a :class:`flask.Flask` instance,
sets up an HTML renderer using pandoc.
"""
def __init__(self, source_format, app=None, pandoc_args=[],
pre_render=False):
"""
Initializes Flask-FlatPages-Pandoc.
:param source_format: the source file format; directly passed
to pandoc.
:type source_format: string
:param app: your application. Can be omitted if you call
:meth:`init_app` later.
:type app: :class:`flask.Flask`
:param pandoc_args: extra arguments passed to pandoc
:type pandoc_args: sequence
:param pre_render: pre-render the page as :class:`flask.Markup`
:type pre_render: boolean
"""
self.source_format = source_format
self.pandoc_args = pandoc_args
self.pre_render = pre_render
if app:
self.init_app(app)
def init_app(self, app):
"""
Used to initialize an application. This is useful when passing
an app later.
:param app: your application
:type app: :class:`flask.Flask`
"""
self.app = app
# The following lambda expression works around Flask-FlatPage's
# reflection magic.
self.app.config["FLATPAGES_HTML_RENDERER"] = lambda t: self.renderer(t)
def renderer(self, text):
"""
Renders a flat page to HTML.
:param text: the text of the flat page
:type text: string
"""
#if type(text) == str:
# text = str(text, self.app.config["FLATPAGES_ENCODING"])
if self.pre_render:
text = render_template_string(Markup(text))
extra_args = [
"--filter=pandoc-crossref",
"--filter=pandoc-citeproc",
"--filter=pandoc-sidenote",
"--standalone",
"--mathml",
"--base-header-level=2",
"--highlight-style", "pygments",
"--bibliography=pages/all.bib",
"--csl=pages/lncs.csl",
"-Mreference-section-title=References",
"-Mlink-citations=true"
]
pandocver = int(pypandoc.get_pandoc_version()[0])
if pandocver < 2:
extra_args.append("-S")
format_str = "markdown+raw_tex+yaml_metadata_block"
else:
format_str = "markdown+raw_tex+smart+yaml_metadata_block+header_attributes"
output = pypandoc.convert_text(
text.encode("utf8"),
'html',
format = format_str,
extra_args=extra_args
)
return output