-
Notifications
You must be signed in to change notification settings - Fork 1
/
generate.py
129 lines (106 loc) · 4.75 KB
/
generate.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
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
import argparse
import datetime
import json
import pathlib
import markdown
from bs4 import BeautifulSoup
from jinja2 import Environment, FileSystemLoader
jinja_env = Environment(
loader=FileSystemLoader("templates")
)
area_order = [
{"title": "Introduction to WOLFcon AI Pre-conference Workshop", "link": "/intro-pre-conference/index.html" },
{"title": "General Overview of AI and Machine Learning for Libraries", "link": "/types-of-ai-ml-relevant-to-libraries/index.html"},
{"title": "Exploring Large Language Models", "link": "/exploring-llms/index.html"},
{"title": "Ethical Considerations on using AI and Machine Learning for Libraries", "link": "/ethical-considerations-ai-ml-for-libraries/index.html"},
{"title": "FOLIO AI Use Cases", "link": "/folio-ai-use-cases/index.html"},
{"title": "Next Steps for Adopting AI and Machine Learning in FOLIO", "link": "/next-steps-for-adopting-ai-and-ml-in-folio/index.html"},
{"title": "Recommended Resources for Further Learning", "link": "/recommended-resources-for-further-learning/index.html"}
]
def generate_page_navigation(lookup, area_title, topic_title, public=False):
nav_html = "<ol>"
for area in area_order:
area_link = area['link']
if public:
area_link = f"/wolfcon-2024-ai-workshop{area_link}"
if area['title'] == area_title:
nav_html += f"""<li><a href="{area_link}">{area_title}</a><ul>"""
for link, title in lookup.items():
if title == topic_title:
nav_html += f"""<li><strong>{topic_title}</strong></li>"""
else:
nav_html += f"""<li><a href="{link}">{title}</a></li>"""
nav_html += "</ul></li>"
else:
nav_html += f"""<li><a href="{area_link}">{area['title']}</a></li>"""
nav_html += "</ol>"
return nav_html
def get_title(html):
soup = BeautifulSoup(html, "html.parser")
h1 = soup.find("h1")
return h1.text
def navigation_lookup(index):
lookup = {}
soup = BeautifulSoup(markdown.markdown(index.read_text()), "html.parser")
area_title = soup.find("h1").text
topics = soup.find_all("li")
for topic in topics:
link = topic.find("a")
if link is None:
continue
lookup[link.attrs['href']] = link.text
return lookup
def home_page(is_public=False):
quotes_path = pathlib.Path("quotes.json")
with quotes_path.open() as fo:
quotes=json.load(fo)
home_template = jinja_env.get_template("quotes-carousel.html")
page_navigation = "<ol>"
for area in area_order:
link = area['link']
if is_public:
link = f"/wolfcon-2024-ai-workshop{link}"
page_navigation += f"""<li><a href="{link}">{area['title']}</a></li>"""
page_navigation += "</ol>"
index_html = home_template.render(
quotes=quotes,
title="Home Page",
page_navigation=page_navigation
)
index_path = pathlib.Path("index.html")
index_path.write_text(index_html)
def html_pages(is_public=False):
base_dir = pathlib.Path(".")
for row in base_dir.iterdir():
if row.name in ["prep", ".ipynb_checkpoints", ".git", "notebooks", "templates"]:
continue
if row.is_dir():
index = row / "index.md"
area_title = get_title(markdown.markdown(index.read_text()))
print(f"Generates pages for {area_title}")
nav_lookup = navigation_lookup(index)
for page in row.glob("*.md"):
page_output = row / f"{page.stem}.html"
#topic_html = markdown.markdown(page.read_text(), extensions=['footnotes', 'attr_list', 'fenced_code'])
topic_html = markdown.markdown(page.read_text(), extensions=['extra'])
page_title = get_title(topic_html)
match page.stem:
case "i-ching-exercise":
html_template = jinja_env.get_template("i-ching.html")
case _:
html_template = jinja_env.get_template("topic.html")
page_html = html_template.render(
content=topic_html,
title=page_title,
page_navigation=generate_page_navigation(nav_lookup, area_title, page_title, is_public)
)
with page_output.open("w+") as fo:
fo.write(page_html)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--public", help="support for Github pages")
args = parser.parse_args()
current = datetime.datetime.now()
print(f"Generating HTML from Markdown content at {current}")
html_pages(args.public)
home_page(args.public)