Formatters Module
The formatters module provides different output formats for the aggregated code.
Module Overview
The formatters module provides functionality for:
Formatting aggregated code in different output formats
Applying syntax highlighting
Handling custom templates
Detecting the appropriate format based on file extension
Key Classes and Functions
FormatterFactory
- class promptprep.formatters.FormatterFactory
Factory class for creating the appropriate formatter based on the format name.
- classmethod create_formatter(format_name, template_file=None)
Create a formatter instance based on the format name.
- Parameters:
- Returns:
A formatter instance
- Return type:
- Raises:
ValueError – If the format name is not recognized
BaseFormatter
- class promptprep.formatters.BaseFormatter[source]
Base class for all formatters.
- format_output(title, directory_tree, files_content, metadata=None, skipped_files=None)
Format the aggregated code.
- Parameters:
- Returns:
Formatted output
- Return type:
PlainFormatter
- class promptprep.formatters.PlainFormatter
Formatter for plain text output.
- format_output(title, directory_tree, files_content, metadata=None, skipped_files=None)
Format the aggregated code as plain text.
- Parameters:
- Returns:
Formatted output as plain text
- Return type:
- format_file_header(file_path)
Format a file header as plain text.
MarkdownFormatter
- class promptprep.formatters.MarkdownFormatter[source]
Formatter for Markdown output.
- format_output(title, directory_tree, files_content, metadata=None, skipped_files=None)
Format the aggregated code as Markdown.
- Parameters:
- Returns:
Formatted output as Markdown
- Return type:
- format_file_content(file_path, content)
Format the content of a file as Markdown with syntax highlighting.
HTMLFormatter
- class promptprep.formatters.HTMLFormatter
Formatter for HTML output.
- format_output(title, directory_tree, files_content, metadata=None, skipped_files=None)
Format the aggregated code as HTML.
- Parameters:
- Returns:
Formatted output as HTML
- Return type:
- format_file_header(file_path)
Format a file header as HTML.
HighlightedFormatter
- class promptprep.formatters.HighlightedFormatter[source]
Formatter for syntax-highlighted HTML output.
- format_output(title, directory_tree, files_content, metadata=None, skipped_files=None)
Format the aggregated code as syntax-highlighted HTML.
- Parameters:
- Returns:
Formatted output as syntax-highlighted HTML
- Return type:
- format_file_content(file_path, content)
Format the content of a file with syntax highlighting.
CustomFormatter
- class promptprep.formatters.CustomFormatter(template_file)
Formatter for custom output based on a template file.
- Parameters:
template_file (str) – Path to the template file
- format_output(title, directory_tree, files_content, metadata=None, skipped_files=None)
Format the aggregated code using a custom template.
- Parameters:
- Returns:
Formatted output based on the template
- Return type:
- load_template()
Load the template from the template file.
- Returns:
Template content
- Return type:
- Raises:
FileNotFoundError – If the template file doesn’t exist
- replace_placeholders(template, title, directory_tree, files_content, metadata, skipped_files)
Replace placeholders in the template with actual content.
- Parameters:
- Returns:
Template with placeholders replaced
- Return type:
Usage Examples
Basic Usage
from promptprep.formatters import FormatterFactory
# Create a formatter
formatter = FormatterFactory.create_formatter('markdown')
# Format output
formatted_output = formatter.format_output(
title="My Project",
directory_tree="project/\n├── src/\n│ └── main.py\n└── README.md",
files_content={
"src/main.py": "def main():\n print('Hello, world!')",
"README.md": "# My Project\n\nA simple project."
}
)
# Save to file
with open('output.md', 'w') as f:
f.write(formatted_output)
With Metadata
from promptprep.formatters import FormatterFactory
formatter = FormatterFactory.create_formatter('html')
formatted_output = formatter.format_output(
title="My Project",
directory_tree="project/\n├── src/\n│ └── main.py\n└── README.md",
files_content={
"src/main.py": "def main():\n print('Hello, world!')",
"README.md": "# My Project\n\nA simple project."
},
metadata="Files: 2\nLines: 5\nComments: 0",
skipped_files=["large_file.bin"]
)
Custom Template
from promptprep.formatters import FormatterFactory
# Create a custom formatter with a template file
formatter = FormatterFactory.create_formatter('custom', template_file='my_template.txt')
formatted_output = formatter.format_output(
title="My Project",
directory_tree="project/\n├── src/\n│ └── main.py\n└── README.md",
files_content={
"src/main.py": "def main():\n print('Hello, world!')",
"README.md": "# My Project\n\nA simple project."
}
)
Format Detection
from promptprep.formatters import FormatterFactory
# Detect format from file extension
output_file = 'output.md'
format_name = FormatterFactory.detect_format_from_file(output_file)
# Create formatter based on detected format
formatter = FormatterFactory.create_formatter(format_name)
# Format output
formatted_output = formatter.format_output(
title="My Project",
directory_tree="project/\n├── src/\n│ └── main.py\n└── README.md",
files_content={
"src/main.py": "def main():\n print('Hello, world!')",
"README.md": "# My Project\n\nA simple project."
}
)