From 11d04c571b003a253aa31818f283f5eaa22dcf4f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gunnar=20Farneb=C3=A4ck?= Date: Fri, 25 Oct 2024 16:34:48 +0200 Subject: [PATCH] Add convenience functions to compile and watch. --- README.md | 3 +++ src/Commands.jl | 32 ++++++++++++++++++++++++++++++++ src/Typstry.jl | 4 ++-- test/interface/TestCommands.jl | 15 +++++++++++++++ 4 files changed, 52 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 0cfd519..af89351 100644 --- a/README.md +++ b/README.md @@ -83,6 +83,9 @@ julia> render(1:4); - Render documents using the Typst compiler - Display in IJulia.jl, Pluto.jl, and QuartoRunner.jl notebooks - Use the [JuliaMono](https://github.com/cormullion/juliamono) typeface +- Convenience functions to run `typst compile` and `typst watch`: + - `compile("input.typ", "output.pdf")` + - `watch("input.typ", "output.pdf")` ### Planned diff --git a/src/Commands.jl b/src/Commands.jl index 96749f5..df6a5cb 100644 --- a/src/Commands.jl +++ b/src/Commands.jl @@ -532,4 +532,36 @@ TypstError: failed to `run` a `TypstCommand(String[])` showerror(io::IO, te::TypstError) = print(io, "TypstError: failed to `run` a `", TypstCommand, "(", te.command.parameters, ")`") +""" + compile(args...) + +Run `typst compile` with the provided `args`. + +# Examples + + compile("input.typ") + compile("input.typ", "output.pdf") +""" +compile(args...) = (run(TypstCommand(["compile", args...])); nothing) + +""" + watch(args...) + +Run `typst watch` with the provided `args`. This will repeat +compilation of the input file every time it is changed. Use Ctrl-C to +quit the command. + +# Examples + + watch("input.typ") + watch("input.typ", "output.pdf") +""" +function watch(args...) + try + run(TypstCommand(["watch", args...])) + catch e + e isa InterruptException || rethrow(e) + end +end + end # Commands diff --git a/src/Typstry.jl b/src/Typstry.jl index 7ff6180..3abc954 100644 --- a/src/Typstry.jl +++ b/src/Typstry.jl @@ -17,8 +17,8 @@ using .Strings: ContextError, Mode, Typst, TypstString, TypstText, @typst_str, c export ContextError, Mode, Typst, TypstString, TypstText, @typst_str, code, markup, math, context, show_typst include("Commands.jl") -using .Commands: TypstCommand, TypstError, @typst_cmd, julia_mono, preamble, render, set_preamble -export TypstCommand, TypstError, @typst_cmd, julia_mono, preamble, render, set_preamble +using .Commands: TypstCommand, TypstError, @typst_cmd, julia_mono, preamble, render, set_preamble, compile, watch +export TypstCommand, TypstError, @typst_cmd, julia_mono, preamble, render, set_preamble, compile, watch """ compile_workload(examples) diff --git a/test/interface/TestCommands.jl b/test/interface/TestCommands.jl index e96b2ef..4daf986 100644 --- a/test/interface/TestCommands.jl +++ b/test/interface/TestCommands.jl @@ -18,6 +18,21 @@ const tc_ignorestatus = ignorestatus(tc_error) @testset "`julia_mono`" begin @test julia_mono isa String end @testset "`render`" begin end + + @testset "`compile`" begin + mktempdir() do tmpdir + infile = joinpath(tmpdir, "test.typ") + outfile1 = joinpath(tmpdir, "test.pdf") + outfile2 = joinpath(tmpdir, "out.pdf") + write(infile, "= Test Document\n") + compile(infile) + @test isfile(outfile1) + compile(infile, outfile2) + @test isfile(outfile2) + end + end + + @testset "`watch`" begin end end @testset "`Base`" begin