aboutsummaryrefslogtreecommitdiff
path: root/src/Text/Pandoc/Readers/FB2.hs
blob: 66e390bd7881ac5ff29fd4cad674a94e2b5c7a7d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE TupleSections #-}
{-# LANGUAGE OverloadedStrings #-}
{- |
   Module      : Text.Pandoc.Readers.FB2
   Copyright   : Copyright (C) 2018-2020 Alexander Krotov
   License     : GNU GPL, version 2 or above

   Maintainer  : Alexander Krotov <ilabdsf@gmail.com>
   Stability   : alpha
   Portability : portable

Conversion of FB2 to 'Pandoc' document.
-}

{-

TODO:
 - Tables
 - Named styles
 - Parse ID attribute for all elements that have it

-}

module Text.Pandoc.Readers.FB2 ( readFB2 ) where
import Control.Monad.Except (throwError)
import Control.Monad.State.Strict
import Data.ByteString.Base64.Lazy
import Data.Functor
import Data.List (intersperse)
import qualified Data.Map as M
import Data.Text (Text)
import qualified Data.Text as T
import qualified Data.Text.Lazy as TL
import Data.Default
import Data.Maybe
import Text.HTML.TagSoup.Entity (lookupEntity)
import Text.Pandoc.Builder
import Text.Pandoc.Class.PandocMonad (PandocMonad, insertMedia, report)
import Text.Pandoc.Error
import Text.Pandoc.Logging
import Text.Pandoc.Options
import Text.Pandoc.Shared (crFilter)
import Text.Pandoc.XML.Light
import qualified Text.Pandoc.UTF8 as UTF8

type FB2 m = StateT FB2State m

data FB2State = FB2State{ fb2SectionLevel :: Int
                        , fb2Meta :: Meta
                        , fb2Authors :: [Text]
                        , fb2Notes :: M.Map Text Blocks
                        } deriving Show

instance Default FB2State where
  def = FB2State{ fb2SectionLevel = 1
                , fb2Meta = mempty
                , fb2Authors = []
                , fb2Notes = M.empty
                }

instance HasMeta FB2State where
  setMeta field v s = s {fb2Meta = setMeta field v (fb2Meta s)}
  deleteMeta field s = s {fb2Meta = deleteMeta field (fb2Meta s)}

readFB2 :: PandocMonad m => ReaderOptions -> Text -> m Pandoc
readFB2 _ inp =
  case parseXMLElement $ TL.fromStrict $ crFilter inp of
    Left msg -> throwError $ PandocXMLError "" msg
    Right el ->  do
      (bs, st) <- runStateT (parseRootElement el) 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

trim :: Text -> Text
trim = T.strip

removeHash :: Text -> Text
removeHash t = case T.uncons t of
  Just ('#', xs) -> xs
  _              -> t

convertEntity :: Text -> Text
convertEntity e = maybe (T.toUpper e) T.pack $ lookupEntity (T.unpack e)

parseInline :: PandocMonad m => Content -> FB2 m Inlines
parseInline (Elem e) =
  case qName $ elName e of
    "strong" -> strong <$> parseStyleType e
    "emphasis" -> emph <$> parseStyleType e
    "style" -> parseNamedStyle e
    "a" -> parseLinkType e
    "strikethrough" -> strikeout <$> parseStyleType e
    "sub" -> subscript <$> parseStyleType e
    "sup" -> superscript <$> parseStyleType e
    "code" -> pure $ code $ strContent e
    "image" -> parseInlineImageElement e
    name -> do
      report $ IgnoredElement name
      pure mempty
parseInline (Text x) = pure $ text $ cdData x
parseInline (CRef r) = pure $ str $ convertEntity r

parseSubtitle :: PandocMonad m => Element -> FB2 m Blocks
parseSubtitle e = headerWith ("", ["unnumbered"], []) <$> gets fb2SectionLevel <*> parsePType e

-- * Root element parser

parseRootElement :: PandocMonad m => Element -> FB2 m Blocks
parseRootElement e =
  case qName $ elName e of
    "FictionBook" -> do
      -- Parse notes before parsing the rest of the content.
      case filterChild isNotesBody e of
        Nothing -> pure ()
        Just notesBody -> parseNotesBody notesBody
      -- Parse metadata and content
      mconcat <$> mapM parseFictionBookChild (elChildren e)
    name -> report (UnexpectedXmlElement name "root") $> mempty

-- | Parse notes
parseNotesBody :: PandocMonad m => Element -> FB2 m ()
parseNotesBody e = mempty <$ mapM parseNotesBodyChild (elChildren e)

-- | Parse a child of @\<body name="notes">@ element.
parseNotesBodyChild :: PandocMonad m => Element -> FB2 m ()
parseNotesBodyChild e =
  case qName $ elName e of
    "section" -> parseNote e
    _ -> pure ()

isNotesBody :: Element -> Bool
isNotesBody e =
  qName (elName e) == "body" &&
  findAttr (unqual "name") e == Just "notes"

parseNote :: PandocMonad m => Element -> FB2 m ()
parseNote e =
  case findAttr (unqual "id") e of
    Nothing -> pure ()
    Just sectionId -> do
      content <- mconcat <$> mapM parseSectionChild (dropTitle $ elChildren e)
      oldNotes <- gets fb2Notes
      modify $ \s -> s { fb2Notes = M.insert ("#" <> sectionId) content oldNotes }
      pure ()
  where
    isTitle x = qName (elName x) == "title"
    dropTitle (x:xs) = if isTitle x
                         then xs -- Drop note section <title> if present
                         else x:xs
    dropTitle [] = []

-- | Parse a child of @\<FictionBook>@ element.
parseFictionBookChild :: PandocMonad m => Element -> FB2 m Blocks
parseFictionBookChild e =
  case qName $ elName e of
    "stylesheet" -> pure mempty -- stylesheet is ignored
    "description" -> mempty <$ mapM_ parseDescriptionChild (elChildren e)
    "body" -> if isNotesBody e
                then pure mempty
                else mconcat <$> mapM parseBodyChild (elChildren e)
    "binary" -> mempty <$ parseBinaryElement e
    name -> report (UnexpectedXmlElement name "FictionBook") $> mempty

-- | Parse a child of @\<description>@ element.
parseDescriptionChild :: PandocMonad m => Element -> FB2 m ()
parseDescriptionChild e =
  case qName $ elName e of
    "title-info" -> mapM_ parseTitleInfoChild (elChildren e)
    "src-title-info" -> pure () -- ignore
    "document-info" -> pure ()
    "publish-info" -> pure ()
    "custom-info" -> pure ()
    "output" -> pure ()
    name -> do
      report $ IgnoredElement $ name <> " in description"
      pure mempty

-- | Parse a child of @\<body>@ element.
parseBodyChild :: PandocMonad m => Element -> FB2 m Blocks
parseBodyChild e =
  case qName $ elName e of
    "image" -> parseImageElement e
    "title" -> header <$> gets fb2SectionLevel <*> parseTitleType (elContent e)
    "epigraph" -> parseEpigraph e
    "section" -> parseSection e
    name -> report (UnexpectedXmlElement name "body") $> mempty

-- | Parse a @\<binary>@ element.
parseBinaryElement :: PandocMonad m => Element -> FB2 m ()
parseBinaryElement e =
  case (findAttr (unqual "id") e, findAttr (unqual "content-type") e) of
    (Nothing, _) -> report $ IgnoredElement "binary without id attribute"
    (Just _, Nothing) ->
      report $ IgnoredElement "binary without content-type attribute"
    (Just filename, contentType) ->
      insertMedia (T.unpack filename) contentType
                    (decodeLenient
                      (UTF8.fromTextLazy . TL.fromStrict . strContent $ e))

-- * Type parsers

-- | Parse @authorType@
parseAuthor :: PandocMonad m => Element -> FB2 m Text
parseAuthor e = T.unwords . catMaybes <$> mapM parseAuthorChild (elChildren e)

parseAuthorChild :: PandocMonad m => Element -> FB2 m (Maybe Text)
parseAuthorChild e =
  case qName $ elName e of
    "first-name" -> pure $ Just $ strContent e
    "middle-name" -> pure $ Just $ strContent e
    "last-name" -> pure $ Just $ strContent e
    "nickname" -> pure $ Just $ strContent e
    "home-page" -> pure $ Just $ strContent e
    "email" -> pure $ Just $ strContent e
    name -> do
      report $ IgnoredElement $ name <> " in author"
      pure Nothing

-- | Parse @titleType@
parseTitle :: PandocMonad m => Element -> FB2 m Blocks
parseTitle e = header <$> gets fb2SectionLevel <*> parseTitleType (elContent e)

parseTitleType :: PandocMonad m => [Content] -> FB2 m Inlines
parseTitleType c = mconcat . intersperse linebreak . catMaybes <$> mapM parseTitleContent c

parseTitleContent :: PandocMonad m => Content -> FB2 m (Maybe Inlines)
parseTitleContent (Elem e) =
  case qName $ elName e of
    "p" -> Just <$> parsePType e
    "empty-line" -> pure $ Just mempty
    _ -> pure mempty
parseTitleContent _ = pure Nothing

-- | Parse @imageType@
parseImageElement :: PandocMonad m => Element -> FB2 m Blocks
parseImageElement e =
  case href of
    Just src -> pure $ para $ imageWith (imgId, [], []) (removeHash src) title alt
    Nothing -> do
      report $ IgnoredElement " image without href"
      pure mempty
  where alt = maybe mempty str $ findAttr (unqual "alt") e
        title = fromMaybe "" $ findAttr (unqual "title") e
        imgId = fromMaybe "" $ findAttr (unqual "id") e
        href = findAttr (QName "href" (Just "http://www.w3.org/1999/xlink") Nothing) e

-- | Parse @pType@
parsePType :: PandocMonad m => Element -> FB2 m Inlines
parsePType = parseStyleType -- TODO add support for optional "id" and "style" attributes

-- | Parse @citeType@
parseCite :: PandocMonad m => Element -> FB2 m Blocks
parseCite e = blockQuote . mconcat <$> mapM parseCiteChild (elChildren e)

-- | Parse @citeType@ child
parseCiteChild :: PandocMonad m => Element -> FB2 m Blocks
parseCiteChild e =
  case qName $ elName e of
    "p" -> para <$> parsePType e
    "poem" -> parsePoem e
    "empty-line" -> pure horizontalRule
    "subtitle" -> parseSubtitle e
    "table" -> parseTable e
    "text-author" -> para <$> parsePType e
    name -> report (UnexpectedXmlElement name "cite") $> mempty

-- | Parse @poemType@
parsePoem :: PandocMonad m => Element -> FB2 m Blocks
parsePoem e = mconcat <$> mapM parsePoemChild (elChildren e)

parsePoemChild :: PandocMonad m => Element -> FB2 m Blocks
parsePoemChild e =
  case qName $ elName e of
    "title" -> parseTitle e
    "subtitle" -> parseSubtitle e
    "epigraph" -> parseEpigraph e
    "stanza" -> parseStanza e
    "text-author" -> para <$> parsePType e
    "date" -> pure $ para $ text $ strContent e
    name -> report (UnexpectedXmlElement name "poem") $> mempty

parseStanza :: PandocMonad m => Element -> FB2 m Blocks
parseStanza e = fromList . joinLineBlocks . toList . mconcat <$> mapM parseStanzaChild (elChildren e)

joinLineBlocks :: [Block] -> [Block]
joinLineBlocks (LineBlock xs:LineBlock ys:zs) = joinLineBlocks (LineBlock (xs ++ ys) : zs)
joinLineBlocks (x:xs) = x:joinLineBlocks xs
joinLineBlocks [] = []

parseStanzaChild :: PandocMonad m => Element -> FB2 m Blocks
parseStanzaChild e =
  case qName $ elName e of
    "title" -> parseTitle e
    "subtitle" -> parseSubtitle e
    "v" -> lineBlock . (:[]) <$> parsePType e
    name -> report (UnexpectedXmlElement name "stanza") $> mempty

-- | Parse @epigraphType@
parseEpigraph :: PandocMonad m => Element -> FB2 m Blocks
parseEpigraph e =
  divWith (divId, ["epigraph"], []) . mconcat <$> mapM parseEpigraphChild (elChildren e)
  where divId = fromMaybe "" $ findAttr (unqual "id") e

parseEpigraphChild :: PandocMonad m => Element -> FB2 m Blocks
parseEpigraphChild e =
  case qName $ elName e of
    "p" -> para <$> parsePType e
    "poem" -> parsePoem e
    "cite" -> parseCite e
    "empty-line" -> pure horizontalRule
    "text-author" -> para <$> parsePType e
    name -> report (UnexpectedXmlElement name "epigraph") $> mempty

-- | Parse @annotationType@
parseAnnotation :: PandocMonad m => Element -> FB2 m Blocks
parseAnnotation e = mconcat <$> mapM parseAnnotationChild (elChildren e)

parseAnnotationChild :: PandocMonad m => Element -> FB2 m Blocks
parseAnnotationChild e =
  case qName $ elName e of
    "p" -> para <$> parsePType e
    "poem" -> parsePoem e
    "cite" -> parseCite e
    "subtitle" -> parseSubtitle e
    "table" -> parseTable e
    "empty-line" -> pure horizontalRule
    name -> report (UnexpectedXmlElement name "annotation") $> mempty

-- | Parse @sectionType@
parseSection :: PandocMonad m => Element -> FB2 m Blocks
parseSection e = do
  n <- gets fb2SectionLevel
  modify $ \st -> st{ fb2SectionLevel = n + 1 }
  let sectionId = fromMaybe "" $ findAttr (unqual "id") e
  bs <- divWith (sectionId, ["section"], []) . mconcat <$> mapM parseSectionChild (elChildren e)
  modify $ \st -> st{ fb2SectionLevel = n }
  pure bs

parseSectionChild :: PandocMonad m => Element -> FB2 m Blocks
parseSectionChild e =
  case qName $ elName e of
    "title" -> parseBodyChild e
    "epigraph" -> parseEpigraph e
    "image" -> parseImageElement e
    "annotation" -> parseAnnotation e
    "poem" -> parsePoem e
    "cite" -> parseCite e
    "empty-line" -> pure horizontalRule
    "table" -> parseTable e
    "subtitle" -> parseSubtitle e
    "p" -> para <$> parsePType e
    "section" -> parseSection e
    name -> report (UnexpectedXmlElement name "section") $> mempty

-- | parse @styleType@
parseStyleType :: PandocMonad m => Element -> FB2 m Inlines
parseStyleType e = mconcat <$> mapM parseInline (elContent e)

-- | Parse @namedStyleType@
parseNamedStyle :: PandocMonad m => Element -> FB2 m Inlines
parseNamedStyle e = do
  content <- mconcat <$> mapM parseNamedStyleChild (elContent e)
  let lang = maybeToList $ ("lang",) <$> findAttr (QName "lang" Nothing (Just "xml")) e
  case findAttr (unqual "name") e of
    Just name -> pure $ spanWith ("", [name], lang) content
    Nothing -> do
      report $ IgnoredElement "link without required name"
      pure mempty

parseNamedStyleChild :: PandocMonad m => Content -> FB2 m Inlines
parseNamedStyleChild (Elem e) =
  case qName (elName e) of
    "strong" -> strong <$> parseStyleType e
    "emphasis" -> emph <$> parseStyleType e
    "style" -> parseNamedStyle e
    "a" -> parseLinkType e
    "strikethrough" -> strikeout <$> parseStyleType e
    "sub" -> subscript <$> parseStyleType e
    "sup" -> superscript <$> parseStyleType e
    "code" -> pure $ code $ strContent e
    "image" -> parseInlineImageElement e
    name -> do
      report $ IgnoredElement $ name <> " in style"
      pure mempty
parseNamedStyleChild x = parseInline x

-- | Parse @linkType@
parseLinkType :: PandocMonad m => Element -> FB2 m Inlines
parseLinkType e = do
  content <- mconcat <$> mapM parseStyleLinkType (elContent e)
  notes <- gets fb2Notes
  case findAttr (QName "href" (Just "http://www.w3.org/1999/xlink") Nothing) e of
    Just href -> case findAttr (QName "type" Nothing Nothing) e of
                   Just "note" -> case M.lookup href notes of
                                    Nothing -> pure $ link href "" content
                                    Just contents -> pure $ note contents
                   _ -> pure $ link href "" content
    Nothing -> do
      report $ IgnoredElement "link without required href"
      pure mempty

-- | Parse @styleLinkType@
parseStyleLinkType :: PandocMonad m => Content -> FB2 m Inlines
parseStyleLinkType x@(Elem e) =
  case qName (elName e) of
    "a" -> do
      report $ IgnoredElement "nested link"
      pure mempty
    _ -> parseInline x
parseStyleLinkType x = parseInline x

-- | Parse @tableType@
parseTable :: PandocMonad m => Element -> FB2 m Blocks
parseTable _ = pure mempty -- TODO: tables are not supported yet

-- | Parse @title-infoType@
parseTitleInfoChild :: PandocMonad m => Element -> FB2 m ()
parseTitleInfoChild e =
  case qName (elName e) of
    "genre" -> pure ()
    "author" -> parseAuthor e >>= \author -> modify (\st -> st {fb2Authors = author:fb2Authors st})
    "book-title" -> modify (setMeta "title" (text $ strContent e))
    "annotation" -> parseAnnotation e >>= modify . setMeta "abstract"
    "keywords" -> modify (setMeta "keywords" (map (MetaString . trim) $ T.splitOn ","
                                                                      $ strContent e))
    "date" -> modify (setMeta "date" (text $ strContent e))
    "coverpage" -> parseCoverPage e
    "lang" -> pure ()
    "src-lang" -> pure ()
    "translator" -> pure ()
    "sequence" -> pure ()
    name -> report $ IgnoredElement $ name <> " in title-info"

parseCoverPage :: PandocMonad m => Element -> FB2 m ()
parseCoverPage e =
  case findChild (QName "image" (Just "http://www.gribuser.ru/xml/fictionbook/2.0") Nothing) e of
    Just img -> case href of
                  Just src -> modify (setMeta "cover-image" (MetaString $ removeHash src))
                  Nothing -> pure ()
                where href = findAttr (QName "href" (Just "http://www.w3.org/1999/xlink") Nothing) img
    Nothing -> pure ()

-- | Parse @inlineImageType@ element
parseInlineImageElement :: PandocMonad m
                        => Element
                        -> FB2 m Inlines
parseInlineImageElement e =
  case href of
    Just src -> pure $ imageWith ("", [], []) (removeHash src) "" alt
    Nothing -> do
      report $ IgnoredElement "inline image without href"
      pure mempty
  where alt = maybe mempty str $ findAttr (unqual "alt") e
        href = findAttr (QName "href" (Just "http://www.w3.org/1999/xlink") Nothing) e