-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
71 lines (62 loc) · 2.86 KB
/
main.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
import frontmatter
import markdown2
import os
from bs4 import BeautifulSoup
from jinja2 import Environment, FileSystemLoader
def generate_table_rows(talks_dir):
rows = []
for filename in os.listdir(talks_dir):
if filename.endswith(".md") and not filename.startswith("_"):
filepath = os.path.join(talks_dir, filename)
with open(filepath, 'r') as f:
post = frontmatter.load(f)
title = post.metadata.get('title', 'No Title')
year = post.metadata.get('year', '')
labels = post.metadata.get('labels', '')
row_html = f'<tr data-labels="{labels}"><td><a href="talks/html/{filename.replace(".md", ".html")}">{title}</a></td><td>{labels}</td><td>{year}</td></tr>'
rows.append(row_html)
return rows
def update_html_with_table_rows(html_file_path, rows):
with open(html_file_path, 'r', encoding='utf-8') as file:
soup = BeautifulSoup(file, 'html.parser')
tbody = soup.find('tbody')
tbody.clear()
for row_html in rows:
new_row = BeautifulSoup(row_html, 'html.parser').tr
tbody.append(new_row)
with open(html_file_path, 'w', encoding='utf-8') as file:
file.write(str(soup))
def generate_talk_pages(talks_dir, template_path, output_dir):
env = Environment(loader=FileSystemLoader('.'))
template = env.get_template(template_path)
for filename in os.listdir(talks_dir):
if filename.endswith(".md") and not filename.startswith("_"):
filepath = os.path.join(talks_dir, filename)
with open(filepath, 'r') as f:
post = frontmatter.load(f)
title = post.metadata.get('title', 'No Title')
year = post.metadata.get('year', '')
labels = post.metadata.get('labels', '').split(', ')
summary = post.content.split('\n')[0]
markdown_content = post.content
html_content = markdown2.markdown(markdown_content)
rendered_html = template.render(
title=title,
year=year,
labels=labels,
summary=summary,
content=html_content,
)
output_filename = filename.replace('.md', '.html')
output_path = os.path.join(output_dir, output_filename)
os.makedirs(output_dir, exist_ok=True)
with open(output_path, 'w', encoding='utf-8') as out_file:
out_file.write(rendered_html)
if __name__ == "__main__":
talks_dir = './talks'
html_file_path = 'index.html'
template_path = 'template.html'
talks_output_dir = talks_dir + '/html'
rows = generate_table_rows(talks_dir)
update_html_with_table_rows(html_file_path, rows)
generate_talk_pages(talks_dir, template_path, talks_output_dir)