git.iko.soy/typeable/pandoc
on master help sign in
Opt: Change optHighlightStyle to a Maybe String instead of Maybe Style.

Do the parsing/loading of themes later, after option parsing.

commit3ef0cdd8f9dafcd9e4b5c1e5f82e31bed0c3d847 authorJohn MacFarlane <jgm@berkeley.edu> dateSun 6 Oct 2019, 23:15 (7 years ago) parent5f8254c
3 files changed+28−24
src/Text/Pandoc/App/CommandLineOptions.hs+21−19 src/Text/Pandoc/App/Opt.hs+2−3 src/Text/Pandoc/App/OutputSettings.hs+5−2
src/Text/Pandoc/App/CommandLineOptions.hs+21−19
@@ -20,10 +20,12 @@ module Text.Pandoc.App.CommandLineOptions (
2020 parseOptions
2121 , options
2222 , engines
23+ , lookupHighlightStyle
2324 ) where
2425import Prelude
2526import Control.Monad
2627import Control.Monad.Trans
28+import Control.Monad.Except (throwError)
2729import Data.Aeson.Encode.Pretty (encodePretty', Config(..), keyOrder,
2830 defConfig, Indent(..), NumberFormat(..))
2931import Data.Char (toLower)
@@ -34,8 +36,7 @@ import Data.List (isPrefixOf)
3436#endif
3537#endif
3638import Data.Maybe (fromMaybe)
37−import Skylighting (Style, Syntax (..), defaultSyntaxMap, parseTheme,
38− pygments)
39+import Skylighting (Style, Syntax (..), defaultSyntaxMap, parseTheme)
3940import System.Console.GetOpt
4041import System.Environment (getArgs, getProgName)
4142import System.Exit (exitSuccess)
@@ -47,6 +48,7 @@ import Text.Pandoc.Filter (Filter (..))
4748import Text.Pandoc.Highlighting (highlightingStyles)
4849import Text.Pandoc.Writers.Math (defaultMathJaxURL, defaultKaTeXURL)
4950import Text.Pandoc.Shared (ordNub, safeRead, defaultUserDataDirs)
51+import Text.Pandoc.Class (PandocMonad(..), runIOorExplode)
5052import Text.Printf
5153
5254#ifdef EMBED_DATA_FILES
@@ -102,20 +104,6 @@ engines = map ("html",) htmlEngines ++
102104pdfEngines :: [String]
103105pdfEngines = ordNub $ map snd engines
104106
105−lookupHighlightStyle :: String -> IO (Maybe Style)
106−lookupHighlightStyle s
107− | takeExtension s == ".theme" = -- attempt to load KDE theme
108− do contents <- B.readFile s
109− case parseTheme contents of
110− Left _ -> E.throwIO $ PandocOptionError $
111− "Could not read highlighting theme " ++ s
112− Right sty -> return (Just sty)
113− | otherwise =
114− case lookup (map toLower s) highlightingStyles of
115− Just sty -> return (Just sty)
116− Nothing -> E.throwIO $ PandocOptionError $
117− "Unknown highlight-style " ++ s
118−
119107-- | A list of functions, each transforming the options data structure
120108-- in response to a command-line option.
121109options :: [OptDescr (Opt -> IO Opt)]
@@ -296,8 +284,8 @@ options =
296284
297285 , Option "" ["highlight-style"]
298286 (ReqArg
299− (\arg opt -> lookupHighlightStyle arg >>= \style ->
300− return opt{ optHighlightStyle = style })
287+ (\arg opt ->
288+ return opt{ optHighlightStyle = Just arg })
301289 "STYLE|FILE")
302290 "" -- "Style for highlighted code"
303291
@@ -856,7 +844,7 @@ options =
856844 let write = case optOutputFile opt of
857845 Just f -> B.writeFile f
858846 Nothing -> B.putStr
859− sty <- fromMaybe pygments <$> lookupHighlightStyle arg
847+ sty <- runIOorExplode $ lookupHighlightStyle arg
860848 write $ encodePretty'
861849 defConfig{confIndent = Spaces 4
862850 ,confCompare = keyOrder
@@ -965,6 +953,20 @@ splitField s =
965953 (k,_:v) -> (k,v)
966954 (k,[]) -> (k,"true")
967955
956+lookupHighlightStyle :: PandocMonad m => String -> m Style
957+lookupHighlightStyle s
958+ | takeExtension s == ".theme" = -- attempt to load KDE theme
959+ do contents <- readFileLazy s
960+ case parseTheme contents of
961+ Left _ -> throwError $ PandocOptionError $
962+ "Could not read highlighting theme " ++ s
963+ Right sty -> return sty
964+ | otherwise =
965+ case lookup (map toLower s) highlightingStyles of
966+ Just sty -> return sty
967+ Nothing -> throwError $ PandocOptionError $
968+ "Unknown highlight-style " ++ s
969+
968970deprecatedOption :: String -> String -> IO ()
969971deprecatedOption o msg =
970972 runIO (report $ Deprecated o msg) >>=
src/Text/Pandoc/App/Opt.hs+2−3
@@ -23,7 +23,6 @@ module Text.Pandoc.App.Opt (
2323import Prelude
2424import GHC.Generics
2525import Text.Pandoc.Filter (Filter (..))
26−import Text.Pandoc.Highlighting (Style, pygments)
2726import Text.Pandoc.Logging (Verbosity (WARNING))
2827import Text.Pandoc.Options (TopLevelDivision (TopLevelDefault),
2928 TrackChanges (AcceptChanges),
@@ -63,7 +62,7 @@ data Opt = Opt
6362 , optIncremental :: Bool -- ^ Use incremental lists in Slidy/Slideous/S5
6463 , optSelfContained :: Bool -- ^ Make HTML accessible offline
6564 , optHtmlQTags :: Bool -- ^ Use <q> tags in HTML
66− , optHighlightStyle :: Maybe Style -- ^ Style to use for highlighted code
65+ , optHighlightStyle :: Maybe String -- ^ Style to use for highlighted code
6766 , optSyntaxDefinitions :: [FilePath] -- ^ xml syntax defs to load
6867 , optTopLevelDivision :: TopLevelDivision -- ^ Type of the top-level divisions
6968 , optHTMLMathMethod :: HTMLMathMethod -- ^ Method to print HTML math
@@ -137,7 +136,7 @@ defaultOpts = Opt
137136 , optIncremental = False
138137 , optSelfContained = False
139138 , optHtmlQTags = False
140− , optHighlightStyle = Just pygments
139+ , optHighlightStyle = Just "pygments"
141140 , optSyntaxDefinitions = []
142141 , optTopLevelDivision = TopLevelDefault
143142 , optHTMLMathMethod = PlainMath
src/Text/Pandoc/App/OutputSettings.hs+5−2
@@ -35,7 +35,7 @@ import System.IO (stdout)
3535import Text.Pandoc
3636import Text.Pandoc.App.FormatHeuristics (formatFromFilePaths)
3737import Text.Pandoc.App.Opt (Opt (..))
38−import Text.Pandoc.App.CommandLineOptions (engines)
38+import Text.Pandoc.App.CommandLineOptions (engines, lookupHighlightStyle)
3939import Text.Pandoc.BCP47 (Lang (..), parseBCP47)
4040import qualified Text.Pandoc.UTF8 as UTF8
4141
@@ -105,6 +105,9 @@ optToOutputSettings opts = do
105105 syntaxMap <- foldM addSyntaxMap defaultSyntaxMap
106106 (optSyntaxDefinitions opts)
107107
108+ hlStyle <- maybe (return Nothing) (fmap Just . lookupHighlightStyle)
109+ (optHighlightStyle opts)
110+
108111 -- note: this reverses the list constructed in option parsing,
109112 -- which in turn was reversed from the command-line order,
110113 -- so we end up with the correct order in the variable list:
@@ -221,7 +224,7 @@ optToOutputSettings opts = do
221224 , writerTopLevelDivision = optTopLevelDivision opts
222225 , writerListings = optListings opts
223226 , writerSlideLevel = optSlideLevel opts
224− , writerHighlightStyle = optHighlightStyle opts
227+ , writerHighlightStyle = hlStyle
225228 , writerSetextHeaders = optSetextHeaders opts
226229 , writerEpubSubdirectory = optEpubSubdirectory opts
227230 , writerEpubMetadata = epubMetadata