Skip to content

Commit

Permalink
fix the HTML5 rendering of \hyperref
Browse files Browse the repository at this point in the history
fixes plastex#316

It looks like the template for \hyperref in the HTML5 renderer was for
the \hyperref{URL}{category}{name}{text} version, but only the
\hyperref[ref]{text} version was implemented.

This commit changes the template to use the `idref` property if it's
present, which is true for the square bracket version which links to a
ref.
  • Loading branch information
christianp committed Mar 12, 2024
1 parent 61cb8a5 commit b9b603f
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 2 deletions.
2 changes: 1 addition & 1 deletion plasTeX/Renderers/HTML5/hyperref.jinja2s
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ name: hyperdef
<a name="{{ obj.attributes.category }}.{{ obj.attributes.name }}" >{{ obj }}</a>

name: hyperref
<a href="{{ obj.attributes.url }}#{{ obj.attributes.category }}.{{ obj.attributes.name}}" >{{ obj }}</a>
<a href="{% if obj.idref %}{{ obj.idref.label.url }}{% else %}{{ obj.attributes.url }}#{{ obj.attributes.category }}.{{ obj.attributes.name}}{% endif %}" >{{ obj }}</a>

name: hyperlink
<a href="{{ obj.idref.label.url }}" >{{ obj }}</a>
Expand Down
48 changes: 47 additions & 1 deletion unittests/Packages/hyperref.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
from plasTeX.TeX import TeX
from pathlib import Path

from plasTeX.TeX import TeX, TeXDocument
from plasTeX.Config import defaultConfig
from plasTeX.Renderers.HTML5 import Renderer
from plasTeX.Renderers.HTML5.Config import addConfig

def test_href_no_char_sub():
"""Test for issue #342"""
Expand All @@ -18,3 +23,44 @@ def test_href_no_char_sub():
assert len(hrefs) == 1
assert hrefs[0].attributes['url'].textContent == 'a--file.pdf'

def test_hyperref_html5(tmpdir):
""" Test for issue #316 """

root = Path(__file__).parent
config = defaultConfig()
addConfig(config)
config['files']['split-level'] = -100
tex = TeX(TeXDocument(config=config))
tex.input(r"""
\documentclass{article}
\usepackage{hyperref}
\begin{document}
A link to:
\hyperref[numbered]{numbered section}
\hyperref[not-numbered]{not numbered section}
\section{Numbered}\label{numbered}
This section is numbered.
\section*{Not numbered}\label{not-numbered}
This section is not numbered.
\end{document}
""")
doc = tex.parse()
doc.userdata['working-dir'] = Path(__file__).parent

with tmpdir.as_cwd():
Renderer().render(doc)

assert (Path(tmpdir)/'index.html').exists()

html = (Path(tmpdir)/'index.html').read_text()

assert '<a href="index.html#numbered" >numbered section</a>' in html
assert '<a href="index.html#not-numbered" >not numbered section</a>' in html

0 comments on commit b9b603f

Please sign in to comment.