Skip to content

Commit

Permalink
Preserve non-printing characters in show(::IO, ::TypstString)
Browse files Browse the repository at this point in the history
  • Loading branch information
jakobjpeters committed Oct 16, 2024
1 parent daec9ad commit 3d76857
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 6 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,7 @@

### Bug Fixes

- If a `TypstString` contains any characters that satisfy `!isprint`,
`show(::IO, ::TypstString)` now prints a format that preserves those characters.
- Delete redundant `show_typst` documentation for `String`
- Account for a [Typst bug with single-letter strings in `math` mode](https://github.com/typst/typst/issues/274#issue-1639854848)
1 change: 1 addition & 0 deletions docs/source/references/internals.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ Typstry.Strings.show_array
Typstry.Strings.show_parameters
Typstry.Strings.show_raw
Typstry.Strings.show_vector
Typstry.Strings.escape
```

### Dates.jl
Expand Down
50 changes: 45 additions & 5 deletions src/Strings.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ Typstry.Strings
module Strings

import Base: IOBuffer, ==, codeunit, isvalid, iterate, ncodeunits, pointer, repr, show
using Base: escape_raw_string
using .Docs: HTML, Text
using .Iterators: Stateful
using .Meta: isexpr, parse
Expand Down Expand Up @@ -183,7 +182,7 @@ macro typst_str(s::String)
end
end

current ≀ final && push!(args, s[current:final])
current > final || push!(args, s[current:final])
:(TypstString(TypstText($_s)))
end

Expand Down Expand Up @@ -475,6 +474,23 @@ show_vector(io, x) = math_mode(io, x) do io, x
print(io, "\n", _indent ^ _depth, ")")
end

"""
escape(io, n)
Print `\\` to `io` `n` times.
# Examples
```jldoctest
julia> julia> Typstry.Strings.escape(stdout, 2)
\\
```
"""
escape(io, n) =
for _ in 1:n
print(io, '\\')
end

## Dates.jl

"""
Expand Down Expand Up @@ -919,9 +935,33 @@ typst"a"
```
"""
function show(io::IO, ts::TypstString)
print(io, "typst")
enclose((io, text) -> escape_raw_string(io, replace(text,
r"(\\+)\(" => s"\1\1(")), io, ts.text, "\"")
text = ts.text

if all(isprint, text)
escapes = 0

print(io, "typst\"")

for c in text
if c == '\\' escapes += 1
else
if c == '\"' escape(io, escapes + 1)
elseif c == '(' escape(io, escapes)
end

escapes = 0
end

print(io, c)
end

escape(io, escapes)
print(io, "\"")
else
print(io, TypstString, "(", TypstText, "(")
show(io, text)
print(io, "))")
end
end

"""
Expand Down
4 changes: 3 additions & 1 deletion test/interface/TestStrings.jl
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ show_typst(io, ::X) = print(io, io[:x]::Int)

const pairs = [
typst"" => ""
typst"x" => "x"
typst"\\" => "\\" # \
typst"\ " => "\\ "
typst"\\ " => "\\\\ "
typst"(x)" => "(x)"
typst"𝒂(x)𝒃" => "𝒂(x)𝒃"
typst"𝒂𝒃(x)𝒄𝒅" => "𝒂𝒃(x)𝒄𝒅"
Expand Down

0 comments on commit 3d76857

Please sign in to comment.