diff --git a/README.md b/README.md index af89351..409bb81 100644 --- a/README.md +++ b/README.md @@ -83,9 +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")` +- Emulation of Typst command line interface. + - `typst("w input.typ output.pdf")` + - `typst(["compile", infile, outfile]) ### Planned diff --git a/src/Commands.jl b/src/Commands.jl index df6a5cb..78cd04d 100644 --- a/src/Commands.jl +++ b/src/Commands.jl @@ -21,7 +21,7 @@ using ..Typstry: Strings, Typst, TypstString, TypstText, @typst_str, unwrap using .Strings: enclose, join_with, _show_typst using Artifacts: @artifact_str using Preferences: @load_preference, @set_preferences! -using Typst_jll: typst +import Typst_jll # Internals @@ -67,7 +67,7 @@ format(::MIME"image/svg+xml") = "svg" # `Typstry` """ - TypstCommand(::Vector{String}) + TypstCommand(::AbstractVector{<:AbstractString}) TypstCommand(::TypstCommand; kwargs...) The Typst compiler and its parameters. @@ -93,7 +93,7 @@ mutable struct TypstCommand const ignore_status::Bool compiler::Cmd - TypstCommand(parameters) = new(parameters, false, typst()) + TypstCommand(parameters) = new(parameters, false, Typst_jll.typst()) TypstCommand(tc::TypstCommand; ignorestatus = tc.ignore_status, kwargs...) = new(tc.parameters, ignorestatus, Cmd(tc.compiler; kwargs...)) end @@ -533,35 +533,48 @@ showerror(io::IO, te::TypstError) = print(io, "TypstError: failed to `run` a `", TypstCommand, "(", te.command.parameters, ")`") """ - compile(args...) + typst(parameters::AbstractVector{<:AbstractString}; catch_interrupt = true, ignorestatus = true) -Run `typst compile` with the provided `args`. +Run `typst` with the provided `parameters`. When `catch_interrupt` is true, +CTRL-C quietly quits the command. When `ignorestatus` is true, a typst +failure will not imply a julia error. + +Use `TypstCommand` if you need to capture output. # Examples - compile("input.typ") - compile("input.typ", "output.pdf") -""" -compile(args...) = (run(TypstCommand(["compile", args...])); nothing) + typst(["compile", "my document.typ"]) + typst(["watch", "input.typ", "output.pdf"]) + typst(["--version"]) -""" - watch(args...) +--- + + typst(args::AbstractString) -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. +Convenience method intended for interactive use, emulating the typst +command line interface. Be aware, however, that it strictly splits +`args` on spaces and does not provide any shell-style escape +mechanism, so if you have filenames with spaces you should use the +previous method. # Examples - watch("input.typ") - watch("input.typ", "output.pdf") -""" -function watch(args...) - try - run(TypstCommand(["watch", args...])) - catch e - e isa InterruptException || rethrow(e) + typst("c my_document.typ") + typst("watch input.typ output.pdf") +""" +function typst(parameters::AbstractVector{<:AbstractString}; + catch_interrupt = true, ignorestatus = true) + tc = addenv(TypstCommand(TypstCommand([parameters...]); ignorestatus), + "TYPST_FONT_PATHS" => julia_mono) + if catch_interrupt + try run(tc) + catch e e isa InterruptException || rethrow() + end + else run(tc) end + nothing end +typst(args::AbstractString) = typst(split(args)) + end # Commands diff --git a/src/Typstry.jl b/src/Typstry.jl index 3abc954..7767fc1 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, compile, watch -export TypstCommand, TypstError, @typst_cmd, julia_mono, preamble, render, set_preamble, compile, watch +using .Commands: TypstCommand, TypstError, @typst_cmd, julia_mono, preamble, render, set_preamble, typst +export TypstCommand, TypstError, @typst_cmd, julia_mono, preamble, render, set_preamble, typst """ compile_workload(examples) diff --git a/test/interface/TestCommands.jl b/test/interface/TestCommands.jl index 4daf986..e8d0574 100644 --- a/test/interface/TestCommands.jl +++ b/test/interface/TestCommands.jl @@ -19,20 +19,23 @@ const tc_ignorestatus = ignorestatus(tc_error) @testset "`render`" begin end - @testset "`compile`" begin + @testset "`typst`" 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) + typst(["compile", infile]) @test isfile(outfile1) - compile(infile, outfile2) + typst(["c", infile, outfile2]) @test isfile(outfile2) + # Only check that it runs without error. + redirect_stdout(devnull) do + typst("--help") + typst("fonts --variants") + end end end - - @testset "`watch`" begin end end @testset "`Base`" begin