Skip to main content

snex - snippet extractor

(Accompanying blog post)

Extract snippets for blog posts or examples.

How to use

Installation

pip install snex

Tag Snippets

Let's assume that you have a project in /path/to/your/project. You navigate to the region where you want to extract a snippet and tag it as follows (# is regarded as comment prefix):

# :snippet snippet-name-without-whitespace

def foobar():
   doit()
foobar()

# :endsnippet
  • Empty lines after the start and before the end are removed.
  • A snippet name is mandatory.
  • The snippet name is sanitized to prevent malicious code to overwrite arbitrary files on your system.

Advanced snippet tagging

You can also overwrite the lang config to use a different language for this snippet.

# :snippet snippet-name-without-whitespace lang: scala

Everything after the snippet name is parsed as YAML dict: { $text_after_snippet_name }, e.g. lang: scala, other_param: "hello world" is parsed as { lang: scala, other_param: "hello world" } YAML.

This means that you can also customise your parameter substitutions with a config like:

config {
  default {
    "output_template": "```{{lang}} - {{other_param}}\n{{{snippet}}}\n```\n",
    "valid_param_keys": [ "lang", "name", "other_param" ]
    ...
  }
}

The output template is parsed as mustache template.

Setup

create a snex.conf.yaml in the root directory of a project you want to create snippets from:

default:
  output_path: "snippets"
  comment_prefix: "# "
  comment_suffix: ""

src:
  lang: "python"
  root: "src"
  glob: "**/*.py"

github:
  comment_prefix: "# "
  lang: "python"
  path: "https://raw.githubusercontent.com/jwbargsten/snex/master/src/snex/core.py"

The config syntax is YAML.

You have 3 layers of settings in a section:

  1. the global default config in docs/snippets/global-default-config.md
  2. the config section default in your snex.conf.yaml file (which overwrites the global default).
  3. the specific config section in your snex.conf.yaml (the section name is prefix for snippets default is reserved.). The configuration in a specific section overwrites the default section which overwrites the global default config.

Run

You created a /path/to/your/project/snex.conf.yaml like described in the previous topic.

From the project directory

cd /path/to/your/project
snex run

This will read snex.conf.yaml in the current directory and dump the snippets into the configured output_path.

From a different directory

snex run /path/to/your/project

This will read /path/to/your/project/snex.conf.yaml and dump the snippets into the configured output_path.

From a different directory to a different snippet output directory

snex run /path/to/your/project /path/custom/snippet/output/dir

This will read /path/to/your/project/snex.conf.yaml and dump the snippets into /path/custom/snippet/output/dir.

TAKE CARE

This invocation will overwrite the output dir of all defined config sections. Which means that all snippets are dumped into the same directory.

Caveats (or features)

  • Snippets are overwritten without confirmation. This makes it easy to update everything, but you have to take care that you will not overwrite stuff you want to keep.

Metadata

Release files for snex 3000.2.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for snex 3000.2.1
File Size Uploaded Provenance
snex-3000.2.1.tar.gz 14.6 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for snex 3000.2.1
File Interpreter ABI Platform
snex-3000.2.1-py3-none-any.whl Python 3 none any Details

Total release size: 25.5 kB

Release files / snex-3000.2.1.tar.gz

Download URL snex-3000.2.1.tar.gz
Size 14.6 kB
Tags Source
SHA-256 checksum
How to use checksums
7151ae36055af8f1e4aba82f842523897dee9e7a39cca89b7f752362d5571b62
BLAKE2b-256 checksum
How to use checksums
a9066f2417782e7905b6cb2234e6c0d4096687c06ce65cb72d6afbb595ec2bc4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.0.0 CPython/3.11.7

Release files / snex-3000.2.1-py3-none-any.whl

Download URL snex-3000.2.1-py3-none-any.whl
Size 10.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
31268b272a8bc042d48dfe051a7cb090b1a124f6fdb6d926189a2568132c0814
BLAKE2b-256 checksum
How to use checksums
c282516135e5c2476bda8faeee3910fc74bbf07d7d33bbeb0ca3394d58ed0b5f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.0.0 CPython/3.11.7

Security for snex 3000.2.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page