Implement customising left margin and contents margin

This commit is contained in:
RunasSudo 2021-09-02 02:13:53 +10:00
parent 1cd9201d59
commit d213caee2d
Signed by: RunasSudo
GPG Key ID: 7234E476BF21C61A
2 changed files with 14 additions and 5 deletions

View File

@ -40,6 +40,9 @@ with renderer_cls() as renderer:
doc.author = rawdoc.get('author', '') doc.author = rawdoc.get('author', '')
doc.footer = rawdoc.get('footer', '') doc.footer = rawdoc.get('footer', '')
doc.lmarg = rawdoc.get('lmarg', '1cm')
doc.contentsmarg = rawdoc.get('contentsmarg', '1cm')
doc.full_label_map = {} doc.full_label_map = {}
# Preprocess custom tokens # Preprocess custom tokens

View File

@ -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
@ -19,7 +19,7 @@ import hashlib
import mistletoe import mistletoe
import mistletoe.latex_renderer import mistletoe.latex_renderer
LMARG = '1cm' doc = None
def format(fstr, *args, **kwargs): def format(fstr, *args, **kwargs):
fstr2 = fstr fstr2 = fstr
@ -29,7 +29,8 @@ def format(fstr, *args, **kwargs):
fstr2 = fstr2.replace('<', '{') fstr2 = fstr2.replace('<', '{')
fstr2 = fstr2.replace('>', '}') fstr2 = fstr2.replace('>', '}')
kwargs['lmarg'] = LMARG kwargs['lmarg'] = doc.lmarg
kwargs['contentsmarg'] = doc.contentsmarg
return fstr2.format(*args, **kwargs) return fstr2.format(*args, **kwargs)
@ -52,6 +53,7 @@ class LaTeXRenderer(mistletoe.latex_renderer.LaTeXRenderer):
def render_raw_text(self, token): def render_raw_text(self, token):
result = super().render_raw_text(token) result = super().render_raw_text(token)
result = result.replace('%', r'\%')
result = result.replace('★★★', r'\texorpdfstring{\freeserif ★★★}{★★★}') result = result.replace('★★★', r'\texorpdfstring{\freeserif ★★★}{★★★}')
return result return result
@ -135,10 +137,11 @@ class LaTeXRenderer(mistletoe.latex_renderer.LaTeXRenderer):
if token.level == 3: if token.level == 3:
# Section # Section
heading_last, self.heading_last = self.heading_last, False heading_last, self.heading_last = self.heading_last, False
return format(r'{<pagebreak>\par<hyperlink>\leftskip=\quotemargin\bfseries\makebox[<lmarg>][l]{<label>}<content>\phantomsection\addcontentsline{toc}{subsubsection}{\protect\numberline{<label>} <content>}\nopagebreak\par}', return format(r'{<pagebreak>\par<hyperlink>\leftskip=\quotemargin\bfseries\parbox[t]{<lmarg>}{<label_rendered>}<content>\phantomsection\addcontentsline{toc}{subsubsection}{\protect\numberline{<label>} <content>}\nopagebreak\par}',
pagebreak=r'\pagebreak[3]' if not heading_last else '', pagebreak=r'\pagebreak[3]' if not heading_last else '',
hyperlink=hyperlink, hyperlink=hyperlink,
label=token.label, label=token.label,
label_rendered=token.label.replace('', r'\\'),
content=self.render_inner(token) content=self.render_inner(token)
) )
@ -265,6 +268,9 @@ class LaTeXRenderer(mistletoe.latex_renderer.LaTeXRenderer):
return self.render_inner(token) return self.render_inner(token)
def render_document(self, token): def render_document(self, token):
global doc
doc = token
template = r''' template = r'''
\documentclass[a4paper,12pt]{article} \documentclass[a4paper,12pt]{article}
<packages> <packages>
@ -290,7 +296,7 @@ class LaTeXRenderer(mistletoe.latex_renderer.LaTeXRenderer):
\newcommand{\toc@sectionfalse}{\setcounter{toc@section}{0}} \newcommand{\toc@sectionfalse}{\setcounter{toc@section}{0}}
\titlecontents{section}[0pt]{\vspace{0.21cm}\toc@sectionfalse\bfseries}{}{\uppercase}{\titlerule*[1pc]{.}\contentspage}[] \titlecontents{section}[0pt]{\vspace{0.21cm}\toc@sectionfalse\bfseries}{}{\uppercase}{\titlerule*[1pc]{.}\contentspage}[]
\titlecontents{subsection}[0pt]{\vspace{0.21cm}\bfseries\scshape}{}{\toc@sectionfalse}{\titlerule*[1pc]{.}\contentspage}[] \titlecontents{subsection}[0pt]{\vspace{0.21cm}\bfseries\scshape}{}{\toc@sectionfalse}{\titlerule*[1pc]{.}\contentspage}[]
\dottedcontents{subsubsection}[\dimexpr<lmarg>+0.5cm\relax]{\ifnum\value{toc@section}=0\vspace{0.21cm}\fi\toc@sectiontrue}{1cm}{1pc}[] \dottedcontents{subsubsection}[\dimexpr<contentsmarg>+0.5cm\relax]{\ifnum\value{toc@section}=0\vspace{0.21cm}\fi\toc@sectiontrue}{<contentsmarg>}{1pc}[]
\makeatother \makeatother
% Fonts % Fonts
\usepackage[math-style=ISO, bold-style=ISO]{unicode-math} \usepackage[math-style=ISO, bold-style=ISO]{unicode-math}