Skip to content

Commit

Permalink
Add typst function to mimic command line interface.
Browse files Browse the repository at this point in the history
  • Loading branch information
GunnarFarneback committed Oct 31, 2024
1 parent 11d04c5 commit 5c61c9b
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 32 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
57 changes: 35 additions & 22 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 @@ -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
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, 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)
Expand Down
13 changes: 8 additions & 5 deletions test/interface/TestCommands.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 5c61c9b

Please sign in to comment.