Automatically create parent Directory entries for each discovery
Before this change it was possible for a file and a directory have the same path. This change resolves that. Conflicts between files and directories are now correctly discovered.
This commit is contained in:
+22
-3
@@ -4,8 +4,10 @@ from urllib.parse import urlparse
|
|||||||
|
|
||||||
from omicron.ssg.output import (
|
from omicron.ssg.output import (
|
||||||
Output,
|
Output,
|
||||||
|
OutputError,
|
||||||
Content,
|
Content,
|
||||||
ContentError,
|
ContentError,
|
||||||
|
Directory,
|
||||||
is_content,
|
is_content,
|
||||||
create_content,
|
create_content,
|
||||||
File,
|
File,
|
||||||
@@ -32,6 +34,7 @@ class Site:
|
|||||||
def __init__(self, site: Path):
|
def __init__(self, site: Path):
|
||||||
self.site_path = site.absolute()
|
self.site_path = site.absolute()
|
||||||
self.content: list[Content] = []
|
self.content: list[Content] = []
|
||||||
|
self.directories: list[Directory] = []
|
||||||
self.other_outputs: list[Output] = []
|
self.other_outputs: list[Output] = []
|
||||||
self.by_tag: dict[str, list[Content]] = {}
|
self.by_tag: dict[str, list[Content]] = {}
|
||||||
self.by_path: dict[Path, Output] = {}
|
self.by_path: dict[Path, Output] = {}
|
||||||
@@ -98,7 +101,23 @@ class Site:
|
|||||||
raise ContentError(
|
raise ContentError(
|
||||||
f"path '{output.destination}' is already claimed by another output"
|
f"path '{output.destination}' is already claimed by another output"
|
||||||
)
|
)
|
||||||
|
new_dirs: list[Directory] = []
|
||||||
|
for parent in output.destination.parents:
|
||||||
|
if parent == Path("."):
|
||||||
|
break
|
||||||
|
if parent in self.by_path:
|
||||||
|
if not isinstance(self.by_path[parent], Directory):
|
||||||
|
raise OutputError(
|
||||||
|
f"path '{parent}' is claimed by a file but needed as a directory"
|
||||||
|
)
|
||||||
|
break
|
||||||
|
new_dirs.append(Directory(self, parent))
|
||||||
self.by_path[output.destination] = output
|
self.by_path[output.destination] = output
|
||||||
|
if isinstance(output, Directory):
|
||||||
|
self.directories.append(output)
|
||||||
|
for d in new_dirs:
|
||||||
|
self.by_path[d.destination] = d
|
||||||
|
self.directories.append(d)
|
||||||
|
|
||||||
def home_url(self) -> str:
|
def home_url(self) -> str:
|
||||||
return self.base_dir or "/"
|
return self.base_dir or "/"
|
||||||
@@ -163,8 +182,8 @@ class Site:
|
|||||||
if self.base_dir and not self.base_dir.startswith("/"):
|
if self.base_dir and not self.base_dir.startswith("/"):
|
||||||
raise ConfigError("base_url config value must be an absolute path")
|
raise ConfigError("base_url config value must be an absolute path")
|
||||||
self.discover()
|
self.discover()
|
||||||
self.output_path.mkdir(parents=True, exist_ok=True)
|
outputs: list[Output] = [*self.directories, *self.content]
|
||||||
for content in self.content:
|
for output in sorted(outputs, key=lambda o: o.destination.as_posix()):
|
||||||
content.write()
|
output.write()
|
||||||
for output in self.other_outputs:
|
for output in self.other_outputs:
|
||||||
output.write()
|
output.write()
|
||||||
|
|||||||
Reference in New Issue
Block a user