@@ -16,6 +16,7 @@ https://jats.nlm.nih.gov/publishing/tag-library
1616module Text.Pandoc.Writers.JATS ( writeJATS ) where
1717import Prelude
1818import Control.Monad.Reader
19+import Control.Monad.State
1920import Data.Char (toLower)
2021import Data.Generics (everywhere, mkT)
2122import Data.List (isSuffixOf, partition, isPrefixOf)
@@ -39,11 +40,16 @@ import qualified Text.XML.Light as Xml
3940data JATSVersion = JATS1_1
4041 deriving (Eq, Show)
4142
42−type JATS = ReaderT JATSVersion
43+data JATSState = JATSState
44+ { jatsNotes :: [(Int, Doc)] }
45+
46+type JATS a = StateT JATSState (ReaderT JATSVersion a)
4347
4448writeJATS :: PandocMonad m => WriterOptions -> Pandoc -> m Text
4549writeJATS opts d =
46− runReaderT (docToJATS opts d) JATS1_1
50+ runReaderT (evalStateT (docToJATS opts d)
51+ (JATSState{ jatsNotes = [] }))
52+ JATS1_1
4753
4854-- | Convert Pandoc document to string in JATS format.
4955docToJATS :: PandocMonad m => WriterOptions -> Pandoc -> JATS m Text
@@ -52,7 +58,7 @@ docToJATS opts (Pandoc meta blocks) = do
5258 isBackBlock _ = False
5359 let (backblocks, bodyblocks) = partition isBackBlock blocks
5460 let elements = hierarchicalize bodyblocks
55− let backElements = hierarchicalize backblocks
61+ let backElements = hierarchicalize $ backblocks
5662 let colwidth = if writerWrapText opts == WrapAuto
5763 then Just $ writerColumns opts
5864 else Nothing
@@ -77,8 +83,10 @@ docToJATS opts (Pandoc meta blocks) = do
7783 meta
7884 main <- (render' . vcat) <$>
7985 mapM (elementToJATS opts' startLvl) elements
80− back <- (render' . vcat) <$>
81− mapM (elementToJATS opts' startLvl) backElements
86+ notes <- reverse . map snd <$> gets jatsNotes
87+ backs <- mapM (elementToJATS opts' startLvl) backElements
88+ let fns = inTagsIndented "fn-group" $ vcat notes
89+ let back = render' $ vcat backs $$ fns
8290 let context = defField "body" main
8391 $ defField "back" back
8492 $ defField "mathml" (case writerHTMLMathMethod opts of
@@ -368,9 +376,18 @@ inlineToJATS _ Space = return space
368376inlineToJATS opts SoftBreak
369377 | writerWrapText opts == WrapPreserve = return cr
370378 | otherwise = return space
371−inlineToJATS opts (Note contents) =
379+inlineToJATS opts (Note contents) = do
372380 -- TODO technically only <p> tags are allowed inside
373− inTagsIndented "fn" <$> blocksToJATS opts contents
381+ notes <- gets jatsNotes
382+ let notenum = case notes of
383+ (n, _):_ -> n + 1
384+ [] -> 1
385+ thenote <- inTags True "fn" [("id","fn" ++ show notenum)]
386+ <$> blocksToJATS opts contents
387+ modify $ \st -> st{ jatsNotes = (notenum, thenote) : notes }
388+ return $ inTags False "xref" [("ref-type", "fn"),
389+ ("rid", "fn" ++ show notenum)]
390+ $ text (show notenum)
374391inlineToJATS opts (Cite _ lst) =
375392 -- TODO revisit this after examining the jats.csl pipeline
376393 inlinesToJATS opts lst