T.P.Highlighting: Export lookupHighlightingStyle.

[API change]
Previously this lived in an unexported module
T.P.App.CommandLineOptions, under the name `lookupHighlightStyle`.
This commit is contained in:
John MacFarlane 2022-08-14 22:50:57 -07:00
parent 5d9b36ae53
commit 4f8ee2212a
3 changed files with 30 additions and 22 deletions

View file

@ -22,12 +22,10 @@ module Text.Pandoc.App.CommandLineOptions (
, parseOptionsFromArgs , parseOptionsFromArgs
, options , options
, engines , engines
, lookupHighlightStyle
, setVariable , setVariable
) where ) where
import Control.Monad import Control.Monad
import Control.Monad.Trans import Control.Monad.Trans
import Control.Monad.Except (throwError)
import Control.Monad.State.Strict import Control.Monad.State.Strict
import Data.Aeson.Encode.Pretty (encodePretty', Config(..), keyOrder, import Data.Aeson.Encode.Pretty (encodePretty', Config(..), keyOrder,
defConfig, Indent(..), NumberFormat(..)) defConfig, Indent(..), NumberFormat(..))
@ -41,7 +39,7 @@ import Data.Maybe (fromMaybe, isJust)
import Data.Text (Text) import Data.Text (Text)
import HsLua (Exception, getglobal, openlibs, peek, run, top) import HsLua (Exception, getglobal, openlibs, peek, run, top)
import Safe (tailDef) import Safe (tailDef)
import Skylighting (Style, Syntax (..), defaultSyntaxMap, parseTheme) import Skylighting (Syntax (..), defaultSyntaxMap)
import System.Console.GetOpt import System.Console.GetOpt
import System.Environment (getArgs, getProgName) import System.Environment (getArgs, getProgName)
import System.Exit (exitSuccess) import System.Exit (exitSuccess)
@ -54,7 +52,7 @@ import Text.Pandoc.App.Opt (Opt (..), LineEnding (..), IpynbOutput (..),
DefaultsState (..), applyDefaults, DefaultsState (..), applyDefaults,
fullDefaultsPath) fullDefaultsPath)
import Text.Pandoc.Filter (Filter (..)) import Text.Pandoc.Filter (Filter (..))
import Text.Pandoc.Highlighting (highlightingStyles) import Text.Pandoc.Highlighting (highlightingStyles, lookupHighlightingStyle)
import Text.Pandoc.Shared (ordNub, elemText, safeStrRead, defaultUserDataDir) import Text.Pandoc.Shared (ordNub, elemText, safeStrRead, defaultUserDataDir)
import Text.Printf import Text.Printf
@ -946,7 +944,7 @@ options =
(ReqArg (ReqArg
(\arg opt -> do (\arg opt -> do
let write = maybe B.putStr B.writeFile $ optOutputFile opt let write = maybe B.putStr B.writeFile $ optOutputFile opt
sty <- runIOorExplode $ lookupHighlightStyle arg sty <- runIOorExplode $ lookupHighlightingStyle arg
write $ encodePretty' write $ encodePretty'
defConfig{confIndent = Spaces 4 defConfig{confIndent = Spaces 4
,confCompare = keyOrder ,confCompare = keyOrder
@ -1059,20 +1057,6 @@ writersNames = sort
splitField :: String -> (String, String) splitField :: String -> (String, String)
splitField = second (tailDef "true") . break (`elemText` ":=") splitField = second (tailDef "true") . break (`elemText` ":=")
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 $ T.pack $
"Could not read highlighting theme " ++ s
Right sty -> return sty
| otherwise =
case lookup (T.toLower $ T.pack s) highlightingStyles of
Just sty -> return sty
Nothing -> throwError $ PandocOptionError $ T.pack $
"Unknown highlight-style " ++ s
deprecatedOption :: String -> String -> IO () deprecatedOption :: String -> String -> IO ()
deprecatedOption o msg = deprecatedOption o msg =
runIO (report $ Deprecated (T.pack o) (T.pack msg)) >>= runIO (report $ Deprecated (T.pack o) (T.pack msg)) >>=

View file

@ -37,8 +37,8 @@ import System.IO (stdout)
import Text.Pandoc import Text.Pandoc
import Text.Pandoc.App.FormatHeuristics (formatFromFilePaths) import Text.Pandoc.App.FormatHeuristics (formatFromFilePaths)
import Text.Pandoc.App.Opt (Opt (..)) import Text.Pandoc.App.Opt (Opt (..))
import Text.Pandoc.App.CommandLineOptions (engines, lookupHighlightStyle, import Text.Pandoc.App.CommandLineOptions (engines, setVariable)
setVariable) import Text.Pandoc.Highlighting (lookupHighlightingStyle)
import Text.Pandoc.Writers.Custom (writeCustom) import Text.Pandoc.Writers.Custom (writeCustom)
import qualified Text.Pandoc.UTF8 as UTF8 import qualified Text.Pandoc.UTF8 as UTF8
@ -128,7 +128,8 @@ optToOutputSettings opts = do
syntaxMap <- foldM addSyntaxMap defaultSyntaxMap syntaxMap <- foldM addSyntaxMap defaultSyntaxMap
(optSyntaxDefinitions opts) (optSyntaxDefinitions opts)
hlStyle <- traverse (lookupHighlightStyle . T.unpack) $ optHighlightStyle opts hlStyle <- traverse (lookupHighlightingStyle . T.unpack) $
optHighlightStyle opts
let setVariableM k v = return . setVariable k v let setVariableM k v = return . setVariable k v

View file

@ -30,6 +30,7 @@ module Text.Pandoc.Highlighting ( highlightingStyles
, breezeDark , breezeDark
, haddock , haddock
, Style , Style
, lookupHighlightingStyle
, fromListingsLanguage , fromListingsLanguage
, toListingsLanguage , toListingsLanguage
) where ) where
@ -39,6 +40,10 @@ import Data.Maybe (fromMaybe)
import qualified Data.Text as T import qualified Data.Text as T
import Skylighting import Skylighting
import Text.Pandoc.Definition import Text.Pandoc.Definition
import Text.Pandoc.Class (PandocMonad, readFileLazy)
import Text.Pandoc.Error (PandocError(..))
import Control.Monad.Except (throwError)
import System.FilePath (takeExtension)
import Text.Pandoc.Shared (safeRead) import Text.Pandoc.Shared (safeRead)
highlightingStyles :: [(T.Text, Style)] highlightingStyles :: [(T.Text, Style)]
@ -214,3 +219,21 @@ toListingsLanguage lang = M.lookup (T.toLower lang) langToListingsMap
-- | Determine skylighting language name from listings language name. -- | Determine skylighting language name from listings language name.
fromListingsLanguage :: T.Text -> Maybe T.Text fromListingsLanguage :: T.Text -> Maybe T.Text
fromListingsLanguage lang = M.lookup lang listingsToLangMap fromListingsLanguage lang = M.lookup lang listingsToLangMap
-- | Lookup style from a name. If the name is a standard style,
-- load it; if it ends in ".theme", attempt to load a KDE theme
-- from the file path specified.
lookupHighlightingStyle :: PandocMonad m => String -> m Style
lookupHighlightingStyle s
| takeExtension s == ".theme" = -- attempt to load KDE theme
do contents <- readFileLazy s
case parseTheme contents of
Left _ -> throwError $ PandocOptionError $ T.pack $
"Could not read highlighting theme " ++ s
Right sty -> return sty
| otherwise =
case lookup (T.toLower $ T.pack s) highlightingStyles of
Just sty -> return sty
Nothing -> throwError $ PandocOptionError $ T.pack $
"Unknown highlight-style " ++ s