mirror of
https://github.com/jgm/pandoc.git
synced 2026-08-28 17:20:47 +08:00
Remove code duplication around version info.
Text.Pandoc.App.CommandLineOptions and pandoc-cli/src/pandoc.hs had similar code for generating version information. To avoid duplication, we now export `versionInfo` from Text.Pandoc.App [API change]. (The function is reexported from the non-public module Text.Pandoc.App.CommandLineOptions.) This function has three parameters that can be filled in when it is called by pandoc-cli. This change will make it simpler to revise version information.
This commit is contained in:
+11
-28
@@ -16,17 +16,12 @@ module Main where
|
||||
import qualified Control.Exception as E
|
||||
import System.Environment (getArgs, getProgName)
|
||||
import Text.Pandoc.App ( convertWithOpts, defaultOpts, options
|
||||
, parseOptionsFromArgs, handleOptInfo )
|
||||
, parseOptionsFromArgs, handleOptInfo, versionInfo )
|
||||
import Text.Pandoc.Error (handleError)
|
||||
import System.Exit (exitSuccess)
|
||||
import Data.Monoid (Any(..))
|
||||
import PandocCLI.Lua
|
||||
import PandocCLI.Server
|
||||
import qualified Text.Pandoc.UTF8 as UTF8
|
||||
import Text.Pandoc.Version (pandocVersion)
|
||||
import Text.Pandoc.Data (defaultUserDataDir)
|
||||
import Text.Pandoc.Scripting (ScriptingEngine(..))
|
||||
import Data.Version (showVersion)
|
||||
import qualified Data.Text as T
|
||||
|
||||
#ifdef NIGHTLY
|
||||
@@ -51,7 +46,7 @@ main = E.handle (handleError . Left) $ do
|
||||
let hasVersion = getAny $ foldMap
|
||||
(\s -> Any (s == "-v" || s == "--version"))
|
||||
(takeWhile (/= "--") rawArgs)
|
||||
let versionOr action = if hasVersion then versionInfo else action
|
||||
let versionOr action = if hasVersion then versionInfoCLI else action
|
||||
case prg of
|
||||
"pandoc-server.cgi" -> versionOr runCGI
|
||||
"pandoc-server" -> versionOr $ runServer rawArgs
|
||||
@@ -67,37 +62,25 @@ main = E.handle (handleError . Left) $ do
|
||||
Left e -> handleOptInfo engine e
|
||||
Right opts -> convertWithOpts engine opts
|
||||
|
||||
copyrightMessage :: String
|
||||
copyrightMessage =
|
||||
"Copyright (C) 2006-2024 John MacFarlane. Web: https://pandoc.org\n"
|
||||
++
|
||||
"This is free software; see the source for copying conditions. There is no\n"
|
||||
++
|
||||
"warranty, not even for merchantability or fitness for a particular purpose."
|
||||
|
||||
flagSettings :: String
|
||||
flagSettings = "Features: " ++
|
||||
getFeatures :: [String]
|
||||
getFeatures = [
|
||||
#ifdef VERSION_pandoc_server
|
||||
"+server"
|
||||
#else
|
||||
"-server"
|
||||
#endif
|
||||
++ " " ++
|
||||
,
|
||||
#ifdef VERSION_hslua_cli
|
||||
"+lua"
|
||||
#else
|
||||
"-lua"
|
||||
#endif
|
||||
]
|
||||
|
||||
versionInfo :: IO ()
|
||||
versionInfo = do
|
||||
defaultDatadir <- defaultUserDataDir
|
||||
versionInfoCLI :: IO ()
|
||||
versionInfoCLI = do
|
||||
scriptingEngine <- getEngine
|
||||
UTF8.putStr $ T.unlines $ map T.pack
|
||||
[ "pandoc " ++ showVersion pandocVersion ++ versionSuffix
|
||||
, flagSettings
|
||||
, "Scripting engine: " ++ T.unpack (engineName scriptingEngine)
|
||||
, "User data directory: " ++ defaultDatadir
|
||||
, copyrightMessage
|
||||
]
|
||||
exitSuccess
|
||||
versionInfo getFeatures
|
||||
(Just $ T.unpack (engineName scriptingEngine))
|
||||
versionSuffix
|
||||
|
||||
@@ -26,6 +26,7 @@ module Text.Pandoc.App (
|
||||
, parseOptionsFromArgs
|
||||
, options
|
||||
, applyFilters
|
||||
, versionInfo
|
||||
) where
|
||||
import qualified Control.Exception as E
|
||||
import Control.Monad ( (>=>), when, forM, forM_ )
|
||||
@@ -55,7 +56,7 @@ import Text.Pandoc.Image (svgToPng)
|
||||
import Text.Pandoc.App.Opt (Opt (..), LineEnding (..), defaultOpts,
|
||||
IpynbOutput (..), OptInfo(..))
|
||||
import Text.Pandoc.App.CommandLineOptions (parseOptions, parseOptionsFromArgs,
|
||||
options, handleOptInfo)
|
||||
options, handleOptInfo, versionInfo)
|
||||
import Text.Pandoc.App.Input (InputParameters (..), readInput)
|
||||
import Text.Pandoc.App.OutputSettings (OutputSettings (..), optToOutputSettings,
|
||||
sandbox')
|
||||
|
||||
@@ -21,6 +21,7 @@ module Text.Pandoc.App.CommandLineOptions (
|
||||
, options
|
||||
, engines
|
||||
, setVariable
|
||||
, versionInfo
|
||||
) where
|
||||
import Control.Monad.Trans
|
||||
import Control.Monad.State.Strict
|
||||
@@ -46,6 +47,7 @@ import System.IO (stdout)
|
||||
import Text.DocTemplates (Context (..), ToContext (toVal), Val (..))
|
||||
import Text.Pandoc
|
||||
import Text.Pandoc.Builder (setMeta)
|
||||
import Data.Version (showVersion)
|
||||
import Text.Pandoc.App.Opt (Opt (..), LineEnding (..), IpynbOutput (..),
|
||||
DefaultsState (..), applyDefaults,
|
||||
fullDefaultsPath, OptInfo(..))
|
||||
@@ -193,13 +195,7 @@ handleOptInfo engine info = E.handle (handleError . Left) $ do
|
||||
,"text-styles"])
|
||||
,confNumFormat = Generic
|
||||
,confTrailingNewline = True} sty
|
||||
VersionInfo -> do
|
||||
defaultDatadir <- defaultUserDataDir
|
||||
UTF8.hPutStrLn stdout
|
||||
$ T.pack
|
||||
$ "pandoc " ++ T.unpack pandocVersionText ++
|
||||
"\nUser data directory: " ++ defaultDatadir ++
|
||||
('\n':copyrightMessage)
|
||||
VersionInfo -> versionInfo [] Nothing ""
|
||||
Help -> do
|
||||
prg <- getProgName
|
||||
UTF8.hPutStr stdout (T.pack $ usageMessage prg options)
|
||||
@@ -1269,3 +1265,21 @@ normalizePath fp =
|
||||
#else
|
||||
normalizePath = id
|
||||
#endif
|
||||
|
||||
-- | Print version information with customizable features and scripting engine
|
||||
versionInfo :: [String] -> Maybe String -> String -> IO ()
|
||||
versionInfo features mbScriptingEngineName suffix = do
|
||||
defaultDatadir <- defaultUserDataDir
|
||||
let featuresLine = if null features
|
||||
then []
|
||||
else ["Features: " ++ unwords features]
|
||||
let scriptingLine = case mbScriptingEngineName of
|
||||
Nothing -> []
|
||||
Just name -> ["Scripting engine: " ++ name]
|
||||
UTF8.putStr $ T.unlines $ map T.pack $
|
||||
["pandoc " ++ showVersion pandocVersion ++ suffix] ++
|
||||
featuresLine ++
|
||||
scriptingLine ++
|
||||
["User data directory: " ++ defaultDatadir,
|
||||
copyrightMessage]
|
||||
exitSuccess
|
||||
|
||||
Reference in New Issue
Block a user