用Jinja2模板编辑头条
editfrontmatter的Python项目详细描述
批量编辑yaml前端内容
使用jinja2模板编辑前端标记文件。
说明:
这是一个python3
(v3.5.3+)类/模块,可用于批处理和
在降价文件中编辑头条。前台通过
Jinja2模板处理器。
特点:
- 添加/更新/删除任何重要字段
- 通过oyaml
- 使用Jinja2模板进行配置和处理:
- 使用Jinja2筛选器进行复杂处理
- 使用多个模板增量处理文件
- 在代码的敏感区域使用异常
- 实施示例:
- 用Sphinx和hosted on Github记录
文档:
安装:
pip install editfrontmatter
tl;dr示例用法:
- 带有yaml前端的原始标记文件(example1.md)
--- title: "EditFrontMatter Class Example 1" description: "Edit 2 fields in this front matter" catagories: [programming, python, markdown] deleteme: this will be deleted tags: [front matter, administration, testing] # comments and spaces will be eliminated (see docs) author: "Karl N. Redman" creatordisplayname: "Karl N. Redman" creatoremail: "karl.redman@example.com" date: 2019-05-23T17:43:45-05:00 lastmodifierdisplayname: "Karl N. Redman" lastmodifieremail: "karl.redman@gmail.com" lastmod: 2019-05-23T17:43:45-05:00 toc: false type: "page" hasMath: false draft: false weight: 5 --- # EditFontMatter Class Example 1 Edit several fields of front matter. ## Fields affected in this example: * toc * note: uses local template variable * pre: false * post: true * draft: * note: uses jinja2 filter (callback) * pre: false * post: true * hasMath * note: uses program variable * pre: true * post: false * stuff: * note: uses program variable to create field * pre: did not exist * post: (list) ['one', 'two', 'three'] * deleteme: * note: removed from final result * pre: this will be deleted * post: N/A
- jinja2模板,用于更新源标记文件(template1.j2)的前端数据
{%settoc="true"%}toc: {{toc}}draft: {{false|canPublish}}hasMath: {{hasMath}}stuff: {{addedVariable}}
- 用jinja2模板编辑标记文件的python程序(example1.py)
#!/usr/bin/env python3# -*- coding: utf-8 -*-fromeditfrontmatterimportEditFrontMatterimportosdefcanPublish_func(val):# do some processing....returnTruedefmain():# generic path - overridden by env var `TEST_DATA_DIR`DATA_PATH="../data/"if"TEST_DATA_DIR"inos.environ:DATA_PATH=os.path.abspath(os.environ.get("TEST_DATA_DIR"))+"/"# set path to input filefile_path=os.path.abspath(DATA_PATH+"example1.md")# initialize `template_str` with template file contenttemplate_str=''.join(open(os.path.abspath(DATA_PATH+"template1.j2"),"r").readlines())print(template_str)# instantiate the processorproc=EditFrontMatter(file_path=file_path,template_str=template_str)# set fields to delete from yamlproc.keys_toDelete=['deleteme']# add a filter and callback functionproc.add_JinjaFilter('canPublish',canPublish_func)# populate variables and run processorproc.run({'toc':'no effect','hasMath':"false",'addedVariable':['one','two','three']})# dump fileprint(proc.dumpFileData())if__name__=='__main__':main()
- 最终输出:
--- title: EditFrontMatter Class Example 1 description: Edit 2 fields in this front matter catagories: - programming - python - markdown tags: - front matter - administration - testing author: Karl N. Redman creatordisplayname: Karl N. Redman creatoremail: karl.redman@example.com date: 2019-05-23 22:43:45 lastmodifierdisplayname: Karl N. Redman lastmodifieremail: karl.redman@gmail.com lastmod: 2019-05-23 22:43:45 toc: true type: page hasMath: false draft: true weight: 5 stuff: - one - two - three --- # EditFontMatter Class Example 1 Edit several fields of front matter. ## Fields affected in this example: * toc * note: uses local template variable * pre: false * post: true * draft: * note: uses jinja2 filter (callback) * pre: false * post: true * hasMath * note: uses program variable * pre: true * post: false * stuff: * note: uses program variable to create field * pre: did not exist * post: (list) ['one', 'two', 'three'] * deleteme: * note: removed from final result * pre: this will be deleted * post: N/A