Skip to content

Commit d6e8f5f

Browse files
authored
Merge pull request #89 from pydsigner/GH-73_hide_url_file_extensions
Add WebIndexPathCalc (#73)
2 parents 53e9f34 + adaf4af commit d6e8f5f

File tree

5 files changed

+78
-49
lines changed

5 files changed

+78
-49
lines changed

examples/code_index.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,15 @@
1212
CustodyEntry,
1313
DirectCopyStep,
1414
InputBuildSettings,
15-
JinjaExtendedMarkdownStep,
15+
JinjaMarkdownStep,
1616
JinjaRenderStep,
1717
OutputDirPathCalc,
1818
PathCalc,
1919
REMatcher,
2020
ResourcePackerStep,
2121
Rule,
2222
UnpackArchiveStep,
23+
WebIndexPathCalc,
2324
WorkingDirPathCalc,
2425
)
2526

@@ -131,8 +132,8 @@ def __call__(self, path: Path, output_paths: list[Path]):
131132
# Render markdown files and stop processing.
132133
Rule(
133134
REMatcher(r'.*\.md'),
134-
[WorkingDirPathCalc('.html'), None],
135-
JinjaExtendedMarkdownStep(
135+
[WebIndexPathCalc('working_dir', '.html'), None],
136+
JinjaMarkdownStep(
136137
default_template='base.j.html',
137138
pygments_params={'classprefix': 'pyg-'},
138139
)
@@ -149,7 +150,7 @@ def __call__(self, path: Path, output_paths: list[Path]):
149150
[WorkingDirPathCalc('.html'), None],
150151
CodeIndexStep(
151152
'*.py',
152-
OutputDirPathCalc('.html')
153+
WebIndexPathCalc('output_dir', '.html')
153154
)
154155
),
155156
# Ignore all other Jinja templates.

examples/code_index/index.jinja.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<main>
55
<h1>Examples</h1>
66
{% for source, target in leaves %}
7-
<a href="{{target.as_posix()}}"><h2>{{ source.name }}</h2></a>
7+
<a href="{{target.parent.as_posix()}}"><h2>{{ source.name }}</h2></a>
88
{% endfor %}
99
</main>
1010
{% endblock %}

src/anchovy/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,5 @@
1010
from .include import RequestsFetchStep, UnpackArchiveStep, URLLibFetchStep
1111
from .jinja import JinjaExtendedMarkdownStep, JinjaMarkdownStep, JinjaRenderStep
1212
from .minify import AssetMinifierStep, CSSMinifierStep, HTMLMinifierStep, ResourcePackerStep
13-
from .paths import DirPathCalc, OutputDirPathCalc, REMatcher, WorkingDirPathCalc
13+
from .paths import DirPathCalc, OutputDirPathCalc, REMatcher, WebIndexPathCalc, WorkingDirPathCalc
1414
from .simple import BaseCommandStep, BaseStandardStep, DirectCopyStep

src/anchovy/paths.py

+28
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,34 @@ def __init__(self,
9898
super().__init__('working_dir', ext, transform)
9999

100100

101+
class WebIndexPathCalc(DirPathCalc[T]):
102+
"""
103+
DirPathCalc which additionally nests its input paths into an index
104+
structure so that file extensions can be omitted in URLs.
105+
"""
106+
index_base = 'index'
107+
108+
def __init__(self,
109+
dest: Path | ContextDir,
110+
ext: str | None = None,
111+
transform: t.Callable[[Path], Path] | None = None,
112+
index_base: str | None = None):
113+
if transform:
114+
def full_transform(path: Path):
115+
return self._web_transform(transform(path))
116+
else:
117+
full_transform = self._web_transform
118+
super().__init__(dest, ext, full_transform)
119+
self.index_base = index_base or self.index_base
120+
121+
def _web_transform(self, path: Path) -> Path:
122+
"""
123+
Transform a/b.c to a/b/index.c, while leaving a/index.c as-is.
124+
"""
125+
if path.stem == self.index_base:
126+
return path
127+
return (path.with_suffix('') / self.index_base).with_suffix(path.suffix)
128+
101129

102130
class REMatcher(Matcher[re.Match | None]):
103131
"""

test/artifacts/code_index.json

+43-43
Original file line numberDiff line numberDiff line change
@@ -86,20 +86,20 @@
8686
"working_dir/static/css_pack.css"
8787
]
8888
},
89-
"working_dir/basic_site.html": {
89+
"working_dir/basic_site/index.html": {
9090
"working_dir/basic_site.md": [
91-
"working_dir/basic_site.html"
91+
"working_dir/basic_site/index.html"
9292
],
9393
"input_dir/base.jinja.html": [
94-
"working_dir/basic_site.html"
94+
"working_dir/basic_site/index.html"
9595
]
9696
},
97-
"working_dir/gallery.html": {
97+
"working_dir/gallery/index.html": {
9898
"working_dir/gallery.md": [
99-
"working_dir/gallery.html"
99+
"working_dir/gallery/index.html"
100100
],
101101
"input_dir/base.jinja.html": [
102-
"working_dir/gallery.html"
102+
"working_dir/gallery/index.html"
103103
]
104104
},
105105
"output_dir/static/css_pack.css": {
@@ -112,14 +112,14 @@
112112
"output_dir/index.html"
113113
]
114114
},
115-
"output_dir/basic_site.html": {
116-
"working_dir/basic_site.html": [
117-
"output_dir/basic_site.html"
115+
"output_dir/basic_site/index.html": {
116+
"working_dir/basic_site/index.html": [
117+
"output_dir/basic_site/index.html"
118118
]
119119
},
120-
"output_dir/gallery.html": {
121-
"working_dir/gallery.html": [
122-
"output_dir/gallery.html"
120+
"output_dir/gallery/index.html": {
121+
"working_dir/gallery/index.html": [
122+
"output_dir/gallery/index.html"
123123
]
124124
}
125125
},
@@ -128,23 +128,23 @@
128128
"path",
129129
{
130130
"sha1": "",
131-
"m_time": 1706995623.6526809,
131+
"m_time": 1706996735.449964,
132132
"size": 4096
133133
}
134134
],
135135
"working_dir/basic_site.py": [
136136
"path",
137137
{
138138
"sha1": "c4983436e017a238d94796250a749d74cbc76835",
139-
"m_time": 1706995623.6526809,
139+
"m_time": 1706996735.4489732,
140140
"size": 1071
141141
}
142142
],
143143
"working_dir/gallery.py": [
144144
"path",
145145
{
146146
"sha1": "3cdd5e3cc41664211b63f98b94a0791282f1ea4d",
147-
"m_time": 1706995623.6526809,
147+
"m_time": 1706996735.449964,
148148
"size": 1407
149149
}
150150
],
@@ -159,24 +159,24 @@
159159
"working_dir/index.jinja.html": [
160160
"path",
161161
{
162-
"sha1": "c361ccf60628e76f571f8229d9c896603f7042ab",
163-
"m_time": 1706995623.6602678,
164-
"size": 298
162+
"sha1": "522ee3defec401e1a661c862b313cb76a8683d9f",
163+
"m_time": 1706996735.457506,
164+
"size": 305
165165
}
166166
],
167167
"input_dir/index.jinja.html": [
168168
"path",
169169
{
170-
"sha1": "c361ccf60628e76f571f8229d9c896603f7042ab",
171-
"m_time": 1700432465.1655195,
172-
"size": 298
170+
"sha1": "522ee3defec401e1a661c862b313cb76a8683d9f",
171+
"m_time": 1706996649.193668,
172+
"size": 305
173173
}
174174
],
175175
"working_dir/static/core.css": [
176176
"path",
177177
{
178178
"sha1": "23b855543fd50b49b743413cc221e1bee266c6c9",
179-
"m_time": 1706995623.6736958,
179+
"m_time": 1706996735.4615524,
180180
"size": 306
181181
}
182182
],
@@ -192,7 +192,7 @@
192192
"path",
193193
{
194194
"sha1": "16d39823b42dfc58eb0b12d04cc54c233575f398",
195-
"m_time": 1706995623.681307,
195+
"m_time": 1706996735.470035,
196196
"size": 5551
197197
}
198198
],
@@ -208,7 +208,7 @@
208208
"path",
209209
{
210210
"sha1": "0cc6ab8a6e988e4f3ff88a30dca17f4b8087a37b",
211-
"m_time": 1706995623.6852465,
211+
"m_time": 1706996735.4745364,
212212
"size": 36
213213
}
214214
],
@@ -224,24 +224,24 @@
224224
"path",
225225
{
226226
"sha1": "bf995c9fb6202933eb3d6b433b38bae484e2f7d1",
227-
"m_time": 1706995623.7074897,
227+
"m_time": 1706996735.4912386,
228228
"size": 1184
229229
}
230230
],
231231
"working_dir/gallery.md": [
232232
"path",
233233
{
234234
"sha1": "e03d87bee6bd515e50c752acae7a24cc6700aef7",
235-
"m_time": 1706995623.7105067,
235+
"m_time": 1706996735.494867,
236236
"size": 1514
237237
}
238238
],
239239
"working_dir/index.html": [
240240
"path",
241241
{
242-
"sha1": "e653663027dfc58550559eaa9d2ba816dd94b8e0",
243-
"m_time": 1706995623.7190287,
244-
"size": 427
242+
"sha1": "0b2f6b68215f5d1135e3deec703b012033fcea97",
243+
"m_time": 1706996735.5039737,
244+
"size": 417
245245
}
246246
],
247247
"working_dir:*.py": [
@@ -257,63 +257,63 @@
257257
"path",
258258
{
259259
"sha1": "5620127ab0d2261762dac0c30d1fe79e86dcac03",
260-
"m_time": 1706995623.7240794,
260+
"m_time": 1706996735.508551,
261261
"size": 5859
262262
}
263263
],
264-
"working_dir/basic_site.html": [
264+
"working_dir/basic_site/index.html": [
265265
"path",
266266
{
267267
"sha1": "ffe41a786d2d6e21e277aa1db53db621384aa76f",
268-
"m_time": 1706995623.8251395,
268+
"m_time": 1706996735.6131053,
269269
"size": 4852
270270
}
271271
],
272272
"input_dir/base.jinja.html": [
273273
"path",
274274
{
275275
"sha1": "cd1fad850af896b28d5cd43b64837e1360a9b0a9",
276-
"m_time": 1700432465.1645195,
276+
"m_time": 1706996724.3570445,
277277
"size": 349
278278
}
279279
],
280-
"working_dir/gallery.html": [
280+
"working_dir/gallery/index.html": [
281281
"path",
282282
{
283283
"sha1": "607b78c4b55e9b7598d74c7dae26847536932758",
284-
"m_time": 1706995623.8331532,
284+
"m_time": 1706996735.6226301,
285285
"size": 6386
286286
}
287287
],
288288
"output_dir/static/css_pack.css": [
289289
"path",
290290
{
291291
"sha1": "c363895ad4c4f75aff899690c56eef2e300980be",
292-
"m_time": 1706995623.8389888,
292+
"m_time": 1706996735.6266575,
293293
"size": 2800
294294
}
295295
],
296296
"output_dir/index.html": [
297297
"path",
298298
{
299-
"sha1": "239ba6c1e7802f77005bcef7ce3eae4ef59df4ae",
300-
"m_time": 1706995623.8761384,
301-
"size": 263
299+
"sha1": "310b5f2330ee78a24b009ca52df2583bd4e29d9f",
300+
"m_time": 1706996735.6696851,
301+
"size": 253
302302
}
303303
],
304-
"output_dir/basic_site.html": [
304+
"output_dir/basic_site/index.html": [
305305
"path",
306306
{
307307
"sha1": "f217f536a707d91b90b6d50ab94fb5e9c310a5f2",
308-
"m_time": 1706995623.9063706,
308+
"m_time": 1706996735.693998,
309309
"size": 4439
310310
}
311311
],
312-
"output_dir/gallery.html": [
312+
"output_dir/gallery/index.html": [
313313
"path",
314314
{
315315
"sha1": "ddac506b836ee7fffaed7f1dad18967bd11acf0e",
316-
"m_time": 1706995623.9233155,
316+
"m_time": 1706996735.7010386,
317317
"size": 5887
318318
}
319319
]

0 commit comments

Comments
 (0)