Skip to main content

Python package for extracting tagged code snippets from local files or remote sources.

Project description

tagsnip is a Python CLI tool for extracting tagged code snippets from local files or remote URLs, such as GitHub raw files. It serves as the backend for the tagsnip LaTeX package and enables dynamic inclusion of code snippets directly in LaTeX documents.

Installation

Install tagsnip from PyPI:

pip install tagsnip

After installation, the command tagsnip should be available in your shell:

tagsnip --help

Usage

tagsnip -s <source> -t <tag> [-o <output>]
tagsnip -c <output>

Command-line options

-s, --source   Local file path or remote URL.  
-t, --tag      Snippet tag to extract.  
-o, --output   Optional output file path.
-c, --cleanup  Removes temporary files at specified path. 

--source and --tag are mandatory arguments. If --output is provided, tagsnip writes the extracted snippet to the selected file. Otherwise, the snippet is written to stdout.

When --output is used, tagsnip also writes a metadata file next to the output file. For example, if the output file is snippet.tmp, then the metadata file is snippet.tmp.meta. Both files can be removed using the --cleanup option.

The metadata file contains the original start and end line numbers of the extracted snippet. This is used by the LaTeX package to preserve source line numbering.

Supported sources

  • Local text files
  • Remote text files available over HTTP or HTTPS
  • GitHub raw file URLs

Tag format

tagsnip extracts code between two matching markers:

tagsnip-start <tag>
...
tagsnip-end <tag>

Tags are case-sensitive. Markers must appear on separate lines. The tag must follow the marker name after one space.

For example:

# tagsnip-start demo
print("This line will be extracted.")
# tagsnip-end demo

The marker lines themselves are not included in the extracted snippet.

Error handling

tagsnip reports errors when:

  • The tag is not found
  • Start/end markers are mismatched
  • Multiple start markers exist
  • The source cannot be fetched

Quick example

Example source file

# tagsnip-start demo
def main():
    x = 1
    y = 2
    print(x + y)

    return 0
# tagsnip-end demo

if __name__ == "__main__":
    import sys
    sys.exit(main())

CLI usage

Write the snippet to stdout:

tagsnip -s example.py -t demo

Write the snippet to a file:

tagsnip -s example.py -t demo -o out/out.txt

Get snippet from URL:

tagsnip -s https://raw.githubusercontent.com/brozrost/tagsnip/main/docs/example.py -t demo

Remove temporary files:

tagsnip -c out/out.txt

Python usage

from tagsnip import extractor

snippet = extractor.extract_from_file("example.py", "demo")

Compatibility note

The Python package and the LaTeX package should be kept in sync. Backwards compatibility is not guaranteed.

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

tagsnip-1.0.0.tar.gz (9.3 kB view details)

Uploaded Source

Built Distribution

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

tagsnip-1.0.0-py3-none-any.whl (6.7 kB view details)

Uploaded Python 3

File details

Details for the file tagsnip-1.0.0.tar.gz.

File metadata

  • Download URL: tagsnip-1.0.0.tar.gz
  • Upload date:
  • Size: 9.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.2

File hashes

Hashes for tagsnip-1.0.0.tar.gz
Algorithm Hash digest
SHA256 fbb94305990c073452c82d2ee07022120653acbef7e3ede7a58e5bca797664ed
MD5 f70474de320093dfac78636ca07636c3
BLAKE2b-256 b3564d8bab6f44db76bf27150b7a53c997dd46d1b288d28ffcd138fcebb732b0

See more details on using hashes here.

File details

Details for the file tagsnip-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: tagsnip-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 6.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.2

File hashes

Hashes for tagsnip-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 64708eb7319759e4976b4c0886b6f002c42ba81e3ca4add155f001c39c2f4fe7
MD5 efa0ab0405b3d5f91fe9d8bd3fdab53e
BLAKE2b-256 1b03a31a73833c26ae0e92a2b8ecd4906b31663f6c90164b4eb5f41accc24673

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