用于标记的可扩展的、与解析器无关的“include”指令。
markdata的Python项目详细描述
markdata:把数据放在标记之外!
markdata是一个python库和命令行工具,用于管理标记文件中的数据(如代码、图表、表等)。其目标是推广一个简单的规则:散文和非散文补充(统称为"数据")应尽可能分开管理。这种理念的好处包括:
遵守干燥原则:多个文件可以包含来自同一来源的数据。
更高质量的数据:可以测试代码示例,可以直接从序列化格式生成表,可以从源代码构建图表,等等。
人性化标记:更少的非散文内容意味着更短的文件,更易于更新和维护。
有关详细信息,请参见使用python修复markdown的缺点。
安装
< Buff行情>:感叹号:markdata需要python>;=3.6.0。:感叹号:
$ pip install markdata
用法
作为库
importmarkdatadefmy_directive():passmarkdown=markdata.markdata(# A string or file-like object.file_or_str,# A dictionary of custom directives (optional).## Format: {'name': my_directive}.directives={},# The type of front matter to parse (optional).fm_format=None,# 'JSON', 'YAML', or 'TOML'# The directory that paths will be resolved relative to (optional).root=None)
从命令行
$ markdata --help
Usage: markdata [OPTIONS] SOURCE [DESTINATION] A flavor-agnostic extension framework for Markdown. Reads from <SOURCE> and writes to <DESTINATION>. If <SOURCE> is a single file, it will be converted to Markdown and written to <DESTINATION> (default: stdout). If <SOURCE> is a directory, all child Markdata files will be converted to Markdown and written to <DESTINATION> (default: overwrite <SOURCE>).Options: --fm-type [JSON|YAML|TOML] The type of front matter to parse. --root PATH The directory that paths will be resolved relative to. --directives PATH The directory containing your custom directives. --version Show the version and exit. --help Show this message and exit.
指令
$ markdata --help Usage: markdata [OPTIONS] SOURCE [DESTINATION] A flavor-agnostic extension framework for Markdown. Reads from <SOURCE> and writes to <DESTINATION>. If <SOURCE> is a single file, it will be converted to Markdown and written to <DESTINATION> (default: stdout). If <SOURCE> is a directory, all child Markdata files will be converted to Markdown and written to <DESTINATION> (default: overwrite <SOURCE>).Options: --fm-type [JSON|YAML|TOML] The type of front matter to parse. --root PATH The directory that paths will be resolved relative to. --directives PATH The directory containing your custom directives. --version Show the version and exit. --help Show this message and exit.
指令
markdata的功能由指令驱动,这些指令是调用python函数的标记片段。指令可以通过两种方式定义:
# Example directives <!-- This is a "block" directive --> ```callout{'title': 'NOTE', 'classes': ['warning']} This is a callout message. ``` <!-- This is an "inline" directive --> `table{'path': '../data/table.yml', 'classes': ['table'], 'caption': 'My data'}`
table
指令从yaml、json或csv文件创建一个html表(带有可选的标题和类)。在此文件上调用markdata
后,输出将是如下行的内容:
<tableclass="table"><caption>My data</caption><thead><!-- headers --></thead><tbody><!-- rows --></tbody></table>
内置指令
markdata有两个内置的通用指令:
deftable(path:str,classes:List[str]=[],caption:str="")->str:"""Return an HTML table built from structured data (CSV, JSON, or YAML). `path` [required]: A path (relative to the directive-containing file) to a CSV, JSON, or YAML file. `classes` [optional]: A list of HTML classes to apply to the table. `caption` [optional]: A caption for the table. Example: `table{'path': 'table.yml', 'classes': ['table'], 'caption': '...'}` """
表
指令(上面讨论的)从外部数据源(csv、json或yaml)创建html表。这使您可以避免以原始标记或HTML格式编写和维护表。
defdocument(path:str,span:Tuple[int,int]=[])->str:"""Return the contents of a document (or part of it). `path` [required]: A path (relative to the directive-containing file) to a local file. `span` [optional]: A tuple ([begin, end]) indicating the beginning and ending line of the snippet (defaults to the entire file). Example: `document{'path': 'my_file.py', 'span': (10, 13)}` """
文档
指令包含外部文本文件(任何类型的标记、python等)的内容。例如,这允许您在自己的文件中编写代码示例(可以对其进行适当的测试和整理)。
自己写
< Buff行情>注意:传递给自定义指令的第一个参数将是从文件的前端创建的字典(默认情况下,{}
)。
当table
和document
试图解决最常见的需求时,markdata的真正威力来自于在您自己的指令中利用python。
创建指令有三个步骤:
设计指令定义:选择内联或块指令,并决定它将接受哪些参数。
编写一个python函数:这个函数需要接受在步骤(1)中定义的参数。因此,如果您正在实现内置的
表
指令,那么您将拥有表{'path':'../data/table.yml','classes':['table','caption':'my data'}
的指令定义和表的函数定义(fm:dict,path:str,classes:list[str]=[],caption:str=)->;str:
。使用block指令时,传递给后端函数的第一个参数是其内容(请参见我们的警告示例)。将指令与函数关联:
- 使用库:
# When using Markdata as a library, you simply pass your function to `markdata`:importmarkdatadefimplementation(path):passmarkdown=markdata.markdata(file_or_str,directives={'directive':implementation})
- 使用命令行工具:markdata将把所有python模块与它们的
main
函数关联起来,这些函数位于传递给--directives='my_dir'
的目录中
查看我们的测试用例以获取一些示例。
转换器
转换器是可以导入并在自己的指令中使用的实用程序。
# from markdata.converters import to_html_tabledefto_html_table(rows:List[List[str]],caption:str="",classes:List[str]=[])->str:"""Convert the given rows into an HTML table. The first entry in `rows` will be used as the table headers. """
FAQ
< Buff行情>为什么只降价?Asciidoc和RestructuredText呢?
asciidoc和structuredtext比markdown有更多功能丰富的语法。在Asciidoc和RestructuredText中,MarkData的许多功能都是现成的(以某种形式)可用的。
也就是说,与asciidoc和structuredtext相比,markdown有一个更大的语法分析器(实际上每种语言都有一个原生的markdown库)、编辑器(和编辑器插件)、linter和静态站点生成器的生态系统。
markdata的目标是在不影响其可移植性的情况下丰富markdown的语法(参见下一个问题)。
< Buff行情>markdata支持什么样的降价?
所有这些都
关于降价的一个常见抱怨是,它的许多最佳功能都与特定的"味道"相关(请参见可能与其他降价相关工具不兼容的"味道"。
markdata通过充当比另一个降价实现更多的"预处理器"(即markdata+markdown<;=>;sass+css
)来避免此问题。它的用户可以完全控制它输出的内容,这意味着他们可以选择输出原始的html(有效地完全绕过了口味的问题),或者使用仅限于他们最喜欢的口味的功能:
如果我已经在使用静态站点生成器(ssg)呢?
虽然许多SSG都内置了对外部数据源的支持(例如,jekyll,jekyll,hugo,以及gatsby,但使用m仍然有好处。ARKDATA: < /P>
如前一个问题所讨论的,markdata不引入新的标记语法,这意味着markdata指令可以与任何支持标记的ssg一起使用。这意味着您可以更改SSG,而无需更新访问数据的语法(即模板语言)。
markdata管理da的能力ta比大多数ssg提供的功能强大得多:您不仅可以迭代静态资源文件,还可以完全访问python及其库生态系统。例如,您可以
- 使用超流行的请求从api获取数据library;
- 包括用seaborn创建的绘图和图表
- 使用熊猫转换数据