git.iko.soy/typeable/pandoc
on master help sign in
Put date in YYYY-MM-DD format if possible for HTML, docx metadata.

Added normalizeDate to Text.Pandoc.Shared.

commit3a0b3df00701eef2a0549487e08fa4d63c3ab8d9 authorJohn MacFarlane <fiddlosopher@gmail.com> dateSat 28 Jan 2012, 15:54 (15 years ago) parent98e5b61
8 files changed+31−9
pandoc.cabal+4 src/Text/Pandoc/Shared.hs+17 src/Text/Pandoc/Writers/Docx.hs+4−3 src/Text/Pandoc/Writers/HTML.hs+2−2 tests/s5.basic.html+1−1 tests/s5.fancy.html+1−1 tests/s5.inserts.html+1−1 tests/writer.html+1−1
pandoc.cabal+4
@@ -203,6 +203,8 @@ Library
203203 zip-archive >= 0.1.1.7 && < 0.2,
204204 utf8-string >= 0.3 && < 0.4,
205205 old-time >= 1 && < 1.2,
206+ old-locale >= 1 && < 1.1,
207+ time >= 1.2 && < 1.5,
206208 HTTP >= 4000.0.5 && < 4000.3,
207209 texmath >= 0.6 && < 0.7,
208210 xml >= 1.3.5 && < 1.4,
@@ -294,6 +296,8 @@ Executable pandoc
294296 zip-archive >= 0.1.1.7 && < 0.2,
295297 utf8-string >= 0.3 && < 0.4,
296298 old-time >= 1 && < 1.2,
299+ old-locale >= 1 && < 1.1,
300+ time >= 1.2 && < 1.5,
297301 HTTP >= 4000.0.5 && < 4000.3,
298302 texmath >= 0.6 && < 0.7,
299303 xml >= 1.3.5 && < 1.4,
src/Text/Pandoc/Shared.hs+17
@@ -46,6 +46,8 @@ module Text.Pandoc.Shared (
4646 toRomanNumeral,
4747 escapeURI,
4848 tabFilter,
49+ -- * Date/time
50+ normalizeDate,
4951 -- * Pandoc block and inline list processing
5052 orderedListMarkers,
5153 normalizeSpaces,
@@ -81,9 +83,12 @@ import System.Directory
8183import System.FilePath ( (</>) )
8284import Data.Generics (Typeable, Data)
8385import qualified Control.Monad.State as S
86+import Control.Monad (msum)
8487import Paths_pandoc (getDataFileName)
8588import Text.Pandoc.Highlighting (Style, pygments)
8689import Text.Pandoc.Pretty (charWidth)
90+import System.Locale (defaultTimeLocale)
91+import Data.Time
8792
8893--
8994-- List processing
@@ -217,6 +222,18 @@ tabFilter tabStop =
217222 x : go (spsToNextStop - 1) xs
218223 in go tabStop
219224
225+--
226+-- Date/time
227+--
228+
229+-- | Parse a date and convert (if possible) to "YYYY-MM-DD" format.
230+normalizeDate :: String -> Maybe String
231+normalizeDate s = fmap (formatTime defaultTimeLocale "%F")
232+ (msum $ map (\fs -> parsetimeWith fs s) formats :: Maybe Day)
233+ where parsetimeWith = parseTime defaultTimeLocale
234+ formats = ["%x","%m/%d/%Y", "%D","%F", "%d %b %Y",
235+ "%d %B %Y", "%b. %d, %Y", "%B %d, %Y"]
236+
220237--
221238-- Pandoc block and inline list processing
222239--
src/Text/Pandoc/Writers/Docx.hs+4−3
@@ -97,7 +97,7 @@ writeDocx :: Maybe FilePath -- ^ Path specified by --reference-docx
9797 -> WriterOptions -- ^ Writer options
9898 -> Pandoc -- ^ Document to convert
9999 -> IO B.ByteString
100−writeDocx mbRefDocx opts doc@(Pandoc (Meta tit auths _) _) = do
100+writeDocx mbRefDocx opts doc@(Pandoc (Meta tit auths date) _) = do
101101 let datadir = writerUserDataDir opts
102102 refArchive <- liftM toArchive $
103103 case mbRefDocx of
@@ -161,7 +161,8 @@ writeDocx mbRefDocx opts doc@(Pandoc (Meta tit auths _) _) = do
161161 ,("xmlns:dcmitype","http://purl.org/dc/dcmitype/")
162162 ,("xmlns:xsi","http://www.w3.org/2001/XMLSchema-instance")]
163163 $ mknode "dc:title" [] (stringify tit)
164− : mknode "dcterms:created" [("xsi:type","dcterms:W3CDTF")] () -- put doc date here
164+ : mknode "dcterms:created" [("xsi:type","dcterms:W3CDTF")]
165+ (maybe "" id $ normalizeDate $ stringify date)
165166 : mknode "dcterms:modified" [("xsi:type","dcterms:W3CDTF")] () -- put current time here
166167 : map (mknode "dc:creator" [] . stringify) auths
167168 let docPropsEntry = toEntry docPropsPath epochtime $ fromString $ showTopElement' docProps
@@ -653,4 +654,4 @@ inlineToOpenXML opts (Image alt (src, tit)) = do
653654 else do
654655 liftIO $ UTF8.hPutStrLn stderr $
655656 "Could not find image `" ++ src ++ "', skipping..."
656− inlinesToOpenXML opts alt
657+ inlinesToOpenXML opts alt
src/Text/Pandoc/Writers/HTML.hs+2−2
@@ -182,13 +182,13 @@ inTemplate :: TemplateTarget a
182182inTemplate opts tit auths authsMeta date toc body' newvars =
183183 let title' = renderHtml tit
184184 date' = renderHtml date
185+ dateMeta = maybe [] (\x -> [("date-meta",x)]) $ normalizeDate date'
185186 variables = writerVariables opts ++ newvars
186− context = variables ++
187+ context = variables ++ dateMeta ++
187188 [ ("body", dropWhile (=='\n') $ renderHtml body')
188189 , ("pagetitle", stripTags title')
189190 , ("title", title')
190191 , ("date", date')
191− , ("date-meta", stripTags date')
192192 , ("idprefix", writerIdentifierPrefix opts)
193193 , ("slidy-url", "http://www.w3.org/Talks/Tools/Slidy2")
194194 , ("s5-url", "s5/default") ] ++
tests/s5.basic.html+1−1
@@ -6,7 +6,7 @@
66 <meta name="generator" content="pandoc" />
77 <meta name="author" content="Sam Smith" />
88 <meta name="author" content="Jen Jones" />
9− <meta name="date" content="July 15, 2006" />
9+ <meta name="date" content="2006-07-15" />
1010 <title>My S5 Document</title>
1111 <!-- configuration parameters -->
1212 <meta name="defaultView" content="slideshow" />
tests/s5.fancy.html+1−1
@@ -6,7 +6,7 @@
66 <meta name="generator" content="pandoc" />
77 <meta name="author" content="Sam Smith" />
88 <meta name="author" content="Jen Jones" />
9− <meta name="date" content="July 15, 2006" />
9+ <meta name="date" content="2006-07-15" />
1010 <title>My S5 Document</title>
1111 <!-- configuration parameters -->
1212 <meta name="defaultView" content="slideshow" />
tests/s5.inserts.html+1−1
@@ -6,7 +6,7 @@
66 <meta name="generator" content="pandoc" />
77 <meta name="author" content="Sam Smith" />
88 <meta name="author" content="Jen Jones" />
9− <meta name="date" content="July 15, 2006" />
9+ <meta name="date" content="2006-07-15" />
1010 <title>My S5 Document</title>
1111 <link rel="stylesheet" href="main.css" type="text/css" />
1212 STUFF INSERTED
tests/writer.html+1−1
@@ -6,7 +6,7 @@
66 <meta name="generator" content="pandoc" />
77 <meta name="author" content="John MacFarlane" />
88 <meta name="author" content="Anonymous" />
9− <meta name="date" content="July 17, 2006" />
9+ <meta name="date" content="2006-07-17" />
1010 <title>Pandoc Test Suite</title>
1111</head>
1212<body>