Skip to content

Commit 758ff05

Browse files
ag-eitiltjgm
authored andcommitted
HTML5 writer: Add suffix to multiple footnote section ids
The first (and often only) `<aside id=footnotes>` block remains unchanged, however any additional blocks from `--reference-location` are distinguished as `#footnotes-2`, `#footnotes-3`, and so on. No other existing writer seems to implement per-section IDs, including HTML4.
1 parent bf674e8 commit 758ff05

File tree

4 files changed

+232
-4
lines changed

4 files changed

+232
-4
lines changed

src/Text/Pandoc/Writers/HTML.hs

+19-4
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ import Data.String (fromString)
8787
data WriterState = WriterState
8888
{ stNotes :: [Html] -- ^ List of notes
8989
, stEmittedNotes :: Int -- ^ How many notes we've already pushed out to the HTML
90+
, stEmittedNoteBlocks :: Int -- ^ How many @\<div class=footnote>@ blocks we've already pushed out
9091
, stMath :: Bool -- ^ Math is used in document
9192
, stQuotes :: Bool -- ^ <q> tag is used
9293
, stHighlighting :: Bool -- ^ Syntax highlighting is used
@@ -102,7 +103,11 @@ data WriterState = WriterState
102103
}
103104

104105
defaultWriterState :: WriterState
105-
defaultWriterState = WriterState {stNotes= [], stEmittedNotes = 0, stMath = False, stQuotes = False,
106+
defaultWriterState = WriterState {stNotes= [],
107+
stEmittedNotes = 0,
108+
stEmittedNoteBlocks = 0,
109+
stMath = False,
110+
stQuotes = False,
106111
stHighlighting = False,
107112
stHtml5 = False,
108113
stEPUBVersion = Nothing,
@@ -530,6 +535,16 @@ footnoteSection opts refLocation startCounter notes = do
530535
let hrtag = if refLocation /= EndOfBlock
531536
then (if html5 then H5.hr else H.hr) <> nl
532537
else mempty
538+
idName <- do
539+
blockCount <- gets stEmittedNoteBlocks
540+
modify $ \st -> st{ stEmittedNoteBlocks = blockCount + 1 }
541+
return $
542+
-- Keep the first note section's id undecorated to maintain a target for
543+
-- old links which don't expect numbered sections, or for when the notes
544+
-- are rendered all together at the end of the document.
545+
if blockCount <= 0
546+
then "footnotes"
547+
else "footnotes-" <> show (blockCount + 1)
533548
let additionalClassName = case refLocation of
534549
EndOfBlock -> "footnotes-end-of-block"
535550
EndOfDocument -> "footnotes-end-of-document"
@@ -539,17 +554,17 @@ footnoteSection opts refLocation startCounter notes = do
539554
let container x
540555
| html5
541556
, epubVersion == Just EPUB3
542-
= H5.section ! A.id "footnotes"
557+
= H5.section ! A.id (fromString idName)
543558
! A.class_ className
544559
! customAttribute "epub:type" "footnotes" $ x
545560
| html5
546561
, refLocation == EndOfDocument
547562
-- Note: we need a section for a new slide in slide formats.
548-
= H5.section ! A5.id "footnotes"
563+
= H5.section ! A5.id (fromString idName)
549564
! A5.class_ className
550565
! A5.role "doc-endnotes"
551566
$ x
552-
| html5 = H5.aside ! prefixedId opts "footnotes"
567+
| html5 = H5.aside ! prefixedId opts (fromString idName)
553568
! A5.class_ className
554569
! A5.role "doc-footnote"
555570
$ x

test/command/8770-block.md

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
```
2+
% pandoc -t html5 --reference-location=block
3+
# Section 1
4+
5+
hello[^1]
6+
7+
: Sample table.[^2]
8+
9+
-----------
10+
Fruit[^3]
11+
-----------
12+
Bans[^4]
13+
-----------
14+
15+
# Section 2
16+
17+
dolly[^5]
18+
19+
[^1]: doc footnote
20+
[^2]: caption footnote
21+
[^3]: header footnote
22+
[^4]: table cell footnote
23+
[^5]: doc footnote
24+
^D
25+
<h1 id="section-1">Section 1</h1>
26+
<p>hello<a href="#fn1" class="footnote-ref" id="fnref1"
27+
role="doc-noteref"><sup>1</sup></a></p>
28+
<aside id="footnotes" class="footnotes footnotes-end-of-block"
29+
role="doc-footnote">
30+
<ol>
31+
<li id="fn1"><p>doc footnote<a href="#fnref1" class="footnote-back"
32+
role="doc-backlink">↩︎</a></p></li>
33+
</ol>
34+
</aside>
35+
<table style="width:17%;">
36+
<caption>Sample table.<a href="#fn2" class="footnote-ref" id="fnref2"
37+
role="doc-noteref"><sup>2</sup></a></caption>
38+
<colgroup>
39+
<col style="width: 16%" />
40+
</colgroup>
41+
<thead>
42+
<tr class="header">
43+
<th style="text-align: center;">Fruit<a href="#fn3" class="footnote-ref"
44+
id="fnref3" role="doc-noteref"><sup>3</sup></a></th>
45+
</tr>
46+
</thead>
47+
<tbody>
48+
<tr class="odd">
49+
<td style="text-align: center;">Bans<a href="#fn4" class="footnote-ref"
50+
id="fnref4" role="doc-noteref"><sup>4</sup></a></td>
51+
</tr>
52+
</tbody>
53+
</table>
54+
<aside id="footnotes-2" class="footnotes footnotes-end-of-block"
55+
role="doc-footnote">
56+
<ol start="2">
57+
<li id="fn2"><p>caption footnote<a href="#fnref2" class="footnote-back"
58+
role="doc-backlink">↩︎</a></p></li>
59+
<li id="fn3"><p>header footnote<a href="#fnref3" class="footnote-back"
60+
role="doc-backlink">↩︎</a></p></li>
61+
<li id="fn4"><p>table cell footnote<a href="#fnref4"
62+
class="footnote-back" role="doc-backlink">↩︎</a></p></li>
63+
</ol>
64+
</aside>
65+
<h1 id="section-2">Section 2</h1>
66+
<p>dolly<a href="#fn5" class="footnote-ref" id="fnref5"
67+
role="doc-noteref"><sup>5</sup></a></p>
68+
<aside id="footnotes-3" class="footnotes footnotes-end-of-block"
69+
role="doc-footnote">
70+
<ol start="5">
71+
<li id="fn5"><p>doc footnote<a href="#fnref5" class="footnote-back"
72+
role="doc-backlink">↩︎</a></p></li>
73+
</ol>
74+
</aside>
75+
```

test/command/8770-document.md

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
```
2+
% pandoc -t html5 --reference-location=document
3+
# Section 1
4+
5+
hello[^1]
6+
7+
: Sample table.[^2]
8+
9+
-----------
10+
Fruit[^3]
11+
-----------
12+
Bans[^4]
13+
-----------
14+
15+
# Section 2
16+
17+
dolly[^5]
18+
19+
[^1]: doc footnote
20+
[^2]: caption footnote
21+
[^3]: header footnote
22+
[^4]: table cell footnote
23+
[^5]: doc footnote
24+
^D
25+
<h1 id="section-1">Section 1</h1>
26+
<p>hello<a href="#fn1" class="footnote-ref" id="fnref1"
27+
role="doc-noteref"><sup>1</sup></a></p>
28+
<table style="width:17%;">
29+
<caption>Sample table.<a href="#fn2" class="footnote-ref" id="fnref2"
30+
role="doc-noteref"><sup>2</sup></a></caption>
31+
<colgroup>
32+
<col style="width: 16%" />
33+
</colgroup>
34+
<thead>
35+
<tr class="header">
36+
<th style="text-align: center;">Fruit<a href="#fn3" class="footnote-ref"
37+
id="fnref3" role="doc-noteref"><sup>3</sup></a></th>
38+
</tr>
39+
</thead>
40+
<tbody>
41+
<tr class="odd">
42+
<td style="text-align: center;">Bans<a href="#fn4" class="footnote-ref"
43+
id="fnref4" role="doc-noteref"><sup>4</sup></a></td>
44+
</tr>
45+
</tbody>
46+
</table>
47+
<h1 id="section-2">Section 2</h1>
48+
<p>dolly<a href="#fn5" class="footnote-ref" id="fnref5"
49+
role="doc-noteref"><sup>5</sup></a></p>
50+
<section id="footnotes" class="footnotes footnotes-end-of-document"
51+
role="doc-endnotes">
52+
<hr />
53+
<ol>
54+
<li id="fn1"><p>doc footnote<a href="#fnref1" class="footnote-back"
55+
role="doc-backlink">↩︎</a></p></li>
56+
<li id="fn2"><p>caption footnote<a href="#fnref2" class="footnote-back"
57+
role="doc-backlink">↩︎</a></p></li>
58+
<li id="fn3"><p>header footnote<a href="#fnref3" class="footnote-back"
59+
role="doc-backlink">↩︎</a></p></li>
60+
<li id="fn4"><p>table cell footnote<a href="#fnref4"
61+
class="footnote-back" role="doc-backlink">↩︎</a></p></li>
62+
<li id="fn5"><p>doc footnote<a href="#fnref5" class="footnote-back"
63+
role="doc-backlink">↩︎</a></p></li>
64+
</ol>
65+
</section>
66+
```

test/command/8770-section.md

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
```
2+
% pandoc -t html5 --reference-location=section
3+
# Section 1
4+
5+
hello[^1]
6+
7+
: Sample table.[^2]
8+
9+
-----------
10+
Fruit[^3]
11+
-----------
12+
Bans[^4]
13+
-----------
14+
15+
# Section 2
16+
17+
dolly[^5]
18+
19+
[^1]: doc footnote
20+
[^2]: caption footnote
21+
[^3]: header footnote
22+
[^4]: table cell footnote
23+
[^5]: doc footnote
24+
^D
25+
<h1 id="section-1">Section 1</h1>
26+
<p>hello<a href="#fn1" class="footnote-ref" id="fnref1"
27+
role="doc-noteref"><sup>1</sup></a></p>
28+
<table style="width:17%;">
29+
<caption>Sample table.<a href="#fn2" class="footnote-ref" id="fnref2"
30+
role="doc-noteref"><sup>2</sup></a></caption>
31+
<colgroup>
32+
<col style="width: 16%" />
33+
</colgroup>
34+
<thead>
35+
<tr class="header">
36+
<th style="text-align: center;">Fruit<a href="#fn3" class="footnote-ref"
37+
id="fnref3" role="doc-noteref"><sup>3</sup></a></th>
38+
</tr>
39+
</thead>
40+
<tbody>
41+
<tr class="odd">
42+
<td style="text-align: center;">Bans<a href="#fn4" class="footnote-ref"
43+
id="fnref4" role="doc-noteref"><sup>4</sup></a></td>
44+
</tr>
45+
</tbody>
46+
</table>
47+
<aside id="footnotes" class="footnotes footnotes-end-of-section"
48+
role="doc-footnote">
49+
<hr />
50+
<ol>
51+
<li id="fn1"><p>doc footnote<a href="#fnref1" class="footnote-back"
52+
role="doc-backlink">↩︎</a></p></li>
53+
<li id="fn2"><p>caption footnote<a href="#fnref2" class="footnote-back"
54+
role="doc-backlink">↩︎</a></p></li>
55+
<li id="fn3"><p>header footnote<a href="#fnref3" class="footnote-back"
56+
role="doc-backlink">↩︎</a></p></li>
57+
<li id="fn4"><p>table cell footnote<a href="#fnref4"
58+
class="footnote-back" role="doc-backlink">↩︎</a></p></li>
59+
</ol>
60+
</aside>
61+
<h1 id="section-2">Section 2</h1>
62+
<p>dolly<a href="#fn5" class="footnote-ref" id="fnref5"
63+
role="doc-noteref"><sup>5</sup></a></p>
64+
<aside id="footnotes-2" class="footnotes footnotes-end-of-section"
65+
role="doc-footnote">
66+
<hr />
67+
<ol start="5">
68+
<li id="fn5"><p>doc footnote<a href="#fnref5" class="footnote-back"
69+
role="doc-backlink">↩︎</a></p></li>
70+
</ol>
71+
</aside>
72+
```

0 commit comments

Comments
 (0)