Package: markplates
MarkPlatesA templating utility for keeping code included in Markdown documents in sync with the original source.The problem I hope to solve is to simplify keeping external files up to date with markdown documents that contain them. This happens to me frequently when an editor makes a suggestion to an article that will modify the underlying code it is quoting.InstallingYou can download and install the latest version of MarkPlates from the PyPI with this command:$python3-mpipinstall--upgrademarkplatesMarkPlates is currently tested against Python 3.6, 3.7, 3.8, and 3.9.UsageRunningmarkplatesis as simple as handing it a file:$markplatestemplate.mdtThis will process the template intemplate.mdt, filling it in with data specified in the template.Theexamplesdirectory has thesimple.mdttemplate:# Sample MarkPlates Template{{ set_path("./examples") }}

This is an example of importing an entire file. Without a range specified, the first line is skipped to not include the shell specifier. Double blank lines are condensed into single lines:
{{ import_source("testfile.py") }}

If you want to include the shell specifier, you can include it explicitly in a range. This silly example imports some of the lines from the file, demonstrating different ranges. Note that the ranges are compiled into a single list, so over-lapping ranges are only shown once:
{{ import_source("testfile.py", [1, 5, "2", 3, "8-$", "$", "$-2"]) }}

Functions can be imported from a file by name:

{{ import_function("testfile.py", "flying_pig_menu") }}

The import_repl tag captures stdout and stderr from a REPL session:
{{ import_repl(
"""
def func(x):
    if x:
        print(x)

func(True)
func(False) """) }}This demonstrates setting the path and pulling in some of the lines of a file. You can also examine theREADME.mdtfile in this library which is used to create thisREADME.md.To use on your own project create a markdown document with special tags to indicate amarkplatesfunction call.  The delimiter for these tags is{{function goes here}}.Note:if you need to add{{characters which should not be processed as a template, you can put them in a{{ '' }}block to escape them. Template processing is done withJinja2so Markplates uses the same escape sequences.Markplatessupports these functions:set_path("path/to/source/files", [show_skipped_section_marker])import_source("source_file_name", [list of line number ranges], language=None, filename=False)import_function("source_file_name", "function_name", language=None, filename=False)import_repl("code to run in repl")set_path()Theset_path()function allows you to specify the base directory to use when searching for source files.  Each call to this function will apply from that point in the template down.The path must be included in single or double qoutes. If not specified, the path defaults to ".", the current directory.Examples:{{set_path(".")}}  #sets path to the default
{{set_path("/home/user/my/project")}} # points the path to your projectTheset_path()command is not required as all other functions will take full paths to files.This command takes an optionalshow_skipped_section_markerparameter which defaults toFalse.  When set to true, if a template expansion shows disjoint sections of a file which are separated by 2 or more lines, that gap will be shown in the output with a marker line:​# ...This option defaults to false to not break backward compatibility.import_source()Theimport_source()function will pull in the contents of a source file.  Optional line number ranges can be specified (see description below). The filename must be in quotes.If no line number ranges are specified, the first line of the file will be omitted.  This is to prevent the#!/usr/bin/env pythonline from cluttering the markdown article. If you want to include the first line, use the range:1-$.Examples:{{import_source("__main__.py")}} # includes all but line 1 from `__main__.py` file
{{import_source("__main__.py", ["1-$",])}} # imports all lines from `__main__.py`
{{import_source("__main__.py", [1, "3", "5-$"])}} # imports all lines except lines 2 and 4 from `__main__.py`
{{import_source("__main__.py", language="python", filename=True)}}
# includes all but line 1 from `__main__.py` file, puts the
# contents in a python block with the filename as a comment in
# the first line of the block.MarkPlateswill display an error message tostderrif a file is not found.import_function()Theimport_functionfunction searches a source file for the named function, class, class method or assigned variable. The function name supports dot-references, for example to get at the class methodarea()inside the classSqaure, the function name would be "Square.area". To retrieve a nested function, name both the parent and child function, for example "outer.inner".The first piece of code matching the given name is returned, (and you shouldn't have multiple things with the same name anyway!). The source file is parsed, not loaded, so no import side-effects take place.Whitespace following the function will not be included.Examples:{{import_function("__main__.py", "condense_ranges")}} # imports the function named `condense_ranges` from `__main__.py`Thelanguageandfilenameparameters are treated the same way they are inimport_source().import_repl()Theimport_replfunction takes the input parameter and splits it into separate lines.  Each line of the input will be run in a REPL with thestdoutandstderrcaptured to insert into the final output. The result should appear similar to a copy-paste from running the same commands manually.There is an exception, however.  Blank input lines are used for spacing and will not display the>>>prompt one would normally see in the REPL.Example:{{import_repl(
"""
def func(x):
    if x:
        print(x)

func(True)
func(False) """) }}Output:>>> def func(x):
...     if x:
...         print(x)

>>> func(True)
True
>>> func(False)Line Number RangesLine number ranges allow you to specify which lines you want to include from the source file.   Ranges can be in the following form:3 or "3" : an integer adds just that line from the input"5-7" : a range adds lines between start and end inclusive (so 5, 6, 7)"10-$" : an unlimited range includes start line to the end of the file"$" : the last line"$-3" : negative indexing, the last line and the three lines that proceed itNote:LINE NUMBERING STARTS AT 1!Copy to ClipboardThe-coption will copy most of the output to the clipboard.  The first two lines are skipped, which generally are theh1title and the following blank line.  This is done to simplify copying the output to the Real Python CMS system.Features to ComeI'd like to add:Developer InstructionsCapturing the results of a shell command and inserting into the fileRunningblackover the included Python sourceWindows and Mac testing/supportInterested?Let me know!  If you're interested in the results or would like to help out, please raise an issue and I'll be in touch!Release History1.6.0 Addshow_skippedoption toset_path()and improved error reporting1.5.0 Added$and$-nas valid line ranges. Fixed several bugs1.4.0 Added -c option, bug fixes1.3.0 Minor bug fixes1.2.0 Addedlanguageandfilenameoptions forimport_sourceandimport_replmethods1.1.0 Addedimport_replfunctionality1.0.0 Initial releaseLicense plate graphic thanks toACME License Maker