work on pyscript/typst api

This commit is contained in:
Jean-Marie 'Histausse' Mineau 2026-06-06 18:04:47 +02:00
parent 84bf7ce78c
commit fe4b003971
Signed by: histausse
GPG key ID: B66AEEDA9B645AD2
3 changed files with 62 additions and 56 deletions

View file

@ -63,7 +63,7 @@
state-pyscript-default-interpreter.update(x => pyscript-default-interpreter)
}
show: html-show
show raw.where(block: true, lang: "python"): pyscript-show
show raw.where(block: true, lang: "python-run"): pyscript-show
html.html(lang: lang, {
html-head(
url,

View file

@ -26,22 +26,12 @@
// TODO DOCUMENT THIS!
#let pyscript-show(it) = {
let metadata = if it.lang != "python" {
none
} else {
parse-pep723(it.text)
let metadata = parse-pep723(it.text)
if metadata == none {
metadata = (:)
}
if metadata != none and "pyscript" in metadata.at(
"tool", default: (:)
) and metadata.at(
"tool", default: (:)
).at(
"pyscript", default: (:)
).at(
"run", default: true
) {
state-use-pyscript.update(x => true)
context {
state-use-pyscript.update(x => true)
context {
let config = metadata.at("tool", default: (:)).at("pyscript", default: (:))
let pyscript-config = (:)
@ -67,10 +57,24 @@
let attrs = (
type: "py"
)
if true { // TODO: pygame ?
let default_val_terminal = true
let default_val_worker = true
let default_val_canvas = none
// TODO: canvas: can only be used once by page, need inserting canvas html tag and pyscript hook to link it to pyodide
if config.at("pygame", default: false) {
default_val_terminal = false
default_val_worker = false
default_val_canvas = "canvas"
}
if config.at("terminal", default: true) {
attrs.insert("terminal", "")
}
if config.at("worker", default: true) {
attrs.insert("worker", "")
}
if config.at("canvas", default: default_val_canvas) not in (none, false) {
attrs.insert("canvas", config.at("canvas", default: default_val_canvas))
}
if pyscript-config != (:) {
attrs.insert("config", json.encode(pyscript-config))
@ -80,47 +84,28 @@
if config.at("repl", default: false) {
script = "import code\n" + script + "\ncode.interact(banner='', local=globals())"
}
if not config.at("hide-code", default: false) { //and not config.at("hide-meta", default: false) {
it
let displayed-code = it.text;
if config.at("hide-meta", default: false) {
displayed-code = displayed-code.replace(get-pep723(displayed-code), "").trim("\n")
}
if not config.at("hide-code", default: false) {
raw(
displayed-code,
block: true,
lang: "python",
align: it.align,
syntaxes: it.syntaxes,
theme: it.theme,
tab-size: it.tab-size,
)
}
/* Don't work, will require some way of unsetting show rule of something
} else if not config.at("hide-code", default: false) and config.at("hide-meta", default: false) {
let meta-lines = get-pep723(it.text).split("\n")
let code-lines = it.text.split("\n")
let meta-line-0 = none
for i in range(code-lines.len()) {
let match = true
for j in range(meta-lines.len()) {
if (i + j) >= code-lines.len() or meta-lines.at(j) != code-lines.at(i + j) {
match = false
break
}
if match {
meta-line-0 = i
}
}
}
show raw.line: it => {
if meta-line-0 == none {
it
} else if it.number > meta-line-0 and it.number <= meta-line-0 + meta-lines.len() {
none
} else {
it
}
}
it
}*/
html.elem(
"script",
attrs: attrs,
script
)
}} else {
it
}
//raw(block: true, lang: "json", json.encode(metadata))
}

View file

@ -74,10 +74,7 @@
#lorem(400)
```python
# /// script
# [tool.pyscript]
# ///
```python-run
n = ""
while not n.isnumeric():
n = input("enter a valid number: ")
@ -92,7 +89,7 @@ for i in range(1, int(n) + 1):
else:
print(i)
```
```python
```python-run
# /// script
# # requires-python = ">=3.11" # not supported yet
# dependencies = [
@ -115,6 +112,30 @@ pprint([(k, v["title"]) for k, v in data.items()][:10])
# Inline script metadata
```
```python-run
# /// script
# # requires-python = ">=3.11" # not supported yet
# dependencies = [
# "rich",
# ]
#
# [tool.pyscript]
# repl = true
# hide-meta = true
# [tool.pyscript.files]
# "https://peps.python.org/api/peps.json" = "./peps.json"
# ///
import json
from rich.pretty import pprint
with open("./peps.json") as fd:
data = json.load(fd)
pprint([(k, v["title"]) for k, v in data.items()][:10])
# >>> print(data["723"]["title"])
# Inline script metadata
```
#summ.card
Test, `this is not a code block`, end test.