Add script to repository
This commit is contained in:
parent
2f1b3fc2c9
commit
a9f74286f8
1 changed files with 89 additions and 0 deletions
89
Syntax Highlighting.py
Normal file
89
Syntax Highlighting.py
Normal file
|
@ -0,0 +1,89 @@
|
|||
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 = {
|
||||
"command_line": "# ls -a",
|
||||
"c": """
|
||||
/* Adding Vectors
|
||||
*
|
||||
* A Program that adds vectors given the components.
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
int main() {
|
||||
float u1, u2, v1, v2; /* Components */
|
||||
|
||||
/* Prompt user for variables */
|
||||
printf("Enter the first component of vector 𝐮 (𝑢₁): ");
|
||||
scanf("%f", &u1);
|
||||
|
||||
printf("Enter the second component of vector 𝐮 (𝑢₂): ");
|
||||
scanf("%f", &u2);
|
||||
|
||||
printf("Enter the first component of vector 𝐯 (𝑣₁): ");
|
||||
scanf("%f", &v1);
|
||||
|
||||
printf("Enter the second component of vector 𝐯 (𝑣₂): ");
|
||||
scanf("%f", &v2);
|
||||
|
||||
/* Print the sum of the vectors */
|
||||
printf("𝐮 + 𝐯 = <%.2f, %.2f>\\n", u1 + v1, u2 + v2);
|
||||
|
||||
return 0;
|
||||
}
|
||||
"""
|
||||
}
|
||||
|
||||
# Define colors for different classes (modify as needed)
|
||||
class_colors = {
|
||||
"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)
|
||||
# Add more class-color mappings as needed
|
||||
}
|
||||
|
||||
# Generate HTML code section to define classes based on colors
|
||||
html_class_definitions = "\n".join(f".{cls} {{ color: {color}; }}" for cls, color in class_colors.items())
|
||||
|
||||
# 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 == "command_line":
|
||||
lexer = get_lexer_by_name("bash")
|
||||
elif snippet_name == "c":
|
||||
lexer = get_lexer_by_name("c")
|
||||
else:
|
||||
lexer = get_lexer_by_name("plaintext")
|
||||
|
||||
# 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}")
|
||||
|
Reference in a new issue