Skip to content

Commit

Permalink
Support an ignorestatus keyword parameter for render
Browse files Browse the repository at this point in the history
  • Loading branch information
jakobjpeters committed Oct 28, 2024
1 parent 3525925 commit 217f90d
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 24 deletions.
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
- Support Typst version 0.12
- 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` keyword parameter

### Bug Fixes

Expand Down
1 change: 0 additions & 1 deletion docs/source/references/internals.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ Typstry.Strings.dates
```@docs
Typstry.Commands
Typstry.Commands.default_preamble
Typstry.Commands.typst_compiler
Typstry.Commands.apply
Typstry.Commands.format
```
44 changes: 21 additions & 23 deletions src/Commands.jl
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,11 @@ using Typst_jll: typst
"""
default_preamble
"""
const default_preamble = typst"""
const default_preamble = """
#set page(margin: 1em, height: auto, width: auto, fill: white)
#set text(16pt, font: "JuliaMono")
"""

"""
typst_compiler
A constant `Cmd` that is the Typst compiler given
by Typst_jll.jl with no additional parameters.
"""
const typst_compiler = typst()

"""
apply(f, tc, args...; kwargs...)
"""
Expand Down Expand Up @@ -80,6 +72,8 @@ format(::MIME"image/svg+xml") = "svg"
The Typst compiler and its parameters.
Keyword parameters have the same semantics as for a `Cmd`.
!!! info
This type implements the `Cmd` interface.
However, the interface is undocumented, which may result in unexpected behavior.
Expand All @@ -96,12 +90,12 @@ typst`help`
"""
mutable struct TypstCommand
const parameters::Vector{String}
const ignore_status::Bool
compiler::Cmd
ignore_status::Bool

TypstCommand(parameters::Vector{String}) = new(parameters, typst_compiler, false)
TypstCommand(parameters) = new(parameters, false, typst())
TypstCommand(tc::TypstCommand; ignorestatus = tc.ignore_status, kwargs...) =
new(tc.parameters, Cmd(tc.compiler; kwargs...), ignorestatus)
new(tc.parameters, ignorestatus, Cmd(tc.compiler; kwargs...))
end

"""
Expand Down Expand Up @@ -171,42 +165,45 @@ TypstString(TypstText("#set page(margin: 1em, height: auto, width: auto, fill: w
const preamble = TypstString(TypstText(@load_preference "preamble" default_preamble))

"""
render(x;
render(value;
input = "input.typ",
output = "output.pdf",
open = true,
ignorestatus = true,
preamble = preamble,
context...)
Render to a document using
[`show(::IO,\u00A0::MIME"text/typst",\u00A0::Typst)`](@ref).
This first generates the `input` file containing
the [`preamble`](@ref) and formatted value.
the [`preamble`](@ref) and formatted `value`.
Then it is compiled to the `output` document,
whose format is inferred by its file extension to be `pdf`, `png`, or `svg`.
The document may be automatically `open`ed by the default viewer.
The [`ignorestatus`](@ref) flag may be set.
# Examples
```jldoctest
julia> render(Any[true 1; 1.2 1 // 2]);
```
"""
function render(x;
function render(value;
input = "input.typ",
output = "output.pdf",
open = true,
ignorestatus = true,
preamble = preamble,
context...)
Base.open(input; truncate = true) do file
_show_typst(file, preamble)
_show_typst(IOContext(file, context...), x)
_show_typst(IOContext(file, context...), value)
println(file)
end

run(addenv(TypstCommand(["compile", input, output, "--open"][begin:end - !open]),
"TYPST_FONT_PATHS" => julia_mono))
run(addenv(TypstCommand(TypstCommand(
["compile", input, output, "--open"][begin:end - !open]);
ignorestatus), "TYPST_FONT_PATHS" => julia_mono))
end

"""
Expand Down Expand Up @@ -322,7 +319,7 @@ getindex(tc::TypstCommand, i) = i == 1 ? only(tc.compiler) : tc.parameters[i - 1
See also [`TypstCommand`](@ref).
"""
hash(tc::TypstCommand, h::UInt) =
foldr(hash, (TypstCommand, tc.compiler, tc.parameters, tc.ignore_status, h))
hash((TypstCommand, tc.compiler, tc.parameters, tc.ignore_status), h)

"""
ignorestatus(::TypstCommand)
Expand Down Expand Up @@ -419,7 +416,7 @@ See also [`TypstCommand`](@ref).
"""
function run(tc::TypstCommand, args...; kwargs...)
process = run(ignorestatus(Cmd(`$(tc.compiler) $(tc.parameters)`)), args...; kwargs...)
success(process) || tc.ignore_status || throw(TypstError(tc))
tc.ignore_status || success(process) || throw(TypstError(tc))
process
end

Expand Down Expand Up @@ -498,7 +495,7 @@ Environments, such as Pluto.jl notebooks, may use these methods to `display` val
# Examples
```jldoctest
julia> show(IOContext(devnull, :preamble => typst""), "image/svg+xml", Typst(1));
julia> show(IOContext(devnull, :preamble => typst""), "image/svg+xml", Typst(1))
```
"""
function show(io::IO, m::Union{
Expand All @@ -507,7 +504,8 @@ function show(io::IO, m::Union{
input = tempname()
output = input * "." * format(m)

render(t; input, output, open = false, preamble = unwrap(io, TypstString, :preamble, preamble))
render(t; input, output, open = false, ignorestatus = false,
preamble = unwrap(io, TypstString, :preamble, preamble))
write(io, read(output))

nothing
Expand Down

0 comments on commit 217f90d

Please sign in to comment.