@@ -211,11 +211,10 @@ writeDocx opts doc@(Pandoc meta _) = do
211211 let doc' = stripInvalidChars . walk fixDisplayMath $ doc
212212 username <- lookup "USERNAME" <$> getEnvironment
213213 utctime <- getCurrentTime
214− refArchive <- liftM (toArchive . toLazy) $
215− case writerReferenceDocx opts of
216− Just f -> B.readFile f
217− Nothing -> readDataFile datadir "reference.docx"
218− distArchive <- liftM (toArchive . toLazy) $ readDataFile datadir "reference.docx"
214+ distArchive <- getDefaultReferenceDocx datadir
215+ refArchive <- case writerReferenceDocx opts of
216+ Just f -> liftM (toArchive . toLazy) $ B.readFile f
217+ Nothing -> return distArchive
219218
220219 parsedDoc <- parseXml refArchive distArchive "word/document.xml"
221220 let wname f qn = qPrefix qn == Just "w" && f (qName qn)
@@ -474,7 +473,7 @@ writeDocx opts doc@(Pandoc meta _) = do
474473 settingsEntry <- copyChildren refArchive distArchive settingsPath epochtime settingsList
475474
476475 let entryFromArchive arch path =
477− maybe (fail $ path ++ " corrupt or missing in reference docx")
476+ maybe (fail $ path ++ " missing in reference docx")
478477 return
479478 (findEntryByPath path arch `mplus` findEntryByPath path distArchive)
480479 docPropsAppEntry <- entryFromArchive refArchive "docProps/app.xml"
@@ -1204,11 +1203,12 @@ defaultFootnotes = [ mknode "w:footnote"
12041203
12051204parseXml :: Archive -> Archive -> String -> IO Element
12061205parseXml refArchive distArchive relpath =
1207− case ((findEntryByPath relpath refArchive `mplus`
1208− findEntryByPath relpath distArchive)
1209− >>= parseXMLDoc . UTF8.toStringLazy . fromEntry) of
1210− Just d -> return d
1211− Nothing -> fail $ relpath ++ " corrupt or missing in reference docx"
1206+ case findEntryByPath relpath refArchive `mplus`
1207+ findEntryByPath relpath distArchive of
1208+ Nothing -> fail $ relpath ++ " missing in reference docx"
1209+ Just e -> case parseXMLDoc . UTF8.toStringLazy . fromEntry $ e of
1210+ Nothing -> fail $ relpath ++ " corrupt in reference docx"
1211+ Just d -> return d
12121212
12131213-- | Scales the image to fit the page
12141214-- sizes are passed in emu
@@ -1219,3 +1219,28 @@ fitToPage (x, y) pageWidth
12191219 (pageWidth, round $
12201220 ((fromIntegral pageWidth) / ((fromIntegral :: Integer -> Double) x)) * (fromIntegral y))
12211221 | otherwise = (x, y)
1222+
1223+getDefaultReferenceDocx :: Maybe FilePath -> IO Archive
1224+getDefaultReferenceDocx datadir = do
1225+ let paths = ["[Content_Types].xml",
1226+ "_rels/.rels",
1227+ "docProps/app.xml",
1228+ "docProps/core.xml",
1229+ "word/document.xml",
1230+ "word/fontTable.xml",
1231+ "word/footnotes.xml",
1232+ "word/numbering.xml",
1233+ "word/settings.xml",
1234+ "word/webSettings.xml",
1235+ "word/styles.xml",
1236+ "word/_rels/document.xml.rels",
1237+ "word/_rels/footnotes.xml.rels",
1238+ "word/theme/theme1.xml"]
1239+ let pathToEntry path = do epochtime <- (floor . utcTimeToPOSIXSeconds) <$>
1240+ getCurrentTime
1241+ contents <- toLazy <$> readDataFile datadir
1242+ ("docx/" ++ path)
1243+ return $ toEntry path epochtime contents
1244+ entries <- mapM pathToEntry paths
1245+ let archive = foldr addEntryToArchive emptyArchive entries
1246+ return archive