77from logger import get_logger
88from pathlib import Path
99
10- yaml_safe = YAML (typ = ' safe' )
10+ yaml_safe = YAML (typ = " safe" )
1111yaml_rw = YAML ()
1212yaml_rw .indent (mapping = 2 , sequence = 4 , offset = 2 )
1313yaml_rw .preserve_quotes = True
1818BASE_DIR = Path (__file__ ).parent
1919PROJECT_ROOT = BASE_DIR .parent
2020
21- OUTPUT_DIR = Path (os .getenv (' CHEATSHEET_OUTPUT_DIR' ) or PROJECT_ROOT / "output" )
21+ OUTPUT_DIR = Path (os .getenv (" CHEATSHEET_OUTPUT_DIR" ) or PROJECT_ROOT / "output" )
2222TEMPLATES_DIR = BASE_DIR / "templates"
2323LAYOUTS_DIR = BASE_DIR / "layouts"
2424CHEATSHEETS_DIR = PROJECT_ROOT / "cheatsheets"
3030
3131def load_yaml (file_path : Path ) -> dict | None :
3232 try :
33- with open (file_path , "r" , encoding = ' utf-8' ) as file :
33+ with open (file_path , "r" , encoding = " utf-8" ) as file :
3434 return yaml_safe .load (file )
3535 except FileNotFoundError :
3636 logging .error (f"Error: YAML file '{ file_path } ' not found." )
@@ -43,54 +43,50 @@ def load_yaml(file_path: Path) -> dict | None:
4343def load_layout ():
4444 keyboard_layouts = load_yaml (LAYOUTS_DIR / "keyboard_layouts.yaml" )
4545 system_mappings = load_yaml (LAYOUTS_DIR / "system_mappings.yaml" )
46-
46+
4747 if keyboard_layouts is None or system_mappings is None :
4848 logging .error ("Failed to load configuration files." )
4949 return None , None
50-
50+
5151 return keyboard_layouts , system_mappings
5252
53+
5354def replace_shortcut_names (shortcut , system_mappings ):
54- arrow_key_mappings = {
55- "Up" : "↑" ,
56- "Down" : "↓" ,
57- "Left" : "←" ,
58- "Right" : "→"
59- }
55+ arrow_key_mappings = {"Up" : "↑" , "Down" : "↓" , "Left" : "←" , "Right" : "→" }
6056 try :
6157 processed_parts = []
6258 i = 0
6359 while i < len (shortcut ):
64- if shortcut [i ] == '+' :
65- if i + 1 < len (shortcut ) and shortcut [i + 1 ] == '+' :
66- processed_parts .append ('+' )
60+ if shortcut [i ] == "+" :
61+ if i + 1 < len (shortcut ) and shortcut [i + 1 ] == "+" :
62+ processed_parts .append ("+" )
6763 i += 2
6864 else :
69- processed_parts .append (' <sep>' )
65+ processed_parts .append (" <sep>" )
7066 i += 1
7167 else :
72- current_part = ''
73- while i < len (shortcut ) and shortcut [i ] != '+' :
68+ current_part = ""
69+ while i < len (shortcut ) and shortcut [i ] != "+" :
7470 current_part += shortcut [i ]
7571 i += 1
7672 if current_part .strip ():
7773 part = current_part .strip ()
7874 part = system_mappings .get (part .lower (), part )
79- if part in ['⌘' , '⌥' , '⌃' , '⇧' ]:
75+ if part in ["⌘" , "⌥" , "⌃" , "⇧" ]:
8076 part = f'<span class="modifier-symbol">{ part } </span>'
8177
8278 part = arrow_key_mappings .get (part , part )
8379 processed_parts .append (part )
8480
85-
86- return '' .join (processed_parts )
81+ return "" .join (processed_parts )
8782 except Exception as e :
88- logging .error (f"Error replacing shortcut names: { e } " )
89- return shortcut
83+ logging .error (f"Error replacing shortcut names: { e } " )
84+ return shortcut
85+
9086
9187def normalize_shortcuts (data , system_mappings ):
9288 normalized = {}
93- allow_text = data .get (' AllowText' , False )
89+ allow_text = data .get (" AllowText" , False )
9490 try :
9591 for section , shortcuts in data .get ("shortcuts" , {}).items ():
9692 normalized [section ] = {}
@@ -112,17 +108,16 @@ def get_layout_info(data):
112108 "system" : layout .get ("system" , "Darwin" ),
113109 }
114110
111+
115112def generate_html (data , keyboard_layouts , system_mappings ):
116113 template_path = "cheatsheets/cheatsheet-template.html"
117114 layout_info = get_layout_info (data )
118- data ["shortcuts" ] = normalize_shortcuts (
119- data , system_mappings .get (layout_info ["system" ], {})
120- )
115+ data ["shortcuts" ] = normalize_shortcuts (data , system_mappings .get (layout_info ["system" ], {}))
121116 data ["layout" ] = layout_info
122117 data ["keyboard_layout" ] = keyboard_layouts .get (layout_info ["keyboard" ], {}).get ("layout" )
123118 data ["render_keys" ] = data .get ("RenderKeys" , True )
124119 data ["allow_text" ] = data .get ("AllowText" , False )
125-
120+
126121 return render_template (template_path , data )
127122
128123
@@ -141,15 +136,17 @@ def validate_and_lint(yaml_file):
141136
142137 return True
143138
139+
144140def write_html_content (html_output , html_content ):
145141 try :
146- with open (html_output , "w" , encoding = ' utf-8' ) as file :
142+ with open (html_output , "w" , encoding = " utf-8" ) as file :
147143 file .write (html_content )
148144 except IOError as e :
149145 logging .error (f"Error writing to output file: { e } " )
150146 return False
151147 return True
152148
149+
153150def main (yaml_file ):
154151 if not validate_and_lint (yaml_file ):
155152 return None , None
@@ -199,7 +196,7 @@ def generate_index(cheatsheets):
199196
200197 if cheatsheets :
201198 OUTPUT_DIR .mkdir (exist_ok = True , parents = True )
202-
199+
203200 html_content = generate_index (cheatsheets )
204201 if html_content :
205202 index_output = os .path .join (OUTPUT_DIR , "index.html" )
0 commit comments