Skip to content

Commit

Permalink
Add typst function to mimic command line interface (#8)
Browse files Browse the repository at this point in the history
* Add convenience functions to compile and watch.

* Add typst function to mimic command line interface.

* Update after review.
  • Loading branch information
GunnarFarneback authored Oct 31, 2024
1 parent 5f8dbb4 commit a59bf8c
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 5 deletions.
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
- Throw a `ContextError` for context values of an incorrect type
- The `preamble` used in `render` and some `show` methods can now be specified using `set_preamble`
- `render` now supports the `ignorestatus = true` keyword parameter
- Emulation of Typst command line interface. `typst("compile input.typ output.pdf")`

### Bug Fixes

Expand Down
41 changes: 38 additions & 3 deletions src/Commands.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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.
Expand All @@ -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
Expand Down Expand Up @@ -532,4 +532,39 @@ TypstError: failed to `run` a `TypstCommand(String[])`
showerror(io::IO, te::TypstError) = print(io,
"TypstError: failed to `run` a `", TypstCommand, "(", te.command.parameters, ")`")

"""
typst(args::AbstractString; catch_interrupt = true, ignorestatus = true)
Convenience function 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 it will not work if there are, e.g., filenames with
spaces.
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
typst("c document.typ")
typst("watch input.typ output.pdf")
"""
function typst(args::AbstractString;
catch_interrupt = true, ignorestatus = true)
tc = addenv(TypstCommand(TypstCommand(split(args)); 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() = typst(split(args))

end # Commands
4 changes: 2 additions & 2 deletions src/Typstry.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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, typst
export TypstCommand, TypstError, @typst_cmd, julia_mono, preamble, render, set_preamble, typst

"""
compile_workload(examples)
Expand Down
20 changes: 20 additions & 0 deletions test/interface/TestCommands.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,26 @@ const tc_ignorestatus = ignorestatus(tc_error)
@testset "`julia_mono`" begin @test julia_mono isa String end

@testset "`render`" begin end

@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")
cd(tmpdir) do
typst("compile test.typ")
typst("c test.typ out.pdf")
end
@test isfile(outfile1)
@test isfile(outfile2)
# Only check that it runs without error.
redirect_stdout(devnull) do
typst("--help")
typst("fonts --variants")
end
end
end
end

@testset "`Base`" begin
Expand Down

0 comments on commit a59bf8c

Please sign in to comment.