git.iko.soy/typeable/pandoc
on master help sign in
JATS writer: ensure validity of pub-date.

We try to parse the date and convert to year, month, day, as expected in pub-date. We also add an iso-8601-date attribute if possible.

commit5bbaedac57d656d3f38a574763b2ab5451a20158 authorJohn MacFarlane <jgm@berkeley.edu> dateTue 11 Jun 2019, 17:01 (7 years ago) parentf7c0d2f
3 files changed+30−14
data/templates/default.jats+4−12 src/Text/Pandoc/Writers/JATS.hs+22 test/writer.jats+4−2
data/templates/default.jats+4−12
@@ -120,22 +120,14 @@ $endif$
120120</author-notes>
121121$endif$
122122$if(date)$
123−$if(date.iso-8601)$
124−<pub-date pub-type="epub" iso-8601-date="$date.iso-8601$">
125−$else$
126−<pub-date pub-type="epub">
127−$endif$
123+<pub-date pub-type="epub"$if(date.iso-8601)$ iso-8601-date="$date.iso-8601$"$endif$>
128124$if(date.day)$
129−<day>$pub-date.day$</day>
125+<day>$date.day$</day>
130126$endif$
131127$if(date.month)$
132−<month>$pub-date.month$</month>
133−$endif$
134−$if(date.year)$
135−<year>$pub-date.year$</year>
136−$else$
137−<string-date>$date$</string-date>
128+<month>$date.month$</month>
138129$endif$
130+<year>$date.year$</year>
139131</pub-date>
140132$endif$
141133$if(article.volume)$
src/Text/Pandoc/Writers/JATS.hs+22
@@ -20,7 +20,9 @@ import Control.Monad.State
2020import Data.Char (toLower)
2121import Data.Generics (everywhere, mkT)
2222import Data.List (isSuffixOf, partition, isPrefixOf)
23+import qualified Data.Map as M
2324import Data.Maybe (fromMaybe)
25+import Data.Time (toGregorian, Day, parseTimeM, defaultTimeLocale, formatTime)
2426import Data.Text (Text)
2527import Text.Pandoc.Class (PandocMonad, report)
2628import Text.Pandoc.Definition
@@ -88,8 +90,21 @@ docToJATS opts (Pandoc meta blocks) = do
8890 backs <- mapM (elementToJATS opts' startLvl) backElements
8991 let fns = inTagsIndented "fn-group" $ vcat notes
9092 let back = render' $ vcat backs $$ fns
93+ let date = case getField "date" metadata -- an object
94+ `mplus`
95+ (getField "date" metadata >>= parseDate) of
96+ Nothing -> mempty
97+ Just day ->
98+ let (y,m,d) = toGregorian day
99+ in M.insert ("year" :: String) (show y)
100+ $ M.insert "month" (show m)
101+ $ M.insert "day" (show d)
102+ $ M.insert "iso-8601"
103+ (formatTime defaultTimeLocale "%F" day)
104+ $ mempty
91105 let context = defField "body" main
92106 $ defField "back" back
107+ $ resetField ("date" :: String) date
93108 $ defField "mathml" (case writerHTMLMathMethod opts of
94109 MathML -> True
95110 _ -> False) metadata
@@ -505,3 +520,10 @@ demoteHeaderAndRefs (Header _ _ ils) = Para ils
505520demoteHeaderAndRefs (Div ("refs",cls,kvs) bs) =
506521 Div ("",cls,kvs) bs
507522demoteHeaderAndRefs x = x
523+
524+parseDate :: String -> Maybe Day
525+parseDate s = msum (map (\fs -> parsetimeWith fs s) formats) :: Maybe Day
526+ where parsetimeWith = parseTimeM True defaultTimeLocale
527+ formats = ["%x","%m/%d/%Y", "%D","%F", "%d %b %Y",
528+ "%e %B %Y", "%b. %e, %Y", "%B %e, %Y",
529+ "%Y%m%d", "%Y%m", "%Y"]
test/writer.jats+4−2
@@ -22,8 +22,10 @@
2222<string-name>Anonymous</string-name>
2323</contrib>
2424</contrib-group>
25−<pub-date pub-type="epub">
26−<string-date>July 17, 2006</string-date>
25+<pub-date pub-type="epub" iso-8601-date="2006-07-17">
26+<day>17</day>
27+<month>7</month>
28+<year>2006</year>
2729</pub-date>
2830</article-meta>
2931</front>