promptprep.formatters module

Makes your code look nice in different output formats.

class promptprep.formatters.BaseFormatter[source]

Bases: ABC

The foundation for all our formatters.

__init__()[source]

Initialize the formatter.

abstract format_code_content(content: str, file_path: str) str[source]

Format code content with line numbers.

abstract format_directory_tree(tree: str) str[source]

Format the directory tree.

abstract format_error(error_msg: str) str[source]

Format error messages.

abstract format_file_header(file_path: str) str[source]

Format a file header.

abstract format_metadata(metadata: Dict[str, Any]) str[source]

Format metadata section.

abstract format_skipped_files(skipped_files: List[tuple]) str[source]

Format skipped files section.

get_file_extension(file_path: str) str[source]

Get the extension of a file.

class promptprep.formatters.CustomTemplateFormatter(template_file: str, base_format: str = 'plain')[source]

Bases: BaseFormatter

Lets you design your own output format using a template file.

Your template can use these placeholders: - ${DIRECTORY_TREE} - Shows your folder structure - ${FILE_HEADER:path} - Adds a header for each file - ${FILE_CONTENT:path} - Puts in the actual code - ${METADATA} - Adds stats about your codebase - ${SKIPPED_FILES} - Lists any files that were too big - ${FILES} - All your files with headers and content - ${TITLE} - The main title

__init__(template_file: str, base_format: str = 'plain')[source]

Initialize custom template formatter.

Parameters:
  • template_file – Path to the template file

  • base_format – The base format to use for sections not defined in the template (plain, markdown, html, highlighted)

format_code_content(content: str, file_path: str) str[source]

Format code content using the base formatter.

format_directory_tree(tree: str) str[source]

Format the directory tree using the base formatter.

format_error(error_msg: str) str[source]

Format error messages using the base formatter.

format_file_header(file_path: str) str[source]

Format a file header using the base formatter.

format_metadata(metadata: Dict[str, Any]) str[source]

Format metadata section using the base formatter.

format_skipped_files(skipped_files: List[tuple]) str[source]

Format skipped files section using the base formatter.

render_template(directory_tree: str, files_content: Dict[str, str], metadata: Dict[str, Any], skipped_files: List[tuple], title: str = 'Code Aggregation') str[source]

Render the template with the provided content.

Parameters:
  • directory_tree – ASCII representation of the directory tree

  • files_content – Dictionary mapping file paths to their content

  • metadata – Dictionary of metadata about the codebase

  • skipped_files – List of (file_path, size) tuples for skipped files

  • title – Title of the output (default: “Code Aggregation”)

Returns:

The rendered template content

class promptprep.formatters.HighlightedFormatter(html_output: bool = True)[source]

Bases: BaseFormatter

Adds syntax highlighting to make your code pop.

__init__(html_output: bool = True)[source]

Gets ready to highlight your code.

Parameters:

html_output – True for web pages, False for terminal colors

format_code_content(content: str, file_path: str) str[source]

Format code content with syntax highlighting (without line numbers).

format_directory_tree(tree: str) str[source]

Format the directory tree with highlighting.

format_error(error_msg: str) str[source]

Format error messages.

format_file_header(file_path: str) str[source]

Format a file header with highlighting.

format_metadata(metadata: Dict[str, Any]) str[source]

Format metadata section.

format_skipped_files(skipped_files: List[tuple]) str[source]

Format skipped files section.

get_full_html(content: str, title: str = 'Code Aggregation') str[source]

Wrap content in a complete HTML document if in HTML mode.

class promptprep.formatters.HtmlFormatter[source]

Bases: BaseFormatter

Creates a nice-looking webpage with your code.

__init__()[source]

Initialize HTML formatter with CSS styles.

format_code_content(content: str, file_path: str) str[source]

Format code content in HTML (without line numbers).

format_directory_tree(tree: str) str[source]

Format the directory tree in HTML.

format_error(error_msg: str) str[source]

Format error messages in HTML.

format_file_header(file_path: str) str[source]

Format a file header in HTML.

format_metadata(metadata: Dict[str, Any]) str[source]

Format metadata section in HTML.

format_skipped_files(skipped_files: List[tuple]) str[source]

Format skipped files section in HTML.

get_full_html(content: str, title: str = 'Code Aggregation') str[source]

Wrap content in a complete HTML document.

class promptprep.formatters.MarkdownFormatter[source]

Bases: BaseFormatter

Makes your code look great in Markdown documents.

format_code_content(content: str, file_path: str) str[source]

Format code content in Markdown (without line numbers).

format_directory_tree(tree: str) str[source]

Format the directory tree in Markdown.

format_error(error_msg: str) str[source]

Format error messages in Markdown.

format_file_header(file_path: str) str[source]

Format a file header in Markdown.

format_metadata(metadata: Dict[str, Any]) str[source]

Format metadata section in Markdown.

format_skipped_files(skipped_files: List[tuple]) str[source]

Format skipped files section in Markdown.

class promptprep.formatters.PlainTextFormatter[source]

Bases: BaseFormatter

Keeps things simple with plain text output.

format_code_content(content: str, file_path: str) str[source]

Format code content in plain text (without line numbers).

format_directory_tree(tree: str) str[source]

Format the directory tree in plain text.

format_error(error_msg: str) str[source]

Format error messages in plain text.

format_file_header(file_path: str) str[source]

Format a file header in plain text.

format_metadata(metadata: Dict[str, Any]) str[source]

Format metadata section in plain text.

format_skipped_files(skipped_files: List[tuple]) str[source]

Format skipped files section in plain text.

promptprep.formatters.get_formatter(output_format: str = 'plain', template_file: str | None = None, base_format: str = 'plain') BaseFormatter[source]

Picks the right formatter for your needs.

Parameters:
  • output_format – How you want it to look (plain, markdown, html, highlighted, custom)

  • template_file – Your template file (needed for custom format)

  • base_format – Backup format for custom templates (defaults to plain)

Returns:

The formatter that’ll do the job

Raises:

ValueError – If format is unknown or template is missing when needed