Skip to main content

extract code from within triple-quotes (```) in markdown files

Project description

get_code_from_markdown

Extract code from within triple-quotes (```) in markdown files (code blocks).

Usage

Getting code blocks from markdown files

The language parameter defaults to python. This will find codeblocks that start with ```python

from get_code_from_markdown import *

blocks = get_code_from_markdown(["# get_code_from_markdown", "```python", "print('hello world')", "```"])
# or
blocks = get_code_from_markdown_filename("README.md", language="python")
# or
with open("README.md", "r") as f:
    blocks = get_code_from_markdown_file(f, language="python")

Running code blocks from markdown files

You can use a subprocess or just exec the code to see if it works.

from get_code_from_markdown import run_code_from_markdown_blocks
blocks = ["print('hello world')"]

# run function uses exec() by default
run_code_from_markdown_blocks(blocks)

# you can choose to use subprocess output instead
# this will use python3 by default
run_code_from_markdown_blocks(blocks, method=RunMethods.SUBPROCESS)

# give arguments to specify a different command
run_code_from_markdown_blocks(blocks, method=RunMethods.SUBPROCESS(["python3", "-c"]))

Format of outputted code blocks

The format is a list of \n-delimited strings, each representing a block of code from the parsed markdown file. You can split the code blocks further into individual lines of code with string.split("\n")

Development

  • Use black (code linter)
  • Run pytest (unit tests)
  • Build a package for PyPI: python -m build --sdist --wheel

Legal

This is a derivative work of Codeblocks which provides a simpler version with less dependencies. It is covered by the same Apache license (see LICENSE file in this git repo). Use Codeblocks for its CLI and automatic formatting features.

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

get_code_from_markdown-1.0.1.tar.gz (6.9 kB view details)

Uploaded Source

Built Distribution

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

get_code_from_markdown-1.0.1-py3-none-any.whl (7.2 kB view details)

Uploaded Python 3

File details

Details for the file get_code_from_markdown-1.0.1.tar.gz.

File metadata

  • Download URL: get_code_from_markdown-1.0.1.tar.gz
  • Upload date:
  • Size: 6.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.2

File hashes

Hashes for get_code_from_markdown-1.0.1.tar.gz
Algorithm Hash digest
SHA256 a25f5f8c2c61928bfdd99624ece963a786c47beb778ccd2741b08b2386fba4e5
MD5 3d3b6b2499562d6aa5cbef93b148775c
BLAKE2b-256 5580eb91bb00ab6b90e7c724834a792be6812589f73849dbc8887d230a2ef733

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for get_code_from_markdown-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 323b4567c942670a1bb6c974de115b8910c31553f5e3e8def97c65ddde63aadc
MD5 ce8468ef6eafa020f05503313d0f2d36
BLAKE2b-256 21888309addda38030601eb0b05ef92b16c3fbff022c46b265b2ef8685d7b113

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