Allow subrules to have multiple levels (1.1, 1.2, 2.1, ... style)

This commit is contained in:
RunasSudo 2021-11-30 19:37:45 +11:00
parent ae62f90bf8
commit 1fcea09a59
Signed by: RunasSudo
GPG Key ID: 7234E476BF21C61A
3 changed files with 18 additions and 7 deletions

View File

@ -188,10 +188,15 @@ class LaTeXRenderer(mistletoe.latex_renderer.LaTeXRenderer):
else:
hyperlink = '{}'
return format(r'\N{\par<hyperlink>\leftskip=\dimexpr\quotemargin+<lmarg>+<level>cm\relax\hangindent=1cm\parskip=<parskip>\makebox[<lmarg>][l]{\maxsizebox{\dimexpr<lmarg>-1mm\relax}{!}{<label>}}<content><pagebreak>\par}\N',
rendered_labels = []
for label_part in token.label.split():
rendered_labels.append(format(r'\makebox[<lmarg>][l]{\maxsizebox{\dimexpr<lmarg>-1mm\relax}{!}{<label>}}', label=label_part))
return format(r'\N{\par<hyperlink>\leftskip=\dimexpr\quotemargin+<lmarg>+<level>cm\relax\hangindent=<numlabels>cm\parskip=<parskip><labels><content><pagebreak>\par}\N',
hyperlink=hyperlink,
parskip=r'\parskip',# if token.level <= 1 else '0cm',
label=token.label,
labels=''.join(rendered_labels),
numlabels=len(rendered_labels),
level=token.level,
content=content,
pagebreak=r'\nopagebreak' if content.strip().endswith(':') else ''

View File

@ -1,5 +1,5 @@
# legalmd: Markdown-based legal markup
# Copyright © 2019 Lee Yingtong Li (RunasSudo)
# Copyright © 2019, 2021 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
@ -57,7 +57,7 @@ class NumberedHeading(mistletoe.block_token.BlockToken):
mistletoe.block_token.add_token(NumberedHeading)
class Subrules(mistletoe.block_token.BlockToken):
pattern = re.compile(r'\t*\(([0-9A-Za-z–]+)\)\s+.')
pattern = re.compile(r'\t*(\([0-9A-Za-z–]+\)|[0-9A-Za-z–]+\.)\s+.')
def __init__(self, children):
self.children = children
@ -90,10 +90,11 @@ class Subrules(mistletoe.block_token.BlockToken):
return children
mistletoe.block_token.remove_token(mistletoe.block_token.List)
mistletoe.block_token.add_token(Subrules)
class SubrulesItem(mistletoe.block_token.BlockToken):
pattern = re.compile(r'(\t*)(\([0-9A-Za-z–]+\))?\s*(.+)')
pattern = re.compile(r'(\t*)((?:\([0-9A-Za-z–]+\)|[0-9A-Za-z–]+\.)(?: \([0-9A-Za-z–]+\)| [0-9A-Za-z–]+\.)*)?\s*(.+)')
def __init__(self, level, label, children):
self.level = level
@ -142,7 +143,10 @@ class SubrulesItem(mistletoe.block_token.BlockToken):
cur_level = child.level
# Section
section = next(x for x in reversed(self.parent.parent.children[0:self.parent.parent.children.index(self.parent)]) if isinstance(x, NumberedHeading))
section = next((x for x in reversed(self.parent.parent.children[0:self.parent.parent.children.index(self.parent)]) if isinstance(x, NumberedHeading)), None)
if not section:
return None
labels.append(section.label)
return ''.join(reversed(labels))

View File

@ -1,5 +1,5 @@
# legalmd: Markdown-based legal markup
# Copyright © 2019 Lee Yingtong Li (RunasSudo)
# Copyright © 2019, 2021 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
@ -235,6 +235,8 @@ class RTFRenderer(mistletoe.base_renderer.BaseRenderer):
content = self.render_inner(token)
if token.label:
# TODO: Multiple label levels
return format(r'{\fi<fi>\li<li><pagebreak> <label>\tab <content>\par}',
fi=cm_to_twip(-1),
li=cm_to_twip(LMARG + token.level + 1)+self.quotemargin,