Skip to content

Commit

Permalink
Try again to fix show_typst for AbstractString and Complex
Browse files Browse the repository at this point in the history
  • Loading branch information
jakobjpeters committed May 30, 2024
1 parent 137f0a3 commit 83fb032
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 44 deletions.
14 changes: 8 additions & 6 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,24 @@

### Strings

- Remove type piracy by requiring values to be either a `TypstString` or wrapped in `Typst`
- Format may be specified by implementing a custom `context`
- Pass formatting configuration to a `TypstString` with keyword parameters instead of `Pair{Symbol}`s
- Remove type piracy of `show` with the `text/typst` MIME type
- Values may instead be wrapped in `Typst`
- Format may be configured by implementing a custom `context`
- Support `show(::IO, ::MIME"application/pdf", ::TypstString)`
- Formatting options in `TypstString` are now passed as keyword parameters instead of `Pair{Symbol}`s
- Replace `typst_text` constructor with `TypstText` wrapper
- `show_typst`
- Implement `show_typst(x)` which prints to `stdout`
- Implemented for `AbstractArray`, `Tuple`, `Typst`, `TypstText`, and `Unsigned`
- Implemented for `AbstractArray`, `Complex{Bool}`, `Tuple`, `Typst`, `TypstText`, and `Unsigned`
- `nothing` now corresponds to Typst's `none`
- `AbtractMatrix` and `AbstractVector` in `code` mode now correspond to a Typst array
- `OrdinalRange{<:Integer, <:Integer}` and `StepRangeLen{<:Integer, <:Integer, <:Integer}`
- `code` mode implicitely uses the Typst default `step` if it is equal to `1`
- `code` mode implicitily uses the Typst default `step` if it is equal to `1`
- `markup` and `math` mode now correspond to a Typst vector
- The `Text` format is simpler in `markup` mode
- New default setting `parenthesize = true`
- Used for `Complex` and `Rational`
- The `inline` setting has been renamed to `block` to be consistent with Typst's `equation` function
- Rename the `inline` setting to `block` for consistency with Typst's `equation` function
- This toggles the default behavior, which is now inline

### Commands
Expand Down
59 changes: 43 additions & 16 deletions docs/src/getting_started.md
Original file line number Diff line number Diff line change
@@ -1,40 +1,67 @@

```@meta
DocTestSetup = :(using Typstry)
```

# Getting Started

## Workflow
## Basics

### Setup
### Strings

Print Julia values in [`Typst`](@ref) format using `show` with the `text/typst` MIME type.

```jldoctest 1
julia> import Base: show
julia> show(stdout, "text/typst", Typst(π))
π
```

julia> import Typstry: show_typst
Some methods use an `IOContext` to configure the formatting.

julia> using Typstry
```jldoctest 1
julia> show(IOContext(stdout, :mode => code), "text/typst", Typst(π))
3.141592653589793
```

### Strings
Instead of printing, construct a [`TypstString`](@ref).

At the base of Typstry.jl is the [`show_typst`](@ref) function,
which prints Julia values in Typst format.
```jldoctest 1
julia> TypstString(π)
typst"π"
julia> TypstString(π; mode = code)
typst"3.141592653589793"
```

This formatting may be customized with an `IOContext`
Alternatively, use [`@typst_str`](@ref) to construct
a `TypstString` with formatted interpolation.

```jldoctest 1
julia> typst"$ \(pi) approx \(pi; mode = code) $"
typst"$ π approx 3.141592653589793 $"
```

### Commands

Use [`render`](@ref) to quickly generate a Typst source file and compile it into a document.

```jldoctest 1
julia> render(Any[true 1; 1.2 1 // 2]);
```

## Examples
A source file is compiled using a [`TypstCommand`](@ref) created using its constructor or [`@typst_cmd`](@ref).

This Typst source file and corresponding document were generated from Julia using \
[`show(::IO, ::MIME"text/typst", ::Union{Typst, TypstString})`](@ref) to print Julia values
to Typst format and a [`TypstCommand`](@ref) to render it.
```jldoctest 1
julia> TypstCommand(["help"])
typst`help`
julia> run(typst`compile input.typ output.pdf`);
```

## Examples

A [`Mode`](@ref) specifies the current Typst context.
The formatting of each type corresponds to the most useful Typst value for a given mode.
If no such value exists, it is formatted to render in a canonical representation.
This Typst source file was generated from Julia using `show` with the
`text/typst` MIME type and compiled using a `TypstCommand`.

!!! note
Although many of the values are rendered similarly across modes,
Expand Down
10 changes: 5 additions & 5 deletions docs/src/tutorials/interface.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,10 @@ julia> println(TypstString(π; mode = markup))

```jldoctest 1
julia> println(TypstString("[\"a\"]"))
"[\"a\"]"
"[\\\"a\\\"]"
julia> println(TypstString(text"[\"a\"]"))
#"[\"a\"]"
"[\\\"a\\\"]"
julia> println(TypstString(typst"[\"a\"]"))
["a"]
Expand Down Expand Up @@ -143,9 +143,9 @@ $vec(

### Consider edge cases

- `#1 / 2` is valid Typst source text, but is parsed partially in `code` `Mode` as `(#1) / 2`
- `1 / 2` may be ambiguous in a `math` `Mode` expression
- `$1 / 2$` is not ambiguous in `markup` `Mode`
- `#1 / 2` is valid Typst source text, but is parsed partially in `code` mode as `(#1) / 2`
- `1 / 2` may be ambiguous in a `math` mode expression
- `$1 / 2$` is not ambiguous in `markup` mode

```jldoctest 1
julia> println(TypstString(1 // 2; mode = code))
Expand Down
10 changes: 10 additions & 0 deletions src/commands.jl
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,11 @@ The Typst source text may begin with the following `preamble`:
```typst
$preamble
```
# Examples
```jldoctest
julia> render(Any[true 1; 1.2 1 // 2]);
```
"""
function render(x;
input = "input.typ",
Expand Down Expand Up @@ -314,6 +319,11 @@ See also [`TypstCommand`](@ref).
If [`ignorestatus`](@ref) has been applied,
this will not throw an error in Julia.
Otherwise, the Typst error will be printed before the Julia error.
# Examples
```jldoctest
julia> run(typst`compile input.typ output.pdf`);
```
"""
function run(tc::TypstCommand, args...; kwargs...)
process = run(ignorestatus(Cmd(`$(tc.compiler) $(tc.parameters)`)), args...; kwargs...)
Expand Down
45 changes: 28 additions & 17 deletions src/strings.jl
Original file line number Diff line number Diff line change
Expand Up @@ -553,6 +553,7 @@ and the [Typst Documentation](https://typst.app/docs/), respectively.
| `AbstractMatrix` | `:block`, `:depth`, `:indent`, `:mode`, ... | `:augment`, `:column_gap`, `:delim`, `:gap`, `:row_gap` |
| `AbstractString` | `:mode` | |
| `Bool` | `:mode` | |
| `Complex{Bool}` | `:block`, `:mode`, `:parenthesize` | |
| `Complex` | `:block`, `:mode`, `:parenthesize`, ... | |
| `Irrational` | `:mode`, ... | |
| `Nothing` | `:mode` | |
Expand All @@ -561,6 +562,7 @@ and the [Typst Documentation](https://typst.app/docs/), respectively.
| `Regex` | `:mode` | |
| `Signed` | | |
| `StepRangeLen{<:Integer,\u00A0<:Integer,\u00A0<:Integer}` | `:mode`, ... | |
| `String` | `:mode` | |
| `Text` | `:mode`, ... | |
| `Tuple` | `:block`, `:depth`, `:indent`, `:mode`, ... | `:delim`, `:gap` |
| `Typst` | ... | |
Expand Down Expand Up @@ -592,26 +594,32 @@ show_typst(io, x::AbstractMatrix) = mode(io) == code ?
end, IOContext(io, :depth => _depth, :mode => math), eachrow(x), ";\n"; indent)
print(io, "\n", indent ^ depth, ")")
end, IOContext(io, :parenthesize => false), x, math_pad(io); indent = indent(io), depth = depth(io))
show_typst(io, x::AbstractString) = enclose((io, x) -> escape_string(io, x, "\"\$"), io,
(mode(io) == markup ? (String(x), "\"", "\"") : (escape_string(x), "\"\\\"", "\\\"\""))...
) # TODO: test that escaping is correct and remove string allocation
show_typst(io, x::AbstractString) = show_typst(io, Text(x))
function show_typst(io, x::Bool)
_mode = mode(io)

if _mode == code print(io, x)
elseif _mode == markup print(io, "#", x)
else enclose(print, io, x, "\"")
end
_mode == markup && print(io, "#")
_mode == math ? enclose(print, io, x, "\"") : print(io, x)
end
show_typst(io, x::Complex{Bool}) = show_typst(io, Complex(Int(real(x)), Int(imag(x))))
show_typst(io, x::Complex) = enclose(io, x, math_pad(io)) do io, x
enclose(IOContext(io, :mode => math, :parenthesize => false), x,
(mode(io) == math && parenthesize(io) ? ("(", ")") : ("", ""))...) do io, x
imaginary = imag(x)
_real, _imaginary = real(x), abs(imaginary)
__real, __imaginary = _real == 0, _imaginary == 0
___imaginary = signbit(imaginary)

__real && !__imaginary || _show_typst(io, _real)

if _imaginary != 0
if !__real enclose(print, io, ___imaginary ? "-" : "+", " ")
elseif ___imaginary print(io, "-")
end

_show_typst(io, real(x))
enclose(print, io, signbit(imaginary) ? "-" : "+", " ")
_show_typst(io, abs(imaginary))
print(io, "i")
_imaginary == 1 || _show_typst(io, abs(imaginary))
print(io, "i")
end
end
end
show_typst(io, x::Irrational) =
Expand Down Expand Up @@ -645,9 +653,9 @@ function show_typst(io, x::Regex)
end
end
end
function show_typst(io, x::Text)
code_mode(io)
print_quoted(io, repr(x)) # TODO: remove string allocation
function show_typst(io, x::Text) # TODO: remove allocation
s = string(x)
mode(io) == markup ? show(io, escape_string(s, "\"\$")) : print_quoted(io, repr(s))
end
show_typst(io, x::Typst) = show_typst(io, x.value)
show_typst(io, x::TypstText) = print(io, x.value)
Expand Down Expand Up @@ -679,9 +687,9 @@ show_typst(io, x::Union{
#=
AbstractDict
AbstractIrrational
AbstractSet
Enum
Expr
Set
Symbol
=#

Expand Down Expand Up @@ -808,13 +816,15 @@ function show(io::IO, ts::TypstString)
end

"""
show(::IO, ::MIME"text/typst", ::Typst)
show(::IO, ::MIME"text/typst", ::Union{Typst, TypstString, TypstText})
Print the Typst format.
Print the [`Typst`](@ref) format.
This method provides formatting data to [`show_typst`](@ref)
specified by a default and custom [`context`](@ref).
See also [`TypstString`](@ref) and [`TypstText`](@ref).
# Examples
```jldoctest
julia> show(stdout, "text/typst", typst"a")
Expand Down Expand Up @@ -872,6 +882,7 @@ const examples = [
Any[true 1; 1.2 1 // 2] => AbstractMatrix,
"a" => AbstractString,
true => Bool,
im => Complex{Bool},
1 + 2im => Complex,
π => Irrational,
nothing => Nothing,
Expand Down

0 comments on commit 83fb032

Please sign in to comment.