Skip to content
Closed
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
20 changes: 20 additions & 0 deletions src/docc/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
from .context import Context
from .document import Document, Node, OutputNode, Visit, Visitor
from .performance import measure
from .plugins.listing import ListingSource
from .settings import Settings
from .source import Source

Expand Down Expand Up @@ -157,6 +158,9 @@ def main(command_line: Sequence[str] | None = None) -> None:
rmtree(output_root, ignore_errors=True)

with measure("Wrote outputs (%.4f s)", level=logging.INFO):
non_listing_outputs: Set[Path] = set()
resolved: Dict[Source, Path] = {}

for source, context_ in contexts.items():
document = context_[Document]
extension = document.extension()
Expand All @@ -172,8 +176,24 @@ def main(command_line: Sequence[str] | None = None) -> None:
output_path = Path(
output_path.with_suffix(output_path.suffix + extension)
)
resolved[source] = output_path
if not isinstance(source, ListingSource):
non_listing_outputs.add(output_path)

for source, context_ in contexts.items():
output_path = resolved.get(source)
if output_path is None:
continue
if (
isinstance(source, ListingSource)
and output_path in non_listing_outputs
):
# Another source already writes here (e.g. an
# ``__init__.py`` rendered as the package's index page).
continue

output_path.parent.mkdir(parents=True, exist_ok=True)

document = context_[Document]
with open(output_path, "w", encoding="utf-8") as destination:
document.root.visit(_OutputVisitor(context_, destination))
10 changes: 10 additions & 0 deletions src/docc/plugins/python/cst.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,11 +156,21 @@ def relative_path(self) -> Optional[PurePath]:
"""
return self._relative_path

@property
def is_package_init(self) -> bool:
"""
Whether this source is an ``__init__.py`` rendered as the package
directory's index page.
"""
return self.absolute_path.name == "__init__.py"

@property
def output_path(self) -> PurePath:
"""
Where to put the output derived from this source.
"""
if self.is_package_init:
return self._relative_path.parent / "index"
return self._relative_path

def open(self) -> TextIO:
Expand Down
Loading