Skip to content

Commit 5ce91a7

Browse files
author
Alexander Krotov
committed
FB2 reader: replace some errors with warnings
Now FB2 reader can read writer.fb2, which does not validate (yet).
1 parent 287c171 commit 5ce91a7

File tree

2 files changed

+18
-9
lines changed

2 files changed

+18
-9
lines changed

src/Text/Pandoc/Logging.hs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

106107
instance 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

216220
showPos :: 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

309315
messageVerbosity:: LogMessage -> Verbosity
310316
messageVerbosity 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

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,17 @@ import Control.Monad.State.Strict
4646
import Data.ByteString.Lazy.Char8 ( pack )
4747
import Data.ByteString.Base64.Lazy
4848
import Data.Char (isSpace, toUpper)
49+
import Data.Functor
4950
import Data.List (dropWhileEnd, intersperse)
5051
import Data.List.Split (splitOn)
5152
import Data.Text (Text)
5253
import Data.Default
5354
import Data.Maybe
5455
import Text.HTML.TagSoup.Entity (lookupEntity)
5556
import Text.Pandoc.Builder
56-
import Text.Pandoc.Class (PandocMonad, insertMedia)
57+
import Text.Pandoc.Class (PandocMonad, insertMedia, report)
5758
import Text.Pandoc.Error
59+
import Text.Pandoc.Logging
5860
import Text.Pandoc.Options
5961
import Text.Pandoc.Shared (crFilter)
6062
import 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
126128
parseBlock _ = 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.
139141
parseDescriptionChild :: 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@
232234
parsePoem :: 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

246248
parseStanza :: PandocMonad m => Element -> FB2 m Blocks
247249
parseStanza 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@
263265
parseEpigraph :: 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@
279281
parseAnnotation :: 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@
294296
parseSection :: 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@
320322
parseStyleType :: PandocMonad m => Element -> FB2 m Inlines

0 commit comments

Comments
 (0)