From a6b4a353d0c308d00d7aa7e59b60c3bd0cc4e4cf Mon Sep 17 00:00:00 2001 From: 7000pctAUTO Date: Thu, 29 Jan 2026 11:34:24 +0000 Subject: [PATCH] Initial commit: Add http-convert project --- src/http_convert/highlighter.py | 40 +++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 src/http_convert/highlighter.py diff --git a/src/http_convert/highlighter.py b/src/http_convert/highlighter.py new file mode 100644 index 0000000..50c3179 --- /dev/null +++ b/src/http_convert/highlighter.py @@ -0,0 +1,40 @@ +from rich.syntax import Syntax +from rich.console import Console +from io import StringIO + + +class SyntaxHighlighter: + THEMES = { + "curl": "bash", + "httpie": "bash", + "fetch": "javascript", + "axios": "javascript", + } + + @staticmethod + def highlight(code: str, format: str) -> str: + theme = SyntaxHighlighter.THEMES.get(format.lower(), "bash") + + try: + syntax = Syntax(code, theme, word_wrap=True) + console = Console(file=StringIO(), force_terminal=True, width=120) + console.print(syntax) + return console.file.getvalue() + except Exception: + return code + + @staticmethod + def highlight_curl(code: str) -> str: + return SyntaxHighlighter.highlight(code, "curl") + + @staticmethod + def highlight_httpie(code: str) -> str: + return SyntaxHighlighter.highlight(code, "httpie") + + @staticmethod + def highlight_fetch(code: str) -> str: + return SyntaxHighlighter.highlight(code, "fetch") + + @staticmethod + def highlight_axios(code: str) -> str: + return SyntaxHighlighter.highlight(code, "axios")