aboutsummaryrefslogtreecommitdiff
path: root/src/Text/Pandoc
diff options
context:
space:
mode:
authorAlexander Krotov <ilabdsf@gmail.com>2019-05-11 10:34:17 +0300
committerAlexander Krotov <ilabdsf@gmail.com>2019-05-11 10:34:17 +0300
commite9343b96bc043828595ff9b68aacf7f7005fc54a (patch)
treea8944aff08a16b3d523202016920249f7d7f86af /src/Text/Pandoc
parent692f88fd8fd343478428407a21a71b3d1e9cdbe8 (diff)
downloadpandoc-e9343b96bc043828595ff9b68aacf7f7005fc54a.tar.gz
FB2 reader: use XML.Light.Input.parseXMLDoc to parse the root element
Diffstat (limited to 'src/Text/Pandoc')
-rw-r--r--src/Text/Pandoc/Readers/FB2.hs23
1 files changed, 11 insertions, 12 deletions
diff --git a/src/Text/Pandoc/Readers/FB2.hs b/src/Text/Pandoc/Readers/FB2.hs
index 484d5ceb8..d22dca8b0 100644
--- a/src/Text/Pandoc/Readers/FB2.hs
+++ b/src/Text/Pandoc/Readers/FB2.hs
@@ -61,14 +61,15 @@ instance HasMeta FB2State where
deleteMeta field s = s {fb2Meta = deleteMeta field (fb2Meta s)}
readFB2 :: PandocMonad m => ReaderOptions -> Text -> m Pandoc
-readFB2 _ inp = do
- let parsedXml = parseXML $ crFilter inp
-
- (bs, st) <- runStateT (mapM parseBlock $ parsedXml) def
- let authors = if null $ fb2Authors st
- then id
- else setMeta "author" (map text $ reverse $ fb2Authors st)
- pure $ Pandoc (authors $ fb2Meta st) (toList . mconcat $ bs)
+readFB2 _ inp =
+ case parseXMLDoc $ crFilter inp of
+ Nothing -> throwError $ PandocParseError "Not an XML document"
+ Just e -> do
+ (bs, st) <- runStateT (parseRootElement e) def
+ let authors = if null $ fb2Authors st
+ then id
+ else setMeta "author" (map text $ reverse $ fb2Authors st)
+ pure $ Pandoc (authors $ fb2Meta st) $ toList bs
-- * Utility functions
@@ -103,13 +104,11 @@ parseSubtitle e = headerWith ("", ["unnumbered"], []) <$> gets fb2SectionLevel <
-- * Root element parser
-parseBlock :: PandocMonad m => Content -> FB2 m Blocks
-parseBlock (Elem e) =
+parseRootElement :: PandocMonad m => Element -> FB2 m Blocks
+parseRootElement e =
case qName $ elName e of
- "?xml" -> pure mempty
"FictionBook" -> mconcat <$> mapM parseFictionBookChild (elChildren e)
name -> report (UnexpectedXmlElement name "root") $> mempty
-parseBlock _ = pure mempty
-- | Parse a child of @\<FictionBook>@ element.
parseFictionBookChild :: PandocMonad m => Element -> FB2 m Blocks