Skip to main content

Embed-Markdown embeds code snippets into Markdown files.

Project description

Embed-Markdown User Guide

Embed-Markdown embeds code snippets into Markdown files. It helps keep examples up to date by syncing code referenced in Markdown files with your actual source code. This also makes it easier to ensure samples are unit tested, etc. Snippets can be extracted from any text file (not necessarily even a from a code file).

Topics

Installation

  1. Run pip install embed-markdown
  2. Make sure embed-markdown is in the 'PATH'
    1. Run pip show embed-markdown to show where it's installed
    2. Edit ~/.bash_profile (or equivalence of it) to include the path
    3. Run source ~/.bash_profile

Quick Start

To use this tool, first add source code references to your Markdown files. Then run embed-markdown to update all Markdown files inside the current directory (and all subdirectories) with the latest source code referenced.

Source Code References

Source code is embedded using a comment in your Markdown file like this:

<!-- embed:relative_script_path:cmd:args -->
- CAUTION: This should be placed on a line by itself to avoid losing data. Do
- not modify embedded code directly because changes will be overwritten the
- next time the script runs; instead, change the source code the embed
- directive references.

relative_script_path

This is the relative path from the .md file to a source file, e.g. ../src/api.js.

cmd

This controls how a source code snippet is selected. There are two approaches:

1) by Scope

When cmd is "scope", embed-md will extract the next code block (inside a pair of "{" and "}"). In this mode, args is a colon separated list of nested scopes. The script scans through the target script and recursively narrows down on each scope. When the innermost scope is found, the script extracts lines until the next block scope ends (a complete pair of "{" and "}"). For example, <!-- embed:../src/api.js:scope:class SomeApi --> extracts

class SomeApi {
    // Some comments
    method (a) {
        if (a) {
            console.log(a)
        }
    }
}

Whereas <!-- embed:../src/api.js:scope:class SomeApi:Some comments --> extracts

// Some comments
method (a) {
    if (a) {
        console.log(a)
    }
}

And <!-- embed:../src/api.js:scope:class SomeApi:Some comments:if (a) --> extracts

if (a) {
    console.log(a)
}

2) by Section

When cmd is "section", embed-md will extract lines between the section start and end patterns. In this mode, args is two colon delimited strings with pattern start_pattern:end_pattern. Code between lines with substring start_pattern and end_pattern are extracted from the target script. For example, <!-- embed:../example.js:section:Example 1 start:Example 1 end --> extracts

const a = 1

from

// Example 1 start
const a = 1
// Example 1 end

Remove Embedded Source Code

Run embed-markdown --remove to remove all embedded code snippets, leaving only the embed directive.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

embed-markdown-1.0.1.tar.gz (35.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

embed_markdown-1.0.1-py3-none-any.whl (8.7 kB view details)

Uploaded Python 3

File details

Details for the file embed-markdown-1.0.1.tar.gz.

File metadata

  • Download URL: embed-markdown-1.0.1.tar.gz
  • Upload date:
  • Size: 35.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.11.3 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.64.0 CPython/3.10.4

File hashes

Hashes for embed-markdown-1.0.1.tar.gz
Algorithm Hash digest
SHA256 305946a5749427dab74fe9ba4209d00d7c8289c9a08801474668dbd2884e517f
MD5 5364339f07cb19e6ceb38c39bd425f47
BLAKE2b-256 1901ff87412d55137c67c0974dd874919f56056d764b7c29615c259d99ae0ae4

See more details on using hashes here.

File details

Details for the file embed_markdown-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: embed_markdown-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 8.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.11.3 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.64.0 CPython/3.10.4

File hashes

Hashes for embed_markdown-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 3528c3ffcdf061826b96e977c71f341c6b9168591893dbacc57077906f7bbb44
MD5 accfc26a57272c0169a31bb36e8ba88b
BLAKE2b-256 75c4356df561a66e125d58ba3f48ac8dd394b54f889c8fa04118522a8152908c

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page