forked from StatProofBook/StatProofBook.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdate_ToC.py
More file actions
109 lines (90 loc) · 4.29 KB
/
update_ToC.py
File metadata and controls
109 lines (90 loc) · 4.29 KB
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
def reconstruct_toc(markdown_content):
"""
Backtransforms a simplified markdown Table of Contents (with unnumbered sections/subsections)
into the original I/ToC.md format, using internal counters for numbering.
Args:
markdown_content (str): The content of the simplified structure.md file.
Returns:
str: The reconstructed I/ToC.md content.
"""
final_output_lines = []
# Helper to convert integer to Roman numeral
def to_roman(num):
romans = {
1: 'I', 2: 'II', 3: 'III', 4: 'IV',
5: 'V', 9: 'IX', 10: 'X', 40: 'XL',
50: 'L', 90: 'XC', 100: 'C', 400: 'CD',
500: 'D', 900: 'CM', 1000: 'M'
}
result = ""
# Iterate in descending order of values to build the Roman numeral
for value, symbol in sorted(romans.items(), reverse=True):
while num >= value:
result += symbol
num -= value
return result
# Initial fixed lines matching I/ToC.md template
final_output_lines.extend([
"---",
"layout: page",
"title: \"Table of Contents\"",
"---",
"", # Blank line
"", # Blank line
"**[Proofs](/P/-temp-)** are printed in **bold** – *[Definitions](/D/-temp-)* are set in *italics* <br>",
"**Proofs**: [by Number](/I/PbN), [by Topic](/I/PbT) – *Definitions*: [by Number](/I/DbN), [by Topic](/I/DbT) <br>",
"<u>Specials:</u> [General Theorems](/S/ChI), [Probability Distributions](/S/ChII), [Statistical Models](/S/ChIII), [Model Selection Criteria](/S/ChIV) <br>",
"", # First <br> before Chapter I h3
"", # Second <br> before Chapter I h3
])
chapter_index = 0
current_section_num = 0
current_subsection_num = 0
current_subsub_num = 0
lines = markdown_content.strip().splitlines()
for i, line_str in enumerate(lines):
line = line_str.strip()
if line.startswith("# Chapter "):
if chapter_index > 0:
final_output_lines.extend(
[""]*2
)
final_output_lines.append("<br>") # Add <br> before subsequent chapters
chapter_index += 1
roman_chapter = to_roman(chapter_index)
chapter_title = line[len("# Chapter ") + line.find(': ') - len("Chapter ")-1 :].strip() # Extracts "General Theorems" etc.
final_output_lines.append(f'<h3 id="{chapter_title}">Chapter {roman_chapter}: {chapter_title}</h3>')
final_output_lines.append("") # Always a blank line after an h3
# Reset counters for a new chapter
current_section_num = 0
current_subsection_num = 0
current_subsub_num = 0
elif line.startswith("## "):
# Reset sub-subsection counter when a new section starts
current_subsection_num = 0
current_subsub_num = 0
current_section_num += 1
section_title = line[len("## "):].strip()
final_output_lines.append(" ") # Indented blank line after section <p> tag, matching original
final_output_lines.append(f'{current_section_num}. <p id="{section_title}">{section_title}</p>')
elif line.startswith("### "):
# Reset sub-subsection counter when a new subsection starts
current_subsub_num = 0
current_subsection_num += 1
subsection_title = line[len("### "):].strip() # Extracts "Random experiments"
# In the original, the p id is the title, and the text below is also the title
final_output_lines.append(" ")
final_output_lines.append(f' <p id="{subsection_title}"></p>')
final_output_lines.append(f' {current_section_num}.{current_subsection_num}. {subsection_title} <br>')
elif line.startswith("- "):
current_subsub_num += 1
item_content = line[len("- "):] # Get content after "- "
final_output_lines.append(f'    {current_section_num}.{current_subsection_num}.{current_subsub_num}. {item_content} <br>')
return "\n".join(final_output_lines)
if __name__ == '__main__':
with open('./book_content.md', 'r') as f:
md = f.read()
with open("./I/ToC.md", 'w') as f:
f.write(
reconstruct_toc(md)
)