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:
  • format_name (str) – The name of the format (‘plain’, ‘markdown’, ‘html’, ‘highlighted’, or ‘custom’)

  • template_file (str) – Path to the template file for custom format (default: None)

Returns:

A formatter instance

Return type:

BaseFormatter

Raises:

ValueError – If the format name is not recognized

classmethod detect_format_from_file(file_path)

Detect the appropriate format based on the file extension.

Parameters:

file_path (str) – Path to the output file

Returns:

The detected format name

Return type:

str

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:
  • title (str) – The title of the output

  • directory_tree (str) – ASCII directory tree

  • files_content (dict) – Dictionary mapping file paths to their content

  • metadata (str) – Metadata about the codebase (default: None)

  • skipped_files (list) – List of files that were skipped (default: None)

Returns:

Formatted output

Return type:

str

format_file_header(file_path)[source]

Format a file header.

Parameters:

file_path (str) – Path to the file

Returns:

Formatted file header

Return type:

str

format_file_content(file_path, content)

Format the content of a file.

Parameters:
  • file_path (str) – Path to the file

  • content (str) – Content of the file

Returns:

Formatted file content

Return type:

str

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:
  • title (str) – The title of the output

  • directory_tree (str) – ASCII directory tree

  • files_content (dict) – Dictionary mapping file paths to their content

  • metadata (str) – Metadata about the codebase (default: None)

  • skipped_files (list) – List of files that were skipped (default: None)

Returns:

Formatted output as plain text

Return type:

str

format_file_header(file_path)

Format a file header as plain text.

Parameters:

file_path (str) – Path to the file

Returns:

Formatted file header

Return type:

str

format_file_content(file_path, content)

Format the content of a file as plain text.

Parameters:
  • file_path (str) – Path to the file

  • content (str) – Content of the file

Returns:

Formatted file content

Return type:

str

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:
  • title (str) – The title of the output

  • directory_tree (str) – ASCII directory tree

  • files_content (dict) – Dictionary mapping file paths to their content

  • metadata (str) – Metadata about the codebase (default: None)

  • skipped_files (list) – List of files that were skipped (default: None)

Returns:

Formatted output as Markdown

Return type:

str

format_file_header(file_path)[source]

Format a file header as Markdown.

Parameters:

file_path (str) – Path to the file

Returns:

Formatted file header

Return type:

str

format_file_content(file_path, content)

Format the content of a file as Markdown with syntax highlighting.

Parameters:
  • file_path (str) – Path to the file

  • content (str) – Content of the file

Returns:

Formatted file content with Markdown code blocks

Return type:

str

get_language_from_extension(file_path)

Determine the language for syntax highlighting based on the file extension.

Parameters:

file_path (str) – Path to the file

Returns:

Language name for syntax highlighting

Return type:

str

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:
  • title (str) – The title of the output

  • directory_tree (str) – ASCII directory tree

  • files_content (dict) – Dictionary mapping file paths to their content

  • metadata (str) – Metadata about the codebase (default: None)

  • skipped_files (list) – List of files that were skipped (default: None)

Returns:

Formatted output as HTML

Return type:

str

format_file_header(file_path)

Format a file header as HTML.

Parameters:

file_path (str) – Path to the file

Returns:

Formatted file header

Return type:

str

format_file_content(file_path, content)

Format the content of a file as HTML.

Parameters:
  • file_path (str) – Path to the file

  • content (str) – Content of the file

Returns:

Formatted file content with HTML pre tags

Return type:

str

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:
  • title (str) – The title of the output

  • directory_tree (str) – ASCII directory tree

  • files_content (dict) – Dictionary mapping file paths to their content

  • metadata (str) – Metadata about the codebase (default: None)

  • skipped_files (list) – List of files that were skipped (default: None)

Returns:

Formatted output as syntax-highlighted HTML

Return type:

str

format_file_header(file_path)[source]

Format a file header as HTML.

Parameters:

file_path (str) – Path to the file

Returns:

Formatted file header

Return type:

str

format_file_content(file_path, content)

Format the content of a file with syntax highlighting.

Parameters:
  • file_path (str) – Path to the file

  • content (str) – Content of the file

Returns:

Syntax-highlighted file content

Return type:

str

highlight_code(code, lexer_name)

Apply syntax highlighting to code.

Parameters:
  • code (str) – Code to highlight

  • lexer_name (str) – Name of the lexer to use for highlighting

Returns:

Highlighted code as HTML

Return type:

str

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:
  • title (str) – The title of the output

  • directory_tree (str) – ASCII directory tree

  • files_content (dict) – Dictionary mapping file paths to their content

  • metadata (str) – Metadata about the codebase (default: None)

  • skipped_files (list) – List of files that were skipped (default: None)

Returns:

Formatted output based on the template

Return type:

str

load_template()

Load the template from the template file.

Returns:

Template content

Return type:

str

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:
  • template (str) – Template content

  • title (str) – The title of the output

  • directory_tree (str) – ASCII directory tree

  • files_content (dict) – Dictionary mapping file paths to their content

  • metadata (str) – Metadata about the codebase

  • skipped_files (list) – List of files that were skipped

Returns:

Template with placeholders replaced

Return type:

str

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."
    }
)