Skip to content

Commit 77f07fd

Browse files
authored
Merge branch 'main' into feat-automodule
2 parents f3d7848 + d89c03f commit 77f07fd

File tree

5 files changed

+103
-38
lines changed

5 files changed

+103
-38
lines changed

Makefile

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@ $(EXAMPLE_INTERLINKS)/test.md: $(EXAMPLE_INTERLINKS)/test.qmd _extensions/interl
1919

2020
examples/%/_site: examples/%/_quarto.yml
2121
cd examples/$* \
22-
&& quarto add --no-prompt ../..
23-
cd examples/$* && quartodoc build _quarto.yml --verbose
22+
&& quarto add --no-prompt ../.. \
23+
&& quarto add --no-prompt quarto-ext/shinylive
24+
cd examples/$* && quartodoc build --config _quarto.yml --verbose
2425
cd examples/$* && quartodoc interlinks
2526
quarto render $(dir $<)
2627

docs/get-started/basic-building.qmd

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,27 +9,46 @@ jupyter:
99

1010
**tl;dr**: Once you've configured quartodoc in your `_quarto.yml` file, use the following commands to build and preview a documentation site.
1111

12+
## Create documentation files with `quartodoc build`
13+
14+
Automatically generate `.qmd` files with reference api documentation. This is written by default to the reference/ folder in your quarto project.
15+
1216
```bash
13-
# Create the documentation files. These are written
14-
# by default to the reference/ folder in your docs.
15-
quartodoc build --verbose
17+
quartodoc build
18+
```
1619

17-
# Create optional inventory files, which allow you to
18-
# link to API doc pages within and across documentation
19-
# sites. These are put in the _inv folder in your docs.
20-
quartodoc interlinks
20+
If you are iterating on your docstrings while previewing your site with `quarto preview`, you will likely want to rebuild the doc pages automatically when docstrings change. The `--watch` flag does exactly this.
2121

22-
# Preview the documentation site.
23-
# Use quarto render to generate the final site.
24-
quarto preview
22+
```bash
23+
quartodoc build --watch
2524
```
2625

27-
## Rebuilding doc pages
26+
For more information on the `quartodoc build` command, use `--help` in the terminal like so:
2827

29-
While using `quarto preview`, you can preview updates to your docstrings by regenerating their documentation pages:
28+
```bash
29+
quartodoc build --help
30+
```
31+
32+
```{python}
33+
#|echo: false
34+
!quartodoc build --help
35+
```
3036

37+
38+
## Create inventory files with `quartodoc interlinks`
39+
40+
Inventory files facilitate linking to API doc pages within and across `quartodoc` sites. This is optional.
41+
3142
```bash
32-
quartodoc build --verbose
43+
quartodoc interlinks
44+
```
45+
46+
## Preview the site with `quarto preview`
47+
48+
Use `quarto` to preview the site:
49+
50+
```bash
51+
quarto preview
3352
```
3453

3554
## Speeding up preview

docs/get-started/overview.qmd

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -85,20 +85,18 @@ quarto preview
8585

8686
## Rebuilding site
8787

88-
As mentioned in the previous section, you can preview your quartodoc site using these commands:
88+
You can preview your `quartodoc` site using the following commands:
89+
90+
First, watch for changes to the library you are documenting so that your docs will automatically re-generate:
8991

9092
```bash
91-
quartodoc build
92-
quarto preview
93+
quartodoc build --watch
9394
```
9495

95-
In order to rebuild your API documentation pages during a quarto preview, re-run
96-
the quartodoc build command:
96+
Second, preview your site:
9797

9898
```bash
99-
# with quarto preview running, you only need to re-run quartodoc build,
100-
# which will re-create the pages of your API documentation.
101-
quartodoc build
99+
quarto preview
102100
```
103101

104102
## Looking up objects

quartodoc/__main__.py

Lines changed: 61 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,44 @@
11
import click
22
import contextlib
33
import os
4+
import time
45
import sphobjinv as soi
56
import yaml
6-
7+
import importlib
78
from pathlib import Path
8-
9+
from watchdog.observers import Observer
10+
from functools import partial
11+
from watchdog.events import FileSystemEventHandler
912
from quartodoc import Builder, convert_inventory
1013

14+
def get_package_path(package_name):
15+
"""
16+
Get the path to a package installed in the current environment.
17+
"""
18+
try:
19+
lib = importlib.import_module(package_name)
20+
return lib.__path__[0]
21+
except ModuleNotFoundError:
22+
raise ModuleNotFoundError(f"Package {package_name} not found. Please install it in your environment.")
23+
24+
class FileChangeHandler(FileSystemEventHandler):
25+
"""
26+
A handler for file changes.
27+
"""
28+
def __init__(self, callback):
29+
self.callback = callback
30+
31+
@classmethod
32+
def print_event(cls, event):
33+
print(f'Rebuilding docs. Detected: {event.event_type} path : {event.src_path}')
34+
35+
def on_modified(self, event):
36+
self.print_event(event)
37+
self.callback()
38+
39+
def on_created(self, event):
40+
self.print_event(event)
41+
self.callback()
1142

1243
def _enable_logs():
1344
import logging
@@ -40,24 +71,44 @@ def cli():
4071
pass
4172

4273

74+
75+
76+
4377
@click.command()
44-
@click.argument("config", default="_quarto.yml")
45-
@click.option("--filter", nargs=1, default="*")
46-
@click.option("--dry-run", is_flag=True, default=False)
47-
@click.option("--verbose", is_flag=True, default=False)
48-
def build(config, filter, dry_run, verbose):
78+
@click.option("--config", default="_quarto.yml", help="Change the path to the configuration file. The default is `./_quarto.yml`")
79+
@click.option("--filter", nargs=1, default="*", help="Specify the filter to select specific files. The default is '*' which selects all files.")
80+
@click.option("--dry-run", is_flag=True, default=False, help="If set, prevents new documents from being generated.")
81+
@click.option("--watch", is_flag=True, default=False, help="If set, the command will keep running and watch for changes in the package directory.")
82+
@click.option("--verbose", is_flag=True, default=False, help="Enable verbose logging.")
83+
def build(config, filter, dry_run, watch, verbose):
84+
"""
85+
Generate API docs based on the given configuration file (`./_quarto.yml` by default).
86+
"""
4987
if verbose:
5088
_enable_logs()
5189

5290
builder = Builder.from_quarto_config(config)
91+
doc_build = partial(builder.build, filter=filter)
5392

5493
if dry_run:
55-
# click.echo(builder.render_index())
5694
pass
5795
else:
5896
with chdir(Path(config).parent):
59-
builder.build(filter=filter)
60-
97+
if watch:
98+
pkg_path = get_package_path(builder.package)
99+
print(f"Watching {pkg_path} for changes...")
100+
event_handler = FileChangeHandler(callback=doc_build)
101+
observer = Observer()
102+
observer.schedule(event_handler, pkg_path, recursive=True)
103+
observer.start()
104+
try:
105+
while True:
106+
time.sleep(1)
107+
except KeyboardInterrupt:
108+
observer.stop()
109+
observer.join()
110+
else:
111+
doc_build()
61112

62113
@click.command()
63114
@click.argument("config", default="_quarto.yml")

setup.cfg

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ install_requires =
3232
pydantic<2.0
3333
pyyaml
3434
typing-extensions>=4.4.0
35-
35+
watchdog>=3.0.0
3636

3737
[options.extras_require]
3838
dev =
@@ -46,10 +46,6 @@ console_scripts =
4646
quartodoc = quartodoc.__main__:cli
4747

4848

49-
[project.scripts]
50-
quartodoc = "quartodoc.cli:main"
51-
52-
5349
[bdist_wheel]
5450
universal = 1
5551

0 commit comments

Comments
 (0)