Rename --code to --markdown, closes #42

This commit is contained in:
Simon Willison
2025-02-18 21:15:11 -08:00
parent cfc9fa584f
commit b62c447787
3 changed files with 25 additions and 18 deletions

View File

@@ -64,10 +64,10 @@ This will output the contents of every file, with each file preceded by its rela
files-to-prompt path/to/directory --cxml files-to-prompt path/to/directory --cxml
``` ```
- `--code`: Output as fenced code blocks. - `--markdown`: Output as Markdown with fenced code blocks.
```bash ```bash
files-to-prompt path/to/directory --code files-to-prompt path/to/directory --markdown
``` ```
- `-o/--output <file>`: Write the output to a file instead of printing it to the console. - `-o/--output <file>`: Write the output to a file instead of printing it to the console.
@@ -214,12 +214,12 @@ Contents of file2.txt
</documents> </documents>
``` ```
## --code fenced code block output ## --markdown fenced code block output
The `--code` option will output the files as fenced code blocks, which can be useful for pasting into Markdown documents. The `--markdown` option will output the files as fenced code blocks, which can be useful for pasting into Markdown documents.
```bash ```bash
files-to-prompt path/to/directory --code files-to-prompt path/to/directory --markdown
``` ```
The language tag will be guessed based on the filename. The language tag will be guessed based on the filename.

View File

@@ -52,11 +52,11 @@ def add_line_numbers(content):
return "\n".join(numbered_lines) return "\n".join(numbered_lines)
def print_path(writer, path, content, cxml, code, line_numbers): def print_path(writer, path, content, cxml, markdown, line_numbers):
if cxml: if cxml:
print_as_xml(writer, path, content, line_numbers) print_as_xml(writer, path, content, line_numbers)
elif code: elif markdown:
print_as_code(writer, path, content, line_numbers) print_as_markdown(writer, path, content, line_numbers)
else: else:
print_default(writer, path, content, line_numbers) print_default(writer, path, content, line_numbers)
@@ -84,7 +84,7 @@ def print_as_xml(writer, path, content, line_numbers):
global_index += 1 global_index += 1
def print_as_code(writer, path, content, line_numbers): def print_as_markdown(writer, path, content, line_numbers):
lang = EXT_TO_LANG.get(path.split(".")[-1], "") lang = EXT_TO_LANG.get(path.split(".")[-1], "")
# Figure out how many backticks to use # Figure out how many backticks to use
backticks = "```" backticks = "```"
@@ -108,13 +108,13 @@ def process_path(
ignore_patterns, ignore_patterns,
writer, writer,
claude_xml, claude_xml,
code, markdown,
line_numbers=False, line_numbers=False,
): ):
if os.path.isfile(path): if os.path.isfile(path):
try: try:
with open(path, "r") as f: with open(path, "r") as f:
print_path(writer, path, f.read(), claude_xml, code, line_numbers) print_path(writer, path, f.read(), claude_xml, markdown, line_numbers)
except UnicodeDecodeError: except UnicodeDecodeError:
warning_message = f"Warning: Skipping file {path} due to UnicodeDecodeError" warning_message = f"Warning: Skipping file {path} due to UnicodeDecodeError"
click.echo(click.style(warning_message, fg="red"), err=True) click.echo(click.style(warning_message, fg="red"), err=True)
@@ -158,7 +158,12 @@ def process_path(
try: try:
with open(file_path, "r") as f: with open(file_path, "r") as f:
print_path( print_path(
writer, file_path, f.read(), claude_xml, code, line_numbers writer,
file_path,
f.read(),
claude_xml,
markdown,
line_numbers,
) )
except UnicodeDecodeError: except UnicodeDecodeError:
warning_message = ( warning_message = (
@@ -220,7 +225,9 @@ def read_paths_from_stdin(use_null_separator):
help="Output in XML-ish format suitable for Claude's long context window.", help="Output in XML-ish format suitable for Claude's long context window.",
) )
@click.option( @click.option(
"--code", is_flag=True, help="Output in triple backtick fenced code blocks" "--markdown",
is_flag=True,
help="Output Markdown with fenced code blocks",
) )
@click.option( @click.option(
"line_numbers", "line_numbers",
@@ -245,7 +252,7 @@ def cli(
ignore_patterns, ignore_patterns,
output_file, output_file,
claude_xml, claude_xml,
code, markdown,
line_numbers, line_numbers,
null, null,
): ):
@@ -275,7 +282,7 @@ def cli(
... ...
</documents> </documents>
If the `--code` flag is provided, the output will be structured as follows: If the `--markdown` flag is provided, the output will be structured as follows:
\b \b
path/to/file1.py path/to/file1.py
@@ -316,7 +323,7 @@ def cli(
ignore_patterns, ignore_patterns,
writer, writer,
claude_xml, claude_xml,
code, markdown,
line_numbers, line_numbers,
) )
if claude_xml: if claude_xml:

View File

@@ -377,7 +377,7 @@ def test_paths_from_arguments_and_stdin(tmpdir):
assert "Contents of file2" in result.output assert "Contents of file2" in result.output
def test_code(tmpdir): def test_markdown(tmpdir):
runner = CliRunner() runner = CliRunner()
with tmpdir.as_cwd(): with tmpdir.as_cwd():
os.makedirs("test_dir") os.makedirs("test_dir")
@@ -389,7 +389,7 @@ def test_code(tmpdir):
f.write("This is javascript") f.write("This is javascript")
with open("test_dir/code.unknown", "w") as f: with open("test_dir/code.unknown", "w") as f:
f.write("This is an unknown file type") f.write("This is an unknown file type")
result = runner.invoke(cli, ["test_dir", "--code"]) result = runner.invoke(cli, ["test_dir", "--markdown"])
assert result.exit_code == 0 assert result.exit_code == 0
actual = result.output actual = result.output
expected = ( expected = (