Basic Front Matter Editing Example

Synopsis

Example program that performs the following actions:

  1. Reads a Jinja2 template

    {% set toc = "true" %}
    
    toc: {{ toc }}
    draft: {{ false | canPublish }}
    hasMath: {{ hasMath }}
    stuff: {{ addedVariable }}
    
  2. Reads a mardown file that contains yaml front matter via editfrontmatter.EditFrontMatter.readFile()

    ---
    title: "EditFrontMatter Class Example 1"
    description: "Edit some 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
    
  3. Extracts the front matter from the source file

  4. Prorammitically edits the front matter via editfrontmatter.EditFrontMatter.run()

  5. Concatinates the edited front matter with the original file content via editfrontmatter.EditFrontMatter.dumpFileData()

  6. Prints the edited file to stdout

Dependencies
editfrontmatter>=0.0.1
Jinja2>=2.10.1
MarkupSafe>=1.1.1
oyaml>=0.9
PyYAML>=5.1
Platform

Unix, Windows, python >=v3.5.3

License

MIT

Author

Karl N. Redman

homepage

EditFrontMatter Example 1

Current Release

version: 0.0.1

New in version 0.0.1: Initial Version

canPublish_func(val)[source]
Description

Example callback function used to populate a jinja2 variable. Note that this function must be reentrant for threaded applications.

Parameters

val (object) – generic object for the example

Returns

example content (True)

main()[source]
Description

Basic example function for processing yaml with a jinja template and a markdown file with yaml content.

Envvar

TEST_DATA_DIR Variable used to specify the path to the data files.

Variables
  • DATA_PATH (str) – generic path if running from the local example1 directory

  • file_path (str) – path of data (markdown) file to be read

  • template_str (str) – string containing the contents of a Jinja2 template

  • proc (EditFrontMatter) – the EditFrontmatter object

Returns

program exit status (0 or 1)

Example

To run from a non program directory use the environment variable:

TEST_DATA_DIR="./data/" example1/example1.py