aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJohn MacFarlane <[email protected]>2025-05-11 18:10:11 -0700
committerJohn MacFarlane <[email protected]>2025-05-11 18:10:11 -0700
commit1239eaa4bea2074d658c3c9f4ad788c7cfef4873 (patch)
tree1afbedc1c171820fd276ea97aff55f07776b7783 /src
parent5901d1ca86f0d6ec624751505940b49811013f64 (diff)
Add new option `--variable-json`.
This allows non-string values (booleans, lists, maps) to be given to template variables on the command line. Closes #10341. Supersedes #10342.
Diffstat (limited to 'src')
-rw-r--r--src/Text/Pandoc/App/CommandLineOptions.hs18
1 files changed, 18 insertions, 0 deletions
diff --git a/src/Text/Pandoc/App/CommandLineOptions.hs b/src/Text/Pandoc/App/CommandLineOptions.hs
index e65bb65ae..a4fec880d 100644
--- a/src/Text/Pandoc/App/CommandLineOptions.hs
+++ b/src/Text/Pandoc/App/CommandLineOptions.hs
@@ -25,6 +25,7 @@ module Text.Pandoc.App.CommandLineOptions (
import Control.Monad.Trans
import Control.Monad.State.Strict
import Data.Containers.ListUtils (nubOrd)
+import Data.Aeson (eitherDecode)
import Data.Aeson.Encode.Pretty (encodePretty', Config(..), keyOrder,
defConfig, Indent(..), NumberFormat(..))
import Data.Bifunctor (second)
@@ -372,6 +373,23 @@ options =
"KEY[:VALUE]")
""
+ , Option "" ["variable-json"]
+ (ReqArg
+ (\arg opt -> do
+ let (key, json) = splitField arg
+ case eitherDecode (B.fromStrict . UTF8.fromString $ json) of
+ Right (val :: Val Text) ->
+ return opt{ optVariables =
+ let Context m = optVariables opt
+ in Context $ M.insert (T.pack key) val m }
+ -- note that this replaces any existing value, which
+ -- is different from what --variable does
+ Left err' -> optError $ PandocOptionError $
+ "Could not parse '" <> T.pack json <> "' as JSON:\n" <>
+ T.pack err')
+ "KEY[:JSON]")
+ ""
+
, Option "" ["wrap"]
(ReqArg
(\arg opt ->