Shell Escape for Typst typesetting system. Linux Only.

Overview

Shell Escape for Typst

This is a simple shell escape for Typst. It allows you to run shell commands directly from Typst compiler.

That said, it does not mean that you should run shell commands from Typst. In fact, I would highly recommend against it. This is a very dangerous feature and should be used with extreme caution, and, if possible (it is possible), not at all.

Usage

You don't.

Usage

Please, don't. I beg you.

Usage

Fine. But be aware that you aren't just playing with fire anymore. You are planning with fire, in a forest, during a drought, with a flamethrower, near the lake of gasoline, with fifteen nuclear power plants nearby.

Clone the repo and make sure you have cargo and Typst CLI installed.

Run cargo build. This will create a binary in /target. Get the shell-escape.typ file and #import it in your Typst project. Run the built executable before compiling your project.

A note of caution

This is a very dangerous feature. It's not just dangerous, it's extremely dangerous. There is a reason shell-escape will never be implemented in Typst Not only is it very bad for security, you can ruin your workspace. You are literally opening a window to an undefined behaviour from a safe environment of the Typst virtual machine.

And don't even think of running this along with typst-lsp, or any other compiler instance. There will be no guarantees on the order of execution of commands. This can result in the deadlock, and you will be lucky if only your compiler deadlocks.

You have been warned.

High-level API

To run a command, use #exec-command.

Argument Type Description Kind Default
command string Command to run. positional
method-stdout function Function to call when the command writes to stdout, used to interpret stdout. For example, if command returns .json, pass json. named read
method-stderr function Function to call when the command writes to stderr. named read
format-stdout string File extension of stdout. For example, if you want to read svg image, you should use image function with ".svg" format named ""
format-stderr string File extension of stderr. named ""
custom-hash string Discriminator which helps defeat the limitation of function purity. Can be any string. If your command is pure, it's not needed. named ""
allow-non-zero-error-code bool If false, the function will panic if command finishes execution with non-zero error code. named true

It returns a dictionary with three entries:

Key Type Description
stdout any (most likely string) Stdout of the command, read with the given method-stdout
stderr any (most likely string) Stderr of the command, read with the given method-stdout
error-code int Exit code of command

Example:

Calculate 2 + 2 using Python:
#exec-command("python -c \"print(2 + 2)\"")

Returns #(stdout: "4\n", stderr: "", error-code: 0)

See example-*.typ files for more.

HTTP API (curl wrapper)

To make it easier to use, there is a wrapper around curl command for making get-requests. It's called #http-get.

Argument Type Description Kind Default
url string URL to make a request to. positional
method function Function to interpret output with. named read
format string File extension of the response. named ""

There is also #encode-url function for URL parameter encoding.

Low-level API

I will not document everything, but here is an overview:

  • #exec-command-async queries a command for execution. It does not return anything.

  • #wait-one waits for one command to finish execution. It returns a dictionary with two entries: command and result. There are no guarantees on the order of commands, so you need to check the command field to see which command finished execution.

  • #get-stdout and #get-stderr return stdout and stderr of a last executed (and waited for) command respectively.

  • #reset-and-terminate-all terminates all running commands. You should run it before exiting your program.

In theory, this API allows you to run multiple commands in parallel, but I wouldn't recommend it. It's not tested, just like everything else here, and I'm not sure if it works.

How it works

It mounds a custom userspace filesystem. The only way Typst can interact with the outer world is by reading from files, and we are using this to our advantage.

The filesystem is build in a way that allows us to build commands piece by piece and execute them. For example, you ran #exec-command("ls -la /"), Typst does the following (approximately):

Stop all running commands:
#read("<...>/reset")

Send hex-encoded command to the buffer:
#read("<...>/6c73202d6c61202f")

Request an execution of the command in the buffer:
#read("<...>/exec")

Wait for the command to finish execution:
#read("<...>/wait")

Check that command executed successfully:
#read("<...>/diagnostics")

Get the stdout of the command:
#read("<...>/stdout")

Get the stderr of the command:
#read("<...>/stderr")

Except, this won't quite work, because every function in Typst is cached, so subsequent executions may not actually read the file. To fix this, we need to add a "random" string at the start of every file path. This is what discriminator parameters are for. You should not care much about those, unless you work with low-level API.

Limitations

Linux only. Might work on other Unix-like systems or MacOS, but I haven't tested it. Windows is not supported, do not ask.

Uses fuse. Make sure you have user_allow_other option enabled in /etc/fuse.conf.

Currently, the filesystem is hardcoded to be mounted at /tmp/typst-shell-escape/shell-escape. I probably should have made it configurable, but I didn't. Change it in main.rs, and in shell-escape.typ if you need to.

If the command you are running touches /tmp/typst-shell-escape/shell-escape in any way, it will deadlock. This can be fixed, but I won't bother for now.

You might also like...
Check the reproducibility status of your Arch Linux packages (read-only mirror)
Check the reproducibility status of your Arch Linux packages (read-only mirror)

arch-repro-status A CLI tool for querying the reproducibility status of the Arch Linux packages using data from a rebuilderd instance such as reproduc

Boxxy puts bad Linux applications in a box with only their files.

boxxy is a tool for boxing up misbehaving Linux applications and forcing them to put their files and directories in the right place, without symlinks!

A super simple /sbin/init for Linux which allows running one and only one program

Summary High-performance /sbin/init program for Linux This is designed to do literally nothing but accept binaries over the network and run them as a

Self-contained template system with Handlebars and inline shell scripts

Handlematters Self-contained template system with Handlebars and inline shell scripts Introduction Handlematters is a template system that combines Ha

Raw C Shell: interact with your operating system using raw C code, because why not?
Raw C Shell: interact with your operating system using raw C code, because why not?

rcsh Raw C Shell is a minimalist shell with no built in commands. You write entirely in C code and use return; to execute your code. Unlike that silly

TMM is a Linux native game modding tool. it allows to install and depoly mods for Linux native and wine games.

Tux Mod Manager TMM is a Linux native mod manager made with the Tauri toolkit. It can install, load, remove and deploy mods for both Linux native and

Pure-Rust rewrite of the Linux fontconfig library (no system dependencies) - using ttf-parser and allsorts

rust-fontconfig Pure-Rust rewrite of the Linux fontconfig library (no system dependencies) - using allsorts as a font parser in order to parse .woff,

A system fetch tool for Linux, written in Rust.
A system fetch tool for Linux, written in Rust.

fetchit A system fetch tool for Linux, written in Rust. fetchit is a simple system info tool, written in Rust, for Linux based operating systems. It o

An interruption-free notification system for Linux

nofi A Rofi-driven notification manager rofi.mp4 nofi is a distraction-free notification center. While most notification daemons make immediate popups

Owner
Nikolay Stepanov
Nikolay Stepanov
A CLI utility installed as "ansi" to quickly get ANSI escape sequences. Supports the most basic ones, like colors and styles as bold or italic.

'ansi' - a CLI utility to quickly get ANSI escape codes This Rust project called ansi-escape-sequences-cli provides an executable called ansi which ca

Philipp Schuster 5 Jul 28, 2022
Rust crate to enable ANSI escape code support on Windows.

enable-ansi-support: Enable ANSI escape code support on Windows 10 About This crate provides one function, enable_ansi_support, which allows ANSI esca

Rain 9 Nov 18, 2022
Terminal text styling via ANSI escape sequences.

Iridescent Features iridescent is a library for styling terminal text easily. It supports basic ANSI sequences, Xterm-256 colors, and RGB. You can ope

Rob 2 Oct 20, 2022
View Source, but for terminal escape sequences

Escape Artist Escape Artist is a tool for seeing ANSI escape codes in terminal applications. You interact with your shell just like you normally would

Reilly Wood 8 Apr 16, 2023
run Typst in JavaScriptWorld.

Typst.ts Typst.ts allows you to independently run the Typst compiler and exporter (renderer) in your browser. You can: locally run the compilation via

null 9 Apr 18, 2023
Packages for Typst.

Typst Packages An experimental package repository for Typst. A searchable list of all packages that were submitted here is available in the official d

Typst 91 Jul 4, 2023
An mdBook backend to output Typst markup, pdf, png, or svg

mdbook-typst mdbook-typst is a backend for mdBook. The backend converts the book to Typst markup and can output any format Typst can (currently pdf, p

Christian Legnitto 18 Dec 16, 2023
LaTeX support for Typst, powered by Rust and WASM.

MiTeX LaTeX support for Typst, powered by Rust and WASM. MiTeX processes LaTeX code into an abstract syntax tree (AST). Then it transforms the AST int

null 58 Jan 1, 2024
A tool to compare how Typst documents would look using different fonts or font variants.

typst-font-compare A tool to compare how Typst documents would look using different fonts or font variants. Installation cargo install --path . Usage

null 3 Feb 15, 2024
LaTeX support for Typst, powered by Rust and WASM

MiTeX LaTeX support for Typst, powered by Rust and WASM. MiTeX processes LaTeX code into an abstract syntax tree (AST). Then it transforms the AST int

null 303 Nov 24, 2024