From 3ef0cdd8f9dafcd9e4b5c1e5f82e31bed0c3d847 Mon Sep 17 00:00:00 2001 From: John MacFarlane Date: Sun, 6 Oct 2019 23:15:52 -0700 Subject: Opt: Change optHighlightStyle to a Maybe String instead of Maybe Style. Do the parsing/loading of themes later, after option parsing. --- src/Text/Pandoc/App/CommandLineOptions.hs | 40 ++++++++++++++++--------------- src/Text/Pandoc/App/Opt.hs | 5 ++-- src/Text/Pandoc/App/OutputSettings.hs | 7 ++++-- 3 files changed, 28 insertions(+), 24 deletions(-) diff --git a/src/Text/Pandoc/App/CommandLineOptions.hs b/src/Text/Pandoc/App/CommandLineOptions.hs index adf1d3715..960d7cb5f 100644 --- a/src/Text/Pandoc/App/CommandLineOptions.hs +++ b/src/Text/Pandoc/App/CommandLineOptions.hs @@ -20,10 +20,12 @@ module Text.Pandoc.App.CommandLineOptions ( parseOptions , options , engines + , lookupHighlightStyle ) where import Prelude import Control.Monad import Control.Monad.Trans +import Control.Monad.Except (throwError) import Data.Aeson.Encode.Pretty (encodePretty', Config(..), keyOrder, defConfig, Indent(..), NumberFormat(..)) import Data.Char (toLower) @@ -34,8 +36,7 @@ import Data.List (isPrefixOf) #endif #endif import Data.Maybe (fromMaybe) -import Skylighting (Style, Syntax (..), defaultSyntaxMap, parseTheme, - pygments) +import Skylighting (Style, Syntax (..), defaultSyntaxMap, parseTheme) import System.Console.GetOpt import System.Environment (getArgs, getProgName) import System.Exit (exitSuccess) @@ -47,6 +48,7 @@ import Text.Pandoc.Filter (Filter (..)) import Text.Pandoc.Highlighting (highlightingStyles) import Text.Pandoc.Writers.Math (defaultMathJaxURL, defaultKaTeXURL) import Text.Pandoc.Shared (ordNub, safeRead, defaultUserDataDirs) +import Text.Pandoc.Class (PandocMonad(..), runIOorExplode) import Text.Printf #ifdef EMBED_DATA_FILES @@ -102,20 +104,6 @@ engines = map ("html",) htmlEngines ++ pdfEngines :: [String] pdfEngines = ordNub $ map snd engines -lookupHighlightStyle :: String -> IO (Maybe Style) -lookupHighlightStyle s - | takeExtension s == ".theme" = -- attempt to load KDE theme - do contents <- B.readFile s - case parseTheme contents of - Left _ -> E.throwIO $ PandocOptionError $ - "Could not read highlighting theme " ++ s - Right sty -> return (Just sty) - | otherwise = - case lookup (map toLower s) highlightingStyles of - Just sty -> return (Just sty) - Nothing -> E.throwIO $ PandocOptionError $ - "Unknown highlight-style " ++ s - -- | A list of functions, each transforming the options data structure -- in response to a command-line option. options :: [OptDescr (Opt -> IO Opt)] @@ -296,8 +284,8 @@ options = , Option "" ["highlight-style"] (ReqArg - (\arg opt -> lookupHighlightStyle arg >>= \style -> - return opt{ optHighlightStyle = style }) + (\arg opt -> + return opt{ optHighlightStyle = Just arg }) "STYLE|FILE") "" -- "Style for highlighted code" @@ -856,7 +844,7 @@ options = let write = case optOutputFile opt of Just f -> B.writeFile f Nothing -> B.putStr - sty <- fromMaybe pygments <$> lookupHighlightStyle arg + sty <- runIOorExplode $ lookupHighlightStyle arg write $ encodePretty' defConfig{confIndent = Spaces 4 ,confCompare = keyOrder @@ -965,6 +953,20 @@ splitField s = (k,_:v) -> (k,v) (k,[]) -> (k,"true") +lookupHighlightStyle :: PandocMonad m => String -> m Style +lookupHighlightStyle s + | takeExtension s == ".theme" = -- attempt to load KDE theme + do contents <- readFileLazy s + case parseTheme contents of + Left _ -> throwError $ PandocOptionError $ + "Could not read highlighting theme " ++ s + Right sty -> return sty + | otherwise = + case lookup (map toLower s) highlightingStyles of + Just sty -> return sty + Nothing -> throwError $ PandocOptionError $ + "Unknown highlight-style " ++ s + deprecatedOption :: String -> String -> IO () deprecatedOption o msg = runIO (report $ Deprecated o msg) >>= diff --git a/src/Text/Pandoc/App/Opt.hs b/src/Text/Pandoc/App/Opt.hs index 126f5db4c..3fe7cadf1 100644 --- a/src/Text/Pandoc/App/Opt.hs +++ b/src/Text/Pandoc/App/Opt.hs @@ -23,7 +23,6 @@ module Text.Pandoc.App.Opt ( import Prelude import GHC.Generics import Text.Pandoc.Filter (Filter (..)) -import Text.Pandoc.Highlighting (Style, pygments) import Text.Pandoc.Logging (Verbosity (WARNING)) import Text.Pandoc.Options (TopLevelDivision (TopLevelDefault), TrackChanges (AcceptChanges), @@ -63,7 +62,7 @@ data Opt = Opt , optIncremental :: Bool -- ^ Use incremental lists in Slidy/Slideous/S5 , optSelfContained :: Bool -- ^ Make HTML accessible offline , optHtmlQTags :: Bool -- ^ Use tags in HTML - , optHighlightStyle :: Maybe Style -- ^ Style to use for highlighted code + , optHighlightStyle :: Maybe String -- ^ Style to use for highlighted code , optSyntaxDefinitions :: [FilePath] -- ^ xml syntax defs to load , optTopLevelDivision :: TopLevelDivision -- ^ Type of the top-level divisions , optHTMLMathMethod :: HTMLMathMethod -- ^ Method to print HTML math @@ -137,7 +136,7 @@ defaultOpts = Opt , optIncremental = False , optSelfContained = False , optHtmlQTags = False - , optHighlightStyle = Just pygments + , optHighlightStyle = Just "pygments" , optSyntaxDefinitions = [] , optTopLevelDivision = TopLevelDefault , optHTMLMathMethod = PlainMath diff --git a/src/Text/Pandoc/App/OutputSettings.hs b/src/Text/Pandoc/App/OutputSettings.hs index cfb6f7ec2..04ea89eda 100644 --- a/src/Text/Pandoc/App/OutputSettings.hs +++ b/src/Text/Pandoc/App/OutputSettings.hs @@ -35,7 +35,7 @@ import System.IO (stdout) import Text.Pandoc import Text.Pandoc.App.FormatHeuristics (formatFromFilePaths) import Text.Pandoc.App.Opt (Opt (..)) -import Text.Pandoc.App.CommandLineOptions (engines) +import Text.Pandoc.App.CommandLineOptions (engines, lookupHighlightStyle) import Text.Pandoc.BCP47 (Lang (..), parseBCP47) import qualified Text.Pandoc.UTF8 as UTF8 @@ -105,6 +105,9 @@ optToOutputSettings opts = do syntaxMap <- foldM addSyntaxMap defaultSyntaxMap (optSyntaxDefinitions opts) + hlStyle <- maybe (return Nothing) (fmap Just . lookupHighlightStyle) + (optHighlightStyle opts) + -- note: this reverses the list constructed in option parsing, -- which in turn was reversed from the command-line order, -- so we end up with the correct order in the variable list: @@ -221,7 +224,7 @@ optToOutputSettings opts = do , writerTopLevelDivision = optTopLevelDivision opts , writerListings = optListings opts , writerSlideLevel = optSlideLevel opts - , writerHighlightStyle = optHighlightStyle opts + , writerHighlightStyle = hlStyle , writerSetextHeaders = optSetextHeaders opts , writerEpubSubdirectory = optEpubSubdirectory opts , writerEpubMetadata = epubMetadata -- cgit v1.2.3