Advanced, Multi-Pass, Front Matter Editing Example

Program

example2

Synopsis

Example program that performs the following actions:

  1. Derives a class from EditFrontMatter

  2. Performs execution in a try block

    1. raises an exception form the derived class for example purposes

    2. catches the exception and prints a messae

  3. Sets up a signal handler for SIGHUP and SIGINT for testing/example

  4. Set up a try block for catching exceptions: Can be tested by providing a nonexistant file name for input

  5. Uses a Jinja2 template

    {% set toc = "true" %}
    
    toc: {{ toc }}
    draft: {{ false | canPublish }}
    hasMath: {{ hasMath }}
    stuff: {{ addedVariable }}
    
  6. Reads a mardown file that contains yaml front matter via editfrontmatter.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
    
  7. Extracts the front matter from the source file

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

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

  10. Prints the edited file to stdout

  11. Resets the Jinja2 template for secondary processing

  12. Resets the input file for processing against a new template

    draft: {{ true | canPublish }}
    weight: {{ weight }}
    stuff: {{ will_be_deleted }}
    
  13. Reprocesses the previous output as input while concatinating the new input file

    ## Example 2 markdown
    
    This file will be processed normaly and then concatinated with `example`.md
    
    * Changes in output of example1.md:
    * weight
      * note: uses local template variable
      * pre: 5
      * post: 10
    * draft:
      * note: uses jinja2 filter (callback)
      * pre: true
      * post: none
    * stuff:
      * note: deleted from yaml
      * pre: (list) ['one', 'two', 'three']
      * post: N/A
    
  14. Prints the edited output to stdout

Platform

Unix, Windows, python >=v3.5.3

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

MIT

Author

Karl N. Redman

homepage

EditFrontMatter Example 2

Current Release

version: 0.0.1

New in version 0.0.1: Initial Version

exception Signal_Caught[source]

Bases: Exception

SignalHandler(sig, frame)[source]

Generic Signal handler

class Derived_EditFrontMatter(Superfluous, **kwargs)[source]

Bases: editfrontmatter.EditFrontMatter.EditFrontMatter

EditFrontMatter derived class for example purposes

__init__(Superfluous, **kwargs)[source]

A specialized signature for the derived class

canPublish_method(var) → bool[source]

Class level callback method

Note

It’s important that class level callbacks are codded to be reentrant. If the class is used in a threaded app class level veriables would not be thread-safe. Also, use critical sections if working with threads.

Returns: a mock value

run(template_str, file_path, **kwargs) → None[source]

A override method for the baseclass run method

This is just a contrived example meant to demonstrate some advanced usage of the base class.

parent_run(extra_vars)[source]

cheezy method to call a parent method

canPublish_func(var) → bool[source]

Callback function for Jinja2 filters

main()[source]

Main function

Variables
  • DATA_PATH (str) – Path that points to the data file directory. Must end with a /

  • file_path1 (str) – Path to input source 1

  • file_path2 (str) – Path to input source 2

  • template_str1 (str) – Contents of template1

  • template_str2 (str) – Contents of template2

Envvar

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

Returns

program exit status (0 or 1)

Example

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

TEST_DATA_DIR="./data/" example2/example2.py