1+ from logging import debug
12from pathlib import Path
23from typing import Annotated
34from xml .dom .minidom import Document , Element , parseString , Node
5+ from xml .dom .xmlbuilder import DOMBuilder
46
57import typer
68from buzz import require_condition
@@ -24,15 +26,18 @@ def get_html_path() -> Path:
2426 return Path ("index.html" )
2527
2628
27- def build_page (color : ColorScheme ) -> Path :
28- logger .debug (f"Building PDF using scheme { color } " )
29+ def build_page (color : ColorScheme , debug : bool = False ) -> Path :
30+ logger .debug (f"Building page using scheme { color } " )
2931 md_path = Path ("README.md" )
3032
3133 html_content = markdown (md_path .read_text ())
3234 html_content = fill_html (html_content , color )
3335 html_content = inject_divs (html_content )
3436 html_content = inject_photo (html_content )
3537 html_content = tag_emojis (html_content )
38+ html_content = add_download_button (html_content )
39+ if debug :
40+ html_content = inject_live_script (html_content )
3641
3742 html_path = get_html_path ()
3843 logger .debug (f"Writing HTML file to { html_path } " )
@@ -59,10 +64,13 @@ def _pretty_html(dom: Document) -> str:
5964 return "\n " .join ([l for l in dom .toprettyxml (indent = " " ).split ("\n " ) if l .strip ()])
6065
6166
62- def find_element (parent : Document | Element , tag_name : str , class_name : str ) -> Node :
67+ def find_element (parent : Document | Element , tag_name : str , class_name : str | None = None ) -> Node :
6368 elements = []
6469 for node in parent .getElementsByTagName (tag_name ):
65- if node .getAttribute ("class" ) == class_name :
70+ if class_name :
71+ if node .getAttribute ("class" ) == class_name :
72+ elements .append (node )
73+ else :
6674 elements .append (node )
6775 require_condition (
6876 len (elements ) == 1 ,
@@ -77,10 +85,10 @@ def fill_html(html: str, color: ColorScheme) -> str:
7785 html = f"""
7886 <html>
7987 <head>
80- <meta charset="UTF -8" />
88+ <meta charset="utf -8" />
8189 <title>Tucker Beck Resumé</title>
82- <link rel="stylesheet" href="css/styles.css" />
83- <link rel="stylesheet" href="css/{ color } .css" />
90+ <link rel="stylesheet" type="text/css" href="static/ css/styles.css" />
91+ <link rel="stylesheet" type="text/css" href="static/ css/{ color } .css" />
8492 </head>
8593 <body>
8694 { html }
@@ -94,13 +102,13 @@ def fill_html(html: str, color: ColorScheme) -> str:
94102def inject_photo (html : str ) -> str :
95103 logger .debug ("Injecting photo into HTML" )
96104 dom = parseString (html )
97- header_div = find_element (dom , "div" , "header" )
105+ header_div = find_element (dom , "div" , class_name = "header" )
98106
99107 header_photo_div = dom .createElement ("div" )
100108 header_photo_div .setAttribute ("class" , "header-photo" )
101109 header_div .insertBefore (header_photo_div , header_div .firstChild )
102110 header_photo_img = dom .createElement ("img" )
103- header_photo_img .setAttribute ("src" , "images/me.png" )
111+ header_photo_img .setAttribute ("src" , "static/ images/me.png" )
104112 header_photo_img .setAttribute ("alt" , "Tucker Beck Photo" )
105113 header_photo_div .appendChild (header_photo_img )
106114
@@ -170,7 +178,7 @@ def tag_emojis(html: str) -> str:
170178
171179 dom = parseString (html )
172180
173- contact_list_div = find_element (dom , "div" , "contact_list" )
181+ contact_list_div = find_element (dom , "div" , class_name = "contact_list" )
174182 li_elements = contact_list_div .getElementsByTagName ("li" )
175183 for li in li_elements :
176184 text_node = li .firstChild
@@ -186,3 +194,40 @@ def tag_emojis(html: str) -> str:
186194 _move_nodes_in_place (contact_div .nextSibling , li .childNodes [- 1 ], contact_div )
187195
188196 return _pretty_html (dom )
197+
198+
199+ def add_download_button (html : str ) -> str :
200+ logger .debug ("Adding download/print button" )
201+
202+ dom = parseString (html )
203+
204+ sidebar_div = find_element (dom , "div" , class_name = "sidebar" )
205+
206+ text = dom .createTextNode ("Print (Download PDF)" )
207+
208+ span = dom .createElement ("span" )
209+ span .setAttribute ("id" , "button-span" )
210+ span .appendChild (text )
211+
212+ button = dom .createElement ("button" )
213+ button .setAttribute ("id" , "download-button" )
214+ button .setAttribute ("onClick" , "window.print()" )
215+ button .setAttribute ("class" , "no-print" )
216+ button .appendChild (span )
217+ sidebar_div .appendChild (button )
218+
219+ return _pretty_html (dom )
220+
221+
222+ def inject_live_script (html : str ) -> str :
223+ logger .debug ("Adding live reload script" )
224+
225+ dom = parseString (html )
226+
227+ head = find_element (dom , "head" )
228+ meta = dom .createElement ("meta" )
229+ meta .setAttribute ("http-equiv" , "refresh" )
230+ meta .setAttribute ("content" , "3" )
231+ head .appendChild (meta )
232+
233+ return _pretty_html (dom )
0 commit comments