aboutsummaryrefslogtreecommitdiff
path: root/src/Text/Pandoc
diff options
context:
space:
mode:
authorJohn MacFarlane <jgm@berkeley.edu>2021-02-08 09:39:29 -0800
committerJohn MacFarlane <jgm@berkeley.edu>2021-02-08 09:39:59 -0800
commitf70795dc5e431c0132acc8bb2d20bb5dec942de7 (patch)
tree2c9b21f72a1176251a9bd6bf8e873e334a7fea0c /src/Text/Pandoc
parent5cd1c1001fbfc768fdb146f14ae056a9a7ba94eb (diff)
downloadpandoc-f70795dc5e431c0132acc8bb2d20bb5dec942de7.tar.gz
ODT reader: finer-grained errors on parse failure.
See #7091.
Diffstat (limited to 'src/Text/Pandoc')
-rw-r--r--src/Text/Pandoc/Readers/Odt.hs39
1 files changed, 18 insertions, 21 deletions
diff --git a/src/Text/Pandoc/Readers/Odt.hs b/src/Text/Pandoc/Readers/Odt.hs
index 9b66b60ec..9943d3147 100644
--- a/src/Text/Pandoc/Readers/Odt.hs
+++ b/src/Text/Pandoc/Readers/Odt.hs
@@ -1,4 +1,3 @@
-{-# LANGUAGE PatternGuards #-}
{-# LANGUAGE OverloadedStrings #-}
{- |
Module : Text.Pandoc.Reader.Odt
@@ -67,29 +66,27 @@ bytesToOdt bytes = case toArchiveOrFail bytes of
--
archiveToOdt :: Archive -> Either PandocError (Pandoc, MediaBag)
-archiveToOdt archive
- | Just contentEntry <- findEntryByPath "content.xml" archive
- , Just stylesEntry <- findEntryByPath "styles.xml" archive
- , Just contentElem <- entryToXmlElem contentEntry
- , Just stylesElem <- entryToXmlElem stylesEntry
- , Right styles <- chooseMax (readStylesAt stylesElem )
- (readStylesAt contentElem)
- , media <- filteredFilesFromArchive archive filePathIsOdtMedia
- , startState <- readerState styles media
- , Right pandocWithMedia <- runConverter' read_body
- startState
- contentElem
-
- = Right pandocWithMedia
-
- | otherwise
- -- Not very detailed, but I don't think more information would be helpful
- = Left $ PandocParseError "Couldn't parse odt file."
- where
- filePathIsOdtMedia :: FilePath -> Bool
+archiveToOdt archive = either (Left. PandocParseError) Right $ do
+ let onFailure msg Nothing = Left msg
+ onFailure _ (Just x) = Right x
+ contentEntry <- onFailure "Could not find content.xml"
+ (findEntryByPath "content.xml" archive)
+ stylesEntry <- onFailure "Could not find styles.xml"
+ (findEntryByPath "styles.xml" archive)
+ contentElem <- onFailure "Could not find content element"
+ (entryToXmlElem contentEntry)
+ stylesElem <- onFailure "Could not find styles element"
+ (entryToXmlElem stylesEntry)
+ styles <- either (\_ -> Left "Could not read styles") Right
+ (chooseMax (readStylesAt stylesElem ) (readStylesAt contentElem))
+ let filePathIsOdtMedia :: FilePath -> Bool
filePathIsOdtMedia fp =
let (dir, name) = splitFileName fp
in (dir == "Pictures/") || (dir /= "./" && name == "content.xml")
+ let media = filteredFilesFromArchive archive filePathIsOdtMedia
+ let startState = readerState styles media
+ either (\_ -> Left "Could not convert opendocument") Right
+ (runConverter' read_body startState contentElem)
--