Add foreign stdlib with code values and ship v0.0.4.
Bind `lang fences like formulas, run them via local interpreters, polish live-view code cards, and refresh bilingual welcome docs. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
dcd0cdb8f7
commit
b5e9d15391
33 files changed
+1044
-199
No files matched your search
+1
-1
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "marqdo"
|
||||
version = "0.0.3"
|
||||
version = "0.0.4"
|
||||
edition = "2021"
|
||||
license = "Apache-2.0"
|
||||
description = "Marqdo — Markdown markers as an interpreted programming language"
|
||||
|
||||
+21
-179
@@ -1,207 +1,49 @@
|
||||
# 外联 / 胶水标准库(候选)
|
||||
# 外联 / 胶水标准库
|
||||
|
||||
| | |
|
||||
|---|---|
|
||||
| 状态 | **草案 / 暂缓**(本波不实现;五库之后再议) |
|
||||
| 状态 | **已定(v1)** |
|
||||
| 日期 | 2026-08-05 |
|
||||
| 相关 | [stdlib-modules.md](stdlib-modules.md) · [code-vs-comment.md](code-vs-comment.md) · [markdown-mapping.md](markdown-mapping.md) · [stdlib-math.md](stdlib-math.md) |
|
||||
| 灵感 | Markdown \`\`\`lang 代码块;Marqdo 作编排层 |
|
||||
| 相关 | [stdlib-modules.md](stdlib-modules.md) · [stdlib-math.md](stdlib-math.md)(公式同构绑定) |
|
||||
|
||||
---
|
||||
|
||||
## 1. 为什么值得做
|
||||
## 1. 原则
|
||||
|
||||
[code-vs-comment.md](code-vs-comment.md) 已划出**外联面**:\`\`\`lang 是「其它语言」。若运行时能:
|
||||
|
||||
1. **收集**本文件(或导入图)中的围栏代码块;
|
||||
2. 按语言 **subprocess / 嵌入运行时**执行;
|
||||
3. 把 stdout / 约定返回值 **接回 Marqdo 值**;
|
||||
|
||||
则 Marqdo 成为**文学化胶水语言**:结构、叙述、分支循环用 Marqdo,重活交给 Python/JS/Shell/SQL。
|
||||
|
||||
风险:安全(任意代码)、可复现(环境漂移)、view/CI(无解释器)——故为**候选**,默认关闭。
|
||||
**导入即用**:`lib/foreign` / `lib/外联`。解释器来自用户本机(PATH / `set_cmd` / `MARQDO_FOREIGN_*`)。live view 可点 Run;静态导出展示源码,Run 禁用。
|
||||
|
||||
---
|
||||
|
||||
## 2. 与叙述面的边界
|
||||
|
||||
| 面 | 行为 |
|
||||
|----|------|
|
||||
| 叙述段里的 \`\`\` | 今日:注释;**外联库启用后仍可配置为「可调用」或「仅展示」** |
|
||||
| 可执行面附近的 \`\`\` | 建议:**具名块**才可调用,避免误跑文档里的示例 |
|
||||
|
||||
推荐:**只有带名字的围栏**进入外联目录。
|
||||
|
||||
---
|
||||
|
||||
## 3. 命名围栏语法(草案)
|
||||
|
||||
三种候选(实现前锁定一种):
|
||||
|
||||
### 3.1 信息串后缀(推荐试水)
|
||||
|
||||
````markdown
|
||||
```python name=normalize
|
||||
def normalize(s):
|
||||
return s.strip().lower()
|
||||
```
|
||||
````
|
||||
|
||||
或:
|
||||
|
||||
````markdown
|
||||
```python#normalize
|
||||
…
|
||||
```
|
||||
````
|
||||
|
||||
### 3.2 HTML 注释锚点
|
||||
|
||||
````markdown
|
||||
<!-- foreign:normalize -->
|
||||
```python
|
||||
…
|
||||
```
|
||||
````
|
||||
|
||||
### 3.3 紧邻 Marqdo 声明
|
||||
## 2. 绑定语法(与公式同构)
|
||||
|
||||
```markdown
|
||||
*`normalize` = foreign_def lang=python *
|
||||
|
||||
`hi` =
|
||||
```python
|
||||
…
|
||||
```
|
||||
print("hello")
|
||||
```
|
||||
|
||||
**倾向 3.1**:仍是合法 Markdown,多数渲染器忽略未知 info 字段;与「文档即源」一致。
|
||||
*`out` = > run code=`hi` *
|
||||
```
|
||||
|
||||
未命名围栏:view 可语法高亮展示,**不可** `foreign_call`。
|
||||
得到运行时类型 **`code`**(`lang` + `source`)。未绑定的 \`\`\` 围栏仍是叙述注释。
|
||||
|
||||
曾用的 \`\`\`python name=hi **已移除**。
|
||||
|
||||
---
|
||||
|
||||
## 4. 导入与 API
|
||||
|
||||
| 导入 | API 语言 |
|
||||
|------|----------|
|
||||
| `> lib/foreign.mq.md` | 英文 |
|
||||
| `> lib/外联.mq.md` | 中文 |
|
||||
## 3. API
|
||||
|
||||
| 英文 | 中文 | 形参 | 结果 |
|
||||
|------|------|------|------|
|
||||
| `run` | `运行` | `name`, 可选 `stdin` / `名`, `标准输入` | 文本(stdout) |
|
||||
| `run_lang` | `按语言运行` | `lang`, `source`, 可选 `stdin` | 文本 |
|
||||
| `call` | `调用` | `name`, 可选 `args`(JSON 文本) | 文本 |
|
||||
| `langs` | `语言表` | (无) | 已注册语言列表 |
|
||||
| `set_cmd` | `设置命令` | `lang`, `cmd` | None |
|
||||
| `run` | `运行` | `code`(code 值) | 文本 stdout |
|
||||
| `run_lang` | `按语言运行` | `lang`, `source` | 文本 |
|
||||
| `langs` | `语言表` | | 列表 |
|
||||
|
||||
`call` 约定(v1):
|
||||
|
||||
- 宿主把 `args` 经环境变量或临时 JSON 文件传给子进程;
|
||||
- 被调代码打印**一行 JSON** 到 stdout 作为返回值;
|
||||
- 或退出码非 0 → Marqdo 诊断。
|
||||
|
||||
更紧的 Python 嵌入(`pyo3`)列为后期优化,**v1 只用子进程**,便于推理安全边界。
|
||||
live view:Structure 中 code 卡片带命令框 + Run → `POST /api/foreign-run`。
|
||||
|
||||
---
|
||||
|
||||
## 5. 语言支持矩阵(v1)
|
||||
## 4. 一句话
|
||||
|
||||
| 语言 | 探测 | 备注 |
|
||||
|------|------|------|
|
||||
| `python` / `python3` | `PATH` | 优先实现;服务科学计算与 [stdlib-math.md](stdlib-math.md) CAS 外置 |
|
||||
| `node` / `javascript` | `PATH` | 第二 |
|
||||
| `sh` / `bash` | Unix;Windows 可用 Git Bash 或跳过 | 高风险,默认更严 |
|
||||
| `cmd` / `powershell` | Windows | 可选 |
|
||||
| 其它 | 配置表 | `marqdo.toml` / 环境变量注册解释器路径 |
|
||||
|
||||
无解释器时:清晰诊断 `foreign language not available: python`,而非静默跳过。
|
||||
|
||||
---
|
||||
|
||||
## 6. 权限模型(硬性)
|
||||
|
||||
| 场景 | 默认 |
|
||||
|------|------|
|
||||
| `marqdo run` | **拒绝**,除非 `--allow-foreign` |
|
||||
| `marqdo view` | 拒绝(或仅 `--allow-foreign` 的本地演示) |
|
||||
| `view output` / CI 用户站 | **拒绝**(文档示例用录制输出或纯 Marqdo) |
|
||||
| 金样例 | 用 `tests/foreign/` + 环境探测;无 Python 则 `ignore` |
|
||||
|
||||
附加约束(可组合):
|
||||
|
||||
- `--foreign-lang python,node` 白名单;
|
||||
- 超时(默认 5–30s);
|
||||
- 工作目录锁在文件所在根;
|
||||
- 无网络(与 `--allow-net` 分离;子进程环境可剥 `HTTP_PROXY` 等——尽力而为)。
|
||||
|
||||
---
|
||||
|
||||
## 7. 典型胶水故事
|
||||
|
||||
```markdown
|
||||
---
|
||||
title: Normalize names
|
||||
> lib/foreign.mq.md
|
||||
> lib/fs.mq.md
|
||||
---
|
||||
|
||||
```python name=normalize
|
||||
import sys
|
||||
print(sys.stdin.read().strip().lower())
|
||||
```
|
||||
|
||||
# main
|
||||
|
||||
*`raw` = > read_text path=names.txt *
|
||||
*`clean` = > run name=normalize stdin=`raw` *
|
||||
> print text=`clean`
|
||||
```
|
||||
|
||||
中文轨:
|
||||
|
||||
```markdown
|
||||
---
|
||||
> lib/外联.mq.md
|
||||
> lib/文件.mq.md
|
||||
---
|
||||
```
|
||||
|
||||
叙述说明用中文;围栏内语言仍是 Python——**外联块语言 ≠ Marqdo API 语言**。
|
||||
|
||||
---
|
||||
|
||||
## 8. 与数学库的关系
|
||||
|
||||
- 厚 CAS(SymPy)**优先**走外联,而不是塞进默认 `lib/math`。
|
||||
- 官方可提供可选示例:`examples/math-sympy.mq.md`(要求 Python + `--allow-foreign`)。
|
||||
- 数值与 SVG 仍留在数学库,保证无 Python 时可教可演示。
|
||||
|
||||
---
|
||||
|
||||
## 9. 是否定为「标准库」的决策标准
|
||||
|
||||
纳入默认 `lib/foreign` 当且仅当:
|
||||
|
||||
1. 权限模型在 CLI/view/CI 测全;
|
||||
2. 至少一种语言金样例在 CI 稳定(或正式 `#[ignore]` 策略);
|
||||
3. 文档明确「胶水能力默认关闭,不是网页 RCE」。
|
||||
|
||||
若短期内无法保证安全叙事 → 放在 `lib/experimental/foreign` 或 feature `foreign`,用户站不放可点击「运行外联」的按钮。
|
||||
|
||||
---
|
||||
|
||||
## 10. 实现分期
|
||||
|
||||
| 期 | 内容 |
|
||||
|----|------|
|
||||
| F0 | 词法:收集命名围栏 → 模块表(尚不执行) |
|
||||
| F1 | `run` + Python 子进程 + 权限旗标 |
|
||||
| F2 | `call` JSON 约定;超时与 cwd 锁 |
|
||||
| F3 | Node;view 展示块与「需 CLI 权限」提示 |
|
||||
| F4 | 可选嵌入运行时、与 math-cas 示例合流 |
|
||||
|
||||
---
|
||||
|
||||
## 11. 一句话
|
||||
|
||||
**\`\`\`lang 是外联面的源,不是自动执行的陷阱;具名块 + 显式导入 + 默认拒绝权限,才能把 Marqdo 做成安全可讲的胶水语言。**
|
||||
**`name` = + \`\`\`lang 围栏 → code 值;显式 run;本机解释器可配置。**
|
||||
@@ -2,11 +2,11 @@
|
||||
|
||||
| | |
|
||||
|---|---|
|
||||
| 状态 | **五库已落地**;下一波:**数学库**([stdlib-math.md](stdlib-math.md));外联仍暂缓 |
|
||||
| 状态 | **五库 + 数学已落地**;下一波:**外联**([stdlib-foreign.md](stdlib-foreign.md)) |
|
||||
| 日期 | 2026-08-05 |
|
||||
| 本波范围 | 文件 / 系统 / 时间 / JSON / 网络(已完成) |
|
||||
| 下一波 | **数学**(数值+随机+作图+轻量求解) |
|
||||
| 暂缓 | 外联 — [stdlib-foreign.md](stdlib-foreign.md) |
|
||||
| 本波范围 | 文件 / 系统 / 时间 / JSON / 网络 / 数学(已完成) |
|
||||
| 下一波 | **外联**(本机解释器 / 具名围栏) |
|
||||
| 暂缓 | — |
|
||||
| 原则 | 全部经 frontmatter **导入**;除 JSON 外中英分文件;内核保持少而精 |
|
||||
| 相关 | [stdlib.md](stdlib.md) · [stdlib-i18n.md](stdlib-i18n.md) · [keywords-i18n.md](keywords-i18n.md) · [view.md](view.md) |
|
||||
|
||||
@@ -26,7 +26,7 @@
|
||||
| **JSON** | `lib/json.mq.md` | **同文件(特例,§3.4)** | **做** |
|
||||
| **网络(简)** | `lib/net.mq.md` | `lib/网络.mq.md` | **做** |
|
||||
| 数学 | `lib/math.mq.md` | `lib/数学.mq.md` | **下一波** |
|
||||
| 外联 | `lib/foreign.mq.md` | `lib/外联.mq.md` | **暂缓** |
|
||||
| 外联 | `lib/foreign.mq.md` | `lib/外联.mq.md` | **做** |
|
||||
|
||||
---
|
||||
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
---
|
||||
title: lib/foreign
|
||||
description: Run bound code fences and inline sources via local interpreters
|
||||
---
|
||||
|
||||
# set_cmd
|
||||
- lang
|
||||
- cmd
|
||||
|
||||
**> host_foreign_set_cmd lang=`lang` cmd=`cmd`**
|
||||
|
||||
# run
|
||||
- code
|
||||
|
||||
**> host_foreign_run code=`code`**
|
||||
|
||||
# run_lang
|
||||
- lang
|
||||
- source
|
||||
|
||||
**> host_foreign_run_lang lang=`lang` source=`source`**
|
||||
|
||||
# langs
|
||||
|
||||
**> host_foreign_langs**
|
||||
@@ -0,0 +1,25 @@
|
||||
---
|
||||
title: lib/外联
|
||||
description: 通过本机解释器运行绑定的代码围栏与内联源码
|
||||
---
|
||||
|
||||
# 设置命令
|
||||
- 语言
|
||||
- 命令
|
||||
|
||||
**> host_foreign_set_cmd lang=`语言` cmd=`命令`**
|
||||
|
||||
# 运行
|
||||
- 代码
|
||||
|
||||
**> host_foreign_run code=`代码`**
|
||||
|
||||
# 按语言运行
|
||||
- 语言
|
||||
- 源码
|
||||
|
||||
**> host_foreign_run_lang lang=`语言` source=`源码`**
|
||||
|
||||
# 语言表
|
||||
|
||||
**> host_foreign_langs**
|
||||
+34
-6
@@ -1,23 +1,25 @@
|
||||
---
|
||||
title: Welcome to Marqdo
|
||||
description: User docs home — code is documentation
|
||||
description: User docs home — code is documentation (English)
|
||||
> lib/math.mq.md
|
||||
> lib/foreign.mq.md
|
||||
---
|
||||
|
||||
# main
|
||||
|
||||
Hi — thanks for opening this site. Marqdo is a language where **code is documentation**: Markdown markers are the program structure, so what you read is what runs.
|
||||
Hi — thanks for reading this far. I am Marqdo’s developer. Marqdo is a language where **code is documentation, and documentation is code**: Markdown markers are the program structure, so what you read is what runs.
|
||||
|
||||
AI already prefers Markdown. Marqdo asks: why keep docs and code as two worlds? A `.mq.md` file is both. The longer ambition includes OKF-style knowledge YAML and more; for now the focus is a solid interpreter and this site (yes — generated by Marqdo itself).
|
||||
AI already prefers Markdown-shaped output. The idea was: why keep docs and code as two separate worlds? So Marqdo was born as a “docs are code” language. To keep every source renderable as Markdown, files use the `.mq.md` suffix. From early on we aimed for richer tooling — auto-generated YAML for OKF-style knowledge bases, even auto-generated slides — those are later goals. Happily, it already works well as a blogging tool: yes, this site is generated by Marqdo. And when AI writes Marqdo, you tend to burn fewer tokens — wallets everywhere thank you.
|
||||
|
||||
The language is young. You get structure markers, a small bilingual core (`print` / `打印`, …), optional stdlib files under `lib/` (see [`stdlib/`](stdlib/) on this site), local `view`, and static export. Contribute on [GitHub](https://github.com/cflmy/marqdo).
|
||||
The language is still young: structure markers, bilingual core names (`print` / `打印`, …), importable stdlibs (see [`stdlib/`](stdlib/) on this site), local `view`, and static export. Contribute on [GitHub](https://github.com/cflmy/marqdo).
|
||||
|
||||
中文文档请看同目录的 [`00-欢迎.mq.md`](00-欢迎.mq.md)(叙述与内置均用中文)。
|
||||
|
||||
A classic hello via a small function:
|
||||
A small function to say hello:
|
||||
|
||||
> greet who=marqdo
|
||||
|
||||
Named args allow spaces in values:
|
||||
Named args can include spaces:
|
||||
|
||||
> greet who=marqdo user
|
||||
|
||||
@@ -25,3 +27,29 @@ Named args allow spaces in values:
|
||||
- who
|
||||
|
||||
> print text=Welcome, `who`!
|
||||
|
||||
---
|
||||
|
||||
Beyond the basics above, Marqdo also ships standard libraries: table, time, text, files, system, math, foreign, and more.
|
||||
|
||||
Math and foreign deserve a special mention. With the math library you can bind a formula like this:
|
||||
|
||||
`f` =
|
||||
$$
|
||||
x^2 - 2
|
||||
$$
|
||||
|
||||
Then plot it:
|
||||
|
||||
*`_svg` = > plot formula=`f` var=x min=-3 max=3 *
|
||||
|
||||
The foreign library lets you call external code, for example:
|
||||
|
||||
`hi` =
|
||||
```python
|
||||
print("hello-from-python")
|
||||
```
|
||||
|
||||
Under Markdown this stays easy to read — which is the point: Marqdo’s design is code as documentation, documentation as code.
|
||||
|
||||
Welcome again — enjoy exploring Marqdo.
|
||||
+31
-2
@@ -1,13 +1,15 @@
|
||||
---
|
||||
title: 欢迎使用 Marqdo
|
||||
description: 用户文档首页 — 代码即文档(中文版)
|
||||
> lib/数学.mq.md
|
||||
> lib/外联.mq.md
|
||||
---
|
||||
|
||||
# main
|
||||
|
||||
阅读到这里的朋友你好!我是 Marqdo 的开发者。Marqdo 是一门**代码即文档**的语言:Markdown 标记就是程序结构,你读到的就是能跑的程序。
|
||||
阅读到这里的朋友你好!我是 Marqdo 的开发者。Marqdo 是一门**代码即文档,文档即代码**的语言:Markdown 标记就是程序结构,你读到的就是能跑的程序。
|
||||
|
||||
AI 输出本就偏爱 Markdown。作者的念头是:何必再把文档和代码拆成两套?`.mq.md` 双后缀保证它仍是合法 Markdown。更长远的目标包括 OKF 式知识清单等;眼下先把解释器与本站做好——对,这个站点就是 Marqdo 生成的。
|
||||
AI 偏爱 Markdown结构的输出。作者的念头是:何必再把文档和代码拆成两套?我们来实现一个文档即代码的语言,所以marqdo应运而生,为了确保代码一定可以被渲染,marqdo的文件后缀定义为了`.mq.md`。在开发之处我们就尝试希望更丰富的支持,自动生成一个yaml文件使得 OKF 式知识库便捷的自动配置,乃至自动生成ppt,自然这都是以后的目标了。不过相当不错的是,至少这会成为一个不错的博客工具——对,这个站点就是 Marqdo 生成的,而且AI输出的时候会节约不少token,你知道的,token烧的太多,钱包会可怜的饿瘦的。
|
||||
|
||||
语言仍在起步:结构标记、中英内核双名(`打印` / `print` …)、可导入的标准库(见本站 [`stdlib/`](stdlib/))、本地 `view` 与静态导出。欢迎到 [GitHub](https://github.com/cflmy/marqdo) 一起做。
|
||||
|
||||
@@ -25,3 +27,30 @@ English docs: [`00-welcome.mq.md`](00-welcome.mq.md) (English prose and English
|
||||
- 谁
|
||||
|
||||
> 打印 内容=欢迎,`谁`!
|
||||
|
||||
---
|
||||
|
||||
除了上面的一些基本内容之外,我还开发了这个语言的一些标准库,包括表、时间、文本、文件、系统、数学和外联等标准库。
|
||||
|
||||
其中数学库和外联库是我要着重提到的,例如数学库,你可以通过下面的方式调用:
|
||||
|
||||
`f` =
|
||||
$$
|
||||
x^2 - 2
|
||||
$$
|
||||
|
||||
之后让我们画个图像吧!
|
||||
|
||||
*`_svg` = > 绘图 formula=`f` var=x min=-3 max=3 *
|
||||
|
||||
外联库则允许你调用外部代码,例如:
|
||||
|
||||
`hi` =
|
||||
```python
|
||||
print("hello-from-python")
|
||||
```
|
||||
|
||||
这些代码在markdown下面都非常的易读,就像我说的,marqdo的设计思想就是代码即文档,
|
||||
文档即代码。
|
||||
|
||||
最后,再次欢迎你,尽情体验marqdo吧!
|
||||
+1
-1
@@ -9,7 +9,7 @@
|
||||
| [`00-welcome.mq.md`](00-welcome.mq.md) / [`00-欢迎.mq.md`](00-欢迎.mq.md) | 首页(英 / 中) |
|
||||
| [`structure/`](structure/) | 基本结构 |
|
||||
| [`keywords/`](keywords/) | 关键字与 L0 内置 |
|
||||
| [`stdlib/`](stdlib/) | **标准库**(导入方式 + text/表/文件/时间/系统/json/网络/数学) |
|
||||
| [`stdlib/`](stdlib/) | **标准库**(导入方式 + text/表/文件/时间/系统/json/网络/数学/外联) |
|
||||
| [`features/`](features/) | 特性迭代 |
|
||||
|
||||
**不放** `errors/`:失败样例只在 `tests/errors/`。
|
||||
|
||||
@@ -11,7 +11,7 @@ English-named files export English APIs; Chinese-named files export Chinese APIs
|
||||
|
||||
No extra allow flags: importing a module means you intend to use it. Under marqdo view or static export, exit soft-fails and long sleeps are clamped so the console stays up.
|
||||
|
||||
Modules: text (text / 文本), table (table / 表), files (fs / 文件), time (time / 时间), system (sys / 系统), json (json only), net (net / 网络), math (math / 数学).
|
||||
Modules: text (text / 文本), table (table / 表), files (fs / 文件), time (time / 时间), system (sys / 系统), json (json only), net (net / 网络), math (math / 数学), foreign (foreign / 外联).
|
||||
|
||||
Sibling pages in this folder document each module. Chinese overview: 00-索引.mq.md.
|
||||
|
||||
|
||||
@@ -11,7 +11,7 @@ description: 如何导入官方 lib 模块
|
||||
|
||||
导入即表示要用对应能力,无需再加允许旗标。marqdo view 或静态导出里,退出会软失败、长睡眠会被钳制,以免拖垮控制台。
|
||||
|
||||
模块:文本(text / 文本)、表(table / 表)、文件(fs / 文件)、时间(time / 时间)、系统(sys / 系统)、json(仅 json)、网络(net / 网络)、数学(math / 数学)。
|
||||
模块:文本(text / 文本)、表(table / 表)、文件(fs / 文件)、时间(time / 时间)、系统(sys / 系统)、json(仅 json)、网络(net / 网络)、数学(math / 数学)、外联(foreign / 外联)。
|
||||
|
||||
各模块说明见本目录其它页。English: 00-overview.mq.md.
|
||||
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
---
|
||||
title: lib/foreign — external languages
|
||||
description: Run local interpreters from Marqdo (English)
|
||||
> lib/foreign.mq.md
|
||||
---
|
||||
|
||||
# main
|
||||
|
||||
Import lib/foreign.mq.md. Bind a code fence like a formula: empty-RHS assign then a ```lang block. The value type is code. Call run code=`name` to execute via your local interpreter. run_lang runs an inline source string.
|
||||
|
||||
`hi` =
|
||||
```python
|
||||
print("hello-from-python")
|
||||
```
|
||||
|
||||
Default command is python (Windows) or python3 (Unix). Override with set_cmd, env MARQDO_FOREIGN_PYTHON, or the command box in live view. Failures ask you to check that configuration.
|
||||
|
||||
*`out` = > run code=`hi` *
|
||||
|
||||
> print text=`out`
|
||||
@@ -0,0 +1,20 @@
|
||||
---
|
||||
title: lib/外联 — 外部语言
|
||||
description: 从 Marqdo 调用本机解释器(中文)
|
||||
> lib/外联.mq.md
|
||||
---
|
||||
|
||||
# main
|
||||
|
||||
导入 lib/外联.mq.md。代码围栏与公式同构:空 RHS 赋值后跟 lang 代码块,得到 code 类型。用 运行 代码=`名` 调用本机解释器。
|
||||
|
||||
`hi` =
|
||||
```python
|
||||
print("hello-from-python")
|
||||
```
|
||||
|
||||
默认命令可用 设置命令 或 MARQDO_FOREIGN_PYTHON 覆盖;live view 里可改命令框再点 Run。
|
||||
|
||||
*`out` = > 运行 代码=`hi` *
|
||||
|
||||
> print text=`out`
|
||||
@@ -60,6 +60,11 @@ pub fn canonical_param(builtin: &str, param: &str) -> String {
|
||||
}
|
||||
("host_plot", "steps" | "步数") => "steps".into(),
|
||||
("host_plot", "derivative" | "导数") => "derivative".into(),
|
||||
("host_foreign_run", "code" | "代码") => "code".into(),
|
||||
("host_foreign_run" | "host_foreign_run_lang", "stdin" | "标准输入") => "stdin".into(),
|
||||
("host_foreign_set_cmd" | "host_foreign_run_lang", "lang" | "语言") => "lang".into(),
|
||||
("host_foreign_set_cmd", "cmd" | "命令") => "cmd".into(),
|
||||
("host_foreign_run_lang", "source" | "源码") => "source".into(),
|
||||
_ => param.to_string(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
use crate::diagnostics::Span;
|
||||
use crate::formula::Expr as FormulaExpr;
|
||||
use crate::value::CodeBlock;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Module {
|
||||
@@ -91,6 +92,8 @@ pub enum Expr {
|
||||
List(Vec<Expr>),
|
||||
/// Parsed `$$…$$` formula tree (from assignment RHS).
|
||||
Formula(FormulaExpr),
|
||||
/// Bound ```lang … ``` fence (`code` value).
|
||||
Code(CodeBlock),
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
|
||||
@@ -44,6 +44,7 @@ pub fn builtin_type(v: &Value) -> Value {
|
||||
Value::List(_) => "list",
|
||||
Value::Map(_) => "map",
|
||||
Value::Formula(_) => "formula",
|
||||
Value::Code(_) => "code",
|
||||
}
|
||||
.into(),
|
||||
)
|
||||
|
||||
@@ -641,6 +641,10 @@ impl<'a> FnCompiler<'a> {
|
||||
let i = self.add_const(Value::Formula(e.clone()));
|
||||
self.emit(Op::Constant(i));
|
||||
}
|
||||
Expr::Code(c) => {
|
||||
let i = self.add_const(Value::Code(c.clone()));
|
||||
self.emit(Op::Constant(i));
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -0,0 +1,66 @@
|
||||
//! Foreign / code-block helpers (外联).
|
||||
|
||||
/// Default interpreter argv for a language tag (before env / set_cmd).
|
||||
pub fn default_argv(lang: &str) -> Option<Vec<String>> {
|
||||
match lang.to_ascii_lowercase().as_str() {
|
||||
"python" | "python3" | "py" => Some(default_python_argv()),
|
||||
"javascript" | "js" | "node" => Some(vec!["node".into()]),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn default_python_argv() -> Vec<String> {
|
||||
#[cfg(windows)]
|
||||
{
|
||||
vec!["python".into()]
|
||||
}
|
||||
#[cfg(not(windows))]
|
||||
{
|
||||
vec!["python3".into()]
|
||||
}
|
||||
}
|
||||
|
||||
/// Placeholder shown in the view command box.
|
||||
pub fn default_cmd_display(lang: &str) -> String {
|
||||
default_argv(lang)
|
||||
.map(|a| a.join(" "))
|
||||
.unwrap_or_else(|| lang.to_string())
|
||||
}
|
||||
|
||||
/// Parse ```lang … opener → language id (first token).
|
||||
pub fn fence_lang(opener_trimmed: &str) -> Option<String> {
|
||||
let rest = opener_trimmed.strip_prefix("```")?;
|
||||
if rest.is_empty() || rest.chars().all(|c| c == '`') {
|
||||
return None;
|
||||
}
|
||||
let lang = rest.split_whitespace().next()?.to_string();
|
||||
if lang.is_empty() {
|
||||
None
|
||||
} else {
|
||||
Some(lang)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn is_fence_opener(trimmed: &str) -> bool {
|
||||
fence_lang(trimmed).is_some()
|
||||
}
|
||||
|
||||
pub fn is_fence_closer(trimmed: &str) -> bool {
|
||||
trimmed.starts_with("```")
|
||||
&& trimmed
|
||||
.chars()
|
||||
.skip_while(|c| *c == '`')
|
||||
.all(|c| c.is_whitespace())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn fence_lang_python() {
|
||||
assert_eq!(fence_lang("```python").as_deref(), Some("python"));
|
||||
assert_eq!(fence_lang("```python name=x").as_deref(), Some("python"));
|
||||
assert!(fence_lang("```").is_none());
|
||||
}
|
||||
}
|
||||
+37
-1
@@ -2,7 +2,7 @@
|
||||
|
||||
use std::collections::HashMap;
|
||||
|
||||
use crate::host::{fs, json, math, net, sys, time, HostContext};
|
||||
use crate::host::{foreign, fs, json, math, net, sys, time, HostContext};
|
||||
use crate::value::Value;
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
@@ -70,6 +70,10 @@ pub enum HostFn {
|
||||
Plot = 59,
|
||||
PlotPoints = 60,
|
||||
PlotConic = 61,
|
||||
ForeignSetCmd = 62,
|
||||
ForeignRun = 63,
|
||||
ForeignRunLang = 64,
|
||||
ForeignLangs = 65,
|
||||
}
|
||||
|
||||
impl HostFn {
|
||||
@@ -136,6 +140,10 @@ impl HostFn {
|
||||
59 => Self::Plot,
|
||||
60 => Self::PlotPoints,
|
||||
61 => Self::PlotConic,
|
||||
62 => Self::ForeignSetCmd,
|
||||
63 => Self::ForeignRun,
|
||||
64 => Self::ForeignRunLang,
|
||||
65 => Self::ForeignLangs,
|
||||
_ => return None,
|
||||
})
|
||||
}
|
||||
@@ -205,6 +213,10 @@ impl HostFn {
|
||||
"host_plot" | "plot" | "绘图" => Self::Plot,
|
||||
"host_plot_points" | "plot_points" | "绘点列" => Self::PlotPoints,
|
||||
"host_plot_conic" | "plot_conic" | "绘圆锥" => Self::PlotConic,
|
||||
"host_foreign_set_cmd" => Self::ForeignSetCmd,
|
||||
"host_foreign_run" => Self::ForeignRun,
|
||||
"host_foreign_run_lang" => Self::ForeignRunLang,
|
||||
"host_foreign_langs" => Self::ForeignLangs,
|
||||
_ => return None,
|
||||
})
|
||||
}
|
||||
@@ -276,6 +288,10 @@ impl HostFn {
|
||||
Self::Plot => "host_plot",
|
||||
Self::PlotPoints => "host_plot_points",
|
||||
Self::PlotConic => "host_plot_conic",
|
||||
Self::ForeignSetCmd => "host_foreign_set_cmd",
|
||||
Self::ForeignRun => "host_foreign_run",
|
||||
Self::ForeignRunLang => "host_foreign_run_lang",
|
||||
Self::ForeignLangs => "host_foreign_langs",
|
||||
}
|
||||
}
|
||||
|
||||
@@ -312,6 +328,10 @@ impl HostFn {
|
||||
Self::Plot => &["formula", "var", "min", "max"],
|
||||
Self::PlotPoints => &["xs", "ys"],
|
||||
Self::PlotConic => &["kind", "a"],
|
||||
Self::ForeignSetCmd => &["lang", "cmd"],
|
||||
Self::ForeignRun => &["code"],
|
||||
Self::ForeignRunLang => &["lang", "source"],
|
||||
Self::ForeignLangs => &[],
|
||||
}
|
||||
}
|
||||
|
||||
@@ -324,6 +344,8 @@ impl HostFn {
|
||||
Self::Plot => &["steps", "path", "derivative", "grid"],
|
||||
Self::PlotPoints => &["path", "grid"],
|
||||
Self::PlotConic => &["b", "h", "k", "path", "grid"],
|
||||
Self::ForeignSetCmd => &["args"],
|
||||
Self::ForeignRun | Self::ForeignRunLang => &["stdin"],
|
||||
_ => &[],
|
||||
}
|
||||
}
|
||||
@@ -457,6 +479,20 @@ pub fn call_host(
|
||||
bound.get("path"),
|
||||
bound.get("grid"),
|
||||
),
|
||||
HostFn::ForeignSetCmd => foreign::set_cmd(
|
||||
ctx,
|
||||
require(bound, "lang")?,
|
||||
require(bound, "cmd")?,
|
||||
bound.get("args"),
|
||||
),
|
||||
HostFn::ForeignRun => foreign::run(ctx, require(bound, "code")?, bound.get("stdin")),
|
||||
HostFn::ForeignRunLang => foreign::run_lang(
|
||||
ctx,
|
||||
require(bound, "lang")?,
|
||||
require(bound, "source")?,
|
||||
bound.get("stdin"),
|
||||
),
|
||||
HostFn::ForeignLangs => foreign::langs(ctx),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,326 @@
|
||||
//! Foreign language runners (subprocess). Import L1 `lib/foreign` to use.
|
||||
|
||||
use std::io::{Read, Write};
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::process::{Command, Stdio};
|
||||
use std::thread;
|
||||
use std::time::{Duration, Instant, SystemTime, UNIX_EPOCH};
|
||||
|
||||
use crate::foreign;
|
||||
use crate::host::HostContext;
|
||||
use crate::value::{CodeBlock, Value};
|
||||
|
||||
const DEFAULT_TIMEOUT: Duration = Duration::from_secs(10);
|
||||
const MAX_OUTPUT: usize = 1_048_576;
|
||||
|
||||
fn as_text<'a>(v: &'a Value, what: &str) -> Result<&'a str, String> {
|
||||
match v {
|
||||
Value::Text(s) => Ok(s.as_str()),
|
||||
_ => Err(format!("{what} must be text")),
|
||||
}
|
||||
}
|
||||
|
||||
fn opt_text(v: Option<&Value>) -> Result<Option<&str>, String> {
|
||||
match v {
|
||||
None | Some(Value::None) => Ok(None),
|
||||
Some(v) => Ok(Some(as_text(v, "value")?)),
|
||||
}
|
||||
}
|
||||
|
||||
fn as_code(v: &Value) -> Result<&CodeBlock, String> {
|
||||
match v {
|
||||
Value::Code(c) => Ok(c),
|
||||
_ => Err("run needs a code value (bind with `name` = then a ```lang fence)".into()),
|
||||
}
|
||||
}
|
||||
|
||||
/// Register interpreter argv for `lang` (e.g. venv python).
|
||||
pub fn set_cmd(
|
||||
ctx: &mut HostContext,
|
||||
lang: &Value,
|
||||
cmd: &Value,
|
||||
args: Option<&Value>,
|
||||
) -> Result<Value, String> {
|
||||
let lang = as_text(lang, "lang")?.to_ascii_lowercase();
|
||||
let cmd = as_text(cmd, "cmd")?;
|
||||
let mut argv = vec![cmd.to_string()];
|
||||
if let Some(a) = args {
|
||||
match a {
|
||||
Value::List(xs) => {
|
||||
for x in xs {
|
||||
argv.push(x.as_display());
|
||||
}
|
||||
}
|
||||
Value::Text(s) => argv.push(s.clone()),
|
||||
Value::None => {}
|
||||
_ => return Err("set_cmd args must be list or text".into()),
|
||||
}
|
||||
}
|
||||
ctx.foreign_cmds.insert(lang, argv);
|
||||
Ok(Value::None)
|
||||
}
|
||||
|
||||
pub fn run(
|
||||
ctx: &mut HostContext,
|
||||
code: &Value,
|
||||
stdin: Option<&Value>,
|
||||
) -> Result<Value, String> {
|
||||
let block = as_code(code)?;
|
||||
let stdin = opt_text(stdin)?;
|
||||
run_source(ctx, &block.lang, &block.source, stdin)
|
||||
}
|
||||
|
||||
pub fn run_lang(
|
||||
ctx: &mut HostContext,
|
||||
lang: &Value,
|
||||
source: &Value,
|
||||
stdin: Option<&Value>,
|
||||
) -> Result<Value, String> {
|
||||
let lang = as_text(lang, "lang")?;
|
||||
let source = as_text(source, "source")?;
|
||||
let stdin = opt_text(stdin)?;
|
||||
run_source(ctx, lang, source, stdin)
|
||||
}
|
||||
|
||||
pub fn langs(ctx: &HostContext) -> Result<Value, String> {
|
||||
let mut names: Vec<String> = ctx.foreign_cmds.keys().cloned().collect();
|
||||
if !names.iter().any(|n| n == "python" || n == "python3") {
|
||||
names.push("python".into());
|
||||
}
|
||||
names.sort();
|
||||
names.dedup();
|
||||
Ok(Value::List(names.into_iter().map(Value::Text).collect()))
|
||||
}
|
||||
|
||||
/// Shared entry for CLI host and live view `/api/foreign-run`.
|
||||
pub fn run_source(
|
||||
ctx: &HostContext,
|
||||
lang: &str,
|
||||
source: &str,
|
||||
stdin: Option<&str>,
|
||||
) -> Result<Value, String> {
|
||||
let lang_key = lang.to_ascii_lowercase();
|
||||
let argv = resolve_argv(ctx, &lang_key)?;
|
||||
let script_path = write_temp_script(&lang_key, source)?;
|
||||
let result = run_argv(ctx, &argv, &script_path, stdin, &lang_key);
|
||||
let _ = std::fs::remove_file(&script_path);
|
||||
result
|
||||
}
|
||||
|
||||
/// Resolve argv with optional one-shot cmd override (view command box).
|
||||
pub fn resolve_argv_with_override(
|
||||
ctx: &HostContext,
|
||||
lang: &str,
|
||||
cmd_override: Option<&str>,
|
||||
) -> Result<Vec<String>, String> {
|
||||
if let Some(cmd) = cmd_override.map(str::trim).filter(|s| !s.is_empty()) {
|
||||
return Ok(split_cmd_line(cmd));
|
||||
}
|
||||
resolve_argv(ctx, lang)
|
||||
}
|
||||
|
||||
fn resolve_argv(ctx: &HostContext, lang: &str) -> Result<Vec<String>, String> {
|
||||
if let Some(argv) = ctx.foreign_cmds.get(lang) {
|
||||
return Ok(argv.clone());
|
||||
}
|
||||
if lang == "python3" {
|
||||
if let Some(argv) = ctx.foreign_cmds.get("python") {
|
||||
return Ok(argv.clone());
|
||||
}
|
||||
}
|
||||
if lang == "python" {
|
||||
if let Some(argv) = ctx.foreign_cmds.get("python3") {
|
||||
return Ok(argv.clone());
|
||||
}
|
||||
}
|
||||
|
||||
let env_key = format!("MARQDO_FOREIGN_{}", lang.to_ascii_uppercase());
|
||||
if let Ok(v) = std::env::var(&env_key) {
|
||||
let v = v.trim();
|
||||
if !v.is_empty() {
|
||||
return Ok(split_cmd_line(v));
|
||||
}
|
||||
}
|
||||
if lang == "python" || lang == "python3" || lang == "py" {
|
||||
if let Ok(v) = std::env::var("MARQDO_FOREIGN_PYTHON") {
|
||||
let v = v.trim();
|
||||
if !v.is_empty() {
|
||||
return Ok(split_cmd_line(v));
|
||||
}
|
||||
}
|
||||
}
|
||||
if let Some(argv) = foreign::default_argv(lang) {
|
||||
return Ok(argv);
|
||||
}
|
||||
|
||||
Err(format!(
|
||||
"no interpreter configured for lang=`{lang}` — set_cmd lang={lang} cmd=… \
|
||||
or env {env_key} (check your environment)"
|
||||
))
|
||||
}
|
||||
|
||||
fn split_cmd_line(s: &str) -> Vec<String> {
|
||||
s.split_whitespace().map(|p| p.to_string()).collect()
|
||||
}
|
||||
|
||||
fn write_temp_script(lang: &str, source: &str) -> Result<PathBuf, String> {
|
||||
let ext = match lang {
|
||||
"python" | "python3" | "py" => "py",
|
||||
"javascript" | "js" | "node" => "js",
|
||||
other => {
|
||||
if other.len() <= 8 && other.chars().all(|c| c.is_ascii_alphanumeric()) {
|
||||
other
|
||||
} else {
|
||||
"txt"
|
||||
}
|
||||
}
|
||||
};
|
||||
let body = if matches!(lang, "python" | "python3" | "py") {
|
||||
format!("# -*- coding: utf-8 -*-\n{source}")
|
||||
} else {
|
||||
source.to_string()
|
||||
};
|
||||
let name = format!(
|
||||
"marqdo-foreign-{}-{}.{}",
|
||||
std::process::id(),
|
||||
SystemTime::now()
|
||||
.duration_since(UNIX_EPOCH)
|
||||
.map(|d| d.as_nanos())
|
||||
.unwrap_or(0),
|
||||
ext
|
||||
);
|
||||
let path = std::env::temp_dir().join(name);
|
||||
std::fs::write(&path, body.as_bytes())
|
||||
.map_err(|e| format!("failed to write temp foreign script: {e}"))?;
|
||||
Ok(path)
|
||||
}
|
||||
|
||||
fn run_argv(
|
||||
ctx: &HostContext,
|
||||
argv: &[String],
|
||||
script: &Path,
|
||||
stdin: Option<&str>,
|
||||
lang: &str,
|
||||
) -> Result<Value, String> {
|
||||
if argv.is_empty() {
|
||||
return Err("foreign cmd argv is empty — check set_cmd / env".into());
|
||||
}
|
||||
let mut command = Command::new(&argv[0]);
|
||||
for a in &argv[1..] {
|
||||
command.arg(a);
|
||||
}
|
||||
command.arg(script);
|
||||
command.current_dir(&ctx.cwd);
|
||||
command.stdin(Stdio::piped());
|
||||
command.stdout(Stdio::piped());
|
||||
command.stderr(Stdio::piped());
|
||||
command.env("PYTHONIOENCODING", "utf-8");
|
||||
command.env("PYTHONUTF8", "1");
|
||||
|
||||
let mut child = command.spawn().map_err(|e| {
|
||||
format!(
|
||||
"foreign run failed (lang={lang}, cmd={}): {e} — check your interpreter / \
|
||||
MARQDO_FOREIGN_* / set_cmd",
|
||||
argv.join(" ")
|
||||
)
|
||||
})?;
|
||||
|
||||
if let Some(data) = stdin {
|
||||
if let Some(mut pipe) = child.stdin.take() {
|
||||
let _ = pipe.write_all(data.as_bytes());
|
||||
}
|
||||
} else {
|
||||
drop(child.stdin.take());
|
||||
}
|
||||
|
||||
let mut stdout_pipe = child.stdout.take();
|
||||
let mut stderr_pipe = child.stderr.take();
|
||||
let t_out = thread::spawn(move || {
|
||||
let mut buf = Vec::new();
|
||||
if let Some(ref mut r) = stdout_pipe {
|
||||
let _ = r.read_to_end(&mut buf);
|
||||
}
|
||||
String::from_utf8_lossy(&buf).into_owned()
|
||||
});
|
||||
let t_err = thread::spawn(move || {
|
||||
let mut buf = Vec::new();
|
||||
if let Some(ref mut r) = stderr_pipe {
|
||||
let _ = r.read_to_end(&mut buf);
|
||||
}
|
||||
String::from_utf8_lossy(&buf).into_owned()
|
||||
});
|
||||
|
||||
let start = Instant::now();
|
||||
let status = loop {
|
||||
match child.try_wait() {
|
||||
Ok(Some(st)) => break st,
|
||||
Ok(None) if start.elapsed() > DEFAULT_TIMEOUT => {
|
||||
let _ = child.kill();
|
||||
let _ = child.wait();
|
||||
return Err(format!(
|
||||
"foreign run timed out after {}s (lang={lang}, cmd={}) — check your script / environment",
|
||||
DEFAULT_TIMEOUT.as_secs(),
|
||||
argv.join(" ")
|
||||
));
|
||||
}
|
||||
Ok(None) => thread::sleep(Duration::from_millis(20)),
|
||||
Err(e) => {
|
||||
return Err(format!(
|
||||
"foreign run failed (lang={lang}, cmd={}): {e}",
|
||||
argv.join(" ")
|
||||
));
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
let stdout = t_out.join().unwrap_or_default();
|
||||
let stderr = t_err.join().unwrap_or_default();
|
||||
if stdout.len() > MAX_OUTPUT || stderr.len() > MAX_OUTPUT {
|
||||
return Err(format!(
|
||||
"foreign output too large (lang={lang}) — truncate script output"
|
||||
));
|
||||
}
|
||||
|
||||
if !status.success() {
|
||||
let code = status.code().unwrap_or(1);
|
||||
let mut msg = format!(
|
||||
"foreign run failed (lang={lang}, cmd={}, exit={code})",
|
||||
argv.join(" ")
|
||||
);
|
||||
if !stderr.trim().is_empty() {
|
||||
msg.push_str(": ");
|
||||
msg.push_str(stderr.trim());
|
||||
}
|
||||
msg.push_str(" — check your interpreter / MARQDO_FOREIGN_* / set_cmd / script");
|
||||
return Err(msg);
|
||||
}
|
||||
|
||||
let mut out = stdout;
|
||||
if out.ends_with('\n') {
|
||||
out.pop();
|
||||
if out.ends_with('\r') {
|
||||
out.pop();
|
||||
}
|
||||
}
|
||||
Ok(Value::Text(out))
|
||||
}
|
||||
|
||||
/// View/API helper: run with optional command override string.
|
||||
pub fn run_with_cmd_override(
|
||||
cwd: &Path,
|
||||
lang: &str,
|
||||
source: &str,
|
||||
cmd_override: Option<&str>,
|
||||
) -> Result<String, String> {
|
||||
let mut ctx = HostContext::default();
|
||||
ctx.cwd = cwd.to_path_buf();
|
||||
let argv = resolve_argv_with_override(&ctx, lang, cmd_override)?;
|
||||
let script_path = write_temp_script(&lang.to_ascii_lowercase(), source)?;
|
||||
let result = run_argv(&ctx, &argv, &script_path, None, lang);
|
||||
let _ = std::fs::remove_file(&script_path);
|
||||
match result {
|
||||
Ok(Value::Text(s)) => Ok(s),
|
||||
Ok(v) => Ok(v.as_display()),
|
||||
Err(e) => Err(e),
|
||||
}
|
||||
}
|
||||
@@ -108,5 +108,6 @@ fn value_to_json(v: &Value) -> Result<serde_json::Value, String> {
|
||||
serde_json::Value::Object(map)
|
||||
}
|
||||
Value::Formula(e) => serde_json::Value::String(e.as_display()),
|
||||
Value::Code(c) => serde_json::Value::String(c.source.clone()),
|
||||
})
|
||||
}
|
||||
@@ -5,6 +5,7 @@
|
||||
//! Soft side-effects (no process::exit; sleep clamp) apply under view / capture only.
|
||||
|
||||
mod dispatch;
|
||||
pub mod foreign;
|
||||
mod fs;
|
||||
mod json;
|
||||
pub mod math;
|
||||
@@ -15,6 +16,7 @@ mod time;
|
||||
pub use dispatch::{call_host, HostFn};
|
||||
pub use fs::path_under_root;
|
||||
|
||||
use std::collections::HashMap;
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
/// Host capability flags. Default: all enabled.
|
||||
@@ -55,6 +57,8 @@ pub struct HostContext {
|
||||
/// LCG state for `random` / `random_int`.
|
||||
pub rng: u64,
|
||||
pub plots: Vec<PlotArtifact>,
|
||||
/// lang → interpreter argv (from `set_cmd`).
|
||||
pub foreign_cmds: HashMap<String, Vec<String>>,
|
||||
}
|
||||
|
||||
impl Default for HostContext {
|
||||
@@ -68,6 +72,7 @@ impl Default for HostContext {
|
||||
sleep_limit_ms: Some(30_000),
|
||||
rng: 0xC0FF_EE42_CAFE_BABE,
|
||||
plots: Vec::new(),
|
||||
foreign_cmds: HashMap::new(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -395,6 +395,7 @@ impl Interpreter {
|
||||
Ok(Value::List(out))
|
||||
}
|
||||
Expr::Formula(e) => Ok(Value::Formula(e.clone())),
|
||||
Expr::Code(c) => Ok(Value::Code(c.clone())),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -73,6 +73,7 @@ pub fn classify_source(source: &str) -> Vec<ClassifiedLine> {
|
||||
let mut out = Vec::with_capacity(lines.len());
|
||||
let mut in_comment_paragraph = false;
|
||||
let mut in_math_fence = false;
|
||||
let mut in_md_fence = false;
|
||||
|
||||
for (i, text) in lines.into_iter().enumerate() {
|
||||
let base = classify_line(text);
|
||||
@@ -83,6 +84,16 @@ pub fn classify_source(source: &str) -> Vec<ClassifiedLine> {
|
||||
in_math_fence = false;
|
||||
}
|
||||
LineKind::Code
|
||||
} else if in_md_fence {
|
||||
// Markdown ``` fences are narrative / foreign sources — not Marqdo stmts.
|
||||
if is_md_fence_line(trimmed) {
|
||||
in_md_fence = false;
|
||||
}
|
||||
LineKind::Comment
|
||||
} else if is_md_fence_opener(trimmed) {
|
||||
in_md_fence = true;
|
||||
in_comment_paragraph = false;
|
||||
LineKind::Comment
|
||||
} else {
|
||||
match base {
|
||||
LineKind::Blank => {
|
||||
@@ -116,6 +127,18 @@ pub fn classify_source(source: &str) -> Vec<ClassifiedLine> {
|
||||
out
|
||||
}
|
||||
|
||||
fn is_md_fence_opener(trimmed: &str) -> bool {
|
||||
trimmed.starts_with("```")
|
||||
&& !trimmed
|
||||
.chars()
|
||||
.skip_while(|c| *c == '`')
|
||||
.all(|c| c.is_whitespace())
|
||||
}
|
||||
|
||||
fn is_md_fence_line(trimmed: &str) -> bool {
|
||||
trimmed.starts_with("```")
|
||||
}
|
||||
|
||||
/// Function-end / frontmatter HR or empty bold return — never swallowed by paragraphs.
|
||||
fn is_structural_code_line(trimmed: &str) -> bool {
|
||||
if trimmed.len() >= 3
|
||||
|
||||
@@ -11,6 +11,7 @@ pub mod capture;
|
||||
pub mod catalog;
|
||||
pub mod debug;
|
||||
pub mod diagnostics;
|
||||
pub mod foreign;
|
||||
pub mod formula;
|
||||
pub mod host;
|
||||
pub mod input_feed;
|
||||
|
||||
+66
-1
@@ -288,7 +288,7 @@ impl<'a> Cursor<'a> {
|
||||
}))
|
||||
}
|
||||
|
||||
/// RHS on the same line, or empty RHS → following `$$` fence or table.
|
||||
/// RHS on the same line, or empty RHS → following `$$` / ```lang fence or table.
|
||||
/// Returns `(expr, inclusive end_line)`.
|
||||
fn resolve_assign_rhs(&mut self, rhs: &str, span: Span) -> Result<(Expr, u32)> {
|
||||
let rhs = rhs.trim();
|
||||
@@ -297,6 +297,9 @@ impl<'a> Cursor<'a> {
|
||||
if self.peek_is_formula_fence() {
|
||||
return self.consume_formula_fence(span);
|
||||
}
|
||||
if self.peek_is_code_fence() {
|
||||
return self.consume_code_fence(span);
|
||||
}
|
||||
let start_i = self.i;
|
||||
let list = self.consume_table()?;
|
||||
let end_line = if self.i > start_i {
|
||||
@@ -334,6 +337,50 @@ impl<'a> Cursor<'a> {
|
||||
.unwrap_or(false)
|
||||
}
|
||||
|
||||
fn peek_is_code_fence(&self) -> bool {
|
||||
self.peek()
|
||||
.map(|l| crate::foreign::is_fence_opener(l.text.trim()))
|
||||
.unwrap_or(false)
|
||||
}
|
||||
|
||||
fn consume_code_fence(&mut self, span: Span) -> Result<(Expr, u32)> {
|
||||
let open = self
|
||||
.bump()
|
||||
.ok_or_else(|| anyhow::anyhow!("{span}: expected ```lang code fence"))?;
|
||||
let open_line = open.line_no;
|
||||
let trimmed = open.text.trim();
|
||||
let lang = crate::foreign::fence_lang(trimmed).ok_or_else(|| {
|
||||
anyhow::anyhow!("{open_line}:1: expected ```lang fence after empty assignment")
|
||||
})?;
|
||||
if trimmed.contains("name=") {
|
||||
bail!(
|
||||
"{open_line}:1: `name=` on fences was removed; \
|
||||
use `name` = then a ```{lang} fence (same shape as formulas)"
|
||||
);
|
||||
}
|
||||
let mut body_lines: Vec<String> = Vec::new();
|
||||
let mut closed = false;
|
||||
let mut end_line = open_line;
|
||||
while let Some(l) = self.peek() {
|
||||
if crate::foreign::is_fence_closer(l.text.trim()) {
|
||||
end_line = l.line_no;
|
||||
self.bump();
|
||||
closed = true;
|
||||
break;
|
||||
}
|
||||
body_lines.push(l.text.clone());
|
||||
self.bump();
|
||||
}
|
||||
if !closed {
|
||||
bail!("{open_line}:1: unclosed code fence ```{lang}");
|
||||
}
|
||||
let source = body_lines.join("\n");
|
||||
Ok((
|
||||
Expr::Code(crate::value::CodeBlock { lang, source }),
|
||||
end_line,
|
||||
))
|
||||
}
|
||||
|
||||
fn consume_formula_fence(&mut self, span: Span) -> Result<(Expr, u32)> {
|
||||
let open = self
|
||||
.bump()
|
||||
@@ -725,4 +772,22 @@ mod tests {
|
||||
other => panic!("expected formula assign, got {other:?}"),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_code_assign_fence() {
|
||||
let src = "# main\n\n`hi` =\n```python\nprint(1)\n```\n\n> print text=ok\n";
|
||||
let m = parse_source(src).unwrap();
|
||||
match &m.functions[0].body[0] {
|
||||
Stmt::Assign {
|
||||
name,
|
||||
value: Expr::Code(c),
|
||||
..
|
||||
} => {
|
||||
assert_eq!(name, "hi");
|
||||
assert_eq!(c.lang, "python");
|
||||
assert_eq!(c.source, "print(1)");
|
||||
}
|
||||
other => panic!("expected code assign, got {other:?}"),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,13 @@ use std::fmt;
|
||||
|
||||
use crate::formula::Expr as FormulaExpr;
|
||||
|
||||
/// Bound Markdown code fence (`type` → `code`).
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct CodeBlock {
|
||||
pub lang: String,
|
||||
pub source: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub enum Value {
|
||||
None,
|
||||
@@ -18,6 +25,8 @@ pub enum Value {
|
||||
Map(Vec<(String, Value)>),
|
||||
/// Parsed symbolic expression (`type` → `formula`).
|
||||
Formula(FormulaExpr),
|
||||
/// External language source from ```lang fence (`type` → `code`).
|
||||
Code(CodeBlock),
|
||||
}
|
||||
|
||||
impl Value {
|
||||
@@ -31,6 +40,7 @@ impl Value {
|
||||
Value::List(xs) => !xs.is_empty(),
|
||||
Value::Map(xs) => !xs.is_empty(),
|
||||
Value::Formula(_) => true,
|
||||
Value::Code(c) => !c.source.is_empty(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -48,6 +58,7 @@ impl Value {
|
||||
}
|
||||
Value::Map(_) => "<map>".into(),
|
||||
Value::Formula(e) => e.as_display(),
|
||||
Value::Code(c) => format!("```{}\n{}\n```", c.lang, c.source),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -394,6 +394,87 @@ section.block > h2 {
|
||||
.formula-card .katex-display {
|
||||
margin: 0.2rem 0;
|
||||
}
|
||||
.code-card {
|
||||
padding: 0;
|
||||
overflow: hidden;
|
||||
}
|
||||
.code-head {
|
||||
display: flex;
|
||||
flex-wrap: wrap;
|
||||
align-items: center;
|
||||
gap: 0.45rem 0.65rem;
|
||||
padding: 0.65rem 0.85rem;
|
||||
border-bottom: 1px solid var(--line);
|
||||
background: #fafafa;
|
||||
}
|
||||
.code-lang {
|
||||
font: 0.72rem/1 var(--mono);
|
||||
color: var(--muted);
|
||||
text-transform: lowercase;
|
||||
}
|
||||
.code-actions {
|
||||
margin-left: auto;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 0.4rem;
|
||||
}
|
||||
.code-cmd {
|
||||
min-width: 9rem;
|
||||
max-width: 16rem;
|
||||
padding: 0.35rem 0.55rem;
|
||||
border: 1px solid var(--line);
|
||||
border-radius: 8px;
|
||||
font: 0.78rem/1.2 var(--mono);
|
||||
color: var(--ink);
|
||||
background: #fff;
|
||||
}
|
||||
.code-cmd:focus {
|
||||
outline: 2px solid rgba(29,29,31,0.18);
|
||||
outline-offset: 1px;
|
||||
}
|
||||
.code-run {
|
||||
padding: 0.35rem 0.75rem;
|
||||
border: none;
|
||||
border-radius: 8px;
|
||||
background: var(--ink);
|
||||
color: #fff;
|
||||
font: 0.78rem/1.2 var(--sans);
|
||||
font-weight: 600;
|
||||
cursor: pointer;
|
||||
}
|
||||
.code-run:hover { opacity: 0.9; }
|
||||
.code-run:disabled {
|
||||
opacity: 0.45;
|
||||
cursor: not-allowed;
|
||||
}
|
||||
.code-body {
|
||||
margin: 0;
|
||||
padding: 0.85rem 1rem;
|
||||
overflow: auto;
|
||||
background: #1e1e1e;
|
||||
color: #d4d4d4;
|
||||
font: 0.82rem/1.45 var(--mono);
|
||||
}
|
||||
.code-body code {
|
||||
font: inherit;
|
||||
background: none;
|
||||
padding: 0;
|
||||
color: inherit;
|
||||
}
|
||||
.code-out {
|
||||
margin: 0;
|
||||
padding: 0.75rem 1rem;
|
||||
border-top: 1px solid var(--line);
|
||||
background: #f0f0f2;
|
||||
font: 0.8rem/1.45 var(--mono);
|
||||
white-space: pre-wrap;
|
||||
word-break: break-word;
|
||||
}
|
||||
.code-out.err {
|
||||
background: #fff5f5;
|
||||
color: #9b1c1c;
|
||||
}
|
||||
.code-out:not([hidden]) { display: block; }
|
||||
.plots {
|
||||
display: grid;
|
||||
gap: 1rem;
|
||||
@@ -514,6 +595,7 @@ pub fn layout(title: &str, nav: &str, main: &str) -> String {
|
||||
<link rel="shortcut icon" href="https://s3.cflmy.cn/logo/Logo.ico" type="image/x-icon"/>
|
||||
<link rel="apple-touch-icon" href="https://s3.cflmy.cn/logo/Logo.png"/>
|
||||
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.16.11/dist/katex.min.css" crossorigin="anonymous"/>
|
||||
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/highlight.js@11.9.0/styles/github-dark.min.css" crossorigin="anonymous"/>
|
||||
<style>{css}</style>
|
||||
</head>
|
||||
<body>
|
||||
@@ -548,6 +630,63 @@ pub fn layout(title: &str, nav: &str, main: &str) -> String {
|
||||
<script defer src="https://cdn.jsdelivr.net/npm/katex@0.16.11/dist/katex.min.js" crossorigin="anonymous"></script>
|
||||
<script defer src="https://cdn.jsdelivr.net/npm/katex@0.16.11/dist/contrib/auto-render.min.js" crossorigin="anonymous"
|
||||
onload="renderMathInElement(document.body,{{delimiters:[{{left:'$$',right:'$$',display:true}},{{left:'\\(',right:'\\)',display:false}}],ignoredTags:['script','noscript','style','textarea','pre','code','kbd']}});"></script>
|
||||
<script defer src="https://cdn.jsdelivr.net/npm/highlight.js@11.9.0/highlight.min.js" crossorigin="anonymous"
|
||||
onload="document.querySelectorAll('pre.code-body code').forEach(function(el){{hljs.highlightElement(el);}});"></script>
|
||||
<script>
|
||||
(function () {{
|
||||
function initForeignCards() {{
|
||||
var live = !!window.MARQDO_VIEW_LIVE;
|
||||
document.querySelectorAll(".code-card").forEach(function (card) {{
|
||||
var btn = card.querySelector(".code-run");
|
||||
var cmd = card.querySelector(".code-cmd");
|
||||
var out = card.querySelector(".code-out");
|
||||
var body = card.querySelector(".code-body code");
|
||||
if (!btn || !out || !body) return;
|
||||
if (!live) {{
|
||||
btn.disabled = true;
|
||||
btn.title = "Run is available in live marqdo view";
|
||||
if (cmd) cmd.disabled = true;
|
||||
return;
|
||||
}}
|
||||
btn.addEventListener("click", function () {{
|
||||
btn.disabled = true;
|
||||
out.hidden = false;
|
||||
out.classList.remove("err");
|
||||
out.textContent = "Running…";
|
||||
fetch("/api/foreign-run", {{
|
||||
method: "POST",
|
||||
headers: {{ "Content-Type": "application/json" }},
|
||||
body: JSON.stringify({{
|
||||
lang: card.getAttribute("data-lang") || "python",
|
||||
source: body.textContent,
|
||||
cmd: cmd ? cmd.value : ""
|
||||
}})
|
||||
}})
|
||||
.then(function (r) {{ return r.json(); }})
|
||||
.then(function (data) {{
|
||||
if (data.ok) {{
|
||||
out.classList.remove("err");
|
||||
out.textContent = data.stdout === "" ? "(no stdout)" : data.stdout;
|
||||
}} else {{
|
||||
out.classList.add("err");
|
||||
out.textContent = data.error || "foreign run failed";
|
||||
}}
|
||||
}})
|
||||
.catch(function (e) {{
|
||||
out.classList.add("err");
|
||||
out.textContent = String(e);
|
||||
}})
|
||||
.finally(function () {{ btn.disabled = false; }});
|
||||
}});
|
||||
}});
|
||||
}}
|
||||
if (document.readyState === "loading") {{
|
||||
document.addEventListener("DOMContentLoaded", initForeignCards);
|
||||
}} else {{
|
||||
initForeignCards();
|
||||
}}
|
||||
}})();
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
"##,
|
||||
@@ -789,8 +928,13 @@ pub fn page_file(files: &[PathBuf], rel: &str, vm: &FileViewModel, links: &LinkM
|
||||
s.push_str("</div>");
|
||||
s
|
||||
};
|
||||
let live_flag = match links {
|
||||
LinkMode::Live => "true",
|
||||
LinkMode::Static { .. } => "false",
|
||||
};
|
||||
let main = format!(
|
||||
r#"
|
||||
<script>window.MARQDO_VIEW_LIVE = {live};</script>
|
||||
<h1 class="page">{title}<span class="status-pill {status}">{status_label}</span></h1>
|
||||
<p class="meta">{rel}</p>
|
||||
<section class="block">
|
||||
@@ -808,6 +952,7 @@ pub fn page_file(files: &[PathBuf], rel: &str, vm: &FileViewModel, links: &LinkM
|
||||
<pre class="source">{source}</pre>
|
||||
</section>
|
||||
"#,
|
||||
live = live_flag,
|
||||
title = escape(title),
|
||||
status = status,
|
||||
status_label = status_label,
|
||||
|
||||
@@ -117,6 +117,17 @@ fn is_mq_md(path: &Path) -> bool {
|
||||
fn handle(root: &ViewRoot, request: Request) -> Result<()> {
|
||||
let url = request.url().to_string();
|
||||
let method = request.method().clone();
|
||||
|
||||
if method == Method::Post {
|
||||
let (path_part, _) = split_url(&url);
|
||||
if path_part == "/api/foreign-run" {
|
||||
return api_foreign_run(root, request);
|
||||
}
|
||||
let resp = Response::from_string("method not allowed").with_status_code(StatusCode(405));
|
||||
let _ = request.respond(resp);
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
if method != Method::Get {
|
||||
let resp = Response::from_string("method not allowed").with_status_code(StatusCode(405));
|
||||
let _ = request.respond(resp);
|
||||
@@ -338,6 +349,37 @@ fn escape_json(s: &str) -> String {
|
||||
s.replace('\\', "\\\\").replace('"', "\\\"")
|
||||
}
|
||||
|
||||
fn api_foreign_run(root: &ViewRoot, mut request: Request) -> Result<()> {
|
||||
let mut body = Vec::new();
|
||||
std::io::Read::read_to_end(&mut request.as_reader(), &mut body)?;
|
||||
let body = String::from_utf8_lossy(&body);
|
||||
let v: serde_json::Value = serde_json::from_str(&body).unwrap_or(serde_json::Value::Null);
|
||||
let lang = v
|
||||
.get("lang")
|
||||
.and_then(|x| x.as_str())
|
||||
.unwrap_or("python");
|
||||
let source = v.get("source").and_then(|x| x.as_str()).unwrap_or("");
|
||||
let cmd = v
|
||||
.get("cmd")
|
||||
.and_then(|x| x.as_str())
|
||||
.map(str::trim)
|
||||
.filter(|s| !s.is_empty());
|
||||
let json = if source.is_empty() {
|
||||
serde_json::json!({ "ok": false, "error": "missing source" }).to_string()
|
||||
} else {
|
||||
match crate::host::foreign::run_with_cmd_override(
|
||||
&root.root,
|
||||
lang,
|
||||
source,
|
||||
cmd,
|
||||
) {
|
||||
Ok(stdout) => serde_json::json!({ "ok": true, "stdout": stdout }).to_string(),
|
||||
Err(error) => serde_json::json!({ "ok": false, "error": error }).to_string(),
|
||||
}
|
||||
};
|
||||
respond_json(request, json)
|
||||
}
|
||||
|
||||
fn respond_html(request: Request, body: String) -> Result<()> {
|
||||
let header = Header::from_bytes(&b"Content-Type"[..], &b"text/html; charset=utf-8"[..])
|
||||
.map_err(|_| anyhow::anyhow!("header"))?;
|
||||
|
||||
+41
-1
@@ -108,7 +108,7 @@ fn collect_input_prompts_expr(expr: &Expr, out: &mut Vec<String>) {
|
||||
collect_input_prompts_expr(e, out);
|
||||
}
|
||||
}
|
||||
Expr::Literal(_) | Expr::Var(_) | Expr::Interp(_) | Expr::Formula(_) => {}
|
||||
Expr::Literal(_) | Expr::Var(_) | Expr::Interp(_) | Expr::Formula(_) | Expr::Code(_) => {}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -364,6 +364,34 @@ fn render_stmt(stmt: &Stmt, lines: &[ClassifiedLine]) -> (String, u32) {
|
||||
tex
|
||||
)
|
||||
}
|
||||
Stmt::Assign {
|
||||
name,
|
||||
value: Expr::Code(c),
|
||||
..
|
||||
} => {
|
||||
let placeholder = escape(&crate::foreign::default_cmd_display(&c.lang));
|
||||
let lang = escape(&c.lang);
|
||||
let src = escape(&c.source);
|
||||
format!(
|
||||
r#"<div class="card code-card" data-lang="{lang}">
|
||||
<div class="code-head">
|
||||
<span class="badge">code</span>
|
||||
<code class="expr">`{name}` =</code>
|
||||
<span class="code-lang">{lang}</span>
|
||||
<div class="code-actions">
|
||||
<input type="text" class="code-cmd" placeholder="{placeholder}" spellcheck="false" autocomplete="off"/>
|
||||
<button type="button" class="code-run">Run</button>
|
||||
</div>
|
||||
</div>
|
||||
<pre class="code-body"><code class="language-{lang}">{src}</code></pre>
|
||||
<pre class="code-out" hidden></pre>
|
||||
</div>"#,
|
||||
lang = lang,
|
||||
name = escape(name),
|
||||
placeholder = placeholder,
|
||||
src = src,
|
||||
)
|
||||
}
|
||||
Stmt::Assign { name, value, .. } => format!(
|
||||
"<div class=\"card\"><span class=\"badge\">bind</span> <code class=\"expr\">`{}` = {}</code></div>",
|
||||
escape(name),
|
||||
@@ -519,6 +547,7 @@ fn expr_prec(expr: &Expr, parent_prec: u8) -> String {
|
||||
format!("[{}]", parts.join(", "))
|
||||
}
|
||||
Expr::Formula(e) => format!("$$ {} $$", e.as_display()),
|
||||
Expr::Code(c) => format!("```{} …```", c.lang),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -708,6 +737,17 @@ mod tests {
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn structure_code_card() {
|
||||
let src = include_str!("../../tests/lib/foreign-python.mq.md");
|
||||
let module = crate::parse::parse_source(src).unwrap();
|
||||
let html = render_module_structure(&module, src);
|
||||
assert!(html.contains("code-card"), "{html}");
|
||||
assert!(html.contains("code-run"), "{html}");
|
||||
assert!(html.contains("hello-from-python"), "{html}");
|
||||
assert!(!html.contains("python name=hi"), "{html}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn formula_ascii_to_tex_pow_and_mul() {
|
||||
assert_eq!(formula_ascii_to_tex("x^2 - 2"), "x^{2} - 2");
|
||||
|
||||
@@ -627,6 +627,47 @@ fn lib_math_plot() {
|
||||
let _ = std::fs::remove_file("tests/lib/math-plot-plot-1.svg");
|
||||
}
|
||||
|
||||
fn python_available() -> bool {
|
||||
for cmd in ["python", "python3"] {
|
||||
if let Ok(out) = Command::new(cmd)
|
||||
.args(["-c", "print(1)"])
|
||||
.output()
|
||||
{
|
||||
if out.status.success() {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
false
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn lib_foreign_python() {
|
||||
if !python_available() {
|
||||
eprintln!("skip lib_foreign_python: no python on PATH");
|
||||
return;
|
||||
}
|
||||
assert_out("tests/lib/foreign-python.mq.md", "hello-from-python");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn lib_foreign_run_lang() {
|
||||
if !python_available() {
|
||||
eprintln!("skip lib_foreign_run_lang: no python on PATH");
|
||||
return;
|
||||
}
|
||||
assert_out("tests/lib/foreign-run-lang.mq.md", "4");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn lib_foreign_zh() {
|
||||
if !python_available() {
|
||||
eprintln!("skip lib_foreign_zh: no python on PATH");
|
||||
return;
|
||||
}
|
||||
assert_out("tests/lib/外联-python.mq.md", "中文外联");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn bytecode_lib_math_formula() {
|
||||
assert_out_backend(
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
---
|
||||
title: foreign python hello
|
||||
> lib/foreign.mq.md
|
||||
---
|
||||
|
||||
# main
|
||||
|
||||
`hi` =
|
||||
```python
|
||||
print("hello-from-python")
|
||||
```
|
||||
|
||||
*`out` = > run code=`hi` *
|
||||
|
||||
> print text=`out`
|
||||
@@ -0,0 +1,10 @@
|
||||
---
|
||||
title: foreign run_lang
|
||||
> lib/foreign.mq.md
|
||||
---
|
||||
|
||||
# main
|
||||
|
||||
*`out` = > run_lang lang=python source=print(2+2) *
|
||||
|
||||
> print text=`out`
|
||||
@@ -0,0 +1,15 @@
|
||||
---
|
||||
title: foreign zh
|
||||
> lib/外联.mq.md
|
||||
---
|
||||
|
||||
# main
|
||||
|
||||
`hi` =
|
||||
```python
|
||||
print("中文外联")
|
||||
```
|
||||
|
||||
*`out` = > 运行 代码=`hi` *
|
||||
|
||||
> print text=`out`
|
||||
Reference in new issue
Block a user