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.
This commit is contained in:
John MacFarlane 2015-05-08 23:46:02 -07:00
parent 41a457a649
commit 1c2951dfd9
3 changed files with 32 additions and 20 deletions

2
README
View file

@ -646,6 +646,8 @@ Options affecting specific writers
is specified, pandoc will look for a file `epub.css` in the is specified, pandoc will look for a file `epub.css` in the
user data directory (see `--data-dir`). If it is not user data directory (see `--data-dir`). If it is not
found there, sensible defaults will be used. found there, sensible defaults will be used.
(*Deprecated:* use `--css` or the `stylesheet` metadata field
instead.)
`--epub-cover-image=`*FILE* `--epub-cover-image=`*FILE*

View file

@ -689,8 +689,10 @@ options =
, Option "" ["epub-stylesheet"] , Option "" ["epub-stylesheet"]
(ReqArg (ReqArg
(\arg opt -> do (\arg opt -> do
text <- UTF8.readFile arg warn "--epub-stylesheet is deprecated. Use --css or stylesheet in metadata."
return opt { optEpubStylesheet = Just text }) let newvars = optVariables opt ++ [("css",arg)]
return opt { optVariables = newvars,
optStandalone = True })
"FILENAME") "FILENAME")
"" -- "Path of epub.css" "" -- "Path of epub.css"

View file

@ -99,7 +99,7 @@ data EPUBMetadata = EPUBMetadata{
, epubPageDirection :: Maybe ProgressionDirection , epubPageDirection :: Maybe ProgressionDirection
} deriving Show } deriving Show
data Stylesheet = StylesheetPath FilePath data Stylesheet = StylesheetPaths [FilePath]
| StylesheetContents String | StylesheetContents String
deriving Show deriving Show
@ -323,8 +323,14 @@ metadataFromMeta opts meta = EPUBMetadata{
coverImage = lookup "epub-cover-image" (writerVariables opts) `mplus` coverImage = lookup "epub-cover-image" (writerVariables opts) `mplus`
(metaValueToString <$> lookupMeta "cover-image" meta) (metaValueToString <$> lookupMeta "cover-image" meta)
stylesheet = (StylesheetContents <$> writerEpubStylesheet opts) `mplus` stylesheet = (StylesheetContents <$> writerEpubStylesheet opts) `mplus`
((StylesheetPath . metaValueToString) <$> (case lookupMeta "stylesheet" meta of
lookupMeta "stylesheet" meta) Just (MetaBlocks [CodeBlock (_,["css"],_) code])
-> Just (StylesheetContents code)
Just x -> Just (StylesheetPaths [metaValueToString x])
Nothing -> Nothing) `mplus`
(case [x | ("css", x) <- writerVariables opts] of
[] -> Nothing
xs -> Just (StylesheetPaths xs))
pageDirection = case map toLower . metaValueToString <$> pageDirection = case map toLower . metaValueToString <$>
lookupMeta "page-progression-direction" meta of lookupMeta "page-progression-direction" meta of
Just "ltr" -> Just LTR Just "ltr" -> Just LTR
@ -340,20 +346,21 @@ writeEPUB opts doc@(Pandoc meta _) = do
let epub3 = version == EPUB3 let epub3 = version == EPUB3
epochtime <- floor `fmap` getPOSIXTime epochtime <- floor `fmap` getPOSIXTime
let mkEntry path content = toEntry path epochtime content let mkEntry path content = toEntry path epochtime content
let vars = ("epub3", if epub3 then "true" else "false") let vars = ("epub3", if epub3 then "true" else "false") :
: ("css", "stylesheet.css") writerVariables opts
: writerVariables opts let opts'' = opts{ writerEmailObfuscation = NoObfuscation
let opts' = opts{ writerEmailObfuscation = NoObfuscation , writerStandalone = True
, writerStandalone = True , writerSectionDivs = True
, writerSectionDivs = True , writerHtml5 = epub3
, writerHtml5 = epub3 , writerVariables = vars
, writerVariables = vars , writerHTMLMathMethod =
, writerHTMLMathMethod = if epub3
if epub3 then MathML Nothing
then MathML Nothing else writerHTMLMathMethod opts
else writerHTMLMathMethod opts , writerWrapText = True }
, writerWrapText = True } metadata <- getEPUBMetadata opts'' meta
metadata <- getEPUBMetadata opts' meta let opts' = opts''{ writerVariables = ("css", "stylesheet.css") :
filter (\(x,_) -> x /= "css") vars }
-- cover page -- cover page
(cpgEntry, cpicEntry) <- (cpgEntry, cpicEntry) <-
@ -675,7 +682,8 @@ writeEPUB opts doc@(Pandoc meta _) = do
-- stylesheet -- stylesheet
stylesheet <- case epubStylesheet metadata of stylesheet <- case epubStylesheet metadata of
Just (StylesheetPath fp) -> UTF8.readFile fp Just (StylesheetPaths fp) -> concat <$>
mapM UTF8.readFile fp
Just (StylesheetContents s) -> return s Just (StylesheetContents s) -> return s
Nothing -> UTF8.toString `fmap` Nothing -> UTF8.toString `fmap`
readDataFile (writerUserDataDir opts) "epub.css" readDataFile (writerUserDataDir opts) "epub.css"