-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgen.py
More file actions
314 lines (274 loc) · 10.5 KB
/
gen.py
File metadata and controls
314 lines (274 loc) · 10.5 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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
import json
import os
from html import escape
def load_list(json_file_path):
"""Read a list.json. If it's a manifest of the form
{"parts": ["list_0000001.json", ...]} (produced by split_list.py),
load all parts and concatenate them. Otherwise return the JSON as-is."""
with open(json_file_path, 'r') as file:
data = json.load(file)
if isinstance(data, dict) and isinstance(data.get("parts"), list):
base_dir = os.path.dirname(json_file_path)
entries = []
for part_name in data["parts"]:
with open(os.path.join(base_dir, part_name), 'r') as pf:
entries.extend(json.load(pf))
return entries
return data
def generate_html(json_file_path, output_file_path):
# Read the JSON file (may be a manifest of split parts)
papers = load_list(json_file_path)
# Generate the HTML content
html_content = f"""
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Papers</title>
<link rel="stylesheet" href="styles.css">
<style>
main {{
padding-top: 0;
}}
.intro p {{
margin: 6px 0;
}}
.search-inputs {{
display: flex;
gap: 10px;
margin: 16px 0;
flex-wrap: wrap;
align-items: center;
}}
.search-inputs input {{
flex: 1;
min-width: 180px;
padding: 10px;
border: 1px solid var(--hair);
border-radius: 10px;
outline: none;
font-size: 14px;
background: transparent;
color: var(--text);
}}
.search-inputs input:focus {{
border-color: rgba(0, 0, 0, 0.25);
}}
.search-inputs button {{
border: 1px solid var(--hair);
background: transparent;
padding: 9px 10px;
border-radius: 10px;
font-size: 13px;
cursor: pointer;
color: var(--text);
}}
.search-inputs button:hover {{
border-color: rgba(0, 0, 0, 0.25);
}}
#paperCount {{
font-weight: 600;
font-size: 14px;
margin: 0;
}}
#searchCount {{
display: block;
margin-top: 4px;
font-size: 12px;
color: var(--muted);
}}
table {{
width: 100%;
border-collapse: collapse;
margin-top: 14px;
font-size: 14px;
}}
thead th {{
text-align: left;
font-weight: 600;
font-size: 12px;
color: var(--muted);
padding: 10px 8px;
border-bottom: 1px solid var(--hair);
cursor: pointer;
user-select: none;
white-space: nowrap;
}}
thead th:hover {{
color: var(--text);
}}
tbody td {{
padding: 12px 8px;
border-bottom: 1px solid var(--hair2);
vertical-align: top;
}}
@media (max-width: 720px) {{
body {{
margin: 0;
padding: 0;
}}
main {{
padding: 0 20px;
margin: 24px auto;
}}
.search-inputs input {{
min-width: 100%;
}}
}}
</style>
</head>
<body>
<nav>
<a href="index.html">Home</a> ·
<span class="cv-dropdown"><a href="#">CV</a><span class="cv-dropdown-menu"><a href="cv/main.pdf">English</a><a href="cv_ja/main.pdf">日本語</a></span></span> ·
<a href="https://www.linkedin.com/in/lxaw/">LinkedIn</a> ·
<a href="https://github.com/lxaw">GitHub</a> ·
<a href="books_read.html">Books</a> ·
<a href="papers_read.html">Papers</a> ·
<a href="posts.html">Posts</a> ·
<a href="personal_interests.html">Personal</a>
</nav>
<main>
<div class="intro">
<p>This list was curated by <a href="index.html">myself</a>, beginning from about May 2024 to now.</p>
<p class="muted">I typically use this to organize papers I found interesting. Please feel free to do whatever you want with it. Note that this is not every single paper I have ever read, just a collection of ones that I remember to put down.</p>
</div>
<p id="paperCount">So far, we have read {len(papers)} papers. Let's keep it up!</p>
<small id="searchCount">Your search returned {len(papers)} papers. Nice!</small>
<div class="search-inputs">
<input type="text" id="titleSearch" placeholder="Search title...">
<input type="text" id="authorSearch" placeholder="Search author...">
<input type="text" id="yearSearch" placeholder="Search year...">
<input type="text" id="topicSearch" placeholder="Search topic...">
<input type="text" id="venueSearch" placeholder="Search venue...">
<input type="text" id="descriptionSearch" placeholder="Search description...">
<button id="clearSearch">Clear Search</button>
</div>
<table id="paperTable">
<thead>
<tr>
<th data-sort="title">Title</th>
<th data-sort="author">Author</th>
<th data-sort="year">Year</th>
<th data-sort="topic">Topic</th>
<th data-sort="venue">Venue</th>
<th data-sort="description">Description</th>
</tr>
</thead>
<tbody>
{generate_table_rows(papers)}
</tbody>
</table>
<script>
const table = document.getElementById('paperTable');
const tbody = table.querySelector('tbody');
const clearButton = document.getElementById('clearSearch');
const searchInputs = {{
title: document.getElementById('titleSearch'),
author: document.getElementById('authorSearch'),
year: document.getElementById('yearSearch'),
topic: document.getElementById('topicSearch'),
venue: document.getElementById('venueSearch'),
description: document.getElementById('descriptionSearch')
}};
const paperCountElement = document.getElementById('paperCount');
const searchCountElement = document.getElementById('searchCount');
let sortOrder = {{}};
function setupEventListeners() {{
for (let key in searchInputs) {{
searchInputs[key].addEventListener('keyup', searchTable);
}}
clearButton.addEventListener('click', function() {{
for (let key in searchInputs) {{
searchInputs[key].value = '';
}}
searchTable();
}});
table.querySelector('thead').addEventListener('click', function(e) {{
const th = e.target.closest('th');
if (!th) return;
const column = th.dataset.sort;
const dataType = column === 'year' ? 'number' : 'string';
sortOrder[column] = sortOrder[column] === 'asc' ? 'desc' : 'asc';
sortTable(column, dataType, sortOrder[column]);
}});
}}
function searchTable() {{
const rows = tbody.getElementsByTagName('tr');
let numRowsMatch = 0;
for (let i = 0; i < rows.length; i++) {{
const row = rows[i];
const cells = row.getElementsByTagName('td');
let foundMatch = true;
for (let key in searchInputs) {{
const cellText = cells[Object.keys(searchInputs).indexOf(key)].textContent.toLowerCase();
const searchTerm = searchInputs[key].value.toLowerCase();
if (searchTerm && !cellText.includes(searchTerm)) {{
foundMatch = false;
break;
}}
}}
if(foundMatch) {{
row.style.display = '';
numRowsMatch++;
}} else {{
row.style.display = 'none';
}}
}}
updateSearchCount(numRowsMatch);
}}
function updateSearchCount(count) {{
searchCountElement.textContent = `Your search returned ${{count}} papers. Nice!`;
}}
function sortTable(column, dataType, order) {{
const rows = Array.from(tbody.querySelectorAll('tr'));
const columnIndex = Array.from(table.querySelector('thead tr').children).findIndex(th => th.dataset.sort === column);
rows.sort((a, b) => {{
let aValue = a.children[columnIndex].textContent;
let bValue = b.children[columnIndex].textContent;
if (dataType === 'number') {{
return order === 'asc' ? Number(aValue) - Number(bValue) : Number(bValue) - Number(aValue);
}} else {{
return order === 'asc' ? aValue.localeCompare(bValue) : bValue.localeCompare(aValue);
}}
}});
tbody.append(...rows);
}}
setupEventListeners();
</script>
</main>
</body>
</html>
"""
# Write the HTML content to a file
with open(output_file_path, 'w') as file:
file.write(html_content)
def generate_table_rows(papers):
rows = []
for paper in papers:
title_html = escape(paper['title'])
# Embed link into title if it exists
if paper.get('link'):
title_html = (
f'<a href="{escape(paper["link"])}" '
f'target="_blank" rel="noopener noreferrer">'
f'{title_html}</a>'
)
row = f"""
<tr>
<td>{title_html}</td>
<td>{escape(paper['author'])}</td>
<td>{escape(str(paper['year']))}</td>
<td>{escape(paper['topic'])}</td>
<td>{escape(paper['venue'])}</td>
<td>{escape(paper['description'])}</td>
</tr>
"""
rows.append(row)
return ''.join(rows)
if __name__ == "__main__":
json_file_path = "papers/list.json" # Update this path to your JSON file
output_file_path = "papers_read.html" # The output HTML file
generate_html(json_file_path, output_file_path)
print(f"HTML file generated successfully: {output_file_path}")