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) state-pyscript-default-interpreter.update(x => pyscript-default-interpreter)
} }
show: html-show 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.html(lang: lang, {
html-head( html-head(
url, url,

View file

@ -26,22 +26,12 @@
// TODO DOCUMENT THIS! // TODO DOCUMENT THIS!
#let pyscript-show(it) = { #let pyscript-show(it) = {
let metadata = if it.lang != "python" { let metadata = parse-pep723(it.text)
none if metadata == none {
} else { metadata = (:)
parse-pep723(it.text)
} }
if metadata != none and "pyscript" in metadata.at( state-use-pyscript.update(x => true)
"tool", default: (:) context {
) and metadata.at(
"tool", default: (:)
).at(
"pyscript", default: (:)
).at(
"run", default: true
) {
state-use-pyscript.update(x => true)
context {
let config = metadata.at("tool", default: (:)).at("pyscript", default: (:)) let config = metadata.at("tool", default: (:)).at("pyscript", default: (:))
let pyscript-config = (:) let pyscript-config = (:)
@ -67,10 +57,24 @@
let attrs = ( let attrs = (
type: "py" 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", "") attrs.insert("terminal", "")
}
if config.at("worker", default: true) {
attrs.insert("worker", "") 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 != (:) { if pyscript-config != (:) {
attrs.insert("config", json.encode(pyscript-config)) attrs.insert("config", json.encode(pyscript-config))
@ -80,47 +84,28 @@
if config.at("repl", default: false) { if config.at("repl", default: false) {
script = "import code\n" + script + "\ncode.interact(banner='', local=globals())" script = "import code\n" + script + "\ncode.interact(banner='', local=globals())"
} }
let displayed-code = it.text;
if not config.at("hide-code", default: false) { //and not config.at("hide-meta", default: false) { if config.at("hide-meta", default: false) {
it displayed-code = displayed-code.replace(get-pep723(displayed-code), "").trim("\n")
} }
/* 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 not config.at("hide-code", default: false) {
if meta-line-0 == none { raw(
it displayed-code,
} else if it.number > meta-line-0 and it.number <= meta-line-0 + meta-lines.len() { block: true,
none lang: "python",
} else { align: it.align,
it syntaxes: it.syntaxes,
} theme: it.theme,
} tab-size: it.tab-size,
it )
}*/ }
html.elem( html.elem(
"script", "script",
attrs: attrs, attrs: attrs,
script script
) )
}} else {
it
} }
//raw(block: true, lang: "json", json.encode(metadata)) //raw(block: true, lang: "json", json.encode(metadata))
} }

View file

@ -74,10 +74,7 @@
#lorem(400) #lorem(400)
```python ```python-run
# /// script
# [tool.pyscript]
# ///
n = "" n = ""
while not n.isnumeric(): while not n.isnumeric():
n = input("enter a valid number: ") n = input("enter a valid number: ")
@ -92,7 +89,7 @@ for i in range(1, int(n) + 1):
else: else:
print(i) print(i)
``` ```
```python ```python-run
# /// script # /// script
# # requires-python = ">=3.11" # not supported yet # # requires-python = ">=3.11" # not supported yet
# dependencies = [ # dependencies = [
@ -115,6 +112,30 @@ pprint([(k, v["title"]) for k, v in data.items()][:10])
# Inline script metadata # 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 #summ.card
Test, `this is not a code block`, end test. Test, `this is not a code block`, end test.