diff --git a/yaml_to_html.py b/yaml_to_html.py index 87e8cce..5cbd184 100755 --- a/yaml_to_html.py +++ b/yaml_to_html.py @@ -16,11 +16,14 @@ def linkify_cell(value): def repl(m): url = m.group(1) - href = url if url.lower().startswith(("http://", "https://")) else f"https://{url}" + href = ( + url if url.lower().startswith(("http://", "https://")) else f"https://{url}" + ) return f'{url}' return URL_RE.sub(repl, value) + yaml_file = "problems.yaml" html_dir = "docs/" @@ -31,9 +34,6 @@ def repl(m): html_index = f"{html_dir}index.html" html_table_template = f"{html_dir}table_template.html" -# Load data -with open(yaml_file) as yaml_input: - data = pd.json_normalize(yaml.safe_load(yaml_input)) # Choose desired columns all_columns = False @@ -51,55 +51,61 @@ def repl(m): "reference", "implementation"] -if all_columns is False: - columns = default_columns - data = data[columns] - -data = data.map(linkify_cell) - -# Generate plain table -table = data.to_html(render_links=False, - escape=False, # Don't escape HTML in cells (to allow links) - index=False, - table_id="problems", - classes=["display compact", "display", "styled-table"], # Set display style - border=0, - na_rep="") # Leave NaN cells empty - -# Add footer to facilitate individual column search -idx = table.index('') -final_table = table[:idx] + "" + " ".join([""+ i +"" for i in data.columns])+" " + table[idx:] - -default_hidden_columns = {"textual description", "reference", "implementation"} - -column_toggles = "".join( - [ - ( - f'' - ) - for i, col in enumerate(data.columns) - ] -) - -with open(html_table_template, encoding="utf-8") as template_file: - table_template = template_file.read() - -table_markup = ( - table_template - .replace("__COLUMN_TOGGLES__", column_toggles) - .replace("__TABLE__", final_table) -) - -# Write table to file -with open(html_table, "w", encoding="utf-8") as table_file: - table_file.write(table_markup) - -# Merge table and scripts into HTML page -with open(html_index, "wb") as output_file: - for part_path in [html_header, html_table, html_scripts, html_footer]: - with open(part_path, "rb") as part_file: - shutil.copyfileobj(part_file, output_file) +if __name__ == "__main__": + # Load data + with open(yaml_file) as yaml_input: + data = pd.json_normalize(yaml.safe_load(yaml_input)) + + + if all_columns is False: + columns = default_columns + data = data[columns] + + data = data.map(linkify_cell) + + # Generate plain table + table = data.to_html(render_links=False, + escape=False, # Don't escape HTML in cells (to allow links) + index=False, + table_id="problems", + classes=["display compact", "display", "styled-table"], # Set display style + border=0, + na_rep="") # Leave NaN cells empty + + # Add footer to facilitate individual column search + idx = table.index('') + final_table = table[:idx] + "" + " ".join([""+ i +"" for i in data.columns])+" " + table[idx:] + + default_hidden_columns = {"textual description", "reference", "implementation"} + + column_toggles = "".join( + [ + ( + f'' + ) + for i, col in enumerate(data.columns) + ] + ) + + with open(html_table_template, encoding="utf-8") as template_file: + table_template = template_file.read() + + table_markup = ( + table_template + .replace("__COLUMN_TOGGLES__", column_toggles) + .replace("__TABLE__", final_table) + ) + + # Write table to file + with open(html_table, "w", encoding="utf-8") as table_file: + table_file.write(table_markup) + + # Merge table and scripts into HTML page + with open(html_index, "wb") as output_file: + for part_path in [html_header, html_table, html_scripts, html_footer]: + with open(part_path, "rb") as part_file: + shutil.copyfileobj(part_file, output_file)