Skip to content

Commit 739ab67

Browse files
committed
Add WebIndexPathCalc (#73)
1 parent 53e9f34 commit 739ab67

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

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
"""

0 commit comments

Comments
 (0)