Allow subrules to have multiple levels (1.1, 1.2, 2.1, ... style)
This commit is contained in:
parent
ae62f90bf8
commit
1fcea09a59
@ -188,10 +188,15 @@ class LaTeXRenderer(mistletoe.latex_renderer.LaTeXRenderer):
|
|||||||
else:
|
else:
|
||||||
hyperlink = '{}'
|
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,
|
hyperlink=hyperlink,
|
||||||
parskip=r'\parskip',# if token.level <= 1 else '0cm',
|
parskip=r'\parskip',# if token.level <= 1 else '0cm',
|
||||||
label=token.label,
|
labels=''.join(rendered_labels),
|
||||||
|
numlabels=len(rendered_labels),
|
||||||
level=token.level,
|
level=token.level,
|
||||||
content=content,
|
content=content,
|
||||||
pagebreak=r'\nopagebreak' if content.strip().endswith(':') else ''
|
pagebreak=r'\nopagebreak' if content.strip().endswith(':') else ''
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
# legalmd: Markdown-based legal markup
|
# 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
|
# 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
|
# 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)
|
mistletoe.block_token.add_token(NumberedHeading)
|
||||||
|
|
||||||
class Subrules(mistletoe.block_token.BlockToken):
|
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):
|
def __init__(self, children):
|
||||||
self.children = children
|
self.children = children
|
||||||
@ -90,10 +90,11 @@ class Subrules(mistletoe.block_token.BlockToken):
|
|||||||
|
|
||||||
return children
|
return children
|
||||||
|
|
||||||
|
mistletoe.block_token.remove_token(mistletoe.block_token.List)
|
||||||
mistletoe.block_token.add_token(Subrules)
|
mistletoe.block_token.add_token(Subrules)
|
||||||
|
|
||||||
class SubrulesItem(mistletoe.block_token.BlockToken):
|
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):
|
def __init__(self, level, label, children):
|
||||||
self.level = level
|
self.level = level
|
||||||
@ -142,7 +143,10 @@ class SubrulesItem(mistletoe.block_token.BlockToken):
|
|||||||
cur_level = child.level
|
cur_level = child.level
|
||||||
|
|
||||||
# Section
|
# 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)
|
labels.append(section.label)
|
||||||
|
|
||||||
return ''.join(reversed(labels))
|
return ''.join(reversed(labels))
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
# legalmd: Markdown-based legal markup
|
# 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
|
# 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
|
# 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)
|
content = self.render_inner(token)
|
||||||
|
|
||||||
if token.label:
|
if token.label:
|
||||||
|
# TODO: Multiple label levels
|
||||||
|
|
||||||
return format(r'{\fi<fi>\li<li><pagebreak> <label>\tab <content>\par}',
|
return format(r'{\fi<fi>\li<li><pagebreak> <label>\tab <content>\par}',
|
||||||
fi=cm_to_twip(-1),
|
fi=cm_to_twip(-1),
|
||||||
li=cm_to_twip(LMARG + token.level + 1)+self.quotemargin,
|
li=cm_to_twip(LMARG + token.level + 1)+self.quotemargin,
|
||||||
|
Loading…
Reference in New Issue
Block a user