git.iko.soy/typeable/pandoc
on master help sign in
FB2 reader: replace some errors with warnings

Now FB2 reader can read writer.fb2, which does not validate (yet).

commit5ce91a7e01ba3d9362d91c838052e33729efd6ec authorAlexander Krotov <ilabdsf@gmail.com> dateFri 27 Apr 2018, 23:33 (9 years ago) parent287c171
2 files changed+18−9
src/Text/Pandoc/Logging.hs+7 src/Text/Pandoc/Readers/FB2.hs+11−9
src/Text/Pandoc/Logging.hs+7
@@ -101,6 +101,7 @@ data LogMessage =
101101 | Deprecated String String
102102 | NoTranslation String
103103 | CouldNotLoadTranslations String String
104+ | UnexpectedXmlElement String String
104105 deriving (Show, Eq, Data, Ord, Typeable, Generic)
105106
106107instance ToJSON LogMessage where
@@ -211,6 +212,9 @@ instance ToJSON LogMessage where
211212 CouldNotLoadTranslations lang msg ->
212213 ["lang" .= Text.pack lang,
213214 "message" .= Text.pack msg]
215+ UnexpectedXmlElement element parent ->
216+ ["element" .= Text.pack element,
217+ "parent" .= Text.pack parent]
214218
215219
216220showPos :: SourcePos -> String
@@ -305,6 +309,8 @@ showLogMessage msg =
305309 CouldNotLoadTranslations lang m ->
306310 "Could not load translations for " ++ lang ++
307311 if null m then "" else '\n' : m
312+ UnexpectedXmlElement element parent ->
313+ "Unexpected XML element " ++ element ++ " in " ++ parent
308314
309315messageVerbosity:: LogMessage -> Verbosity
310316messageVerbosity msg =
@@ -342,3 +348,4 @@ messageVerbosity msg =
342348 Deprecated{} -> WARNING
343349 NoTranslation{} -> WARNING
344350 CouldNotLoadTranslations{} -> WARNING
351+ UnexpectedXmlElement {} -> WARNING
src/Text/Pandoc/Readers/FB2.hs+11−9
@@ -46,6 +46,7 @@ import Control.Monad.State.Strict
4646import Data.ByteString.Lazy.Char8 ( pack )
4747import Data.ByteString.Base64.Lazy
4848import Data.Char (isSpace, toUpper)
49+import Data.Functor
4950import Data.List (dropWhileEnd, intersperse)
5051import Data.List.Split (splitOn)
5152import Data.Text (Text)
@@ -53,8 +54,9 @@ import Data.Default
5354import Data.Maybe
5455import Text.HTML.TagSoup.Entity (lookupEntity)
5556import Text.Pandoc.Builder
56−import Text.Pandoc.Class (PandocMonad, insertMedia)
57+import Text.Pandoc.Class (PandocMonad, insertMedia, report)
5758import Text.Pandoc.Error
59+import Text.Pandoc.Logging
5860import Text.Pandoc.Options
5961import Text.Pandoc.Shared (crFilter)
6062import Text.XML.Light
@@ -122,7 +124,7 @@ parseBlock (Elem e) =
122124 case qName $ elName e of
123125 "?xml" -> pure mempty
124126 "FictionBook" -> mconcat <$> mapM parseFictionBookChild (elChildren e)
125− name -> throwError $ PandocParseError ("Couldn't parse FB2 file: unexpected element " ++ name ++ ".")
127+ name -> report (UnexpectedXmlElement name "root") $> mempty
126128parseBlock _ = pure mempty
127129
128130-- | Parse a child of @\<FictionBook>@ element.
@@ -133,7 +135,7 @@ parseFictionBookChild e =
133135 "description" -> mempty <$ mapM_ parseDescriptionChild (elChildren e)
134136 "body" -> mconcat <$> mapM parseBodyChild (elChildren e)
135137 "binary" -> mempty <$ parseBinaryElement e
136− name -> throwError $ PandocParseError ("Couldn't parse FB2 file: unexpected element " ++ name ++ "in FictionBook.")
138+ name -> report (UnexpectedXmlElement name "FictionBook") $> mempty
137139
138140-- | Parse a child of @\<description>@ element.
139141parseDescriptionChild :: PandocMonad m => Element -> FB2 m ()
@@ -226,7 +228,7 @@ parseCiteChild e =
226228 "subtitle" -> parseSubtitle e
227229 "table" -> parseTable e
228230 "text-author" -> para <$> parsePType e
229− name -> throwError $ PandocParseError ("Couldn't parse FB2 file: unexpected element " ++ name ++ " in cite.")
231+ name -> report (UnexpectedXmlElement name "cite") $> mempty
230232
231233-- | Parse @poemType@
232234parsePoem :: PandocMonad m => Element -> FB2 m Blocks
@@ -241,7 +243,7 @@ parsePoemChild e =
241243 "stanza" -> parseStanza e
242244 "text-author" -> para <$> parsePType e
243245 "date" -> pure $ para $ text $ strContent e
244− name -> throwError $ PandocParseError ("Couldn't parse FB2 file: unexpected element " ++ name ++ " in poem.")
246+ name -> report (UnexpectedXmlElement name "poem") $> mempty
245247
246248parseStanza :: PandocMonad m => Element -> FB2 m Blocks
247249parseStanza e = fromList . joinLineBlocks . toList . mconcat <$> mapM parseStanzaChild (elChildren e)
@@ -257,7 +259,7 @@ parseStanzaChild e =
257259 "title" -> parseTitle e
258260 "subtitle" -> parseSubtitle e
259261 "v" -> lineBlock . (:[]) <$> parsePType e
260− name -> throwError $ PandocParseError ("Couldn't parse FB2 file: unexpected element " ++ name ++ " in stanza.")
262+ name -> report (UnexpectedXmlElement name "stanza") $> mempty
261263
262264-- | Parse @epigraphType@
263265parseEpigraph :: PandocMonad m => Element -> FB2 m Blocks
@@ -273,7 +275,7 @@ parseEpigraphChild e =
273275 "cite" -> parseCite e
274276 "empty-line" -> pure horizontalRule
275277 "text-author" -> para <$> parsePType e
276− name -> throwError $ PandocParseError ("Couldn't parse FB2 file: unexpected element " ++ name ++ " in epigraph.")
278+ name -> report (UnexpectedXmlElement name "epigraph") $> mempty
277279
278280-- | Parse @annotationType@
279281parseAnnotation :: PandocMonad m => Element -> FB2 m Blocks
@@ -288,7 +290,7 @@ parseAnnotationChild e =
288290 "subtitle" -> parseSubtitle e
289291 "table" -> parseTable e
290292 "empty-line" -> pure horizontalRule
291− name -> throwError $ PandocParseError ("Couldn't parse FB2 file: unexpected element " ++ name ++ " in annotation.")
293+ name -> report (UnexpectedXmlElement name "annotation") $> mempty
292294
293295-- | Parse @sectionType@
294296parseSection :: PandocMonad m => Element -> FB2 m Blocks
@@ -314,7 +316,7 @@ parseSectionChild e =
314316 "subtitle" -> parseSubtitle e
315317 "p" -> para <$> parsePType e
316318 "section" -> parseSection e
317− name -> throwError $ PandocParseError ("Couldn't parse FB2 file: unexpected element " ++ name ++ " in section.")
319+ name -> report (UnexpectedXmlElement name "section") $> mempty
318320
319321-- | parse @styleType@
320322parseStyleType :: PandocMonad m => Element -> FB2 m Inlines