PyParseit is a Python library designed to parse Markdown files and strings to extract code snippets based on specific programming languages. It provides a simple and intuitive interface for developers who want to quickly extract and utilize code blocks from Markdown documents or strings.
- Introduction
- Features
- Installation
- Usage
- Command-Line Interface
- Examples
- API Reference
- Contributing
- License
Markdown is widely used for documentation, blogging, and technical writing. PyParseit simplifies the task of extracting code snippets from Markdown files and strings, making it ideal for static site generators, content management systems, and more.
- Parse Markdown files or strings to extract code blocks.
- Filter code snippets by programming language (e.g., Python, JavaScript, JSON).
- Check for the existence of code snippets of a specific language in files or strings.
- Command-line interface for easy integration into scripts and workflows.
- Customizable and extensible API for developers.
You can install PyParseit via pip:
pip install pyparseit
Alternatively, you can clone the repository and install it manually:
git clone https://github.com/uladkaminski/pyparseit.git
cd pyparseit
python setup.py install
Here's a basic example of how to use PyParseit to extract Python code snippets from a Markdown file:
from pyparseit import parse_markdown_file
Specify the file path and language
file_path = 'example_file.md'
language = 'python'
Parse the Markdown file to extract Python code snippets
python_snippets = parse_markdown_file(file_path, language=language)
Display extracted Python snippets from the file
for snippet in python_snippets:
print(f"Language: {snippet.language}\nContent:\n{snippet.content}\n")
PyParseit can also parse Markdown content directly from a string:
from pyparseit import parse_markdown_string
# Define a Markdown string with multiple code blocks
markdown_string = """
# Sample Markdown
Here is some Python code:
\`\`\`python
def hello_world():
print("Hello, world!")
\`\`\`
Here is some JavaScript code:
\`\`\`javascript
function helloWorld() {
console.log("Hello, world!");
}
\`\`\`
And here is some JSON:
\`\`\`json
{
"name": "John",
"age": 30
}
\`\`\`
"""
# Parse the Markdown string to extract JSON code snippets
json_snippets = parse_markdown_string(markdown_string, language='json')
# Display extracted JSON snippets from the string
print("Extracted JSON Snippets from String:")
for snippet in json_snippets:
print(f"Language: {snippet.language}\nContent:\n{snippet.content}\n")
You can also check if a Markdown file contains code snippets of a specific language:
from pyparseit import contains_snippet_file
# Specify the file path and language
file_path = 'example_file.md'
language = 'python'
# Check if the Markdown file contains Python code snippets
has_python_snippet = contains_snippet_file(file_path, language=language)
print(f"File contains Python snippets: {has_python_snippet}")
Similarly, you can check if a Markdown string contains code snippets of a specific language:
from pyparseit import contains_snippet_string
# Define a Markdown string with multiple code blocks
markdown_string = """
# Sample Markdown
Here is some Python code:
\`\`\`python
def hello_world():
print("Hello, world!")
\`\`\`
Here is some JavaScript code:
\`\`\`javascript
function helloWorld() {
console.log("Hello, world!");
}
\`\`\`
And here is some JSON:
\`\`\`json
{
"name": "John",
"age": 30
}
\`\`\`
"""
# Check if the Markdown string contains Python code snippets
has_python_snippet = contains_snippet_string(markdown_string, language='python')
print(f"String contains Python snippets: {has_python_snippet}")
PyParseit also provides a CLI for easy usage from the terminal:
pyparseit path/to/your/file.md -l python -o output.txt
This command parses the specified Markdown file, extracts Python code snippets, and saves them to output.txt
.
Check out the examples directory for more use cases and demonstrations of how to integrate PyParseit into your projects.
- Description: Parses Markdown content from a file to extract code snippets.
- Parameters:
file_path (str)
: The path to the Markdown file.language (Optional[str])
: The programming language to filter snippets by.
- Returns:
List[CodeSnippet]
: A list of extracted code snippets.
- Description: Parses Markdown content from a string to extract code snippets.
- Parameters:
markdown_string (str)
: The Markdown content as a string.language (Optional[str])
: The programming language to filter snippets by.
- Returns:
List[CodeSnippet]
: A list of extracted code snippets.
- Description: Checks if a Markdown file contains at least one code snippet of a specific language.
- Parameters:
file_path (str)
: The path to the Markdown file.language (Optional[str])
: The programming language to filter snippets by.
- Returns:
bool
:True
if the file contains at least one snippet of the specified language,False
otherwise.
- Description: Checks if a Markdown string contains at least one code snippet of a specific language.
- Parameters:
markdown_string (str)
: The Markdown content as a string.language (Optional[str])
: The programming language to filter snippets by.
- Returns:
bool
:True
if the string contains at least one snippet of the specified language,False
otherwise.
- Description: Represents a code snippet with language and content.
- Attributes:
language: str
: The programming language of the code snippet.content: str
: The code snippet's content.
PyParsecError
: Base exception for parser errors.
Contributions are welcome! If you'd like to contribute to PyParseit, please follow these steps:
- Fork the repository.
- Create a new branch for your feature or bugfix.
- Implement your changes and add tests if applicable.
- Commit your changes and push to your fork.
- Submit a pull request with a description of your changes.
Please ensure your code adheres to the project's coding standards and passes all tests.
This project is licensed under the MIT License. See the LICENSE file for details.