{-# LANGUAGE OverloadedStrings #-}
{- |
   Module      : Text.Pandoc.Writers.LaTeX.Notes
   Copyright   : Copyright (C) 2006-2024 John MacFarlane
   License     : GNU GPL, version 2 or above

   Maintainer  : John MacFarlane <jgm@berkeley.edu>
   Stability   : alpha
   Portability : portable

Output deferred footnote texts as LaTeX.
-}
module Text.Pandoc.Writers.LaTeX.Notes
  ( notesToLaTeX
  ) where

import Text.DocLayout ( Doc, braces, text, vcat )
import Data.Text (Text)

-- | Render the texts of notes whose marks were produced with
-- @\\pandocfootnotemark{n}@. @\\pandocfootnotetext{n}@ restores
-- the footnote mark and hyperref anchor saved by the corresponding
-- mark (see the template). Notes are stored in reverse order.
notesToLaTeX :: [(Int, Doc Text)] -> Doc Text
notesToLaTeX :: [(Int, Doc Text)] -> Doc Text
notesToLaTeX = [Doc Text] -> Doc Text
forall a. [Doc a] -> Doc a
vcat ([Doc Text] -> Doc Text)
-> ([(Int, Doc Text)] -> [Doc Text])
-> [(Int, Doc Text)]
-> Doc Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((Int, Doc Text) -> Doc Text) -> [(Int, Doc Text)] -> [Doc Text]
forall a b. (a -> b) -> [a] -> [b]
map (Int, Doc Text) -> Doc Text
forall {a} {a}. (HasChars a, Show a) => (a, Doc a) -> Doc a
toNoteText ([(Int, Doc Text)] -> [Doc Text])
-> ([(Int, Doc Text)] -> [(Int, Doc Text)])
-> [(Int, Doc Text)]
-> [Doc Text]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [(Int, Doc Text)] -> [(Int, Doc Text)]
forall a. [a] -> [a]
reverse
  where
    toNoteText :: (a, Doc a) -> Doc a
toNoteText (a
num, Doc a
x) =
      Doc a
"\\pandocfootnotetext" Doc a -> Doc a -> Doc a
forall a. Semigroup a => a -> a -> a
<> Doc a -> Doc a
forall a. HasChars a => Doc a -> Doc a
braces (String -> Doc a
forall a. HasChars a => String -> Doc a
text (a -> String
forall a. Show a => a -> String
show a
num)) Doc a -> Doc a -> Doc a
forall a. Semigroup a => a -> a -> a
<> Doc a -> Doc a
forall a. HasChars a => Doc a -> Doc a
braces Doc a
x