@@ -2,6 +2,7 @@
22{-# LANGUAGE CPP #-}
33{-# LANGUAGE FlexibleContexts #-}
44{-# LANGUAGE PatternGuards #-}
5+{-# LANGUAGE OverloadedStrings #-}
56{-# LANGUAGE ScopedTypeVariables #-}
67{- |
78 Module : Text.Pandoc.Writers.EPUB
@@ -58,6 +59,8 @@ import Text.XML.Light (Attr (..), Element (..), Node (..), QName (..),
5859 add_attrs, lookupAttr, node, onlyElems, parseXML,
5960 ppElement, showElement, strContent, unode, unqual)
6061import Text.Pandoc.XML (escapeStringForXML)
62+import Text.DocTemplates (FromContext(lookupContext), Context(..),
63+ ToContext(toVal), Val(..))
6164
6265-- A Chapter includes a list of blocks.
6366data Chapter = Chapter [Block]
@@ -136,6 +139,9 @@ removeNote :: Inline -> Inline
136139removeNote (Note _) = Str ""
137140removeNote x = x
138141
142+toVal' :: String -> Val TS.Text
143+toVal' = toVal . TS.pack
144+
139145mkEntry :: PandocMonad m => FilePath -> B.ByteString -> E m Entry
140146mkEntry path content = do
141147 epubSubdir <- gets stEpubSubdir
@@ -163,8 +169,8 @@ getEPUBMetadata opts meta = do
163169 else return m
164170 let addLanguage m =
165171 if null (epubLanguage m)
166− then case lookup "lang" (writerVariables opts) of
167− Just x -> return m{ epubLanguage = x }
172+ then case lookupContext "lang" (writerVariables opts) of
173+ Just x -> return m{ epubLanguage = TS.unpack x }
168174 Nothing -> do
169175 mLang <- lift $ P.lookupEnv "LANG"
170176 let localeLang =
@@ -345,11 +351,14 @@ metadataFromMeta opts meta = EPUBMetadata{
345351 relation = metaValueToString <$> lookupMeta "relation" meta
346352 coverage = metaValueToString <$> lookupMeta "coverage" meta
347353 rights = metaValueToString <$> lookupMeta "rights" meta
348− coverImage = lookup "epub-cover-image" (writerVariables opts) `mplus`
349− (metaValueToString <$> lookupMeta "cover-image" meta)
354+ coverImage =
355+ (TS.unpack <$> lookupContext "epub-cover-image"
356+ (writerVariables opts))
357+ `mplus` (metaValueToString <$> lookupMeta "cover-image" meta)
350358 mCss = lookupMeta "css" meta <|> lookupMeta "stylesheet" meta
351359 stylesheets = fromMaybe [] (metaValueToPaths <$> mCss) ++
352− [f | ("css",f) <- writerVariables opts]
360+ maybe [] (\t -> [TS.unpack t])
361+ (lookupContext "css" (writerVariables opts))
353362 pageDirection = case map toLower . metaValueToString <$>
354363 lookupMeta "page-progression-direction" meta of
355364 Just "ltr" -> Just LTR
@@ -424,10 +433,13 @@ pandocToEPUB version opts doc = do
424433 (\bs n -> mkEntry ("styles/stylesheet" ++ show n ++ ".css") bs)
425434 stylesheets [(1 :: Int)..]
426435
427− let vars = ("epub3", if epub3 then "true" else "false")
428− : [(x,y) | (x,y) <- writerVariables opts, x /= "css"]
436+ let vars = Context $
437+ M.delete "css" . M.insert "epub3"
438+ (toVal' $ if epub3 then "true" else "false") $
439+ unContext $ writerVariables opts
429440
430− let cssvars useprefix = map (\e -> ("css",
441+ let cssvars useprefix = Context $ M.fromList $ map
442+ (\e -> ("css", toVal' $
431443 (if useprefix
432444 then "../"
433445 else "")
@@ -457,14 +469,16 @@ pandocToEPUB version opts doc = do
457469 (CouldNotDetermineImageSize img err')
458470 cpContent <- lift $ writeHtml
459471 opts'{ writerVariables =
460− ("coverpage","true"):
461− ("pagetitle",
462− escapeStringForXML plainTitle):
463− ("cover-image", coverImage):
464− ("cover-image-width", show coverImageWidth):
465− ("cover-image-height",
466− show coverImageHeight):
467− cssvars True ++ vars }
472+ Context (M.fromList [
473+ ("coverpage", toVal' "true"),
474+ ("pagetitle", toVal' $
475+ escapeStringForXML plainTitle),
476+ ("cover-image", toVal' coverImage),
477+ ("cover-image-width", toVal' $
478+ show coverImageWidth),
479+ ("cover-image-height", toVal' $
480+ show coverImageHeight)]) <>
481+ cssvars True <> vars }
468482 (Pandoc meta [])
469483 coverEntry <- mkEntry "text/cover.xhtml" cpContent
470484 coverImageEntry <- mkEntry ("media/" ++ coverImage)
@@ -474,10 +488,13 @@ pandocToEPUB version opts doc = do
474488
475489 -- title page
476490 tpContent <- lift $ writeHtml opts'{
477− writerVariables = ("titlepage","true"):
478− ("body-type", "frontmatter"):
479− ("pagetitle", escapeStringForXML plainTitle):
480− cssvars True ++ vars }
491+ writerVariables =
492+ Context (M.fromList [
493+ ("titlepage", toVal' "true"),
494+ ("body-type", toVal' "frontmatter"),
495+ ("pagetitle", toVal' $
496+ escapeStringForXML plainTitle)])
497+ <> cssvars True <> vars }
481498 (Pandoc meta [])
482499 tpEntry <- mkEntry "text/title_page.xhtml" tpContent
483500
@@ -564,9 +581,12 @@ pandocToEPUB version opts doc = do
564581
565582 let chapToEntry num (Chapter bs) =
566583 mkEntry ("text/" ++ showChapter num) =<<
567− writeHtml opts'{ writerVariables = ("body-type", bodyType) :
568− ("pagetitle", showChapter num) :
569− cssvars True ++ vars } pdoc
584+ writeHtml opts'{ writerVariables =
585+ Context (M.fromList
586+ [("body-type", toVal' bodyType),
587+ ("pagetitle", toVal' $
588+ showChapter num)])
589+ <> cssvars True <> vars } pdoc
570590 where (pdoc, bodyType) =
571591 case bs of
572592 (Header _ (_,_,kvs) xs : _) ->
@@ -776,9 +796,10 @@ pandocToEPUB version opts doc = do
776796 (writeHtmlStringForEPUB version
777797 opts{ writerTemplate = Nothing
778798 , writerVariables =
779− ("pagetitle",
780− escapeStringForXML plainTitle):
781− writerVariables opts}
799+ Context (M.fromList
800+ [("pagetitle", toVal' $
801+ escapeStringForXML plainTitle)])
802+ <> writerVariables opts}
782803 (Pandoc nullMeta
783804 [Plain $ walk clean tit])) of
784805 Left _ -> TS.pack $ stringify tit
@@ -801,13 +822,13 @@ pandocToEPUB version opts doc = do
801822 then [ unode "li"
802823 [ unode "a" ! [("href", "text/cover.xhtml")
803824 ,("epub:type", "cover")] $
804− "Cover"] |
825+ ("Cover" :: String)] |
805826 isJust (epubCoverImage metadata)
806827 ] ++
807828 [ unode "li"
808829 [ unode "a" ! [("href", "#toc")
809830 ,("epub:type", "toc")] $
810− "Table of contents"
831+ ("Table of contents" :: String)
811832 ] | writerTableOfContents opts
812833 ]
813834 else []
@@ -819,8 +840,9 @@ pandocToEPUB version opts doc = do
819840 ,("hidden","hidden")] $
820841 [ unode "ol" landmarkItems ]
821842 ]
822− navData <- lift $ writeHtml opts'{ writerVariables = ("navpage","true"):
823− cssvars False ++ vars }
843+ navData <- lift $ writeHtml opts'{ writerVariables =
844+ Context (M.fromList [("navpage", toVal' "true")])
845+ <> cssvars False <> vars }
824846 (Pandoc (setMeta "title"
825847 (walk removeNote $ fromList $ docTitle' meta) nullMeta)
826848 (navBlocks ++ landmarks))
@@ -846,7 +868,7 @@ pandocToEPUB version opts doc = do
846868 let apple = UTF8.fromStringLazy $ ppTopElement $
847869 unode "display_options" $
848870 unode "platform" ! [("name","*")] $
849− unode "option" ! [("name","specified-fonts")] $ "true"
871+ unode "option" ! [("name","specified-fonts")] $ ("true" :: String)
850872 appleEntry <- mkEntry "META-INF/com.apple.ibooks.display-options.xml" apple
851873
852874 -- construct archive
@@ -949,6 +971,7 @@ metadataElement version md currentTime =
949971 (("id",id') :
950972 maybe [] (\x -> [("opf:event",x)]) (dateEvent date)) $
951973 dateText date]
974+ schemeToOnix :: String -> String
952975 schemeToOnix "ISBN-10" = "02"
953976 schemeToOnix "GTIN-13" = "03"
954977 schemeToOnix "UPC" = "04"