Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
repos:
- repo: https://github.com/keith/pre-commit-buildifier
rev: 8.2.0
hooks:
- id: buildifier
- id: buildifier-lint
args:
- --warnings=all
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
# rules_typst

Bazel rules for [Typst](https://typst.app/).

## Development

### Pre-commit hooks

This repo uses [pre-commit](https://pre-commit.com/) to run [buildifier](https://github.com/bazelbuild/buildtools/tree/master/buildifier) for formatting and linting Bazel files.

```sh
pip install pre-commit
pre-commit install
```

To manually run against all files:

```sh
pre-commit run --all-files
```
46 changes: 41 additions & 5 deletions examples/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,22 +1,58 @@
load("@bazel_lib//lib:write_source_files.bzl", "write_source_file")
load("@bazel_skylib//rules:build_test.bzl", "build_test")
load("@bazel_skylib//rules:copy_file.bzl", "copy_file")
load("@bazel_skylib//rules:run_binary.bzl", "run_binary")
load("@rules_python//python:defs.bzl", "py_binary")
load("@rules_typst//typst:typst.bzl", "typst")

filegroup(
name = "smile",
srcs = ["smile.svg"],
name = "smile_source",
srcs = ["assets/source/smile.svg"],
)

copy_file(
name = "frown",
src = ":smile",
out = "frown.svg",
src = ":smile_source",
out = "assets/derived/frown.svg",
)

py_binary(
name = "generate_svgs",
srcs = ["tools/generate_svgs.py"],
main = "tools/generate_svgs.py",
)

run_binary(
name = "generated_svg_set",
outs = [
"generated/python/diamond.svg",
"generated/python/stripes.svg",
],
args = [
"$(location generated/python/diamond.svg)",
"$(location generated/python/stripes.svg)",
],
tool = ":generate_svgs",
)

typst(
name = "example",
src = "example.typ",
data = [
":frown",
":smile",
":generated_svg_set",
":smile_source",
],
)

write_source_file(
name = "write_example_pdf",
diff_test = True,
in_file = ":example",
out_file = "generated/source_sync/example.pdf",
)

build_test(
name = "example_build_test",
targets = [":example"],
)
2 changes: 2 additions & 0 deletions examples/MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ module(

bazel_dep(name = "rules_typst", version = "0.0.0")
bazel_dep(name = "bazel_skylib", version = "1.8.2")
bazel_dep(name = "rules_python", version = "1.7.0")
bazel_dep(name = "aspect_bazel_lib", version = "2.22.5", repo_name = "bazel_lib")

local_path_override(
module_name = "rules_typst",
Expand Down
533 changes: 533 additions & 0 deletions examples/MODULE.bazel.lock

Large diffs are not rendered by default.

21 changes: 21 additions & 0 deletions examples/assets/source/smile.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
33 changes: 19 additions & 14 deletions examples/example.typ
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@
$ A = pi r^2 $
$ "area" = pi dot "radius"^2 $
$ cal(A) :=
{ x in RR | x "is natural" } $
#let x = 5
$ #x < 17 $
#set page(width: 14cm, height: auto)

= Multi-directory asset demo

This document loads images from multiple directories via Bazel targets.

#figure(
image("assets/source/smile.svg", width: 50%),
caption: [Static source image from `assets/source/`],
)

#figure(
image("assets/derived/frown.svg", width: 50%),
caption: [Copied image from `assets/derived/`],
)

#figure(
image("./smile.svg", width: 80%),
caption: [
Smile you have an image!
],
image("./generated/python/diamond.svg", width: 50%),
caption: [SVG generated with Python into `generated/python/`],
)

#figure(
image("./frown.svg", width: 80%),
caption: [
Smile you have an image!
],
image("generated/python/stripes.svg", width: 50%),
caption: [Second Python-generated SVG from another output],
)
Binary file added examples/generated/source_sync/example.pdf
Binary file not shown.
49 changes: 49 additions & 0 deletions examples/tools/generate_svgs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#!/usr/bin/env python3
"""Generate simple SVG assets for the Typst example."""

from __future__ import annotations

import os
import sys


def _write(path: str, body: str) -> None:
os.makedirs(os.path.dirname(path), exist_ok=True)
with open(path, "w", encoding="utf-8") as f:
f.write(body)


def _diamond_svg() -> str:
return """<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 200 200">
<rect width="200" height="200" fill="#f5f8ff"/>
<polygon points="100,15 185,100 100,185 15,100" fill="#4b7bec" stroke="#274b8f" stroke-width="6"/>
<circle cx="100" cy="100" r="18" fill="#ffffff"/>
</svg>
"""


def _stripes_svg() -> str:
return """<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 200 200">
<rect width="200" height="200" fill="#fff7ec"/>
<rect y="10" width="200" height="20" fill="#f4a261"/>
<rect y="50" width="200" height="20" fill="#e76f51"/>
<rect y="90" width="200" height="20" fill="#2a9d8f"/>
<rect y="130" width="200" height="20" fill="#264653"/>
<rect y="170" width="200" height="20" fill="#e9c46a"/>
</svg>
"""


def main(argv: list[str]) -> int:
if len(argv) != 2:
print("expected two output paths", file=sys.stderr)
return 1

diamond_out, stripes_out = argv
_write(diamond_out, _diamond_svg())
_write(stripes_out, _stripes_svg())
return 0


if __name__ == "__main__":
raise SystemExit(main(sys.argv[1:]))
6 changes: 6 additions & 0 deletions typst/private/tests/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
load("@bazel_skylib//rules:build_test.bzl", "build_test")

build_test(
name = "simple_build_test",
targets = ["//typst/private/tests/simple:simple"],
)
1 change: 1 addition & 0 deletions typst/private/tests/simple/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ typst(
":frown",
":smile",
],
visibility = ["//typst/private/tests:__pkg__"],
)

build_test(
Expand Down
Loading