Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Patch 2 #14

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,17 @@ Repository [lvignoli/typst-action-example](https://github.com/lvignoli/typst-act
- I was hasty to tag for a v1. I have now deleted it.
As long as Typst is not in a stable state, the action will stay in v0.
You should use `lvignoli/typst-action@main` in the meantime.

## Extra

To specify the output filename use `:` in the filename to denote the name, such as:
```
- name: Typst
uses: lvignoli/typst-action@main
with:
source_file: |
first_file.typ:first.pdf
second_file.typ
third_and_final_file.typ:third.pdf

```
22 changes: 16 additions & 6 deletions entrypoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@
import sys


def compile(filename: str, options: list[str]) -> bool:
def compile(filename: str, options: list[str], outputfilename: str | None) -> bool:
"""Compiles a Typst file with the specified global options.

Returns True if the typst command exited with status 0, False otherwise.
"""
command = ["typst"] + options + ["compile", filename]
logging.debug("Running: " + " ".join(command))
command = ["typst", "compile"] + options + [filename]
if outputfilename is not None:
command.append(outputfilename)
logging.info("Running: " + " ".join(command))

result = subprocess.run(command, capture_output=True, text=True)
try:
Expand All @@ -27,25 +29,33 @@ def main():
logging.basicConfig(level=logging.INFO)

# Parse the positional arguments, expected in the following form
# 1. The Typst files to compile in a line separated string
# 1. The Typst files to compile in a line separated string. Optionally To
# specify the ooutput name separate it with a : so such as:
# input.typ:output.pdf
# 2. The global Typst CLI options, in a line separated string. It means each
# whitespace separated field should be on its own line.
source_files = sys.argv[1].splitlines()
options = sys.argv[2].splitlines()


version = subprocess.run(
["typst", "--version"], capture_output=True, text=True
).stdout
logging.info(f"Using version {version}")

success: dict[str, bool] = {}

for filename in source_files:
for filename_pair in source_files:
(filename, _, outputfilename) = filename_pair.partition(":")
filename = filename.strip()
if filename == "":
continue
if outputfilename == "":
outputfilename = None
elif outputfilename is not None:
outputfilename = outputfilename.strip()
logging.info(f"Compiling {filename}…")
success[filename] = compile(filename, options)
success[filename] = compile(filename, options, outputfilename)

# Log status of each input files.
for filename, status in success.items():
Expand Down