用于标记的可扩展的、与解析器无关的“include”指令。

markdata的Python项目详细描述


markdata:把数据放在标记之外!

构建状态wheelscode style

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的功能由指令驱动,这些指令是调用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行情>

注意:传递给自定义指令的第一个参数将是从文件的前端创建的字典(默认情况下,{})。

tabledocument试图解决最常见的需求时,markdata的真正威力来自于在您自己的指令中利用python。

创建指令有三个步骤:

  1. 设计指令定义:选择内联或块指令,并决定它将接受哪些参数。

  2. 编写一个python函数:这个函数需要接受在步骤(1)中定义的参数。因此,如果您正在实现内置的指令,那么您将拥有表{'path':'../data/table.yml','classes':['table','caption':'my data'}的指令定义和表的函数定义(fm:dict,path:str,classes:list[str]=[],caption:str=)->;str:。使用block指令时,传递给后端函数的第一个参数是其内容(请参见我们的警告示例)。

  3. 将指令与函数关联:

    • 使用库:
    # 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(有效地完全绕过了口味的问题),或者使用仅限于他们最喜欢的口味的功能:

< Buff行情>

如果我已经在使用静态站点生成器(ssg)呢?

虽然许多SSG都内置了对外部数据源的支持(例如,jekyll,jekyllhugo,以及gatsby,但使用m仍然有好处。ARKDATA: < /P>

  • 如前一个问题所讨论的,markdata不引入新的标记语法,这意味着markdata指令可以与任何支持标记的ssg一起使用。这意味着您可以更改SSG,而无需更新访问数据的语法(即模板语言)。

  • markdata管理da的能力ta比大多数ssg提供的功能强大得多:您不仅可以迭代静态资源文件,还可以完全访问python及其库生态系统。例如,您可以

    • 使用超流行的请求从api获取数据library;
    • 包括用seaborn创建的绘图和图表
    • 使用熊猫转换数据

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java无法在spring boot应用程序中启用本机内存跟踪   jakarta ee在Java Web项目上的多窗口   日期将Java时间戳转换为MySQL时间戳,反之亦然   java如何实现异步任务连接到服务器并解析JSON   java为什么我得到索引越界异常?   我们如何在java中以大写字母和小写字母存储同名文件   jni/java:有效不可变本机对象的线程安全发布/共享   Java将文本写入远程文件   int最小硬币算法   java如何设置/获取我在类Vehicle的主方法中创建的类Car的“ford”实例的名称?   java使用计时器在队列已满时重新调度使用者   java从字符串的末尾提取一个子字符串,直到遇到第一个空格为止?   java在SimpleApplication之外正确初始化物理状态