Skip to content

Commit aa571d2

Browse files
committed
Add --typst-input CLI option.
This allows one to pass parameters to typst, which are available at `sys.inputs`, just as `typst` itself does with its `--input` option. [API changes] * ReaderOptions has a new field `readerTypstInputs`. * Opt has a new field `optTypstInputs`. Closes #11588.
1 parent d9838eb commit aa571d2

9 files changed

Lines changed: 41 additions & 9 deletions

File tree

MANUAL.txt

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -765,6 +765,13 @@ header when requesting a document from a URL:
765765
space, and the period will not produce sentence-ending space
766766
in formats like LaTeX. The strings may not contain spaces.
767767

768+
`--typst-input=`*KEY*[`=`*VAL*]
769+
770+
: Set a parameter value that will be made available to the typst
771+
parser in `sys.inputs`, like `--input` in the `typst` CLI.
772+
Either `:` or `=` may be used to separate *KEY* from *VAL*.
773+
Values containing spaces must be quoted.
774+
768775
`--trace[=true|false]`
769776

770777
: Print diagnostic output tracing parser progress to stderr.
@@ -816,7 +823,7 @@ header when requesting a document from a URL:
816823
`-V keyword=Joe -V author=Sue` makes `author` contain a list
817824
of strings: `Joe` and `Sue`.
818825

819-
`--variable-json=`*KEY*[`=`:*JSON*]
826+
`--variable-json=`*KEY*[`=`*JSON*]
820827

821828
: Set the template variable *KEY* to the value specified by a JSON
822829
string (this may be a boolean, a string, a list, or a mapping;

cabal.project

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ source-repository-package
1515
source-repository-package
1616
type: git
1717
location: https://github.com/jgm/typst-hs.git
18-
tag: 97e075131fc794f7370648eb231fdfdc712b5dbd
18+
tag: bd0f68d0d03063bbd902b271f89fbc156b1da2dd
1919

2020
package pandoc
2121
flags: +embed_data_files +http

src/Text/Pandoc/App.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,7 @@ convertWithOpts' scriptingEngine istty datadir opts = do
238238
, readerAbbreviations = abbrevs
239239
, readerExtensions = readerExts
240240
, readerStripComments = optStripComments opts
241+
, readerTypstInputs = optTypstInputs opts
241242
}
242243

243244
metadataFromFile <- getMetadataFromFiles readerNameBase readerOpts

src/Text/Pandoc/App/CommandLineOptions.hs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ options =
303303
let (key, val) = splitField arg
304304
return opt{ optMetadata = addMeta key val $
305305
optMetadata opt })
306-
"KEY[:VALUE]")
306+
"KEY[=VALUE]")
307307
""
308308

309309
, Option "" ["metadata-file"]
@@ -345,7 +345,7 @@ options =
345345
"true|false")
346346
""
347347

348-
, Option "s" ["standalone"]
348+
, Option "s" ["standalone"]
349349
(OptArg
350350
(\arg opt -> do
351351
boolValue <- readBoolFromOptArg "--standalone/-s" arg
@@ -367,7 +367,7 @@ options =
367367
return opt{ optVariables =
368368
setVariable (T.pack key) (T.pack val) $
369369
optVariables opt })
370-
"KEY[:VALUE]")
370+
"KEY[=VALUE]")
371371
""
372372

373373
, Option "" ["variable-json"]
@@ -663,7 +663,7 @@ options =
663663
let (key, val) = splitField arg
664664
return opt{ optRequestHeaders =
665665
(T.pack key, T.pack val) : optRequestHeaders opt })
666-
"NAME:VALUE")
666+
"NAME=VALUE")
667667
""
668668

669669
, Option "" ["no-check-certificate"]
@@ -681,6 +681,14 @@ options =
681681
"FILE")
682682
"" -- "Specify file for custom abbreviations"
683683

684+
, Option "" ["typst-input"]
685+
(ReqArg
686+
(\arg opt -> do
687+
let (key, val) = splitField arg
688+
return opt{ optTypstInputs = (T.pack key, T.pack val) : optTypstInputs opt })
689+
"KEY=VALUE")
690+
""
691+
684692
, Option "" ["indented-code-classes"]
685693
(ReqArg
686694
(\arg opt -> return opt { optIndentedCodeClasses = T.words $

src/Text/Pandoc/App/Opt.hs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@ data Opt = Opt
184184
, optBibliography :: [FilePath] -- ^ Bibliography files
185185
, optCitationAbbreviations :: Maybe FilePath -- ^ Citation abbreviations
186186
, optSandbox :: Bool
187+
, optTypstInputs :: [(Text, Text)] -- ^ List of parameter values for typst
187188
} deriving (Generic, Show)
188189

189190
instance FromJSON Opt where
@@ -271,6 +272,7 @@ instance FromJSON Opt where
271272
<*> o .:? "bibliography" .!= optBibliography defaultOpts
272273
<*> o .:? "citation-abbreviations"
273274
<*> o .:? "sandbox" .!= optSandbox defaultOpts
275+
<*> o .:? "typst-inputs" .!= optTypstInputs defaultOpts
274276

275277
instance ToJSON Opt where
276278
toJSON = genericToJSON defaultOptions{
@@ -848,6 +850,7 @@ defaultOpts = Opt
848850
, optBibliography = []
849851
, optCitationAbbreviations = Nothing
850852
, optSandbox = False
853+
, optTypstInputs = []
851854
}
852855

853856
yamlToMeta :: Value -> Parser Meta

src/Text/Pandoc/Options.hs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ data ReaderOptions = ReaderOptions{
7474
, readerTrackChanges :: TrackChanges -- ^ Track changes setting for docx
7575
, readerStripComments :: Bool -- ^ Strip HTML comments instead of parsing as raw HTML
7676
-- (only implemented in commonmark)
77+
, readerTypstInputs :: [(Text, Text)] -- ^ parameters specified using --typst-input
7778
} deriving (Show, Read, Data, Typeable, Generic)
7879

7980
instance HasSyntaxExtensions ReaderOptions where
@@ -90,6 +91,7 @@ instance Default ReaderOptions
9091
, readerDefaultImageExtension = ""
9192
, readerTrackChanges = AcceptChanges
9293
, readerStripComments = False
94+
, readerTypstInputs = []
9395
}
9496

9597
defaultAbbrevs :: Set.Set Text

src/Text/Pandoc/Readers/Typst.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ import qualified System.FilePath.Posix as Posix
6060
-- | Read Typst from an input string and return a Pandoc document.
6161
readTypst :: (PandocMonad m, ToSources a)
6262
=> ReaderOptions -> a -> m Pandoc
63-
readTypst _opts inp = do
63+
readTypst opts inp = do
6464
let sources = toSources inp
6565
let inputName = case sources of
6666
Sources ((pos, _):_) -> sourceName pos
@@ -73,7 +73,7 @@ readTypst _opts inp = do
7373
currentUTCTime = getCurrentTime,
7474
lookupEnvVar = fmap (fmap T.unpack) . lookupEnv . T.pack,
7575
checkExistence = fileExists }
76-
res <- evaluateTypst ops inputName parsed
76+
res <- evaluateTypst ops (readerTypstInputs opts) inputName parsed
7777
case res of
7878
Left e -> throwError $ PandocParseError $ tshow e
7979
Right content -> do

stack.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ extra-deps:
2727
- djot-0.1.4
2828
- asciidoc-0.1.0.2
2929
- texmath-0.13.1.1
30-
- typst-0.9.0.1
30+
- git: https://github.com/jgm/typst-hs.git
31+
commit: bd0f68d0d03063bbd902b271f89fbc156b1da2dd
3132

3233
ghc-options:
3334
"$locals": -fhide-source-paths -Wno-missing-home-modules

test/command/11588.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
```
2+
% pandoc --typst-input foo=FOO --typst-input bar="bar bim" -f typst -t plain
3+
#sys.inputs.at("foo");
4+
5+
#sys.inputs.at("bar");
6+
^D
7+
FOO
8+
9+
bar bim
10+
```

0 commit comments

Comments
 (0)