-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgenerate_browse_pages.py
More file actions
212 lines (171 loc) · 6.37 KB
/
Copy pathgenerate_browse_pages.py
File metadata and controls
212 lines (171 loc) · 6.37 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#!/usr/bin/env python3
"""
Generate browse pages for major index terms.
Creates .qmd files that use Quarto listings to filter by index fields.
"""
import yaml
import re
from pathlib import Path
from collections import Counter
VOLUMES_DIR = Path("Volumes")
BROWSE_DIR = Path("browse")
BROWSE_DIR.mkdir(exist_ok=True)
def parse_frontmatter(file_path):
"""Extract YAML frontmatter from .qmd file."""
with open(file_path, 'r', encoding='utf-8') as f:
content = f.read()
match = re.match(r'^---\n(.*?)\n---', content, re.DOTALL)
if not match:
return None
try:
return yaml.safe_load(match.group(1))
except:
return None
def collect_terms():
"""Collect all index terms with counts."""
subjects = Counter()
people = Counter()
places = Counter()
article_files = list(VOLUMES_DIR.rglob("*.qmd"))
for file_path in article_files:
fm = parse_frontmatter(file_path)
if not fm:
continue
if 'subjects' in fm and fm['subjects']:
for subject in fm['subjects']:
if subject and isinstance(subject, str):
subjects[subject] += 1
if 'people' in fm and fm['people']:
for person in fm['people']:
if person and isinstance(person, str):
people[person] += 1
if 'places' in fm and fm['places']:
for place in fm['places']:
if place and isinstance(place, str):
places[place] += 1
return subjects, people, places
def create_subject_page(subject, count):
"""Create a browse page for a subject."""
# Create URL-friendly filename
filename = re.sub(r'[^\w\s-]', '', subject.lower())
filename = re.sub(r'[-\s]+', '_', filename)
file_path = BROWSE_DIR / "subjects" / f"{filename}.qmd"
file_path.parent.mkdir(parents=True, exist_ok=True)
# Create description based on subject
descriptions = {
"Lynching": "W.E.B. Du Bois documented lynching with relentless detail, exposing it as a tool of racial terror rather than a response to crime.",
"Educational inequality": "Du Bois championed liberal arts education and documented systematic inequality in Southern schools.",
"Voting rights": "Articles documenting disfranchisement, poll taxes, grandfather clauses, and the fight for Black political power.",
"Pan-Africanism": "Du Bois's vision of global Black solidarity, including coverage of Pan-African Congresses and anti-colonial movements.",
"Women's suffrage": "Du Bois was an early supporter of women's suffrage, connecting it to Black liberation.",
"Jim Crow laws": "Documentation of segregation laws, their enforcement, and their devastating impact on Black communities.",
}
description = descriptions.get(subject, f"Articles on {subject} from The Crisis (1910-1934)")
content = f'''---
title: "{subject}"
description: "{description}"
listing:
contents: "../../Volumes/**/*.qmd"
type: table
sort: "date"
date-format: "YYYY (MMM)"
page-size: 50
fields: [date, title, description]
filter-ui: true
include:
subjects: "{subject}"
---
## {subject} ({count} articles)
{description}
Use the search box below to find specific articles on this topic.
'''
with open(file_path, 'w', encoding='utf-8') as f:
f.write(content)
return file_path
def create_person_page(person, count):
"""Create a browse page for a person."""
filename = re.sub(r'[^\w\s-]', '', person.lower())
filename = re.sub(r'[-\s]+', '_', filename)
file_path = BROWSE_DIR / "people" / f"{filename}.qmd"
file_path.parent.mkdir(parents=True, exist_ok=True)
content = f'''---
title: "{person}"
description: "Articles discussing {person} from The Crisis (1910-1934)"
listing:
contents: "../../Volumes/**/*.qmd"
type: table
sort: "date"
date-format: "YYYY (MMM)"
page-size: 50
fields: [date, title, description]
filter-ui: true
include:
people: "{person}"
---
## {person} ({count} articles)
Articles from *The Crisis* that substantially discuss {person}.
Use the search box below to find specific articles.
'''
with open(file_path, 'w', encoding='utf-8') as f:
f.write(content)
return file_path
def create_place_page(place, count):
"""Create a browse page for a place."""
filename = re.sub(r'[^\w\s-]', '', place.lower())
filename = re.sub(r'[-\s]+', '_', filename)
file_path = BROWSE_DIR / "places" / f"{filename}.qmd"
file_path.parent.mkdir(parents=True, exist_ok=True)
content = f'''---
title: "{place}"
description: "Articles about {place} from The Crisis (1910-1934)"
listing:
contents: "../../Volumes/**/*.qmd"
type: table
sort: "date"
date-format: "YYYY (MMM)"
page-size: 50
fields: [date, title, description]
filter-ui: true
include:
places: "{place}"
---
## {place} ({count} articles)
Articles from *The Crisis* that focus on {place}.
Use the search box below to find specific articles.
'''
with open(file_path, 'w', encoding='utf-8') as f:
f.write(content)
return file_path
def main():
print("Collecting index terms...")
subjects, people, places = collect_terms()
# Generate pages for top subjects (those with 5+ articles)
print("\nGenerating subject browse pages...")
subject_pages = 0
for subject, count in subjects.most_common():
if count >= 5:
create_subject_page(subject, count)
subject_pages += 1
print(f" ✅ {subject} ({count})")
# Generate pages for top people (those with 5+ articles)
print("\nGenerating people browse pages...")
people_pages = 0
for person, count in people.most_common():
if count >= 5 and 'Du Bois' not in person: # Skip DuBois (author of all)
create_person_page(person, count)
people_pages += 1
print(f" ✅ {person} ({count})")
# Generate pages for top places (those with 10+ articles)
print("\nGenerating place browse pages...")
place_pages = 0
for place, count in places.most_common():
if count >= 10:
create_place_page(place, count)
place_pages += 1
print(f" ✅ {place} ({count})")
print(f"\n{'='*80}")
print(f"Generated {subject_pages} subject pages")
print(f"Generated {people_pages} people pages")
print(f"Generated {place_pages} place pages")
if __name__ == "__main__":
main()