Skip to content

Commit cabe099

Browse files
committed
Don't require .html in links
1 parent 06857a7 commit cabe099

File tree

3 files changed

+60
-21
lines changed

3 files changed

+60
-21
lines changed

config.yaml

+6-6
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,22 @@ context:
22
base_url: "http://localhost:8000"
33
current_version: 1.6
44
versions:
5-
- path: policy-archive/policy-version-1-0.html
5+
- path: policy-archive/policy-version-1-0
66
version: 1.0
77
date: "2022-03-01"
8-
- path: policy-archive/policy-version-1-1.html
8+
- path: policy-archive/policy-version-1-1
99
version: 1.1
1010
date: "2022-06-01"
11-
- path: policy-archive/policy-version-1-2.html
11+
- path: policy-archive/policy-version-1-2
1212
version: 1.2
1313
date: "2022-09-01"
14-
- path: policy-archive/policy-version-1-3.html
14+
- path: policy-archive/policy-version-1-3
1515
version: 1.3
1616
date: "2023-01-06"
17-
- path: policy-archive/policy-version-1-4.html
17+
- path: policy-archive/policy-version-1-4
1818
version: 1.4
1919
date: "2023-03-03"
20-
- path: policy-archive/policy-version-1-5.html
20+
- path: policy-archive/policy-version-1-5
2121
version: 1.5
2222
date: "2024-01-16"
2323
- path: /

src/generate.py

+52-13
Original file line numberDiff line numberDiff line change
@@ -89,36 +89,75 @@ def render_file(input_path, output_path, env, page_context={}):
8989
ConversionResult = namedtuple("ConversionResult", ["converted", "skipped"])
9090

9191

92+
import os
93+
import shutil
94+
9295
def render_markdown(input_dir, output_dir, env, page_context={}) -> ConversionResult:
9396
converted = 0
9497
skipped = 0
98+
9599
for root, _, files in os.walk(input_dir):
96100
for filename in files:
97-
# Only Markdown files will have their extension changed, everything
98-
# else is copied.
99101
input_path = os.path.join(root, filename)
100-
should_render = False
101-
if filename.endswith(".md"):
102-
output_filename = replace_extension(filename, "md", "html")
102+
# Determine the relative path (directory structure under input_dir).
103+
relative_path = os.path.relpath(os.path.dirname(input_path), input_dir)
104+
105+
# Decide how to handle the file.
106+
if filename == "index.md":
107+
# Special case index.md to turn into index.html in the exact same location
108+
output_path = os.path.join(
109+
output_dir,
110+
relative_path,
111+
"index.html"
112+
)
113+
should_render = True
114+
elif filename.endswith(".md"):
115+
# Instead of simply replacing .md with .html, place it in:
116+
#
117+
# output_dir / relative_path / [filename-without-.md] / index.html
118+
#
119+
folder_name = os.path.splitext(filename)[0] # remove .md
120+
output_path = os.path.join(
121+
output_dir,
122+
relative_path,
123+
folder_name,
124+
"index.html"
125+
)
103126
should_render = True
127+
104128
elif filename.endswith(".jinja2"):
129+
# No change for jinja2 behavior:
130+
output_filename = filename[:-7] # remove '.jinja2'
131+
output_path = os.path.join(
132+
output_dir,
133+
relative_path,
134+
output_filename
135+
)
105136
should_render = True
106-
output_filename = filename[:-7]
107-
else:
108-
output_filename = filename
109-
relative_path = os.path.relpath(os.path.dirname(input_path), input_dir)
110-
output_path = os.path.normpath(
111-
os.path.join(output_dir, relative_path, output_filename)
112-
)
113137

114-
# Only Markdown files are rendered.
138+
else:
139+
# All other files are copied as-is (unchanged behavior):
140+
output_path = os.path.join(
141+
output_dir,
142+
relative_path,
143+
filename
144+
)
145+
should_render = False
146+
147+
# Normalize the path (important on Windows, but generally good practice).
148+
output_path = os.path.normpath(output_path)
149+
150+
# Render if needed; otherwise just copy the file.
115151
if should_render:
152+
# Ensure the parent directory (or in the case of .md, the subfolder) exists.
153+
os.makedirs(os.path.dirname(output_path), exist_ok=True)
116154
render_file(input_path, output_path, env, page_context=page_context)
117155
converted += 1
118156
else:
119157
os.makedirs(os.path.dirname(output_path), exist_ok=True)
120158
shutil.copy(input_path, output_path)
121159
skipped += 1
160+
122161
return ConversionResult(converted, skipped)
123162

124163

templates/base.html

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ <h4>Policy</h4>
2020
<h4>Resources</h4>
2121
<nav>
2222
<ul>
23-
<li><a href="{{ '/apply-for-inclusion.html' | absolute_url }}">Apply for Inclusion</a></li>
24-
<li><a href="{{ '/moving-forward-together.html' | absolute_url }}">Moving Forward, Together</a></li>
23+
<li><a href="{{ '/apply-for-inclusion' | absolute_url }}">Apply for Inclusion</a></li>
24+
<li><a href="{{ '/moving-forward-together' | absolute_url }}">Moving Forward, Together</a></li>
2525
</ul>
2626
</nav>
2727
<h4>Policy Archive</h4>

0 commit comments

Comments
 (0)