From de859c21aca50f59d6dde26389c31086bcbc2828 Mon Sep 17 00:00:00 2001 From: Gregory Leeman Date: Fri, 18 Oct 2024 11:42:53 +0100 Subject: [PATCH] lazygit --- wf/script.py | 219 +++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 202 insertions(+), 17 deletions(-) diff --git a/wf/script.py b/wf/script.py index 82f4275..21c458d 100755 --- a/wf/script.py +++ b/wf/script.py @@ -291,7 +291,7 @@ def simplify_project(project_data, full_data, follow_mirrors=False): # {{{ "name": project_data.get("nm", ""), "id": project_data.get("id", ""), "children": [], - "description": project_data.get("no", ""), + "description": project_data.get("no", "").rstrip().replace("\n$", ""), "format": project_data.get("metadata", {}).get("layoutMode", None) } children = project_data.get("ch", []) @@ -398,8 +398,6 @@ def filter_project_all(project_data, filters, include_headers=False): # {{{ - - def replace(project_data, regex, replacement): # {{{ project_data["name"] = re.sub(regex, replacement, project_data["name"]) children = project_data.get("children", []) @@ -480,7 +478,7 @@ colors2 = { } -def recolor(project_data, colors): +def recolor(project_data, colors): # {{{ for key, value in colors.items(): if key in project_data["name"]: if key.startswith("x"): @@ -488,6 +486,10 @@ def recolor(project_data, colors): else: project_data["name"] = f"[{value}]{project_data['name'].strip()}[/]" break + # if project_data["format"] == "h1": + # project_data["name"] = f"[base3][underline]{project_data['name']}[/]" + # if project_data["format"] == "h2": + # project_data["name"] = f"[base1][underline]{project_data['name']}[/]" children = project_data.get("children", []) for child in children: recolor(child, colors) @@ -497,28 +499,210 @@ def recolor(project_data, colors): def print_pretty(data, indent=0, color="grey", show_description=True, show_id=False): # {{{ try: - # href = f"https://workflowy.com/#/{data['id'].split('-')[4]}" for item in data["children"]: - - if item["format"] == "h1": - console.print("") - console.print(" " * indent + f"[base3]•[/] [base3][underline]{item['name']}[/][/][base01]{' ' + item['id'].split('-')[4] if show_id else ''}[/]") - elif item["format"] == "h2": - console.print(" " * indent + f"[base3]•[/] [base1][underline]{item['name']}[/][/][base01]{' ' + item['id'].split('-')[4] if show_id else ''}[/]") - - else: - console.print(" " * indent + f"[base3]•[/] [{color}]{item['name']}[/][base01]{' ' + item['id'].split('-')[4] if show_id else ''}[/]") - - + console.print(" " * indent + f"[base3]•[/] [{color}]{item['name']}[/][base01]{' ' + item['id'].split('-')[4] if show_id else ''}[/]") if item["description"] and show_description: console.print(" " * (indent + 1) + f"[base01]{item['description'].replace('\n', '\n' + ' ' * (indent + 1))}[/]") - if item["children"]: print_pretty(item, indent + 1, color, show_description=show_description, show_id=show_id) except Exception as e: console.log(f"Error: {e}") + +# }}} + + +def generate_d3_mindmap_html(data, show_description=True, show_id=False): # {{{ + import json + import re + + # Map of Rich tag colors to hex values or CSS color names + color_map = { + "base3": "#073642", + "base2": "#002b36", + "base1": "#586e75", + "base0": "#657b83", + "base00": "#839496", + "base01": "#93a1a1", + "base02": "#eee8d5", + "base03": "#fdf6e3", + "yellow": "#b58900", + "orange": "#cb4b16", + "red": "#dc322f", + "magenta": "#d33682", + "violet": "#6c71c4", + "blue": "#268bd2", + "cyan": "#2aa198", + "green": "#859900", + } + + def parse_rich_tags(text): + text = re.sub(r"\[(underline)? ?([a-zA-Z0-9]+)?\](.*?)\[/\]", + lambda m: f'{m.group(3)}', + text) + return text + + def remove_rich_tags(text): + text = re.sub(r"\[(underline)? ?([a-zA-Z0-9]+)?\](.*?)\[/\]", r"\3", text) + return text + + # Recursively transform data to add the parsed Rich-style text + def transform_data(item): + node = { + "name": remove_rich_tags(parse_rich_tags(item["name"])), # Parse Rich tags in name + "id": item["id"].split("-")[4] if show_id else "", + "description": parse_rich_tags(item["description"]) if show_description else "" + } + if item["children"]: + node["children"] = [transform_data(child) for child in item["children"]] + return node + + transformed_data = transform_data(data) + + # Create the HTML content with D3.js code + html_content = f""" + + + + + + Mind Map + + + + + + + + + + """ + + + + + # Write the HTML content to a file + with open("d3_mindmap.html", "w") as f: + f.write(html_content) + + print("D3.js mind map HTML generated as 'd3_mindmap.html'.") + + # }}} @@ -559,6 +743,7 @@ def show(parent_id, flat=False, filters=None, color="grey", follow_mirrors=False console.print("") print_pretty(project_data, color=color, show_description=show_description, show_id=show_id) console.print("") + generate_d3_mindmap_html(project_data, show_description=show_description, show_id=show_id) return True # }}}