Library for parsing epub document metadata (Haskell)

root / app / epub-metadata-example.hs

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
{-
   This is a small app for maintaining the example code that
   goes into the Haddock docs for Codec.Epub
-}

import Codec.Epub
import Codec.Epub.Data.Package
import Control.Monad.Except


main :: IO ()
main = do
   -- epub-metadata actions are in MonadIO and MonadError, so we're
   -- using ErrorT here

   result <- runExceptT $ do

      -- Use the getPkgXmlFromZip action to extract the Package
      -- Document as an XML string. There are also other actions
      -- for reading from ByteStringS and directories.
      --
      -- See Codec.Epub.IO

      xmlString <- getPkgXmlFromZip "/path/to/book.epub"

      -- Now the sections of meta-information about the book can
      -- be extracted from that XML using functions like getPackage,
      -- getMetadata, etc.
      --
      -- See Codec.Epub.Parse

      pkg <- getPackage xmlString  -- :: Codec.Epub.Data.Package
      meta <- getMetadata xmlString  -- :: Codec.Epub.Data.Metadata

      -- Parts of these data structures can be used from here
      -- as needed
      --
      -- See Codec.Epub.Data.Package for pkgVersion below
      -- and the others in Codec.Epub.Data.*

      liftIO $ putStrLn $ pkgVersion pkg

      -- There is also pretty-print formatting of these data types
      -- in Codec.Epub.Data through the Formattable typeclass
      --
      -- See Codec.Epub.Format

      liftIO $ putStr $ format meta

   either putStrLn return result