将代码片段插入到标记文档中
markplates的Python项目详细描述
标记牌
A templating utility for keeping code included in Markdown documents in sync with the original source.
我希望解决的问题是简化外部文件与包含它们的标记文档的更新。当一个编辑向一篇文章提出修改它引用的底层代码的建议时,这种情况经常发生在我身上。
安装
您可以使用以下命令从PYPI下载并安装最新版本的标记板:
$ pip install --upgrade markplates
markplates目前是针对python 3.6和python3.7进行测试的。
用法
运行markplates
就像给它一个文件一样简单:
$ markplates template.mdt
这将在template.mdt
中处理模板,用模板中指定的数据填充它。
examples
目录具有simple.mdt
模板:
# Sample MarkPlates Template {{ set_path("./examples") }} This is an example of importing an entire file (minus the first line): {{ import_source("testfile.py") }} While this silly example imports some of the lines from the file, demonstrating ranges: {{ import_source("testfile.py", [5, "2", 3, "8-$", ]) }} {{ import_repl( """ def func(x): if x: print(x) func(True) func(False) """) }}
这演示了设置路径和拉入文件的某些行。您还可以检查此库中用于创建此README.md
的README.mdt
文件。
要在您自己的项目中使用,请创建带有特殊标记的标记文档,以指示markplates
函数调用。这些标记的分隔符是{{
这里是}}
函数。
Note: if you need to add
{{
characters which should not be processed as a template, you can put them in a{{ '' }}
block to escape them. Template processing is done withJinja2
so Markplates uses the same escape sequences.
Markplates
支持以下功能:
set_path("path/to/source/files")
import_source("source_file_name", [list of line number ranges])
import_function("source_file_name", "function_name")
import_repl("code to run in repl")
set_path()
set_path()
函数允许您指定搜索源文件时要使用的基本目录。对该函数的每次调用都将从模板中的该点开始应用。
路径必须包含在单或双qout中。如果未指定,则路径默认为“.”,即当前目录。
示例:
{{set_path(".")}} #sets path to the default
{{set_path("/home/user/my/project")}} # points the path to your project
不需要set_path()
命令,因为所有其他函数都将采用文件的完整路径。
import_source()
import_source()
函数将拉入源文件的内容。可以指定可选的行号范围(请参阅下面的说明)。文件名必须用引号括起来。
如果未指定行号范围,则将忽略文件的第一行。这是为了防止#!/usr/bin/env python
行弄乱降价文章。如果要包含第一行,请使用范围:1-$
。
示例:
{{import_source("__main__.py")}} # includes all but line 1 from `__main__.py` file
{{import_source("__main__.py", ["1-$",])}} # imports all lines from `__main__.py`
{{import_source("__main__.py", [1, "3", "5-$"])}} # imports all lines except lines 2 and 4 from `__main__.py`
^如果找不到文件,{stderr
显示错误消息。
import_function()
inport_function
函数将搜索源文件并仅包含指定的函数。如果源文件中有多个同名函数,则只包含第一个(无论如何,您不应该有多个同名函数!).
函数后面的空白将不包括在内。
示例:
{{import_function("__main__.py", "condense_ranges")}} # imports the function named `condense_ranges` from `__main__.py`
MarkPlates
处理嵌套函数,包括嵌套在指定函数中的任何函数。
import_repl()
import_repl
函数接受输入参数并将其拆分成单独的行。输入的每一行都将在repl中运行,并捕获stdout
和stderr
以插入到最终输出中。结果应该类似于手动运行相同命令的复制粘贴。
不过,也有例外。空白输入行用于间隔,并且不会显示通常在repl中看到的>>>
提示。
示例:
{{import_repl(
"""
def func(x):
if x:
print(x)
func(True)
func(False) """) }}
输出:
>>> def func(x):
... if x:
... print(x)
>>> func(True)
True
>>> func(False)
行号范围
行号范围允许您指定要从源文件中包括哪些行。范围可以采用以下形式:
3或“3”:整数仅将输入的那一行相加
“5-7”:一个范围在开始和结束之间添加行(因此5、6、7)
“10-$”:无限制的范围包括从起始行到文件结尾的距离
Note: LINE NUMBERING STARTS AT 1!
即将推出的功能
我想补充:
- 捕获shell命令的结果并插入文件
- 复制结果Ant标记文件到剪贴板
- 在包含的python源代码上运行
black
- Windows和Mac测试/支持
有兴趣吗?
告诉我!如果你对结果感兴趣或想帮忙,请提出问题,我会与你联系的!
发布历史记录
1.1.0增加了
import_repl
功能1.0.0初始版本
感谢ACME License Maker提供的车牌图像