From d0435f985eed19ce407f4d4bde2cc6edbaee4218 Mon Sep 17 00:00:00 2001 From: Out Of Ideas Date: Tue, 21 May 2024 22:42:31 -0500 Subject: [PATCH] Font weights --- Print Classes.py | 40 +++++++++++++++++++++------ Syntax Highlighting.py | 61 ++++++++++++++++++++++++++++++++++-------- 2 files changed, 82 insertions(+), 19 deletions(-) diff --git a/Print Classes.py b/Print Classes.py index 8f2f32b..9bf2d39 100644 --- a/Print Classes.py +++ b/Print Classes.py @@ -8,22 +8,36 @@ def get_class_definitions(): return style_defs except Exception as e: print(f"Error: {e}") + return "" def parse_class_definitions(class_definitions): class_colors = {} - pattern = r'\s*\.(.*?) {.*?color: (#[0-9A-Fa-f]+).*?}(?: /\* (.*?) \*/)?' - matches = re.findall(pattern, class_definitions) - for match in matches: + class_weights = {} + + color_pattern = r'\.(\w+) {[^}]*?color: (#[0-9A-Fa-f]+)[^}]*}(?: /\* (.*?) \*/)?' + weight_pattern = r'\.(\w+) {[^}]*?(font-(weight|style): \w+)[^}]*}(?: /\* (.*?) \*/)?' + + color_matches = re.findall(color_pattern, class_definitions) + weight_matches = re.findall(weight_pattern, class_definitions) + + for match in color_matches: class_name, color, comment = match - if not class_name.startswith("linenos"): - comment = comment.strip() if comment else "" - class_colors[class_name] = (color, comment) - return class_colors + comment = comment.strip() if comment else "" + class_colors[class_name] = (color, comment) + + for match in weight_matches: + class_name, weight, _, comment = match + comment = comment.strip() if comment else "" + class_weights[class_name] = (weight, comment) + + return class_colors, class_weights def main(): class_definitions = get_class_definitions() if class_definitions: - class_colors = parse_class_definitions(class_definitions) + class_colors, class_weights = parse_class_definitions(class_definitions) + + # Print class colors print("# Define colors for different classes (modify as needed)") print("class_colors = {") for class_name, (color, comment) in class_colors.items(): @@ -32,6 +46,16 @@ def main(): else: print(f' "{class_name}": "{color}",') print("}") + + # Print class weights + print("\n# Define weights for different classes (modify as needed)") + print("class_weights = {") + for class_name, (weight, comment) in class_weights.items(): + if comment: + print(f' "{class_name}": "{weight}", # {comment}') + else: + print(f' "{class_name}": "{weight}",') + print("}") else: print("No class definitions found.") diff --git a/Syntax Highlighting.py b/Syntax Highlighting.py index 4eef87b..3b61f30 100644 --- a/Syntax Highlighting.py +++ b/Syntax Highlighting.py @@ -1,23 +1,21 @@ from pygments import highlight from pygments.lexers import get_lexer_by_name from pygments.formatters import HtmlFormatter -import os # Import the os module +import os # 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. "bash": 'echo "This is an example"', "c": """ #include #include int main() { - printf("This is an example"); + printf("This is an example"); - return 0; + return 0; } - """, + """, "html": """ @@ -41,10 +39,10 @@ int main() {

This is an example

-""" +""" } -# Define colors for different classes (modify as needed) +# Define colors and weights for different classes (modify as needed) class_colors = { "hll": "#ffffcc", "c": "#3D7B7B", # Comment @@ -113,6 +111,36 @@ class_colors = { "il": "#666666", # Literal.Number.Integer.Long } +# Define weights for different classes (modify as needed) +class_weights = { + "c": "font-style: italic", # Comment + "k": "font-weight: bold", # Keyword + "ch": "font-style: italic", # Comment.Hashbang + "cm": "font-style: italic", # Comment.Multiline + "cpf": "font-style: italic", # Comment.PreprocFile + "c1": "font-style: italic", # Comment.Single + "cs": "font-style: italic", # Comment.Special + "ge": "font-style: italic", # Generic.Emph + "ges": "font-weight: bold", # Generic.EmphStrong + "gh": "font-weight: bold", # Generic.Heading + "gp": "font-weight: bold", # Generic.Prompt + "gs": "font-weight: bold", # Generic.Strong + "gu": "font-weight: bold", # Generic.Subheading + "kc": "font-weight: bold", # Keyword.Constant + "kd": "font-weight: bold", # Keyword.Declaration + "kn": "font-weight: bold", # Keyword.Namespace + "kr": "font-weight: bold", # Keyword.Reserved + "nc": "font-weight: bold", # Name.Class + "ni": "font-weight: bold", # Name.Entity + "ne": "font-weight: bold", # Name.Exception + "nn": "font-weight: bold", # Name.Namespace + "nt": "font-weight: bold", # Name.Tag + "ow": "font-weight: bold", # Operator.Word + "sd": "font-style: italic", # Literal.String.Doc + "se": "font-weight: bold", # Literal.String.Escape + "si": "font-weight: bold", # Literal.String.Interpol +} + # Create a directory to store the generated HTML files output_dir = "highlighted_code" if not os.path.exists(output_dir): @@ -130,8 +158,20 @@ for snippet_name, code in code_snippets.items(): 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()) + # Generate class styles for the specific language + css_class_definitions = [] + # Create a set of all class names + all_classes = sorted(set(class_colors.keys()) | set(class_weights.keys())) + # Add class definitions for each class, ensuring alphabetical order + for cls in all_classes: + color = class_colors.get(cls) + weight = class_weights.get(cls) + if color is not None: + css_class_definitions.append(f".{cls} {{ color: {color}; {weight or ''} }}") + elif weight is not None: + css_class_definitions.append(f".{cls} {{ {weight} }}") + + html_class_definitions = "\n".join(css_class_definitions) # Highlight the code formatter = HtmlFormatter() @@ -146,4 +186,3 @@ for snippet_name, code in code_snippets.items(): f.write(html_code) print(f"Generated HTML file for '{snippet_name}' code snippet: {output_file_path}") -