3 个不稳定版本
0.2.1 | 2024 年 6 月 25 日 |
---|---|
0.2.0 | 2023 年 10 月 7 日 |
0.1.0 | 2023 年 10 月 4 日 |
在 文本处理 中排名第 332
36KB
761 行代码(不含注释)
mdbook-ocirun
这是一个为 rust-lang mdBook 项目提供的预处理器。它允许在容器内运行任意命令和代码片段,并将它们的输出包含在 Markdown 文件中。
入门指南
cargo install mdbook-ocirun
您还需要激活预处理器,在您的 book.toml
文件中添加以下内容:
[preprocessor.ocirun]
运行任意命令
假设我们有两个文件
Markdown 文件:file.md
# Title
<!-- ocirun alpine seq 1 10 -->
<!-- ocirun python python script.py -->
Python 文件:script.py
def main():
print("## Generated subtitle")
print(" This comes from the script.py file")
print(" Since I'm in a scripting language,")
print(" I can compute whatever I want")
if __name__ == "__main__":
main()
预处理器将调用 seq 然后 python3,并生成结果文件
# Title
1
2
3
4
5
6
7
8
9
10
## Generated subtitle
This comes from the script.py file
Since I'm in a scripting language,
I can compute whatever I want
详细信息
当遇到模式 <!-- ocirun <image> $1 -->\n
或 <!-- ocirun <image> $1 -->
时,将使用容器运行 $1
命令,如下所示:docker run <image> $1
。工作目录是找到模式的目录(不是根目录)。调用的命令不能接受任何输入(stdin 不使用),但必须以命令行参数的形式提供,并必须在 stdout 中生成输出,stderr 被忽略。
示例
以下内容是有效的
<!-- ocirun python python generate_table.py -->
```rust
<!-- ocirun alpine cat program.rs -->
```
```diff
<!-- ocirun alpine diff a.rs b.rs -->
```
```console
<!-- ocirun alpine ls -l . -->
```
## Example of inline use inside a table
````markdown
Item | Price | # In stock
---|---|---
Juicy Apples | <!-- ocirun node node price.mjs apples --> | *<!-- ocirun node node quantity.mjs apples -->*
Bananas | *<!-- ocirun node node price.mjs bananas -->* | <!-- ocirun node node quantity.mjs bananas -->
渲染结果如下
Item | Price | # In stock
---|---|---
Juicy Apples | 1.99 | *7*
Bananas | *1.89* | 5234
还实现了更多示例,并用作回归测试。您可以在 这里 找到它们。在撰写本文时,示例使用了以下语言:
- Shell
- Bash 脚本
- 批处理脚本
- Python3
- Node
- Rust
运行代码片段
首先您需要配置如何运行您的代码片段,以下是一个Python的示例。
[[preprocessor.ocirun.langs]]
name = "python"
image = "python"
command = ["python", "source"]
所以当我们找到一个代码片段时
```python,ocirun
print('Hello World')
```
我们将添加输出
```console,success
Hello World
```
最终结果应该像这样
print('Hello World')
Hello World
贡献者
我想感谢 @FauconFan 对他的工作,即 mdbook-cmdrun,这是这个项目的起点。
当前版本:0.2.0
许可证:MIT
依赖项
~12–23MB
~338K SLoC