git.iko.soy/typeable/pandoc
on master help sign in
FB2 reader: use XML.Light.Input.parseXMLDoc to parse the root element
e9343b9Alexander Krotov
src/Text/Pandoc/Readers/FB2.hs+11−12
@@ -61,14 +61,15 @@ instance HasMeta FB2State where
61 deleteMeta field s = s {fb2Meta = deleteMeta field (fb2Meta s)}
61 deleteMeta field s = s {fb2Meta = deleteMeta field (fb2Meta s)}
62
62
63readFB2 :: PandocMonad m => ReaderOptions -> Text -> m Pandoc
63readFB2 :: PandocMonad m => ReaderOptions -> Text -> m Pandoc
64−readFB2 _ inp = do
64+readFB2 _ inp =
65− let parsedXml = parseXML $ crFilter inp
65+ case parseXMLDoc $ crFilter inp of
66−
66+ Nothing -> throwError $ PandocParseError "Not an XML document"
67+ Just e -> do
67− (bs, st) <- runStateT (mapM parseBlock $ parsedXml) def
68+ (bs, st) <- runStateT (parseRootElement e) def
68− let authors = if null $ fb2Authors st
69+ let authors = if null $ fb2Authors st
69− then id
70+ then id
70− else setMeta "author" (map text $ reverse $ fb2Authors st)
71+ else setMeta "author" (map text $ reverse $ fb2Authors st)
71− pure $ Pandoc (authors $ fb2Meta st) (toList . mconcat $ bs)
72+ pure $ Pandoc (authors $ fb2Meta st) $ toList bs
72
73
73-- * Utility functions
74-- * Utility functions
74
75
@@ -103,13 +104,11 @@ parseSubtitle e = headerWith ("", ["unnumbered"], []) <$> gets fb2SectionLevel <
103
104
104-- * Root element parser
105-- * Root element parser
105
106
106−parseBlock :: PandocMonad m => Content -> FB2 m Blocks
107+parseRootElement :: PandocMonad m => Element -> FB2 m Blocks
107−parseBlock (Elem e) =
108+parseRootElement e =
108 case qName $ elName e of
109 case qName $ elName e of
109− "?xml" -> pure mempty
110 "FictionBook" -> mconcat <$> mapM parseFictionBookChild (elChildren e)
110 "FictionBook" -> mconcat <$> mapM parseFictionBookChild (elChildren e)
111 name -> report (UnexpectedXmlElement name "root") $> mempty
111 name -> report (UnexpectedXmlElement name "root") $> mempty
112−parseBlock _ = pure mempty
113
112
114-- | Parse a child of @\<FictionBook>@ element.
113-- | Parse a child of @\<FictionBook>@ element.
115parseFictionBookChild :: PandocMonad m => Element -> FB2 m Blocks
114parseFictionBookChild :: PandocMonad m => Element -> FB2 m Blocks