git.iko.soy/typeable/pandoc
on master help sign in
Removed `--epub-stylesheet`; use `--css` instead.

* Removed writerEpubStylesheet in WriterOptions. * Removed `--epub-stylesheet` option. * Allow `--css` to be used with epub. * Allow multiple stylesheets to be used. * Stylesheets will be taken both from `--css` and from the `stylesheet` metadata field (which can contain either a file path or a list of them). Closes #3472, #847.

commitc7e2c718eb01910285dc1334e56cb2f987410478 authorJohn MacFarlane <jgm@berkeley.edu> dateMon 27 Feb 2017, 21:29 (10 years ago) parent1d17dbd
4 files changed+37−47
MANUAL.txt+7−8 src/Text/Pandoc/App.hs+2−13 src/Text/Pandoc/Options.hs−2 src/Text/Pandoc/Writers/EPUB.hs+28−24
MANUAL.txt+7−8
@@ -758,6 +758,12 @@ Options affecting specific writers
758758: Link to a CSS style sheet. This option can be used repeatedly to
759759 include multiple files. They will be included in the order specified.
760760
761+ A stylesheet is required for generating EPUB. If none is
762+ provided using this option (or the `stylesheet` metadata
763+ field), pandoc will look for a file `epub.css` in the
764+ user data directory (see `--data-dir`). If it is not
765+ found there, sensible defaults will be used.
766+
761767`--reference-doc=`*FILE*
762768
763769: Use the specified file as a style reference in producing a
@@ -804,13 +810,6 @@ Options affecting specific writers
804810 LibreOffice, modify the styles as you wish, and save the
805811 file.
806812
807−`--epub-stylesheet=`*FILE*
808−
809−: Use the specified CSS file to style the EPUB. If no stylesheet
810− is specified, pandoc will look for a file `epub.css` in the
811− user data directory (see `--data-dir`). If it is not
812− found there, sensible defaults will be used.
813−
814813`--epub-cover-image=`*FILE*
815814
816815: Use the specified image as the EPUB cover. It is recommended
@@ -847,7 +846,7 @@ Options affecting specific writers
847846 line, be sure to escape them or put the whole filename in single quotes,
848847 to prevent them from being interpreted by the shell. To use the
849848 embedded fonts, you will need to add declarations like the following
850− to your CSS (see `--epub-stylesheet`):
849+ to your CSS (see `--css`):
851850
852851 @font-face {
853852 font-family: DejaVuSans;
src/Text/Pandoc/App.hs+2−13
@@ -112,10 +112,6 @@ convertWithOpts opts = do
112112 mapM_ (UTF8.hPutStrLn stdout) args
113113 exitSuccess
114114
115− epubStylesheet <- case optEpubStylesheet opts of
116− Nothing -> return Nothing
117− Just fp -> Just <$> UTF8.readFile fp
118−
119115 epubMetadata <- case optEpubMetadata opts of
120116 Nothing -> return Nothing
121117 Just fp -> Just <$> UTF8.readFile fp
@@ -319,7 +315,6 @@ convertWithOpts opts = do
319315 writerHighlightStyle = highlightStyle,
320316 writerSetextHeaders = optSetextHeaders opts,
321317 writerEpubMetadata = epubMetadata,
322− writerEpubStylesheet = epubStylesheet,
323318 writerEpubFonts = optEpubFonts opts,
324319 writerEpubChapterLevel = optEpubChapterLevel opts,
325320 writerTOCDepth = optTOCDepth opts,
@@ -493,7 +488,6 @@ data Opt = Opt
493488 , optTopLevelDivision :: TopLevelDivision -- ^ Type of the top-level divisions
494489 , optHTMLMathMethod :: HTMLMathMethod -- ^ Method to print HTML math
495490 , optReferenceDoc :: Maybe FilePath -- ^ Path of reference doc
496− , optEpubStylesheet :: Maybe FilePath -- ^ EPUB stylesheet
497491 , optEpubMetadata :: Maybe FilePath -- ^ EPUB metadata
498492 , optEpubFonts :: [FilePath] -- ^ EPUB fonts to embed
499493 , optEpubChapterLevel :: Int -- ^ Header level at which to split chapters
@@ -559,7 +553,6 @@ defaultOpts = Opt
559553 , optTopLevelDivision = TopLevelDefault
560554 , optHTMLMathMethod = PlainMath
561555 , optReferenceDoc = Nothing
562− , optEpubStylesheet = Nothing
563556 , optEpubMetadata = Nothing
564557 , optEpubFonts = []
565558 , optEpubChapterLevel = 1
@@ -1118,12 +1111,6 @@ options =
11181111 "FILENAME")
11191112 "" -- "Path of custom reference doc"
11201113
1121− , Option "" ["epub-stylesheet"]
1122− (ReqArg
1123− (\arg opt -> return opt { optEpubStylesheet = Just arg })
1124− "FILENAME")
1125− "" -- "Path of epub.css"
1126−
11271114 , Option "" ["epub-cover-image"]
11281115 (ReqArg
11291116 (\arg opt ->
@@ -1430,6 +1417,8 @@ handleUnrecognizedOption "--reference-odt" =
14301417 ("--reference-odt has been removed. Use --reference-doc instead." :)
14311418handleUnrecognizedOption "--parse-raw" =
14321419 (("--parse-raw/-R has been removed. Use +raw_html or +raw_tex extension.\n") :)
1420+handleUnrecognizedOption "--epub-stylesheet" =
1421+ (("--epub-stylesheet has been removed. Use --css instead.\n") :)
14331422handleUnrecognizedOption "-R" = handleUnrecognizedOption "--parse-raw"
14341423handleUnrecognizedOption x =
14351424 (("Unknown option " ++ x ++ ".") :)
src/Text/Pandoc/Options.hs−2
@@ -167,7 +167,6 @@ data WriterOptions = WriterOptions
167167 -- (Nothing = no highlighting)
168168 , writerSetextHeaders :: Bool -- ^ Use setext headers for levels 1-2 in markdown
169169 , writerEpubMetadata :: Maybe String -- ^ Metadata to include in EPUB
170− , writerEpubStylesheet :: Maybe String -- ^ EPUB stylesheet specified at command line
171170 , writerEpubFonts :: [FilePath] -- ^ Paths to fonts to embed
172171 , writerEpubChapterLevel :: Int -- ^ Header level for chapters (separate files)
173172 , writerTOCDepth :: Int -- ^ Number of levels to include in TOC
@@ -203,7 +202,6 @@ instance Default WriterOptions where
203202 , writerHighlightStyle = Just pygments
204203 , writerSetextHeaders = True
205204 , writerEpubMetadata = Nothing
206− , writerEpubStylesheet = Nothing
207205 , writerEpubFonts = []
208206 , writerEpubChapterLevel = 1
209207 , writerTOCDepth = 3
src/Text/Pandoc/Writers/EPUB.hs+28−24
@@ -98,14 +98,10 @@ data EPUBMetadata = EPUBMetadata{
9898 , epubCoverage :: Maybe String
9999 , epubRights :: Maybe String
100100 , epubCoverImage :: Maybe String
101− , epubStylesheet :: Maybe Stylesheet
101+ , epubStylesheets :: [FilePath]
102102 , epubPageDirection :: Maybe ProgressionDirection
103103 } deriving Show
104104
105−data Stylesheet = StylesheetPath FilePath
106− | StylesheetContents String
107− deriving Show
108−
109105data Date = Date{
110106 dateText :: String
111107 , dateEvent :: Maybe String
@@ -240,6 +236,10 @@ metaValueToString (MetaBool True) = "true"
240236metaValueToString (MetaBool False) = "false"
241237metaValueToString _ = ""
242238
239+metaValueToPaths:: MetaValue -> [FilePath]
240+metaValueToPaths (MetaList xs) = map metaValueToString xs
241+metaValueToPaths x = [metaValueToString x]
242+
243243getList :: String -> Meta -> (MetaValue -> a) -> [a]
244244getList s meta handleMetaValue =
245245 case lookupMeta s meta of
@@ -307,7 +307,7 @@ metadataFromMeta opts meta = EPUBMetadata{
307307 , epubCoverage = coverage
308308 , epubRights = rights
309309 , epubCoverImage = coverImage
310− , epubStylesheet = stylesheet
310+ , epubStylesheets = stylesheets
311311 , epubPageDirection = pageDirection
312312 }
313313 where identifiers = getIdentifier meta
@@ -328,9 +328,9 @@ metadataFromMeta opts meta = EPUBMetadata{
328328 rights = metaValueToString <$> lookupMeta "rights" meta
329329 coverImage = lookup "epub-cover-image" (writerVariables opts) `mplus`
330330 (metaValueToString <$> lookupMeta "cover-image" meta)
331− stylesheet = (StylesheetContents <$> writerEpubStylesheet opts) `mplus`
332− ((StylesheetPath . metaValueToString) <$>
333− lookupMeta "stylesheet" meta)
331+ stylesheets = maybe [] id
332+ (metaValueToPaths <$> lookupMeta "stylesheet" meta) ++
333+ [f | ("css",f) <- writerVariables opts]
334334 pageDirection = case map toLower . metaValueToString <$>
335335 lookupMeta "page-progression-direction" meta of
336336 Just "ltr" -> Just LTR
@@ -374,10 +374,21 @@ pandocToEPUB version opts doc@(Pandoc meta _) = do
374374 let writeHtml o = fmap UTF8.fromStringLazy .
375375 writeHtmlStringForEPUB version o
376376 epochtime <- floor <$> lift P.getPOSIXTime
377+ metadata <- getEPUBMetadata opts meta
377378 let mkEntry path content = toEntry path epochtime content
379+
380+ -- stylesheet
381+ stylesheets <- case epubStylesheets metadata of
382+ [] -> (\x -> [B.fromChunks [x]]) <$>
383+ P.readDataFile (writerUserDataDir opts) "epub.css"
384+ fs -> mapM P.readFileLazy fs
385+ let stylesheetEntries = zipWith
386+ (\bs n -> mkEntry ("stylesheet" ++ show n ++ ".css") bs)
387+ stylesheets [(1 :: Int)..]
388+
378389 let vars = ("epub3", if epub3 then "true" else "false")
379− : ("css", "stylesheet.css")
380− : writerVariables opts
390+ : map (\e -> ("css", eRelativePath e)) stylesheetEntries
391+ ++ [(x,y) | (x,y) <- writerVariables opts, x /= "css"]
381392 let opts' = opts{ writerEmailObfuscation = NoObfuscation
382393 , writerSectionDivs = True
383394 , writerVariables = vars
@@ -386,7 +397,6 @@ pandocToEPUB version opts doc@(Pandoc meta _) = do
386397 then MathML
387398 else writerHTMLMathMethod opts
388399 , writerWrapText = WrapAuto }
389− metadata <- getEPUBMetadata opts' meta
390400
391401 -- cover page
392402 (cpgEntry, cpicEntry) <-
@@ -564,13 +574,14 @@ pandocToEPUB version opts doc@(Pandoc meta _) = do
564574 , unode "manifest" $
565575 [ unode "item" ! [("id","ncx"), ("href","toc.ncx")
566576 ,("media-type","application/x-dtbncx+xml")] $ ()
567− , unode "item" ! [("id","style"), ("href","stylesheet.css")
568− ,("media-type","text/css")] $ ()
569577 , unode "item" ! ([("id","nav")
570578 ,("href","nav.xhtml")
571579 ,("media-type","application/xhtml+xml")] ++
572580 [("properties","nav") | epub3 ]) $ ()
573581 ] ++
582+ [ (unode "item" ! [("id","style"), ("href",fp)
583+ ,("media-type","text/css")] $ ()) |
584+ fp <- map eRelativePath stylesheetEntries ] ++
574585 map chapterNode (cpgEntry ++ (tpEntry : chapterEntries)) ++
575586 (case cpicEntry of
576587 [] -> []
@@ -725,19 +736,12 @@ pandocToEPUB version opts doc@(Pandoc meta _) = do
725736 unode "option" ! [("name","specified-fonts")] $ "true"
726737 let appleEntry = mkEntry "META-INF/com.apple.ibooks.display-options.xml" apple
727738
728− -- stylesheet
729− stylesheet <- case epubStylesheet metadata of
730− Just (StylesheetPath fp) -> UTF8.toStringLazy <$> (lift $ P.readFileLazy fp)
731− Just (StylesheetContents s) -> return s
732− Nothing -> UTF8.toString `fmap`
733− (lift $ P.readDataFile (writerUserDataDir opts) "epub.css")
734− let stylesheetEntry = mkEntry "stylesheet.css" $ UTF8.fromStringLazy stylesheet
735−
736739 -- construct archive
737740 let archive = foldr addEntryToArchive emptyArchive
738− (mimetypeEntry : containerEntry : appleEntry : stylesheetEntry : tpEntry :
741+ (mimetypeEntry : containerEntry : appleEntry : tpEntry :
739742 contentsEntry : tocEntry : navEntry :
740− (picEntries ++ cpicEntry ++ cpgEntry ++ chapterEntries ++ fontEntries))
743+ (stylesheetEntries ++ picEntries ++ cpicEntry ++
744+ cpgEntry ++ chapterEntries ++ fontEntries))
741745 return $ fromArchive archive
742746
743747metadataElement :: EPUBVersion -> EPUBMetadata -> UTCTime -> Element