-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate_toc.py
74 lines (60 loc) · 3.27 KB
/
generate_toc.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
def generate_toc(standard_categories, advanced_categories):
"""
Generates the Table of Contents with two levels of hierarchy and up to 4 columns for stats within each category.
Each column can have a maximum of 10 entries.
"""
html_output = "<nav id='toc-nav'>\n<h2>Stats by Category</h2>\n"
# Top-level "Standard Stats" section
html_output += "<button class='collapsible first standard'>Standard Stats</button>\n"
html_output += "<div class='content'>\n<ul>\n"
for category, stats in standard_categories.items():
html_output += f"<li><button class='collapsible standard'>{category}</button>\n"
html_output += "<div class='content'>\n<div class='toc-category-container'>\n"
# Sort stats alphabetically
sorted_stats = sorted(stats)
# Determine the number of columns and split stats accordingly
max_items_per_column = 10
num_columns = min(4, -(-len(sorted_stats) // max_items_per_column)) # Ceiling division
split_size = len(sorted_stats) // num_columns + (len(sorted_stats) % num_columns > 0)
toc_columns = [
sorted_stats[i * split_size: (i + 1) * split_size] for i in range(num_columns)
]
# Create up to 4 columns
for col_items in toc_columns:
html_output += "<div class='toc-column'><ul>\n"
for stat in col_items:
anchor_link = slugify(stat)
html_output += f"<li><a href='#{anchor_link}'>{stat}</a></li>\n"
html_output += "</ul></div>\n"
html_output += "</div></div></li>\n" # Close category and content divs
html_output += "</ul>\n</div>\n"
# Top-level "Advanced Stats" section
html_output += "<button class='collapsible advanced'>Advanced Stats</button>\n"
html_output += "<div class='content'>\n<ul>\n"
for category, stats in advanced_categories.items():
html_output += f"<li><button class='collapsible advanced'>{category}</button>\n"
html_output += "<div class='content'>\n<div class='toc-category-container'>\n"
# Sort stats alphabetically
sorted_stats = sorted(stats)
# Determine the number of columns and split stats accordingly
num_columns = min(4, -(-len(sorted_stats) // max_items_per_column)) # Ceiling division
split_size = len(sorted_stats) // num_columns + (len(sorted_stats) % num_columns > 0)
toc_columns = [
sorted_stats[i * split_size: (i + 1) * split_size] for i in range(num_columns)
]
# Create up to 4 columns
for col_items in toc_columns:
html_output += "<div class='toc-column'><ul>\n"
for stat in col_items:
anchor_link = slugify(stat)
html_output += f"<li><a href='#{anchor_link}' class='advanced'>{stat}</a></li>\n"
html_output += "</ul></div>\n"
html_output += "</div></div></li>\n" # Close category and content divs
html_output += "</ul>\n</div>\n"
html_output += "</nav>\n"
return html_output
def slugify(text):
"""
Converts text into a slug suitable for HTML id and anchor links.
"""
return text.lower().replace(" ", "-").replace("/", "-").replace("%", "").replace(".", "")