git.iko.soy/typeable/pandoc
on master help sign in
EPUB writer: stylesheet changes. Closes #2040.

* Allow `--css` to be used to specify stylesheets. * Deprecated `--epub-stylesheet` and made it a synoynym of `--css`. * If a code block with class "css" is given as contents of the `stylesheet` metadata field, use its literal code as contents of the epub stylesheet. Otherwise, treat it as a filename and read the file. * Note: `--css` and `stylesheet` in metadata are not compatible. `stylesheet` takes precedence.

commit1c2951dfd9ee72e5270cb974a06098adb9178f89 authorJohn MacFarlane <jgm@berkeley.edu> dateFri 8 May 2015, 23:46 (11 years ago) parent41a457a
3 files changed+32−20
README+2 pandoc.hs+4−2 src/Text/Pandoc/Writers/EPUB.hs+26−18
README+2
@@ -646,6 +646,8 @@ Options affecting specific writers
646646 is specified, pandoc will look for a file `epub.css` in the
647647 user data directory (see `--data-dir`). If it is not
648648 found there, sensible defaults will be used.
649+ (*Deprecated:* use `--css` or the `stylesheet` metadata field
650+ instead.)
649651
650652`--epub-cover-image=`*FILE*
651653
pandoc.hs+4−2
@@ -689,8 +689,10 @@ options =
689689 , Option "" ["epub-stylesheet"]
690690 (ReqArg
691691 (\arg opt -> do
692− text <- UTF8.readFile arg
693− return opt { optEpubStylesheet = Just text })
692+ warn "--epub-stylesheet is deprecated. Use --css or stylesheet in metadata."
693+ let newvars = optVariables opt ++ [("css",arg)]
694+ return opt { optVariables = newvars,
695+ optStandalone = True })
694696 "FILENAME")
695697 "" -- "Path of epub.css"
696698
src/Text/Pandoc/Writers/EPUB.hs+26−18
@@ -99,7 +99,7 @@ data EPUBMetadata = EPUBMetadata{
9999 , epubPageDirection :: Maybe ProgressionDirection
100100 } deriving Show
101101
102−data Stylesheet = StylesheetPath FilePath
102+data Stylesheet = StylesheetPaths [FilePath]
103103 | StylesheetContents String
104104 deriving Show
105105
@@ -323,8 +323,14 @@ metadataFromMeta opts meta = EPUBMetadata{
323323 coverImage = lookup "epub-cover-image" (writerVariables opts) `mplus`
324324 (metaValueToString <$> lookupMeta "cover-image" meta)
325325 stylesheet = (StylesheetContents <$> writerEpubStylesheet opts) `mplus`
326− ((StylesheetPath . metaValueToString) <$>
327− lookupMeta "stylesheet" meta)
326+ (case lookupMeta "stylesheet" meta of
327+ Just (MetaBlocks [CodeBlock (_,["css"],_) code])
328+ -> Just (StylesheetContents code)
329+ Just x -> Just (StylesheetPaths [metaValueToString x])
330+ Nothing -> Nothing) `mplus`
331+ (case [x | ("css", x) <- writerVariables opts] of
332+ [] -> Nothing
333+ xs -> Just (StylesheetPaths xs))
328334 pageDirection = case map toLower . metaValueToString <$>
329335 lookupMeta "page-progression-direction" meta of
330336 Just "ltr" -> Just LTR
@@ -340,20 +346,21 @@ writeEPUB opts doc@(Pandoc meta _) = do
340346 let epub3 = version == EPUB3
341347 epochtime <- floor `fmap` getPOSIXTime
342348 let mkEntry path content = toEntry path epochtime content
343− let vars = ("epub3", if epub3 then "true" else "false")
344− : ("css", "stylesheet.css")
345− : writerVariables opts
346− let opts' = opts{ writerEmailObfuscation = NoObfuscation
347− , writerStandalone = True
348− , writerSectionDivs = True
349− , writerHtml5 = epub3
350− , writerVariables = vars
351− , writerHTMLMathMethod =
352− if epub3
353− then MathML Nothing
354− else writerHTMLMathMethod opts
355− , writerWrapText = True }
356− metadata <- getEPUBMetadata opts' meta
349+ let vars = ("epub3", if epub3 then "true" else "false") :
350+ writerVariables opts
351+ let opts'' = opts{ writerEmailObfuscation = NoObfuscation
352+ , writerStandalone = True
353+ , writerSectionDivs = True
354+ , writerHtml5 = epub3
355+ , writerVariables = vars
356+ , writerHTMLMathMethod =
357+ if epub3
358+ then MathML Nothing
359+ else writerHTMLMathMethod opts
360+ , writerWrapText = True }
361+ metadata <- getEPUBMetadata opts'' meta
362+ let opts' = opts''{ writerVariables = ("css", "stylesheet.css") :
363+ filter (\(x,_) -> x /= "css") vars }
357364
358365 -- cover page
359366 (cpgEntry, cpicEntry) <-
@@ -675,7 +682,8 @@ writeEPUB opts doc@(Pandoc meta _) = do
675682
676683 -- stylesheet
677684 stylesheet <- case epubStylesheet metadata of
678− Just (StylesheetPath fp) -> UTF8.readFile fp
685+ Just (StylesheetPaths fp) -> concat <$>
686+ mapM UTF8.readFile fp
679687 Just (StylesheetContents s) -> return s
680688 Nothing -> UTF8.toString `fmap`
681689 readDataFile (writerUserDataDir opts) "epub.css"