Skip to content

Commit

Permalink
Add suffix to multiple HTML5 footnote section ids
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
ag-eitilt committed Apr 10, 2023
1 parent 9bcf1ff commit ad75fa8
Show file tree
Hide file tree
Showing 3 changed files with 808 additions and 4 deletions.
23 changes: 19 additions & 4 deletions src/Text/Pandoc/Writers/HTML.hs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ import Data.String (fromString)
data WriterState = WriterState
{ stNotes :: [Html] -- ^ List of notes
, stEmittedNotes :: Int -- ^ How many notes we've already pushed out to the HTML
, stEmittedNoteBlocks :: Int -- ^ How many <div class=footnote> blocks we've already pushed out
, stMath :: Bool -- ^ Math is used in document
, stQuotes :: Bool -- ^ <q> tag is used
, stHighlighting :: Bool -- ^ Syntax highlighting is used
Expand All @@ -102,7 +103,11 @@ data WriterState = WriterState
}

defaultWriterState :: WriterState
defaultWriterState = WriterState {stNotes= [], stEmittedNotes = 0, stMath = False, stQuotes = False,
defaultWriterState = WriterState {stNotes= [],
stEmittedNotes = 0,
stEmittedNoteBlocks = 0,
stMath = False,
stQuotes = False,
stHighlighting = False,
stHtml5 = False,
stEPUBVersion = Nothing,
Expand Down Expand Up @@ -531,6 +536,16 @@ footnoteSection refLocation startCounter notes = do
let hrtag = if refLocation /= EndOfBlock
then (if html5 then H5.hr else H.hr) <> nl
else mempty
idName <- do
blockCount <- gets stEmittedNoteBlocks
modify $ \st -> st{ stEmittedNoteBlocks = blockCount + 1 }
return $
-- Keep the first note section's id undecorated to maintain a target for
-- old links which don't expect numbered sections, or for when the notes
-- are rendered all together at the end of the document.
if blockCount <= 0
then "footnotes"
else "footnotes-" <> fromString (show $ blockCount + 1)
let additionalClassName = case refLocation of
EndOfBlock -> "footnotes-end-of-block"
EndOfDocument -> "footnotes-end-of-document"
Expand All @@ -540,17 +555,17 @@ footnoteSection refLocation startCounter notes = do
let container x
| html5
, epubVersion == Just EPUB3
= H5.section ! A.id "footnotes"
= H5.section ! A.id idName
! A.class_ className
! customAttribute "epub:type" "footnotes" $ x
| html5
, refLocation == EndOfDocument
, slideVariant == RevealJsSlides -- need a section for a new slide:
= H5.section ! A5.id "footnotes"
= H5.section ! A5.id idName
! A5.class_ className
! A5.role "doc-endnotes"
$ x
| html5 = H5.aside ! A5.id "footnotes"
| html5 = H5.aside ! A5.id idName
! A5.class_ className
! A5.role "doc-endnotes"
$ x
Expand Down
3 changes: 3 additions & 0 deletions test/Tests/Old.hs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ tests pandocPath =
[ extWriterTests' "html4"
, extWriterTests' "html5"
, lhsWriterTests' "html"
, [ test' "block-footnotes" ["-r", "native", "-w", "html5", "-s", "--reference-location=block"]
"writer.native" "writer_blocknotes.html5"
]
]
, test' "reader" ["-r", "html", "-w", "native", "-s"]
"html-reader.html" "html-reader.native"
Expand Down
Loading

0 comments on commit ad75fa8

Please sign in to comment.