From 071e6db304cacb47248994a119182a7cf3aa0e29 Mon Sep 17 00:00:00 2001 From: RunasSudo Date: Sun, 15 Sep 2019 00:32:48 +1000 Subject: [PATCH] Don't generate hyperlinks for un-referenceable headings/items (e.g. inside quotes) --- legalmd/latex_renderer.py | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/legalmd/latex_renderer.py b/legalmd/latex_renderer.py index 76e3ab1..bf786f9 100644 --- a/legalmd/latex_renderer.py +++ b/legalmd/latex_renderer.py @@ -93,11 +93,14 @@ class LaTeXRenderer(mistletoe.latex_renderer.LaTeXRenderer): ) def render_numbered_heading(self, token): - sha = hashlib.sha256() - sha.update(token.full_label().encode('utf-8')) - hyperlink = format(r'\hypertarget{}{}', - linkname=sha.hexdigest() - ) + if token.full_label(): + sha = hashlib.sha256() + sha.update(token.full_label().encode('utf-8')) + hyperlink = format(r'\hypertarget{}{}', + linkname=sha.hexdigest() + ) + else: + hyperlink = '{}' if token.level == 1: # Part @@ -155,11 +158,14 @@ class LaTeXRenderer(mistletoe.latex_renderer.LaTeXRenderer): def render_subrules_item(self, token): if token.label: - sha = hashlib.sha256() - sha.update(token.full_label().encode('utf-8')) - hyperlink = format(r'\hypertarget{}{}', - linkname=sha.hexdigest() - ) + if token.full_label(): + sha = hashlib.sha256() + sha.update(token.full_label().encode('utf-8')) + hyperlink = format(r'\hypertarget{}{}', + linkname=sha.hexdigest() + ) + else: + hyperlink = '{}' return format(r'\N{\par\leftskip=\dimexpr\quotemargin++cm\relax\hangindent=1cm\parskip=\makebox[][l]{