@@ -40,26 +40,14 @@ import Text.DocLayout
4040import Text.Pandoc.Shared
4141import Text.Pandoc.Templates (renderTemplate)
4242import Text.DocTemplates (Context(..), Val(..))
43+import Text.Pandoc.Writers.JATS.Table (tableToJATS)
44+import Text.Pandoc.Writers.JATS.Types
4345import Text.Pandoc.Writers.Math
4446import Text.Pandoc.Writers.Shared
4547import Text.Pandoc.XML
4648import Text.TeXMath
4749import qualified Text.XML.Light as Xml
4850
49−-- | JATS tag set variant
50−data JATSTagSet
51− = TagSetArchiving -- ^ Archiving and Interchange Tag Set
52− | TagSetPublishing -- ^ Journal Publishing Tag Set
53− | TagSetArticleAuthoring -- ^ Article Authoring Tag Set
54− deriving (Eq)
55−
56−-- | Internal state used by the writer.
57−newtype JATSState = JATSState
58− { jatsNotes :: [(Int, Doc Text)] }
59−
60−-- | JATS writer type
61−type JATS a = StateT JATSState (ReaderT JATSTagSet a)
62−
6351-- | Convert a @'Pandoc'@ document to JATS (Archiving and Interchange
6452-- Tag Set.)
6553writeJatsArchiving :: PandocMonad m => WriterOptions -> Pandoc -> m Text
@@ -83,9 +71,14 @@ writeJATS = writeJatsArchiving
8371-- | Convert a @'Pandoc'@ document to JATS.
8472writeJats :: PandocMonad m => JATSTagSet -> WriterOptions -> Pandoc -> m Text
8573writeJats tagSet opts d =
86− runReaderT (evalStateT (docToJATS opts d)
87− (JATSState{ jatsNotes = [] }))
88− tagSet
74+ runReaderT (evalStateT (docToJATS opts d) initialState)
75+ environment
76+ where initialState = JATSState { jatsNotes = [] }
77+ environment = JATSEnv
78+ { jatsTagSet = tagSet
79+ , jatsInlinesWriter = inlinesToJATS
80+ , jatsBlockWriter = blockToJATS
81+ }
8982
9083-- | Convert Pandoc document to string in JATS format.
9184docToJATS :: PandocMonad m => WriterOptions -> Pandoc -> JATS m Text
@@ -110,7 +103,7 @@ docToJATS opts (Pandoc meta blocks) = do
110103 main <- fromBlocks bodyblocks
111104 notes <- gets (reverse . map snd . jatsNotes)
112105 backs <- fromBlocks backblocks
113− tagSet <- ask
106+ tagSet <- asks jatsTagSet
114107 -- In the "Article Authoring" tag set, occurrence of fn-group elements
115108 -- is restricted to table footers. Footnotes have to be placed inline.
116109 let fns = if null notes || tagSet == TagSetArticleAuthoring
@@ -311,7 +304,7 @@ blockToJATS opts (Para lst) =
311304blockToJATS opts (LineBlock lns) =
312305 blockToJATS opts $ linesToPara lns
313306blockToJATS opts (BlockQuote blocks) = do
314− tagSet <- ask
307+ tagSet <- asks jatsTagSet
315308 let blocksToJats' = if tagSet == TagSetArticleAuthoring
316309 then wrappedBlocksToJATS (not . isPara)
317310 else blocksToJATS
@@ -326,7 +319,7 @@ blockToJATS opts (BulletList lst) =
326319 listItemsToJATS opts Nothing lst
327320blockToJATS _ (OrderedList _ []) = return empty
328321blockToJATS opts (OrderedList (start, numstyle, delimstyle) items) = do
329− tagSet <- ask
322+ tagSet <- asks jatsTagSet
330323 let listType =
331324 -- The Article Authoring tag set doesn't allow a more specific
332325 -- @list-type@ attribute than "order".
@@ -356,52 +349,8 @@ blockToJATS _ b@(RawBlock f str)
356349 report $ BlockNotRendered b
357350 return empty
358351blockToJATS _ HorizontalRule = return empty -- not semantic
359−blockToJATS opts (Table _ blkCapt specs th tb tf) =
360− case toLegacyTable blkCapt specs th tb tf of
361− ([], aligns, widths, headers, rows) -> captionlessTable aligns widths headers rows
362− (caption, aligns, widths, headers, rows) -> do
363− captionDoc <- inTagsIndented "caption" <$> blockToJATS opts (Para caption)
364− tbl <- captionlessTable aligns widths headers rows
365− return $ inTags True "table-wrap" [] $ captionDoc $$ tbl
366− where
367− captionlessTable aligns widths headers rows = do
368− let percent w = tshow (truncate (100*w) :: Integer) <> "*"
369− let coltags = vcat $ zipWith (\w al -> selfClosingTag "col"
370− ([("width", percent w) | w > 0] ++
371− [("align", alignmentToText al)])) widths aligns
372− thead <- if all null headers
373− then return empty
374− else inTagsIndented "thead" <$> tableRowToJATS opts True headers
375− tbody <- inTagsIndented "tbody" . vcat <$>
376− mapM (tableRowToJATS opts False) rows
377− return $ inTags True "table" [] $ coltags $$ thead $$ tbody
378−
379−alignmentToText :: Alignment -> Text
380−alignmentToText alignment = case alignment of
381− AlignLeft -> "left"
382− AlignRight -> "right"
383− AlignCenter -> "center"
384− AlignDefault -> "left"
385−
386−tableRowToJATS :: PandocMonad m
387− => WriterOptions
388− -> Bool
389− -> [[Block]]
390− -> JATS m (Doc Text)
391−tableRowToJATS opts isHeader cols =
392− inTagsIndented "tr" . vcat <$> mapM (tableItemToJATS opts isHeader) cols
393−
394−tableItemToJATS :: PandocMonad m
395− => WriterOptions
396− -> Bool
397− -> [Block]
398− -> JATS m (Doc Text)
399−tableItemToJATS opts isHeader [Plain item] =
400− inTags False (if isHeader then "th" else "td") [] <$>
401− inlinesToJATS opts item
402−tableItemToJATS opts isHeader item =
403− inTags False (if isHeader then "th" else "td") [] . vcat <$>
404− mapM (blockToJATS opts) item
352+blockToJATS opts (Table attr blkCapt specs th tb tf) =
353+ tableToJATS opts attr blkCapt specs th tb tf
405354
406355-- | Convert a list of inline elements to JATS.
407356inlinesToJATS :: PandocMonad m => WriterOptions -> [Inline] -> JATS m (Doc Text)
@@ -458,7 +407,7 @@ inlineToJATS opts SoftBreak
458407 | writerWrapText opts == WrapPreserve = return cr
459408 | otherwise = return space
460409inlineToJATS opts (Note contents) = do
461− tagSet <- ask
410+ tagSet <- asks jatsTagSet
462411 -- Footnotes must occur inline when using the Article Authoring tag set.
463412 if tagSet == TagSetArticleAuthoring
464413 then inTagsIndented "fn" <$> wrappedBlocksToJATS (not . isPara) opts contents
@@ -504,7 +453,7 @@ inlineToJATS _ (Math t str) = do
504453 let rawtex = text "<![CDATA[" <> literal str <> text "]]>"
505454 let texMath = inTagsSimple "tex-math" rawtex
506455
507− tagSet <- ask
456+ tagSet <- asks jatsTagSet
508457 return . inTagsSimple tagtype $
509458 case res of
510459 Right r -> let mathMl = text (Xml.ppcElement conf $ fixNS r)