Skip to content

Commit 67892a9

Browse files
committed
Greatly simplify logic for creating example images
1 parent bf01f07 commit 67892a9

9 files changed

+121
-38
lines changed

README.md

+40-14
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,11 @@ The easiest method is to import `wrap-it: wrap-content` from the
2929
## Vanilla
3030

3131
``` typst
32-
#set par(justify: true)
3332
#let fig = figure(
34-
rect(fill: teal, radius: 0.5em, width: 8em),
35-
caption: [A figure],
33+
rect(fill: teal, radius: 0.5em, width: 8em),
34+
caption: [A figure],
3635
)
37-
#let body = lorem(40)
36+
#let body = lorem(30)
3837
#wrap-content(fig, body)
3938
```
4039
![Example 1](https://www.github.com/ntjess/wrap-it/raw/v0.1.1/assets/example-1.png)
@@ -43,10 +42,10 @@ The easiest method is to import `wrap-it: wrap-content` from the
4342

4443
``` typst
4544
#wrap-content(
46-
fig,
47-
body,
48-
align: bottom + right,
49-
column-gutter: 2em
45+
fig,
46+
body,
47+
align: bottom + right,
48+
column-gutter: 2em
5049
)
5150
```
5251
![Example 2](https://www.github.com/ntjess/wrap-it/raw/v0.1.1/assets/example-2.png)
@@ -57,20 +56,47 @@ The easiest way to get a uniform, highly-customizable margin is through
5756
boxing your image:
5857

5958
``` typst
60-
#let boxed = box(fig, inset: 0.5em)
59+
#let boxed = box(fig, inset: 0.25em)
6160
#wrap-content(boxed)[
62-
#lorem(40)
61+
#lorem(30)
6362
]
6463
```
6564
![Example 3](https://www.github.com/ntjess/wrap-it/raw/v0.1.1/assets/example-3.png)
6665

6766
## Wrapping two images in the same paragraph
6867

68+
Note that for longer captions (as is the case in the bottom figure
69+
below), providing an explicit `columns` parameter is necessary to inform
70+
caption text of where to wrap.
71+
6972
``` typst
7073
#let fig2 = figure(
71-
rect(fill: lime, radius: 0.5em),
72-
caption: [Another figure],
74+
rect(fill: lime, radius: 0.5em),
75+
caption: [#lorem(10)],
76+
)
77+
#wrap-top-bottom(
78+
bottom-kwargs: (columns: (1fr, 2fr)),
79+
box(fig, inset: 0.25em),
80+
fig2,
81+
lorem(50),
7382
)
74-
#wrap-top-bottom(boxed, fig2, lorem(62))
7583
```
76-
![Example 4](https://www.github.com/ntjess/wrap-it/raw/v0.1.1/assets/example-4.png)
84+
![Example 4](https://www.github.com/ntjess/wrap-it/raw/v0.1.1/assets/example-4.png)
85+
86+
## Adding a label to a wrapped figure
87+
88+
Typst can only append labels to figures in content mode. So, when
89+
wrapping text around a figure that needs a label, you must first place
90+
your figure in a content block with its label, then wrap it:
91+
92+
``` typst
93+
#show ref: it => underline(text(blue, it))
94+
#let fig = [
95+
#figure(
96+
rect(fill: red, radius: 0.5em, width: 8em),
97+
caption:[Labeled]
98+
)<fig:lbl>
99+
]
100+
#wrap-content(fig, [Fortunately, @fig:lbl's label can be referenced within the wrapped text. #lorem(15)])
101+
```
102+
![Example 5](https://www.github.com/ntjess/wrap-it/raw/v0.1.1/assets/example-5.png)

assets/example-1.png

-21.2 KB
Loading

assets/example-2.png

-21.9 KB
Loading

assets/example-3.png

-23.2 KB
Loading

assets/example-4.png

-12.7 KB
Loading

assets/example-5.png

60.6 KB
Loading

docs/make-example-images.typ

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#import "readme.typ": eval-kwargs, showman-config
2+
#import "@preview/showman:0.1.2": runner
3+
#show raw.where(lang: "example"): it => it
4+
5+
6+
#let get-example-blocks(body) = {
7+
let example-blocks = ()
8+
let example-blocks-regex = regex("```example\n([\s\S]+?)\n```")
9+
for match in body.matches(example-blocks-regex) {
10+
example-blocks.push(match.captures.at(0))
11+
}
12+
example-blocks
13+
}
14+
15+
#let all-blocks = get-example-blocks(read("readme.typ"))
16+
#set page(margin: 1em, ..showman-config.page-size)
17+
// Dummy first page to match showman expectations
18+
#pagebreak()
19+
#for (ii, example) in all-blocks.enumerate() {
20+
eval(
21+
mode: "markup",
22+
"#let output(body) = body;\n" + eval-kwargs.eval-prefix + example,
23+
)
24+
if ii != all-blocks.len() - 1 {
25+
pagebreak()
26+
}
27+
}
28+

docs/readme.typ

+23-11
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,14 @@
44
#let eval-kwargs = (
55
eval-prefix: "
66
#import \"@preview/wrap-it:0.1.1\"
7+
8+
#set par(justify: true)
9+
#let fig = figure(
10+
rect(fill: teal, radius: 0.5em, width: 8em),
11+
caption: [A figure],
12+
)
13+
#let body = lorem(30)
14+
715
#let wrap-content(..args) = output(wrap-it.wrap-content(..args))
816
#let wrap-top-bottom(..args) = output(wrap-it.wrap-top-bottom(..args))
917
",
@@ -23,7 +31,6 @@
2331
underline(it)
2432
}
2533
#set page(height: auto)
26-
2734
= Wrap-It: Wrapping text around figures & content
2835
Until https://github.com/typst/typst/issues/553 is resolved, `typst` doesn't natively support wrapping text around figures or other content. However, you can use `wrap-it` to mimic much of this functionality:
2936
- Wrapping images left or right of their text
@@ -41,8 +48,7 @@ The easiest method is to import `wrap-it: wrap-content` from the `@preview` pack
4148
= Sample use:
4249
== Vanilla
4350

44-
```globalexample
45-
#set par(justify: true)
51+
```example
4652
#let fig = figure(
4753
rect(fill: teal, radius: 0.5em, width: 8em),
4854
caption: [A figure],
@@ -52,7 +58,7 @@ caption: [A figure],
5258
```
5359

5460
== Changing alignment and margin
55-
```globalexample
61+
```example
5662
#wrap-content(
5763
fig,
5864
body,
@@ -63,31 +69,37 @@ column-gutter: 2em
6369

6470
== Uniform margin around the image
6571
The easiest way to get a uniform, highly-customizable margin is through boxing your image:
66-
```globalexample
67-
#let boxed = box(fig, inset: 0.5em)
72+
```example
73+
#let boxed = box(fig, inset: 0.25em)
6874
#wrap-content(boxed)[
6975
#lorem(30)
7076
]
7177
```
7278
== Wrapping two images in the same paragraph
7379
Note that for longer captions (as is the case in the bottom figure below), providing an explicit `columns` parameter is necessary to inform caption text of where to wrap.
74-
```globalexample
80+
```example
7581
#let fig2 = figure(
7682
rect(fill: lime, radius: 0.5em),
7783
caption: [#lorem(10)],
7884
)
79-
#wrap-top-bottom(bottom-kwargs: (columns: (1fr, 2fr)), boxed, fig2, lorem(50))
85+
#wrap-top-bottom(
86+
bottom-kwargs: (columns: (1fr, 2fr)),
87+
box(fig, inset: 0.25em),
88+
fig2,
89+
lorem(50),
90+
)
8091
```
8192

8293
== Adding a label to a wrapped figure
8394
Typst can only append labels to figures in content mode. So, when wrapping text around a figure that needs a label, you must first place your figure in a content block with its label, then wrap it:
95+
8496
```example
8597
#show ref: it => underline(text(blue, it))
8698
#let fig = [
8799
#figure(
88100
rect(fill: red, radius: 0.5em, width: 8em),
89101
caption:[Labeled]
90-
)<figure-1>
102+
)<fig:lbl>
91103
]
92-
#wrap-content(fig, [Fortunately, @figure-1's label can be referenced within the wrapped text. #lorem(15)])
93-
```
104+
#wrap-content(fig, [Fortunately, @fig:lbl's label can be referenced within the wrapped text. #lorem(15)])
105+
```

update_readme.py

+30-13
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,38 @@
11
#! /usr/bin/env python3
2-
from pathlib import Path
3-
import subprocess
42
import re
3+
from pathlib import Path
4+
5+
from showman.converter import Converter
56

67
toml_text = Path("typst.toml").read_text()
78
groups = re.match(r".*version = \"(.*?)\"", toml_text, re.DOTALL)
89
assert groups
910
version = groups.group(1)
1011

11-
cmd = f"""
12-
showman md \
13-
docs/readme.typ \
14-
--output README.md \
15-
--root_dir . \
16-
--assets_dir assets \
17-
--git_url https://www.github.com/ntjess/wrap-it/v{version}/ \
18-
# --log_level DEBUG
19-
"""
20-
print(" ".join(cmd.split()))
21-
subprocess.run(cmd, check=True, shell=True)
12+
13+
class WrapItConverter(Converter):
14+
def _setup_build_folder(self, persist=False):
15+
return (
16+
self.typst_file.parent,
17+
self.typst_file.parent / "make-example-images.typ",
18+
)
19+
20+
def _get_runnable_langs(self):
21+
return ["example"]
22+
23+
def __del__(self):
24+
# No need to clean up build directory
25+
pass
26+
27+
28+
WrapItConverter(
29+
"docs/readme.typ",
30+
assets_dir="assets",
31+
root_dir=".",
32+
showable_labels=["example"],
33+
# log_level="DEBUG",
34+
).save(
35+
out_path="README.md",
36+
remote_url=f"https://www.github.com/ntjess/wrap-it/v{version}/",
37+
force=True,
38+
)

0 commit comments

Comments
 (0)