Created
January 27, 2026 00:40
-
-
Save davidlee/09b5d85b5b17a5f0634c22fe279a58cc to your computer and use it in GitHub Desktop.
little python script to pack files, in markdown code fences & with paths, into a .md file - for upload to your web-based agent of choice
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python3 | |
| """Pack files into a markdown document with code fences for LLM context upload.""" | |
| import sys | |
| from pathlib import Path | |
| # Common extension -> fence language mappings | |
| LANG_MAP = { | |
| ".py": "python", | |
| ".js": "javascript", | |
| ".ts": "typescript", | |
| ".tsx": "tsx", | |
| ".jsx": "jsx", | |
| ".zig": "zig", | |
| ".rs": "rust", | |
| ".go": "go", | |
| ".c": "c", | |
| ".cpp": "cpp", | |
| ".h": "c", | |
| ".hpp": "cpp", | |
| ".java": "java", | |
| ".rb": "ruby", | |
| ".sh": "bash", | |
| ".bash": "bash", | |
| ".zsh": "zsh", | |
| ".json": "json", | |
| ".yaml": "yaml", | |
| ".yml": "yaml", | |
| ".toml": "toml", | |
| ".md": "markdown", | |
| ".sql": "sql", | |
| ".html": "html", | |
| ".css": "css", | |
| ".lua": "lua", | |
| } | |
| def ext_to_lang(path: Path) -> str: | |
| ext = path.suffix.lower() | |
| return LANG_MAP.get(ext, ext.lstrip(".") or "text") | |
| def main(): | |
| if len(sys.argv) < 2 or sys.argv[1] in ("-h", "--help"): | |
| print( | |
| "Usage: md-context-packer.py <output.md> file1 [file2 ...]\n" | |
| " cat files.txt | md-context-packer.py <output.md>", | |
| file=sys.stderr, | |
| ) | |
| sys.exit(0 if len(sys.argv) > 1 else 1) | |
| output = Path(sys.argv[1]) | |
| # Files from args, or stdin if piped | |
| if len(sys.argv) > 2: | |
| files = [Path(f) for f in sys.argv[2:]] | |
| elif not sys.stdin.isatty(): | |
| files = [Path(line.strip()) for line in sys.stdin if line.strip()] | |
| else: | |
| print("Error: no files provided", file=sys.stderr) | |
| sys.exit(1) | |
| with output.open("w") as out: | |
| for path in files: | |
| if not path.exists(): | |
| print(f"Warning: {path} not found, skipping", file=sys.stderr) | |
| continue | |
| lang = ext_to_lang(path) | |
| content = path.read_text() | |
| out.write(f"## `{path}`\n\n```{lang}\n{content}```\n\n") | |
| print(f"Wrote {len(files)} files to {output}", file=sys.stderr) | |
| if __name__ == "__main__": | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment