aboutsummaryrefslogtreecommitdiff
path: root/src/Text/Pandoc/Readers/ODT.hs
blob: e4c2900ff5be0eccac0289af301d10302dcd0985 (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
{-# LANGUAGE OverloadedStrings #-}
{- |
   Module      : Text.Pandoc.Reader.ODT
   Copyright   : Copyright (C) 2015 Martin Linnemann
   License     : GNU GPL, version 2 or above

   Maintainer  : Martin Linnemann <[email protected]>
   Stability   : alpha
   Portability : portable

Entry point to the odt reader.
-}

module Text.Pandoc.Readers.ODT ( readODT ) where

import Codec.Archive.Zip
import Text.Pandoc.XML.Light
import Text.Pandoc.Walk

import Data.Char (isDigit)
import qualified Data.ByteString.Lazy as B

import System.FilePath

import Control.Monad.Except (throwError)

import qualified Data.Text as T

import Text.Pandoc.Class.PandocMonad (PandocMonad)
import qualified Text.Pandoc.Class.PandocMonad as P
import Text.Pandoc.Definition
import Text.Pandoc.Error
import Text.Pandoc.MediaBag
import Text.Pandoc.Options
import qualified Text.Pandoc.UTF8 as UTF8

import Text.Pandoc.Readers.ODT.ContentReader
import Text.Pandoc.Readers.ODT.StyleReader

import Text.Pandoc.Readers.ODT.Generic.Fallible
import Text.Pandoc.Readers.ODT.Generic.XMLConverter
import Text.Pandoc.Shared (filteredFilesFromArchive)

readODT :: PandocMonad m
        => ReaderOptions
        -> B.ByteString
        -> m Pandoc
readODT opts bytes = case readODT' opts bytes of
  Right (doc, mb) -> do
    P.setMediaBag mb
    return $ walk makeFigure doc
  Left e -> throwError e

-- the ODT parser uses old-style figures: an image with title beginning
-- "fig:" in a paragraph by itself.  Convert these to new Figure elements.
makeFigure :: Block -> Block
makeFigure (Para [ Image (ident, classes, kvs) capt (src, tit) ])
  | "fig:" `T.isPrefixOf` tit
  = Figure (ident, [], []) (Caption Nothing [Plain capt'])
      [Plain [Image ("", classes, kvs) capt (src, "")]]
   where
     capt' = case capt of -- strip "Figure 1:" for consistency
                 (Str _ : Space : Str t : Space : xs)
                    | T.all (\c -> isDigit c || c == ':') t
                    , ":" `T.isSuffixOf` t -> xs
                 xs -> xs
makeFigure x = x

--
readODT' :: ReaderOptions
         -> B.ByteString
         -> Either PandocError (Pandoc, MediaBag)
readODT' _ bytes = bytesToODT bytes-- of
--                    Right (pandoc, mediaBag) -> Right (pandoc , mediaBag)
--                    Left  err                -> Left err

--
bytesToODT :: B.ByteString -> Either PandocError (Pandoc, MediaBag)
bytesToODT bytes = case toArchiveOrFail bytes of
  Right archive -> archiveToODT archive
  Left err      -> Left $ PandocParseError
                        $ "Could not unzip ODT: " <> T.pack err

--
archiveToODT :: Archive -> Either PandocError (Pandoc, MediaBag)
archiveToODT archive = do
  let onFailure msg Nothing = Left $ PandocParseError msg
      onFailure _   (Just x) = Right x
  contentEntry <- onFailure "Could not find content.xml"
                   (findEntryByPath "content.xml" archive)
  stylesEntry <- onFailure "Could not find styles.xml"
                   (findEntryByPath "styles.xml" archive)
  contentElem <- entryToXmlElem contentEntry
  stylesElem <- entryToXmlElem stylesEntry
  styles <- either
               (\_ -> Left $ PandocParseError "Could not read styles")
               Right
               (chooseMax (readStylesAt stylesElem ) (readStylesAt contentElem))
  let filePathIsODTMedia :: FilePath -> Bool
      filePathIsODTMedia fp =
        let (dir, name) = splitFileName fp
        in  (dir == "Pictures/") || (dir /= "./" && name == "content.xml")
  let media = filteredFilesFromArchive archive filePathIsODTMedia
  let startState = readerState styles media
  either (\_ -> Left $ PandocParseError "Could not convert opendocument") Right
    (runConverter' read_body startState contentElem)


--
entryToXmlElem :: Entry -> Either PandocError Element
entryToXmlElem entry =
  case parseXMLElement . UTF8.toTextLazy . fromEntry $ entry of
    Right x  -> Right x
    Left msg -> Left $ PandocXMLError (T.pack $ eRelativePath entry) msg