Get Print\ Classes.py to print python dictionary. Get Syntax\ Highlighting.py to generate html code based on per-language dictionaries.
This commit is contained in:
parent
80a47d1527
commit
983bd3ceb9
2 changed files with 86 additions and 38 deletions
|
@ -1,5 +1,6 @@
|
|||
from pygments.lexers import get_all_lexers, get_lexer_by_name
|
||||
from pygments.formatters import HtmlFormatter
|
||||
import re
|
||||
|
||||
def get_class_definitions(language):
|
||||
try:
|
||||
|
@ -9,11 +10,27 @@ def get_class_definitions(language):
|
|||
except Exception as e:
|
||||
print(f"Error: {e}")
|
||||
|
||||
def parse_class_definitions(class_definitions):
|
||||
class_colors = {}
|
||||
pattern = r'\s*\.(.*?) {.*?color: (.*?);'
|
||||
matches = re.findall(pattern, class_definitions)
|
||||
for match in matches:
|
||||
class_name, color = match
|
||||
if not class_name.startswith("linenos"):
|
||||
class_name = class_name.replace("c .", "")
|
||||
class_colors[class_name] = color
|
||||
return class_colors
|
||||
|
||||
def main():
|
||||
language = input("Enter the language: ")
|
||||
class_definitions = get_class_definitions(language)
|
||||
if class_definitions:
|
||||
print(class_definitions)
|
||||
class_colors = parse_class_definitions(class_definitions)
|
||||
print("# Define colors for different classes (modify as needed)")
|
||||
print("class_colors = {")
|
||||
for class_name, color in class_colors.items():
|
||||
print(f" \"{class_name}\": \"{color}\",")
|
||||
print("}")
|
||||
else:
|
||||
print(f"No class definitions found for language '{language}'.")
|
||||
|
||||
|
|
|
@ -5,41 +5,47 @@ import os # Import the os module
|
|||
|
||||
# Define the code snippets and their respective languages
|
||||
code_snippets = {
|
||||
"command_line": "# ls -a",
|
||||
# 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": """
|
||||
/* Adding Vectors
|
||||
*
|
||||
* A Program that adds vectors given the components.
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#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);
|
||||
int main() {
|
||||
printf("This is an example");
|
||||
|
||||
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;
|
||||
}
|
||||
"""
|
||||
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 = {
|
||||
class_colors_c = {
|
||||
"cw": "#ffffff", # Default text color (white)
|
||||
"c": "#ff0000", # Comments (red)
|
||||
"nf": "#00ff00", # Numbers (green)
|
||||
|
@ -51,13 +57,30 @@ class_colors = {
|
|||
"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
|
||||
"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)
|
||||
}
|
||||
|
||||
# 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())
|
||||
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):
|
||||
|
@ -66,12 +89,20 @@ if not os.path.exists(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":
|
||||
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:
|
||||
lexer = get_lexer_by_name("plaintext")
|
||||
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()
|
||||
|
|
Reference in a new issue