This repository has been archived on 2024-05-23. You can view files and clone it, but cannot push or open issues or pull requests.
C-Sy-G/Syntax Highlighting.py

120 lines
3.7 KiB
Python

from pygments import highlight
from pygments.lexers import get_lexer_by_name
from pygments.formatters import HtmlFormatter
import os # Import the os module
# Define the code snippets and their respective languages
code_snippets = {
# These are code examples.
# I recommend only generating HTML code for one language at a time, and using different class_colors.
"command_line": 'echo "This is an example"',
"c": """
#include <stdio.h>
#include <stdlib.h>
int main() {
printf("This is an example");
return 0;
}
""",
"html": """
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Code Example</title>
<style type="text/css">
body {
margin: 40px auto;
max-width: 650px;
line-height: 1.6;
font-size: 18px;
padding: 0 10px;
color: #EBDBB2;
background-color: #1d2021
}
</style>
</head>
<body>
<p>This is an example</p>
</body>
</html>
"""
}
# Define colors for different classes (modify as needed)
class_colors_c = {
"cw": "#ffffff", # Default text color (white)
"c": "#ff0000", # Comments (red)
"nf": "#00ff00", # Numbers (green)
"kd": "#0000ff", # Keywords (blue)
"cm": "#808080", # Comment (gray)
"cp": "#ffff00", # Preprocessor (yellow)
"kt": "#0000ff", # Keyword.Type (blue)
"n": "#00ff00", # Number (green)
"o": "#dcdcaa", # Operator (light yellow)
"s": "#d79921", # String (orange)
"se": "#dcdcaa", # Separator (light yellow)
"w": "#ffffff", # Whitespace (white)
"nc": "#ff00ff", # Class name (magenta)
"kn": "#00ffff", # Keyword (cyan)
"si": "#ff9900", # Special Identifier (orange)
"nb": "#ff66cc", # Built-in name (pink)
"mh": "#cccccc", # Meta information (light gray)
"ss": "#6666ff", # Sub-string (blue)
"ni": "#ffcc66", # Number integer (light orange)
"nd": "#99ccff", # Number decimal (light blue)
"nt": "#ff3366", # HTML tag (red-pink)
"na": "#66ffcc", # Attribute name (light cyan)
}
class_colors_command_line = {
"cw": "#ffffff",
"k": "#FFFF00",
"s": "#00FFFF",
}
class_colors_html = {
"c": "#FFA07A",
"na": "#98FB98",
"nt": "#87CEFA",
}
# Create a directory to store the generated HTML files
output_dir = "highlighted_code"
if not os.path.exists(output_dir):
os.makedirs(output_dir)
# Generate HTML files with syntax-highlighted code snippets
for snippet_name, code in code_snippets.items():
# Choose lexer based on the language
if snippet_name == "c":
lexer = get_lexer_by_name("c")
class_colors = class_colors_c
elif snippet_name == "command_line":
lexer = get_lexer_by_name("bash")
class_colors = class_colors_command_line
elif snippet_name == "html":
lexer = get_lexer_by_name("html")
class_colors = class_colors_html
else:
raise ValueError(f"No lexer for language '{snippet_name}' found.")
# Generate class colors for the specific language
html_class_definitions = "\n".join(f".{cls} {{ color: {color}; }}" for cls, color in class_colors.items())
# Highlight the code
formatter = HtmlFormatter()
highlighted_code = highlight(code, lexer, formatter)
# Combine CSS styles, HTML class definitions, and highlighted code
html_code = f"<style>\n{html_class_definitions}\n</style>\n{highlighted_code}"
# Write the HTML code to a file
output_file_path = os.path.join(output_dir, f"{snippet_name}.html")
with open(output_file_path, "w") as f:
f.write(html_code)
print(f"Generated HTML file for '{snippet_name}' code snippet: {output_file_path}")