Advanced, Multi-Pass, Front Matter Editing Example¶
- Program
example2
- Synopsis
Example program that performs the following actions:
Derives a class from EditFrontMatter
Performs execution in a try block
raises an exception form the derived class for example purposes
catches the exception and prints a messae
Sets up a signal handler for SIGHUP and SIGINT for testing/example
Set up a try block for catching exceptions: Can be tested by providing a nonexistant file name for input
Uses a Jinja2 template
{% set toc = "true" %} toc: {{ toc }} draft: {{ false | canPublish }} hasMath: {{ hasMath }} stuff: {{ addedVariable }}
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
Extracts the front matter from the source file
Prorammitically edits the front matter via
editfrontmatter.EditFrontMatter.EditFrontMatter.run()
Concatinates the edited front matter with the original file content via
editfrontmatter.EditFrontMatter.EditFrontMatter.dumpFileData()
Prints the edited file to stdout
Resets the Jinja2 template for secondary processing
Resets the input file for processing against a new template
draft: {{ true | canPublish }} weight: {{ weight }} stuff: {{ will_be_deleted }}
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
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
- Author
- homepage
- Current Release
version: 0.0.1
New in version 0.0.1: Initial Version
-
class
Derived_EditFrontMatter
(Superfluous, **kwargs)[source]¶ Bases:
editfrontmatter.EditFrontMatter.EditFrontMatter
EditFrontMatter derived class for example purposes
-
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
-