Skip to content

Instantly share code, notes, and snippets.

@caioluders
Created December 8, 2023 21:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save caioluders/fd4cb237b43e5eb6e38a9a650f4707e5 to your computer and use it in GitHub Desktop.
Save caioluders/fd4cb237b43e5eb6e38a9a650f4707e5 to your computer and use it in GitHub Desktop.
gpt made it not my fault does not works. You're supposed to have css rules for the colors
import sys
import re
def ansi_to_html_custom_tags(text):
# Define the mapping from ANSI codes to custom HTML tags
ansi_to_html_map = {
'\x1b[0m': '<g1>', # Reset to default, removing the closing tag
'\x1b[1;30m': '<g2>', # Gray2
'\x1b[1;47m': '<ww>', # White
'\x1b[37;47m': '<ww>', # White
'\x1b[37m': '<ww>', # White
'\x1b[40m': '<ww>', # Originally Gray1, now changed to White
'\x1b[30m': '<g2>', # Black or dark gray, using g2
}
# Remove any ANSI sequences that are not explicitly mapped
text = re.sub(r'\x1b\[\d+(;\d+)*m', lambda match: ansi_to_html_map.get(match.group(), ''), text)
return text
def main(input_file, output_file):
try:
with open(input_file, 'r', encoding='utf-8') as file:
content = file.read()
transformed_content = ansi_to_html_custom_tags(content)
with open(output_file, 'w', encoding='utf-8') as file:
file.write(transformed_content)
print(f"File converted successfully and saved to {output_file}")
except Exception as e:
print(f"An error occurred: {e}")
if __name__ == "__main__":
if len(sys.argv) != 3:
print("Usage: python ansi_to_html.py <input_file> <output_file>")
else:
main(sys.argv[1], sys.argv[2])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment