Skip to content

Commit

Permalink
Create utilities.jl
Browse files Browse the repository at this point in the history
  • Loading branch information
jakobjpeters committed Nov 7, 2024
1 parent a44b69e commit 21f4739
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 63 deletions.
4 changes: 2 additions & 2 deletions src/Commands.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ module Commands
import Base:
==, addenv, detach, eltype, firstindex, getindex, hash, ignorestatus,
iterate, keys, lastindex, length, run, setcpuaffinity, setenv, show, showerror
using ..Typstry: Strings, Typst, TypstString, TypstText, @typst_str, unwrap
using .Strings: enclose, join_with, _show_typst
using ..Typstry: Strings, Typst, TypstString, TypstText, @typst_str, enclose, join_with, unwrap
using .Strings: _show_typst
using Artifacts: @artifact_str
import Typst_jll

Expand Down
42 changes: 1 addition & 41 deletions src/Strings.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,8 @@ Typstry.Strings
module Strings

import Base: IOBuffer, ==, codeunit, isvalid, iterate, ncodeunits, pointer, repr, show, showerror
using ..Typstry: unwrap
using ..Typstry: enclose, join_with, unwrap
using .Docs: HTML, Text
using .Iterators: Stateful
using .Meta: isexpr, parse
using Dates:
Date, DateTime, Day, Hour, Minute, Period, Second, Time, Week,
Expand Down Expand Up @@ -275,24 +274,6 @@ julia> Typstry.Strings.depth(IOContext(stdout, :depth => 0))
"""
depth(io) = unwrap(io, Int, :depth)

"""
enclose(f, io, x, left, right = reverse(left); kwargs...)
Call `f(io,\u00A0x;\u00A0kwargs...)` between printing `left` and `right`, respectfully.
# Examples
```jldoctest
julia> Typstry.Strings.enclose((io, i; x) -> print(io, i, x), stdout, 1, "\\\$ "; x = "x")
\$ 1x \$
```
"""
function enclose(f, io, x, left, right = reverse(left); context...)
print(io, left)
f(io, x; context...)
print(io, right)
end

"""
escape(io, n)
Expand Down Expand Up @@ -322,27 +303,6 @@ julia> Typstry.Strings.indent(IOContext(stdout, :tab_size => 2))
"""
indent(io) = " " ^ unwrap(io, Int, :tab_size)

"""
join_with(f, io, xs, delimeter; kwargs...)
Similar to `join`, except printing with `f(io, x; kwargs...)`.
# Examples
```jldoctest
julia> Typstry.Strings.join_with((io, i; x) -> print(io, -i, x), stdout, 1:4, ", "; x = "x")
-1x, -2x, -3x, -4x
```
"""
function join_with(f, io, xs, delimeter; kwargs...)
_xs = Stateful(xs)

for x in _xs
f(io, x; kwargs...)
isempty(_xs) || print(io, delimeter)
end
end

"""
math_mode(f, io, x; kwargs...)
"""
Expand Down
21 changes: 1 addition & 20 deletions src/Typstry.jl
Original file line number Diff line number Diff line change
@@ -1,16 +1,7 @@

module Typstry

using PrecompileTools: @compile_workload

_unwrap(type, key, value::T) where T = value isa type ? value : throw(ContextError(type, T, key))

"""
unwrap(io, type, key)
unwrap(io, type, key, default)
"""
unwrap(io, type, key) = _unwrap(type, key, io[key])
unwrap(io, type, key, default) = _unwrap(type, key, get(io, key, default))
include("utilities.jl")

include("Strings.jl")
using .Strings: ContextError, Mode, Typst, TypstString, TypstText, @typst_str, code, markup, math, context, show_typst
Expand All @@ -20,16 +11,6 @@ include("Commands.jl")
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)
Given an iterable of value-type pairs, interpolate each value into
a `@typst_str` within a `PrecompileTools.@compile_workload` block.
"""
compile_workload(examples) = @compile_workload for (x, _) in examples
typst"\(x)"
end

compile_workload(Strings.examples)

end # Typstry
61 changes: 61 additions & 0 deletions src/utilities.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@

using .Iterators: Stateful
using PrecompileTools: @compile_workload

"""
compile_workload(examples)
Given an iterable of value-type pairs, interpolate each value into
a `@typst_str` within a `PrecompileTools.@compile_workload` block.
"""
compile_workload(examples) = @compile_workload for (example, _) in examples
TypstString(example)
end

"""
enclose(f, io, x, left, right = reverse(left); kwargs...)
Call `f(io,\u00A0x;\u00A0kwargs...)` between printing `left` and `right`, respectfully.
# Examples
```jldoctest
julia> Typstry.enclose((io, i; x) -> print(io, i, x), stdout, 1, "\\\$ "; x = "x")
\$ 1x \$
```
"""
function enclose(f, io, x, left, right = reverse(left); context...)
print(io, left)
f(io, x; context...)
print(io, right)
end

"""
join_with(f, io, xs, delimeter; kwargs...)
Similar to `join`, except printing with `f(io, x; kwargs...)`.
# Examples
```jldoctest
julia> Typstry.Strings.join_with((io, i; x) -> print(io, -i, x), stdout, 1:4, ", "; x = "x")
-1x, -2x, -3x, -4x
```
"""
function join_with(f, io, xs, delimeter; kwargs...)
_xs = Stateful(xs)

for x in _xs
f(io, x; kwargs...)
isempty(_xs) || print(io, delimeter)
end
end

_unwrap(type, key, value::T) where T = value isa type ? value : throw(ContextError(type, T, key))

"""
unwrap(io, type, key)
unwrap(io, type, key, default)
"""
unwrap(io, type, key) = _unwrap(type, key, io[key])
unwrap(io, type, key, default) = _unwrap(type, key, get(io, key, default))

0 comments on commit 21f4739

Please sign in to comment.