Skip to content
Merged
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
14 changes: 14 additions & 0 deletions docs/user_guide/05-configuration.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,20 @@ To generate a starter configuration file with all options documented:
great-docs config
```

### Documentation in a monorepo

Set `package_root` when the documentation and package live in separate directories.
For example, if your repository contains `docs/great-docs.yml` and
`python/my_package/pyproject.toml`, add:

```{.yaml filename="docs/great-docs.yml"}
package_root: ../python/my_package
```

Run `great-docs build --config docs/great-docs.yml` from the repository root.
Great Docs reads the package metadata and API from the selected package directory.
Documentation paths remain relative to `docs/`, and the site is built into `docs/_site/`.

## Display Name

By default, Great Docs uses your package name exactly as-is for the site title and navbar. For
Expand Down
6 changes: 3 additions & 3 deletions great_docs/_build/facts.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
from .._project.repository import Repository
from ..config import Config

__all__ = ["Facts", "exact_tag", "source_links", "source_ref"]
__all__ = ["Facts", "checkout_root", "exact_tag", "source_links", "source_ref"]


@dataclass(frozen=True)
Expand Down Expand Up @@ -110,11 +110,11 @@ def source_links(
ref=source_ref(config, package_root, warn=warn),
package_root=package_root,
path=config.source_path,
repository_root=None if config.source_path else _checkout_root(package_root),
repository_root=None if config.source_path else checkout_root(package_root),
)


def _checkout_root(package_root: Path) -> Path | None:
def checkout_root(package_root: Path) -> Path | None:
"""
Find the Git checkout that contains the package

Expand Down
4 changes: 2 additions & 2 deletions great_docs/_build/from_repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ def build(
else Path.cwd() / layout.site_dir.relative_to(checkout.resolve())
)
_check_replaceable(output_path)
scripts = _install(checkout, Path(temporary) / "venv", report=report)
scripts = _install(layout.package_root, Path(temporary) / "venv", report=report)

report("🔨 Building documentation...")
command = [str(scripts / _executable("great-docs")), "build"]
Expand Down Expand Up @@ -293,7 +293,7 @@ def _clone(
selected = (checkout / config_path).resolve() if config_path else None
if selected is not None and not selected.is_relative_to(checkout.resolve()):
raise LayoutError("Remote configuration must remain inside the checkout")
layout = Layout.make(checkout, selected)
layout = Layout.make(checkout, selected, checkout=checkout)

# Fetch failures leave the shallow history, which builds with fewer features
if not shallow:
Expand Down
5 changes: 3 additions & 2 deletions great_docs/_build/outputs.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

from __future__ import annotations

import os
import shutil
from dataclasses import replace
from pathlib import Path
Expand Down Expand Up @@ -299,12 +300,12 @@ def remove(layout: Layout, *, report: Callable[[str], None]) -> None:
config_path = layout.config_path
if config_path.exists():
config_path.unlink()
report(f"Removed {config_path.relative_to(layout.package_root)}")
report(f"Removed {os.path.relpath(config_path, layout.package_root)}")

for build_dir in builds:
if build_dir.exists():
shutil.rmtree(build_dir)
report(f"Removed {build_dir.relative_to(layout.package_root)}/ directory")
report(f"Removed {os.path.relpath(build_dir, layout.package_root)}/ directory")
if layout.source_dir != layout.package_root:
if layout.site_dir.exists():
shutil.rmtree(layout.site_dir)
Expand Down
21 changes: 19 additions & 2 deletions great_docs/_build/passes.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,16 @@

from __future__ import annotations

import os
from pathlib import Path
from typing import TYPE_CHECKING

from .._term_player import prerender as term_prerender
from .._website import prerender, quarto_yml, repo_links
from .._website.homepage import aliases
from .._website.navigation import shared_sidebar
from . import content, freeze
from .._yaml import YamlValue
from . import content, facts, freeze
from .steps import Outcome, Skipped

if TYPE_CHECKING:
Expand Down Expand Up @@ -205,7 +208,21 @@ def repository_links(run: Run, config: Config, layout: Layout, log: BuildLog) ->
`None`; the result is a detail line of the previous step.
"""
pages = content.page_map(config, layout, run.user_guide)
count = repo_links.prepare(layout.build_dir, run.quarto_config, pages, layout.package_root)
root = layout.package_root
if not layout.source_dir.is_relative_to(root):
checkout = facts.checkout_root(root)
root = (
checkout
if checkout is not None and layout.source_dir.is_relative_to(checkout)
else Path(os.path.commonpath((root, layout.source_dir)))
)
website: YamlValue = run.quarto_config.get("website")
if isinstance(website, dict) and isinstance(subdir := website.get("repo-subdir"), str):
# Quarto already includes this directory in the URL before the page path.
subdir_path = (root / subdir).resolve()
if subdir_path.is_relative_to(root) and layout.source_dir.is_relative_to(subdir_path):
root = subdir_path
count = repo_links.prepare(layout.build_dir, run.quarto_config, pages, root)
if count:
log.detail(f"Linking {count} page(s) to their authored sources")
return None
Expand Down
2 changes: 1 addition & 1 deletion great_docs/_build/prepare.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def build_directory(
If distinct scripts share a filename or a script uses a reserved filename.
"""
build_dir = layout.build_dir
print(f"Preparing build directory: {build_dir.relative_to(layout.package_root)}/")
print(f"Preparing build directory: {os.path.relpath(build_dir, layout.package_root)}/")

outputs.validate(config, layout)

Expand Down
2 changes: 2 additions & 0 deletions great_docs/_checks/lint.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,8 @@ def run_lint(
# Discovery and introspection print their progress; quiet mode discards it.
with contextlib.redirect_stdout(io.StringIO()) if quiet else contextlib.nullcontext():
layout = Layout.make(project_root, Path(config_path) if config_path else None)
if not layout.source_dir.is_relative_to(layout.package_root):
project_root = layout.source_dir
if fix:
from .config_fix import fix_config_file

Expand Down
18 changes: 11 additions & 7 deletions great_docs/_cli/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -538,7 +538,7 @@ def _extract_frontmatter(text: str) -> str:
frontmatter = _extract_frontmatter(content)
match = freeze_re.search(frontmatter) or exec_freeze_re.search(frontmatter)
if match:
rel_path = str(qmd_file.relative_to(project_root))
rel_path = os.path.relpath(qmd_file, project_root)
per_page_modes[rel_path] = match.group(1).strip()

if per_page_modes:
Expand Down Expand Up @@ -635,9 +635,9 @@ def freeze(
survive future builds.

PAGES are paths to .qmd files relative to your project root (e.g.,
'docs/user_guide/benchmarks.qmd'). Quarto always executes code when rendering
individual files, even with freeze enabled (this is how you update
frozen outputs).
'docs/user_guide/benchmarks.qmd'). If your documentation is outside the package
root, give paths relative to the configuration directory. Quarto always executes
code when rendering individual files, even with freeze enabled.

Use '--clean' to wipe the entire '_freeze/' cache before re-executing.
This forces a full refresh of all specified pages from scratch.
Expand Down Expand Up @@ -672,7 +672,10 @@ def freeze(
click.echo("Error: Specify at least one PAGE, or use --info.", err=True)
sys.exit(1)

missing = [p for p in pages if not (project_root / p).is_file()]
page_root = (
project_root if layout.source_dir.is_relative_to(project_root) else layout.source_dir
)
missing = [p for p in pages if not (page_root / p).is_file()]
if missing:
for page in missing:
click.echo(f"Error: Page not found: {page}", err=True)
Expand Down Expand Up @@ -753,7 +756,8 @@ def freeze(
build_freeze = build_dir / "_freeze"
if build_freeze.is_dir() and rendered:
click.echo()
click.echo(f"Persisting _freeze/ → {persist_dir.relative_to(project_root)}/")
cache_path = os.path.relpath(persist_dir, project_root)
click.echo(f"Persisting _freeze/ → {cache_path}/")
updated_files = freeze_cache.merge(
build_freeze, persist_dir, rendered_pages=rendered.values()
)
Expand All @@ -762,7 +766,7 @@ def freeze(
# Show git status hint
click.echo()
click.echo("To commit the updated freeze cache:")
click.echo(f" git add {persist_dir.relative_to(project_root)}/")
click.echo(f" git add {cache_path}/")
click.echo(f' git commit -m "Update freeze cache for {", ".join(rendered)}"')
elif not rendered:
click.echo()
Expand Down
13 changes: 5 additions & 8 deletions great_docs/_cli/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from .._project import Project
from .._project.layout import CONVENTIONAL_DOC_DIRS, Layout, _find_package_root
from .._project.remote import HttpRemote
from .._project.scaffold import update_gitignore
from .._project.scaffold import update_gitignore, write_config_template
from .._reference.exports import Exports
from .._website import changelog as changelog_page
from .._website import quarto_yml, website_overlay
Expand Down Expand Up @@ -126,10 +126,6 @@ def config(project_path: str | None, force: bool, config_path: str | None = None
great-docs config --force # Overwrite existing file
great-docs config --project-path ../pkg
"""
from pathlib import Path

from ..config import create_default_config

try:
project_root = Path(project_path) if project_path else Path.cwd()
layout = Layout.make(project_root, Path(config_path) if config_path else None, create=True)
Expand All @@ -142,9 +138,7 @@ def config(project_path: str | None, force: bool, config_path: str | None = None
click.echo("Cancelled.")
return

config_content = create_default_config()
config_path.parent.mkdir(parents=True, exist_ok=True)
config_path.write_text(config_content, encoding="utf-8")
write_config_template(layout)
update_gitignore(layout, report=click.echo)
click.echo(f"✓ Created {config_path}")
click.echo("\nEdit this file to customize your documentation settings.")
Expand Down Expand Up @@ -424,6 +418,9 @@ def migrate_layout(
if candidate.is_file():
check_symlinks(candidate)
layout = Layout.make(root, Path(config_path) if config_path else None)
if not layout.source_dir.is_relative_to(layout.package_root):
click.echo("No migration is needed. The documentation already has its own directory.")
return
target = root / destination
is_default_destination = (
ctx.get_parameter_source("destination") == click.core.ParameterSource.DEFAULT
Expand Down
50 changes: 38 additions & 12 deletions great_docs/_project/layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
from __future__ import annotations

import re
from dataclasses import dataclass
from pathlib import Path
from dataclasses import dataclass, field
from pathlib import Path, PureWindowsPath

from ..config import Config
from ..config import Config, read_user_settings

_PROJECT_MANIFESTS = ("pyproject.toml", "setup.py", "go.mod", "Cargo.toml")
CONVENTIONAL_DOC_DIRS = ("docs", "docs-website", "doc", "website")
Expand All @@ -30,6 +30,7 @@ class Layout:
site_dir: Path
freeze_dir: Path
cache_dir: Path
package_root_override: str | None = field(default=None, kw_only=True)

@classmethod
def make(
Expand All @@ -38,13 +39,15 @@ def make(
config_path: Path | None = None,
*,
create: bool = False,
checkout: Path | None = None,
) -> Layout:
"""
Resolve a documentation layout from project context

Automatic selection considers configuration files at the package root
and in `docs/`, `docs-website/`, `doc/`, or `website/`. An explicit
configuration must remain within the discovered package root.
and in `docs/`, `docs-website/`, `doc/`, or `website/`. A configuration
can set `package_root` relative to its directory. This also permits
a configuration outside the discovered package root.

Parameters
----------
Expand All @@ -56,6 +59,8 @@ def make(
create
Permit a missing explicit file or select `docs/great-docs.yml`
when neither conventional configuration exists.
checkout
Limit the configuration and package to a cloned repository.

Returns
-------
Expand All @@ -66,17 +71,37 @@ def make(
------
LayoutError
If automatic selection is ambiguous, an explicit file is missing,
or the selected configuration is outside the package root.
or the configuration and package directories are incompatible.
"""
package_root = _find_package_root(project_path.resolve())
selected = _select_config(package_root, config_path, create=create)
resolved_config = selected.resolve()
if not resolved_config.is_relative_to(package_root):
selected = _select_config(package_root, config_path, create=create).resolve()
if checkout is not None:
checkout = checkout.resolve()
if not selected.is_relative_to(checkout):
raise LayoutError("Remote configuration must remain inside the checkout")
source_dir = selected.parent
override: object = read_user_settings(selected, report=lambda _: None).get("package_root")
if override is not None:
if (
not isinstance(override, str)
or not override.strip()
or Path(override).is_absolute()
or PureWindowsPath(override).anchor
):
raise LayoutError(f"{selected}: package_root must be a non-empty relative path")
package_root = (source_dir / override).resolve()
elif not selected.is_relative_to(package_root):
raise LayoutError(
f"Configuration must be inside the package root {package_root}: {selected}"
f"Configuration must be inside the package root {package_root}: {selected}. "
"Set package_root in the configuration to select a separate package directory."
)
if checkout is not None and not package_root.is_relative_to(checkout):
raise LayoutError("Remote package root must remain inside the checkout")
if override is not None and not any(
(package_root / name).is_file() for name in _PROJECT_MANIFESTS
):
raise LayoutError(f"{selected}: package_root has no project manifest: {package_root}")

source_dir = resolved_config.parent
if source_dir == package_root:
build_dir = package_root / "great-docs"
site_dir = build_dir / "_site"
Expand All @@ -88,12 +113,13 @@ def make(

return cls(
package_root=package_root,
config_path=resolved_config,
config_path=selected,
source_dir=source_dir,
build_dir=build_dir,
site_dir=site_dir,
freeze_dir=source_dir / "_freeze",
cache_dir=cache_dir,
package_root_override=override,
)

def is_reserved_directory(self, path: Path) -> bool:
Expand Down
Loading
Loading