@@ -61,14 +61,15 @@ instance HasMeta FB2State where
6161 deleteMeta field s = s {fb2Meta = deleteMeta field (fb2Meta s)}
6262
6363readFB2 :: PandocMonad m => ReaderOptions -> Text -> m Pandoc
64−readFB2 _ inp = do
65− let parsedXml = parseXML $ crFilter inp
66−
67− (bs, st) <- runStateT (mapM parseBlock $ parsedXml) def
68− let authors = if null $ fb2Authors st
69− then id
70− else setMeta "author" (map text $ reverse $ fb2Authors st)
71− pure $ Pandoc (authors $ fb2Meta st) (toList . mconcat $ bs)
64+readFB2 _ inp =
65+ case parseXMLDoc $ crFilter inp of
66+ Nothing -> throwError $ PandocParseError "Not an XML document"
67+ Just e -> do
68+ (bs, st) <- runStateT (parseRootElement e) def
69+ let authors = if null $ fb2Authors st
70+ then id
71+ else setMeta "author" (map text $ reverse $ fb2Authors st)
72+ pure $ Pandoc (authors $ fb2Meta st) $ toList bs
7273
7374-- * Utility functions
7475
@@ -103,13 +104,11 @@ parseSubtitle e = headerWith ("", ["unnumbered"], []) <$> gets fb2SectionLevel <
103104
104105-- * Root element parser
105106
106−parseBlock :: PandocMonad m => Content -> FB2 m Blocks
107−parseBlock (Elem e) =
107+parseRootElement :: PandocMonad m => Element -> FB2 m Blocks
108+parseRootElement e =
108109 case qName $ elName e of
109− "?xml" -> pure mempty
110110 "FictionBook" -> mconcat <$> mapM parseFictionBookChild (elChildren e)
111111 name -> report (UnexpectedXmlElement name "root") $> mempty
112−parseBlock _ = pure mempty
113112
114113-- | Parse a child of @\<FictionBook>@ element.
115114parseFictionBookChild :: PandocMonad m => Element -> FB2 m Blocks