62 lines
1.9 KiB
Python
62 lines
1.9 KiB
Python
# legalmd: Markdown-based legal markup
|
|
# Copyright © 2019 Lee Yingtong Li (RunasSudo)
|
|
#
|
|
# This program is free software: you can redistribute it and/or modify
|
|
# it under the terms of the GNU Affero General Public License as published by
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
# (at your option) any later version.
|
|
#
|
|
# This program is distributed in the hope that it will be useful,
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
# GNU Affero General Public License for more details.
|
|
#
|
|
# You should have received a copy of the GNU Affero General Public License
|
|
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
import argparse
|
|
import re
|
|
import sys
|
|
|
|
import frontmatter
|
|
import mistletoe
|
|
|
|
from . import legal_token
|
|
from . import latex_renderer, rtf_renderer
|
|
|
|
parser = argparse.ArgumentParser()
|
|
parser.add_argument('--format', default='tex', choices=['tex', 'rtf'])
|
|
parser.add_argument('filename')
|
|
args = parser.parse_args()
|
|
|
|
rawdoc = frontmatter.load(args.filename)
|
|
|
|
renderer_cls = {'tex': latex_renderer.LaTeXRenderer, 'rtf': rtf_renderer.RTFRenderer}[args.format]
|
|
|
|
with renderer_cls() as renderer:
|
|
doc = mistletoe.Document(rawdoc.content.splitlines())
|
|
|
|
doc.title = rawdoc.get('title', '')
|
|
doc.author = rawdoc.get('author', '')
|
|
doc.footer = rawdoc.get('footer', '')
|
|
|
|
doc.full_label_map = {}
|
|
|
|
# Preprocess custom tokens
|
|
def walk_elem(elem, parent=None):
|
|
elem.doc = doc
|
|
elem.parent = parent
|
|
|
|
if isinstance(elem, legal_token.NumberedHeading) or isinstance(elem, legal_token.SubrulesItem):
|
|
full_label = elem.full_label()
|
|
if full_label:
|
|
doc.full_label_map[full_label] = elem
|
|
|
|
if hasattr(elem, 'children'):
|
|
for child in elem.children:
|
|
walk_elem(child, elem)
|
|
|
|
walk_elem(doc)
|
|
|
|
print(renderer.render(doc))
|