Implement rp CLI: rich-print markdown, JSON, code, and more
- Add typer CLI with auto file-type detection by extension - Support markdown (rich.Markdown), JSON (rich.JSON), and 30+ code languages via Pygments syntax highlighting - Features: line numbers toggle, pager support, stdin input, configurable color themes - Project metadata: MIT license, author, classifiers, entry point
This commit is contained in:
83
src/rp/detect.py
Normal file
83
src/rp/detect.py
Normal file
@@ -0,0 +1,83 @@
|
||||
"""File type detection for rp."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
from pathlib import Path
|
||||
|
||||
EXTENSION_MAP: dict[str, str] = {
|
||||
".md": "markdown",
|
||||
".json": "json",
|
||||
".py": "python",
|
||||
".pyw": "python",
|
||||
".js": "javascript",
|
||||
".mjs": "javascript",
|
||||
".ts": "typescript",
|
||||
".tsx": "typescript",
|
||||
".jsx": "jsx",
|
||||
".css": "css",
|
||||
".scss": "scss",
|
||||
".html": "html",
|
||||
".htm": "html",
|
||||
".xml": "xml",
|
||||
".yaml": "yaml",
|
||||
".yml": "yaml",
|
||||
".toml": "toml",
|
||||
".sh": "bash",
|
||||
".bash": "bash",
|
||||
".zsh": "bash",
|
||||
".sql": "sql",
|
||||
".rs": "rust",
|
||||
".go": "go",
|
||||
".java": "java",
|
||||
".c": "c",
|
||||
".h": "c",
|
||||
".cpp": "cpp",
|
||||
".hpp": "cpp",
|
||||
".cc": "cpp",
|
||||
".rb": "ruby",
|
||||
".php": "php",
|
||||
".swift": "swift",
|
||||
".kt": "kotlin",
|
||||
".r": "r",
|
||||
".R": "r",
|
||||
".lua": "lua",
|
||||
".dockerfile": "docker",
|
||||
".ini": "ini",
|
||||
".cfg": "ini",
|
||||
".diff": "diff",
|
||||
".patch": "diff",
|
||||
".tex": "latex",
|
||||
}
|
||||
|
||||
_FILENAME_MAP: dict[str, str] = {
|
||||
"Dockerfile": "docker",
|
||||
"Makefile": "make",
|
||||
}
|
||||
|
||||
|
||||
def detect_type(
|
||||
path: str | None = None,
|
||||
content: str | None = None,
|
||||
explicit_type: str | None = None,
|
||||
) -> str:
|
||||
if explicit_type is not None:
|
||||
return explicit_type
|
||||
|
||||
if path is not None:
|
||||
p = Path(path)
|
||||
name = p.name
|
||||
if name in _FILENAME_MAP:
|
||||
return _FILENAME_MAP[name]
|
||||
suffix = p.suffix.lower()
|
||||
if suffix in EXTENSION_MAP:
|
||||
return EXTENSION_MAP[suffix]
|
||||
|
||||
if content is not None:
|
||||
try:
|
||||
json.loads(content[:1000])
|
||||
return "json"
|
||||
except (json.JSONDecodeError, ValueError):
|
||||
pass
|
||||
|
||||
return "text"
|
||||
Reference in New Issue
Block a user