Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 62 additions & 5 deletions src/generate.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
{nav_tree}
</nav>
<main>
<h1>{title}</h1>
{content}
{navigation}
</main>
</body>
</html>"""
Expand Down Expand Up @@ -64,16 +64,36 @@
opacity: 0.9;
border-bottom: none;
}}
</style>
main.landing ol, main.landing ul {{
text-align: left;
display: inline-block;
margin-bottom: 1.5rem;
padding-left: 1.5rem;
}}
main.landing li {{
margin-bottom: 0.8rem;
}}
</style>
</head>
<body>
<main class="landing">
{content}
{navigation}
</main>
</body>
</html>"""


def flatten_tree(node):
files = []
if isinstance(node, FileNode):
files.append(node)
elif isinstance(node, DirNode):
for child in node.children:
files.extend(flatten_tree(child))
return files


def render_nav_tree(node, current_page_path):
html = ''

Expand Down Expand Up @@ -101,12 +121,33 @@ def render_nav_tree(node, current_page_path):
return html


def process_node(node, output_root, site_root, current_path=''):
def render_navigation(prev_node, next_node, current_page_path):
html = '<div class="navigation">'

if prev_node:
target_html = prev_node.path.replace('.md', '.html')
current_dir = os.path.dirname(current_page_path)
rel_link = os.path.relpath(target_html, current_dir).replace('\\', '/')
title = prev_node.page.meta.get('title', prev_node.name.replace('.md', '').replace('-', ' ').title())
html += f'<a href="{rel_link}" class="prev">← {title}</a>'

if next_node:
target_html = next_node.path.replace('.md', '.html')
current_dir = os.path.dirname(current_page_path)
rel_link = os.path.relpath(target_html, current_dir).replace('\\', '/')
title = next_node.page.meta.get('title', next_node.name.replace('.md', '').replace('-', ' ').title())
html += f'<a href="{rel_link}" class="next">{title} →</a>'

html += '</div>'
return html


def process_node(node, output_root, site_root, flat_tree, current_path=''):
if isinstance(node, DirNode):
if node.path:
os.makedirs(os.path.join(output_root, node.path), exist_ok=True)
for child in node.children:
process_node(child, output_root, site_root)
process_node(child, output_root, site_root, flat_tree)

elif isinstance(node, FileNode):
output_file = os.path.join(output_root, node.path.replace('.md', '.html'))
Expand All @@ -117,6 +158,19 @@ def process_node(node, output_root, site_root, current_path=''):

nav_html = render_nav_tree(site_root, node.path)

# Calculate prev/next
prev_node = None
next_node = None
for i, f_node in enumerate(flat_tree):
if f_node.path == node.path:
if i > 0:
prev_node = flat_tree[i - 1]
if i < len(flat_tree) - 1:
next_node = flat_tree[i + 1]
break

navigation_html = render_navigation(prev_node, next_node, node.path)

title = node.page.meta.get(
'title', os.path.splitext(node.name)[0].replace('-', ' ').title()
)
Expand All @@ -126,13 +180,15 @@ def process_node(node, output_root, site_root, current_path=''):
title=title,
content=node.page.render(),
css_path=css_relative_path,
navigation=navigation_html,
)
else:
html = LAYOUT.format(
title=title,
content=node.page.render(),
css_path=css_relative_path,
nav_tree=nav_html,
navigation=navigation_html,
)

with open(output_file, 'w', encoding='utf-8') as f:
Expand All @@ -146,7 +202,8 @@ def write_output(root_node: DirNode, output_path: str) -> None:
shutil.rmtree(output_path)
os.makedirs(output_path)

process_node(root_node, output_path, root_node)
flat_tree = flatten_tree(root_node)
process_node(root_node, output_path, root_node, flat_tree)

if os.path.exists('static/base.css'):
shutil.copy('static/base.css', os.path.join(output_path, 'base.css'))
Expand Down
30 changes: 23 additions & 7 deletions static/base.css
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ p {
ul,
ol {
margin-bottom: 1.5rem;
padding-left: 2rem;
padding-left: 1.5rem;
}
li {
margin-bottom: 0.5rem;
Expand Down Expand Up @@ -208,12 +208,28 @@ td {
border-bottom: 1px solid var(--quote-border);
}

tr:nth-child(even) {
background-color: rgba(0, 0, 0, 0.02);
.navigation {
display: flex;
justify-content: space-between;
margin-top: 4rem;
padding-top: 2rem;
border-top: 1px solid var(--quote-border);
}

@media (prefers-color-scheme: dark) {
tr:nth-child(even) {
background-color: rgba(255, 255, 255, 0.02);
}
.navigation a {
padding: 0.6rem 1.2rem;
border: 1px solid var(--accent-color);
border-radius: 6px;
font-family: var(--font-heading);
font-weight: 600;
transition: all 0.2s;
}

.navigation a:hover {
background-color: var(--accent-color);
color: #fff;
}

.navigation .next {
margin-left: auto;
}