144 lines
3.2 KiB
Python
144 lines
3.2 KiB
Python
import subprocess
|
|
import json
|
|
from pathlib import Path
|
|
|
|
class Pandoc:
|
|
def __init__(self, path=None):
|
|
if path:
|
|
self.pandoc = path + "/pandoc"
|
|
else:
|
|
self.pandoc = "pandoc"
|
|
|
|
def Command(self, From, To, Input):
|
|
return subprocess.run(
|
|
[self.pandoc, "-f", From, "-t", To],
|
|
input=Input,
|
|
text=True,
|
|
capture_output=True)
|
|
|
|
def Convert(self, text):
|
|
command = self.Command("gfm", "html", text)
|
|
return command.stdout
|
|
|
|
def ConvertToJson(self, text):
|
|
command = self.Command("gfm", "json", text)
|
|
return json.loads(command.stdout)
|
|
|
|
def ConvertFromJson(self, text):
|
|
command = self.Command("json", "html", json.dumps(text));
|
|
return command.stdout
|
|
|
|
class Attr:
|
|
def __init__(self, id, classes=None, keyvalue=None):
|
|
self.id = id
|
|
|
|
if classes is None:
|
|
self.classes = []
|
|
else:
|
|
self.classes = classes
|
|
|
|
if keyvalue is None:
|
|
self.keyvalue = []
|
|
else:
|
|
self.keyvalue = keyvalue
|
|
|
|
def toJson(self):
|
|
return [
|
|
self.id,
|
|
self.classes,
|
|
self.keyvalue
|
|
]
|
|
|
|
class Div:
|
|
def __init__(self, attr, content):
|
|
self.attr = attr
|
|
self.content = content
|
|
|
|
def toJson(self):
|
|
return {
|
|
't' : 'Div',
|
|
'c' : [
|
|
self.attr.toJson(),
|
|
self.content
|
|
]
|
|
}
|
|
|
|
class Span:
|
|
def __init__(self, attr, content):
|
|
self.attr = attr
|
|
self.content = content
|
|
|
|
def toJson(self):
|
|
return {
|
|
't' : 'Span',
|
|
'c' : [
|
|
self.attr.toJson(),
|
|
self.content
|
|
]
|
|
}
|
|
|
|
class Header:
|
|
def __init__(self, attr, num, content):
|
|
self.attr = attr
|
|
self.num = num
|
|
self.content = content
|
|
|
|
def toJson(self):
|
|
return {
|
|
't' : 'Header',
|
|
'c' : [
|
|
self.num,
|
|
self.attr.toJson(),
|
|
self.content
|
|
]
|
|
}
|
|
|
|
class Plain:
|
|
def __init__(self, content):
|
|
self.content = content
|
|
|
|
def toJson(self):
|
|
return {
|
|
't' : 'Plain',
|
|
'c' : [
|
|
self.content
|
|
]
|
|
}
|
|
|
|
class Inline:
|
|
def __init__(self, t, content):
|
|
self.t = t
|
|
self.content = content
|
|
|
|
def toJson(self):
|
|
return {
|
|
't' : self.t,
|
|
'c' : self.content
|
|
}
|
|
|
|
class PString:
|
|
def __init__(self, content):
|
|
self.content = content
|
|
|
|
def toJson(self):
|
|
return {
|
|
't' : 'Str',
|
|
'c' : self.content
|
|
}
|
|
|
|
class Image:
|
|
def __init__(self, attr, caption, url):
|
|
self.attr = attr
|
|
self.caption = caption
|
|
self.url = url
|
|
|
|
def toJson(self):
|
|
return {
|
|
't' : 'Image',
|
|
'c' : [
|
|
self.attr.toJson(),
|
|
self.caption,
|
|
[self.url, ""]
|
|
]
|
|
}
|