diff --git a/Server/pandoc.py b/Server/pandoc.py new file mode 100644 index 0000000..dd6769c --- /dev/null +++ b/Server/pandoc.py @@ -0,0 +1,127 @@ +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 + }