git.iko.soy/typeable/pandoc
on master help sign in
Merge pull request #1516 from mpickering/epubmetadata
e690fe4John MacFarlane
@@ -2972,43 +2972,60 @@ The following fields are recognized:
2972 `GTIN-13`, `UPC`, `ISMN-10`, `DOI`, `LCCN`, `GTIN-14`,
2972 `GTIN-13`, `UPC`, `ISMN-10`, `DOI`, `LCCN`, `GTIN-14`,
2973 `ISBN-13`, `Legal deposit number`, `URN`, `OCLC`,
2973 `ISBN-13`, `Legal deposit number`, `URN`, `OCLC`,
2974 `ISMN-13`, `ISBN-A`, `JP`, `OLCC`.
2974 `ISMN-13`, `ISBN-A`, `JP`, `OLCC`.
2975+
2975`title`
2976`title`
2976 ~ Either a string value, or an object with fields `file-as` and
2977 ~ Either a string value, or an object with fields `file-as` and
2977 `type`, or a list of such objects. Valid values for `type` are
2978 `type`, or a list of such objects. Valid values for `type` are
2978 `main`, `subtitle`, `short`, `collection`, `edition`, `extended`.
2979 `main`, `subtitle`, `short`, `collection`, `edition`, `extended`.
2980+
2979`creator`
2981`creator`
2980 ~ Either a string value, or an object with fields `role`, `file-as`,
2982 ~ Either a string value, or an object with fields `role`, `file-as`,
2981 and `text`, or a list of such objects. Valid values for `role` are
2983 and `text`, or a list of such objects. Valid values for `role` are
2982 [marc relators](http://www.loc.gov/marc/relators/relaterm.html), but
2984 [marc relators](http://www.loc.gov/marc/relators/relaterm.html), but
2983 pandoc will attempt to translate the human-readable versions
2985 pandoc will attempt to translate the human-readable versions
2984 (like "author" and "editor") to the appropriate marc relators.
2986 (like "author" and "editor") to the appropriate marc relators.
2987+
2985`contributor`
2988`contributor`
2986 ~ Same format as `creator`.
2989 ~ Same format as `creator`.
2990+
2987`date`
2991`date`
2988 ~ A string value in `YYYY-MM-DD` format. (Only the year is necessary.)
2992 ~ A string value in `YYYY-MM-DD` format. (Only the year is necessary.)
2989 Pandoc will attempt to convert other common date formats.
2993 Pandoc will attempt to convert other common date formats.
2994+
2990`language`
2995`language`
2991 ~ A string value in [RFC5646] format. Pandoc will default to the local
2996 ~ A string value in [RFC5646] format. Pandoc will default to the local
2992 language if nothing is specified.
2997 language if nothing is specified.
2998+
2993`subject`
2999`subject`
2994 ~ A string value or a list of such values.
3000 ~ A string value or a list of such values.
3001+
2995`description`
3002`description`
2996 ~ A string value.
3003 ~ A string value.
3004+
2997`type`
3005`type`
2998 ~ A string value.
3006 ~ A string value.
3007+
2999`format`
3008`format`
3000 ~ A string value.
3009 ~ A string value.
3010+
3001`relation`
3011`relation`
3002 ~ A string value.
3012 ~ A string value.
3013+
3003`coverage`
3014`coverage`
3004 ~ A string value.
3015 ~ A string value.
3016+
3005`rights`
3017`rights`
3006 ~ A string value.
3018 ~ A string value.
3019+
3007`cover-image`
3020`cover-image`
3008 ~ A string value (path to cover image).
3021 ~ A string value (path to cover image).
3022+
3009`stylesheet`
3023`stylesheet`
3010 ~ A string value (path to CSS stylesheet).
3024 ~ A string value (path to CSS stylesheet).
3011
3025
3026+`page-progression-direction`
3027+ ~ Either `ltr` or `rtl`. Specifies the synonymous spine [attribute][EPUBspine].
3028+
3012Literate Haskell support
3029Literate Haskell support
3013========================
3030========================
3014
3031
@@ -3126,3 +3143,4 @@ Rosenthal.
3126[InDesign ICML]: https://www.adobe.com/content/dam/Adobe/en/devnet/indesign/cs55-docs/IDML/idml-specification.pdf
3143[InDesign ICML]: https://www.adobe.com/content/dam/Adobe/en/devnet/indesign/cs55-docs/IDML/idml-specification.pdf
3127[txt2tags]: http://txt2tags.org/
3144[txt2tags]: http://txt2tags.org/
3128[EPUB]: http://idpf.org/epub
3145[EPUB]: http://idpf.org/epub
3146+[EPUBspine]: http://www.idpf.org/epub/301/spec/epub-publications.html#sec-spine-elem
src/Text/Pandoc/Readers/EPUB.hs+7−6
@@ -14,7 +14,7 @@ import Text.Pandoc.Walk (walk, query)
14import Text.Pandoc.Generic(bottomUp)
14import Text.Pandoc.Generic(bottomUp)
15import Text.Pandoc.Readers.HTML (readHtml)
15import Text.Pandoc.Readers.HTML (readHtml)
16import Text.Pandoc.Options ( ReaderOptions(..), readerTrace)
16import Text.Pandoc.Options ( ReaderOptions(..), readerTrace)
17−import Text.Pandoc.Shared (escapeURI, collapseFilePath)
17+import Text.Pandoc.Shared (escapeURI, collapseFilePath, addMetaField)
18import Text.Pandoc.MediaBag (MediaBag, insertMedia)
18import Text.Pandoc.MediaBag (MediaBag, insertMedia)
19import Text.Pandoc.Compat.Except (MonadError, throwError, runExcept, Except)
19import Text.Pandoc.Compat.Except (MonadError, throwError, runExcept, Except)
20import qualified Text.Pandoc.Builder as B
20import qualified Text.Pandoc.Builder as B
@@ -155,7 +155,7 @@ parseMeta content = do
155-- http://www.idpf.org/epub/30/spec/epub30-publications.html#sec-metadata-elem
155-- http://www.idpf.org/epub/30/spec/epub30-publications.html#sec-metadata-elem
156parseMetaItem :: Element -> Meta -> Meta
156parseMetaItem :: Element -> Meta -> Meta
157parseMetaItem e@(stripNamespace . elName -> field) meta =
157parseMetaItem e@(stripNamespace . elName -> field) meta =
158− B.setMeta (renameMeta field) (B.str $ strContent e) meta
158+ addMetaField (renameMeta field) (B.str $ strContent e) meta
159
159
160renameMeta :: String -> String
160renameMeta :: String -> String
161renameMeta "creator" = "author"
161renameMeta "creator" = "author"
@@ -169,10 +169,10 @@ getManifest archive = do
169 ns <- mkE "xmlns not in namespaces" (lookup "xmlns" namespaces)
169 ns <- mkE "xmlns not in namespaces" (lookup "xmlns" namespaces)
170 as <- liftM ((map attrToPair) . elAttribs)
170 as <- liftM ((map attrToPair) . elAttribs)
171 (findElementE (QName "rootfile" (Just ns) Nothing) docElem)
171 (findElementE (QName "rootfile" (Just ns) Nothing) docElem)
172− root <- mkE "Root not found" (lookup "full-path" as)
172+ manifestFile <- mkE "Root not found" (lookup "full-path" as)
173− let rootdir = dropFileName root
173+ let rootdir = dropFileName manifestFile
174 --mime <- lookup "media-type" as
174 --mime <- lookup "media-type" as
175− manifest <- findEntryByPathE root archive
175+ manifest <- findEntryByPathE manifestFile archive
176 liftM ((,) rootdir) (parseXMLDocE . UTF8.toStringLazy . fromEntry $ manifest)
176 liftM ((,) rootdir) (parseXMLDocE . UTF8.toStringLazy . fromEntry $ manifest)
177
177
178-- Fixup
178-- Fixup
@@ -272,7 +272,8 @@ findAttrE :: MonadError String m => QName -> Element -> m String
272findAttrE q e = mkE "findAttr" $ findAttr q e
272findAttrE q e = mkE "findAttr" $ findAttr q e
273
273
274findEntryByPathE :: MonadError String m => FilePath -> Archive -> m Entry
274findEntryByPathE :: MonadError String m => FilePath -> Archive -> m Entry
275+findEntryByPathE (normalise -> path) a =
275−findEntryByPathE path a = mkE ("No entry on path: " ++ path) $ findEntryByPath path a
276+ mkE ("No entry on path: " ++ path) $ findEntryByPath path a
276
277
277parseXMLDocE :: MonadError String m => String -> m Element
278parseXMLDocE :: MonadError String m => String -> m Element
278parseXMLDocE doc = mkE "Unable to parse XML doc" $ parseXMLDoc doc
279parseXMLDocE doc = mkE "Unable to parse XML doc" $ parseXMLDoc doc
src/Text/Pandoc/Writers/EPUB.hs+47−20
@@ -1,4 +1,4 @@
1−{-# LANGUAGE PatternGuards, CPP, ScopedTypeVariables #-}
1+{-# LANGUAGE PatternGuards, CPP, ScopedTypeVariables, ViewPatterns #-}
2{-
2{-
3Copyright (C) 2010-2014 John MacFarlane <jgm@berkeley.edu>
3Copyright (C) 2010-2014 John MacFarlane <jgm@berkeley.edu>
4
4
@@ -29,7 +29,7 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29Conversion of 'Pandoc' documents to EPUB.
29Conversion of 'Pandoc' documents to EPUB.
30-}
30-}
31module Text.Pandoc.Writers.EPUB ( writeEPUB ) where
31module Text.Pandoc.Writers.EPUB ( writeEPUB ) where
32−import Data.IORef
32+import Data.IORef ( IORef, newIORef, readIORef, modifyIORef )
33import qualified Data.Map as M
33import qualified Data.Map as M
34import Data.Maybe ( fromMaybe )
34import Data.Maybe ( fromMaybe )
35import Data.List ( isPrefixOf, isInfixOf, intercalate )
35import Data.List ( isPrefixOf, isInfixOf, intercalate )
@@ -40,27 +40,35 @@ import qualified Data.ByteString.Lazy as B
40import qualified Data.ByteString.Lazy.Char8 as B8
40import qualified Data.ByteString.Lazy.Char8 as B8
41import qualified Text.Pandoc.UTF8 as UTF8
41import qualified Text.Pandoc.UTF8 as UTF8
42import Text.Pandoc.SelfContained ( makeSelfContained )
42import Text.Pandoc.SelfContained ( makeSelfContained )
43−import Codec.Archive.Zip
43+import Codec.Archive.Zip ( emptyArchive, addEntryToArchive, eRelativePath, fromEntry , Entry, toEntry, fromArchive)
44import Control.Applicative ((<$>))
44import Control.Applicative ((<$>))
45−import Data.Time.Clock.POSIX
45+import Data.Time.Clock.POSIX ( getPOSIXTime )
46−import Data.Time
46+import Data.Time (getCurrentTime,UTCTime, formatTime)
47−import System.Locale
47+import System.Locale ( defaultTimeLocale )
48−import Text.Pandoc.Shared hiding ( Element )
48+import Text.Pandoc.Shared ( trimr, renderTags', safeRead, uniqueIdent, trim
49+ , normalizeDate, readDataFile, stringify, warn
50+ , hierarchicalize, fetchItem' )
49−import qualified Text.Pandoc.Shared as Shared
51+import qualified Text.Pandoc.Shared as S (Element(..))
50import Text.Pandoc.Builder (fromList, setMeta)
52import Text.Pandoc.Builder (fromList, setMeta)
51−import Text.Pandoc.Options
53+import Text.Pandoc.Options ( WriterOptions(..)
54+ , HTMLMathMethod(..)
55+ , EPUBVersion(..)
56+ , ObfuscationMethod(NoObfuscation) )
52import Text.Pandoc.Definition
57import Text.Pandoc.Definition
53−import Text.Pandoc.Walk
58+import Text.Pandoc.Walk (walk, walkM)
54−import Control.Monad.State
59+import Control.Monad.State (modify, get, execState, State, put, evalState)
60+import Control.Monad (foldM, when, mplus, liftM)
55−import Text.XML.Light hiding (ppTopElement)
61+import Text.XML.Light ( unode, Element(..), unqual, Attr(..), add_attrs
62+ , strContent, lookupAttr, Node(..), QName(..), parseXML
63+ , onlyElems, node, ppElement)
56−import Text.Pandoc.UUID
64+import Text.Pandoc.UUID (getRandomUUID)
57−import Text.Pandoc.Writers.HTML
65+import Text.Pandoc.Writers.HTML (writeHtmlString, writeHtml)
58import Data.Char ( toLower, isDigit, isAlphaNum )
66import Data.Char ( toLower, isDigit, isAlphaNum )
59import Network.URI ( unEscapeString )
67import Network.URI ( unEscapeString )
60import Text.Pandoc.MIME (getMimeType)
68import Text.Pandoc.MIME (getMimeType)
61import qualified Control.Exception as E
69import qualified Control.Exception as E
62import Text.Blaze.Html.Renderer.Utf8 (renderHtml)
70import Text.Blaze.Html.Renderer.Utf8 (renderHtml)
63−import Text.HTML.TagSoup
71+import Text.HTML.TagSoup (Tag(TagOpen), fromAttrib, parseTags)
64
72
65-- A Chapter includes a list of blocks and maybe a section
73-- A Chapter includes a list of blocks and maybe a section
66-- number offset. Note, some chapters are unnumbered. The section
74-- number offset. Note, some chapters are unnumbered. The section
@@ -86,6 +94,7 @@ data EPUBMetadata = EPUBMetadata{
86 , epubRights :: Maybe String
94 , epubRights :: Maybe String
87 , epubCoverImage :: Maybe String
95 , epubCoverImage :: Maybe String
88 , epubStylesheet :: Maybe Stylesheet
96 , epubStylesheet :: Maybe Stylesheet
97+ , epubPageDirection :: ProgressionDirection
89 } deriving Show
98 } deriving Show
90
99
91data Stylesheet = StylesheetPath FilePath
100data Stylesheet = StylesheetPath FilePath
@@ -114,6 +123,8 @@ data Title = Title{
114 , titleType :: Maybe String
123 , titleType :: Maybe String
115 } deriving Show
124 } deriving Show
116
125
126+data ProgressionDirection = LTR | RTL | Default deriving Show
127+
117dcName :: String -> QName
128dcName :: String -> QName
118dcName n = QName n Nothing (Just "dc")
129dcName n = QName n Nothing (Just "dc")
119
130
@@ -288,6 +299,7 @@ metadataFromMeta opts meta = EPUBMetadata{
288 , epubRights = rights
299 , epubRights = rights
289 , epubCoverImage = coverImage
300 , epubCoverImage = coverImage
290 , epubStylesheet = stylesheet
301 , epubStylesheet = stylesheet
302+ , epubPageDirection = pageDirection
291 }
303 }
292 where identifiers = getIdentifier meta
304 where identifiers = getIdentifier meta
293 titles = getTitle meta
305 titles = getTitle meta
@@ -310,6 +322,14 @@ metadataFromMeta opts meta = EPUBMetadata{
310 stylesheet = (StylesheetContents <$> writerEpubStylesheet opts) `mplus`
322 stylesheet = (StylesheetContents <$> writerEpubStylesheet opts) `mplus`
311 ((StylesheetPath . metaValueToString) <$>
323 ((StylesheetPath . metaValueToString) <$>
312 lookupMeta "stylesheet" meta)
324 lookupMeta "stylesheet" meta)
325+ pageDirection = maybe Default stringToPageDirection
326+ (lookupMeta "page-progression-direction" meta)
327+ stringToPageDirection (metaValueToString -> s) =
328+ case s of
329+ "ltr" -> LTR
330+ "rtl" -> RTL
331+ _ -> Default
332+
313
333
314-- | Produce an EPUB file from a Pandoc document.
334-- | Produce an EPUB file from a Pandoc document.
315writeEPUB :: WriterOptions -- ^ Writer options
335writeEPUB :: WriterOptions -- ^ Writer options
@@ -374,6 +394,12 @@ writeEPUB opts doc@(Pandoc meta _) = do
374 let mkFontEntry f = mkEntry (takeFileName f) `fmap` B.readFile f
394 let mkFontEntry f = mkEntry (takeFileName f) `fmap` B.readFile f
375 fontEntries <- mapM mkFontEntry $ writerEpubFonts opts'
395 fontEntries <- mapM mkFontEntry $ writerEpubFonts opts'
376
396
397+ -- set page progression direction
398+ let progressionDirection = case epubPageDirection metadata of
399+ LTR -> "ltr"
400+ RTL -> "rtl"
401+ Default -> "default"
402+
377 -- body pages
403 -- body pages
378
404
379 -- add level 1 header to beginning if none there
405 -- add level 1 header to beginning if none there
@@ -493,7 +519,8 @@ writeEPUB opts doc@(Pandoc meta _) = do
493 (pictureNode x)]) ++
519 (pictureNode x)]) ++
494 map pictureNode picEntries ++
520 map pictureNode picEntries ++
495 map fontNode fontEntries
521 map fontNode fontEntries
496− , unode "spine" ! [("toc","ncx")] $
522+ , unode "spine" ! [("toc","ncx")
523+ ,("page-progression-direction", progressionDirection)] $
497 case epubCoverImage metadata of
524 case epubCoverImage metadata of
498 Nothing -> []
525 Nothing -> []
499 Just _ -> [ unode "itemref" !
526 Just _ -> [ unode "itemref" !
@@ -524,8 +551,8 @@ writeEPUB opts doc@(Pandoc meta _) = do
524 let tocLevel = writerTOCDepth opts
551 let tocLevel = writerTOCDepth opts
525
552
526 let navPointNode :: (Int -> String -> String -> [Element] -> Element)
553 let navPointNode :: (Int -> String -> String -> [Element] -> Element)
527− -> Shared.Element -> State Int Element
554+ -> S.Element -> State Int Element
528− navPointNode formatter (Sec _ nums (ident,_,_) ils children) = do
555+ navPointNode formatter (S.Sec _ nums (ident,_,_) ils children) = do
529 n <- get
556 n <- get
530 modify (+1)
557 modify (+1)
531 let showNums :: [Int] -> String
558 let showNums :: [Int] -> String
@@ -537,12 +564,12 @@ writeEPUB opts doc@(Pandoc meta _) = do
537 let src = case lookup ident reftable of
564 let src = case lookup ident reftable of
538 Just x -> x
565 Just x -> x
539 Nothing -> error (ident ++ " not found in reftable")
566 Nothing -> error (ident ++ " not found in reftable")
540− let isSec (Sec lev _ _ _ _) = lev <= tocLevel
567+ let isSec (S.Sec lev _ _ _ _) = lev <= tocLevel
541 isSec _ = False
568 isSec _ = False
542 let subsecs = filter isSec children
569 let subsecs = filter isSec children
543 subs <- mapM (navPointNode formatter) subsecs
570 subs <- mapM (navPointNode formatter) subsecs
544 return $ formatter n tit src subs
571 return $ formatter n tit src subs
545− navPointNode _ (Blk _) = error "navPointNode encountered Blk"
572+ navPointNode _ (S.Blk _) = error "navPointNode encountered Blk"
546
573
547 let navMapFormatter :: Int -> String -> String -> [Element] -> Element
574 let navMapFormatter :: Int -> String -> String -> [Element] -> Element
548 navMapFormatter n tit src subs = unode "navPoint" !
575 navMapFormatter n tit src subs = unode "navPoint" !