git.iko.soy/typeable/pandoc
on master help sign in
ODT reader: finer-grained errors on parse failure.
f70795dJohn MacFarlane
src/Text/Pandoc/Readers/Odt.hs+18−21
@@ -1,4 +1,3 @@
1−{-# LANGUAGE PatternGuards #-}
2{-# LANGUAGE OverloadedStrings #-}
1{-# LANGUAGE OverloadedStrings #-}
3{- |
2{- |
4 Module : Text.Pandoc.Reader.Odt
3 Module : Text.Pandoc.Reader.Odt
@@ -67,29 +66,27 @@ bytesToOdt bytes = case toArchiveOrFail bytes of
67
66
68--
67--
69archiveToOdt :: Archive -> Either PandocError (Pandoc, MediaBag)
68archiveToOdt :: Archive -> Either PandocError (Pandoc, MediaBag)
70−archiveToOdt archive
69+archiveToOdt archive = either (Left. PandocParseError) Right $ do
70+ let onFailure msg Nothing = Left msg
71+ onFailure _ (Just x) = Right x
72+ contentEntry <- onFailure "Could not find content.xml"
71− | Just contentEntry <- findEntryByPath "content.xml" archive
73+ (findEntryByPath "content.xml" archive)
74+ stylesEntry <- onFailure "Could not find styles.xml"
72− , Just stylesEntry <- findEntryByPath "styles.xml" archive
75+ (findEntryByPath "styles.xml" archive)
76+ contentElem <- onFailure "Could not find content element"
73− , Just contentElem <- entryToXmlElem contentEntry
77+ (entryToXmlElem contentEntry)
78+ stylesElem <- onFailure "Could not find styles element"
74− , Just stylesElem <- entryToXmlElem stylesEntry
79+ (entryToXmlElem stylesEntry)
80+ styles <- either (\_ -> Left "Could not read styles") Right
75− , Right styles <- chooseMax (readStylesAt stylesElem )
81+ (chooseMax (readStylesAt stylesElem ) (readStylesAt contentElem))
76− (readStylesAt contentElem)
77− , media <- filteredFilesFromArchive archive filePathIsOdtMedia
78− , startState <- readerState styles media
79− , Right pandocWithMedia <- runConverter' read_body
80− startState
81− contentElem
82−
83− = Right pandocWithMedia
84−
85− | otherwise
86− -- Not very detailed, but I don't think more information would be helpful
87− = Left $ PandocParseError "Couldn't parse odt file."
88− where
89− filePathIsOdtMedia :: FilePath -> Bool
82+ let filePathIsOdtMedia :: FilePath -> Bool
90 filePathIsOdtMedia fp =
83 filePathIsOdtMedia fp =
91 let (dir, name) = splitFileName fp
84 let (dir, name) = splitFileName fp
92 in (dir == "Pictures/") || (dir /= "./" && name == "content.xml")
85 in (dir == "Pictures/") || (dir /= "./" && name == "content.xml")
86+ let media = filteredFilesFromArchive archive filePathIsOdtMedia
87+ let startState = readerState styles media
88+ either (\_ -> Left "Could not convert opendocument") Right
89+ (runConverter' read_body startState contentElem)
93
90
94
91
95--
92--