pandoc.py (view raw)
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 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 |
__author__ = "Peter Molnar"
__copyright__ = "Copyright 2017-2019, Peter Molnar"
__license__ = "apache-2.0"
__maintainer__ = "Peter Molnar"
__email__ = "mail@petermolnar.net"
import subprocess
import logging
from tempfile import gettempdir
import hashlib
import os
import settings
class Pandoc(str):
in_format = 'html'
in_options = []
out_format = 'plain'
out_options = []
columns = None
@property
def hash(self):
return str(hashlib.sha1(self.source.encode()).hexdigest())
@property
def cachefile(self):
return os.path.join(
settings.tmpdir,
"%s_%s.pandoc" % (
self.__class__.__name__,
self.hash
)
)
@property
def cache(self):
if not os.path.exists(self.cachefile):
return False
with open(self.cachefile, 'rt') as f:
self.result = f.read()
return True
def __init__(self, text):
self.source = text
if self.cache:
return
conv_to = '--to=%s' % (self.out_format)
if (len(self.out_options)):
conv_to = '%s+%s' % (
conv_to,
'+'.join(self.out_options)
)
conv_from = '--from=%s' % (self.in_format)
if (len(self.in_options)):
conv_from = '%s+%s' % (
conv_from,
'+'.join(self.in_options)
)
is_pandoc_version2 = False
try:
version = subprocess.check_output(['pandoc', '-v'])
if version.startswith(b'pandoc 2'):
is_pandoc_version2 = True
except OSError:
print("Error: pandoc is not installed!")
cmd = [
'pandoc',
'-o-',
conv_to,
conv_from,
'--no-highlight'
]
if is_pandoc_version2:
# Only pandoc v2 and higher support quiet param
cmd.append('--quiet')
if self.columns:
cmd.append(self.columns)
p = subprocess.Popen(
tuple(cmd),
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)
stdout, stderr = p.communicate(input=text.encode())
if stderr:
logging.warning(
"Error during pandoc covert:\n\t%s\n\t%s",
cmd,
stderr
)
r = stdout.decode('utf-8').strip()
self.result = r
with open(self.cachefile, 'wt') as f:
f.write(self.result)
def __str__(self):
return str(self.result)
def __repr__(self):
return str(self.result)
class PandocMD2HTML(Pandoc):
in_format = 'markdown'
in_options = [
'footnotes',
'pipe_tables',
'strikeout',
# 'superscript',
# 'subscript',
'raw_html',
'definition_lists',
'backtick_code_blocks',
'fenced_code_attributes',
'shortcut_reference_links',
'lists_without_preceding_blankline',
'autolink_bare_uris',
]
out_format = 'html5'
out_options = []
class PandocHTML2MD(Pandoc):
in_format = 'html'
in_options = []
out_format = 'markdown'
out_options = [
'footnotes',
'pipe_tables',
'strikeout',
'raw_html',
'definition_lists',
'backtick_code_blocks',
'fenced_code_attributes',
'shortcut_reference_links',
'lists_without_preceding_blankline',
'autolink_bare_uris',
]
class PandocMD2TXT(Pandoc):
in_format = 'markdown'
in_options = [
'footnotes',
'pipe_tables',
'strikeout',
'raw_html',
'definition_lists',
'backtick_code_blocks',
'fenced_code_attributes',
'shortcut_reference_links',
'lists_without_preceding_blankline',
'autolink_bare_uris',
]
out_format = 'plain'
out_options = []
columns = '--columns=80'
class PandocHTML2TXT(Pandoc):
in_format = 'html'
in_options = []
out_format = 'plain'
out_options = []
columns = '--columns=80'
|