Skip to content

Commit fff9359

Browse files
mishig25sgugger
andauthored
Implement preview command (#139)
* Implement dev command * Make quality * Update src/doc_builder/autodoc.py Co-authored-by: Sylvain Gugger <[email protected]> * Update src/doc_builder/build_doc.py Co-authored-by: Sylvain Gugger <[email protected]> * Update src/doc_builder/build_doc.py Co-authored-by: Sylvain Gugger <[email protected]> * `src_py_path_mapping` -> `source_files_mapping` * `src_py` -> `source_files` * Add soft-dependency `watchdog` * Update is_watchdog_available * Command `preview` * Rm mac-specific `open` command * Reserved sveltekit keywords * Simpler routing base for preview mode * make style * rm -rf * superlcass FileSystemEventHandler init * try catch subsequent builds * Make style * Open localhost:3000 automicalyy * just log * debug file watcher * formatted string * fix hardcoded val * Update kit readme * Chore * Fix relative_path bug * Stop vite auto server open * Better automatic browser open * Cleaner delete file * Add preview readme * doc typo * Support markdown * Fix Co-authored-by: Sylvain Gugger <[email protected]>
1 parent 8f59020 commit fff9359

File tree

9 files changed

+308
-19
lines changed

9 files changed

+308
-19
lines changed

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,19 @@ cd doc-builder
1717
pip install -e .
1818
```
1919

20+
## Previewing
21+
22+
To preview the docs, use the following command:
23+
24+
```bash
25+
doc-builder preview {package_name} {path_to_docs}
26+
```
27+
28+
For example:
29+
```bash
30+
doc-builder preview datasets ~/Desktop/datasets/docs/source/
31+
```
32+
2033
## Doc building
2134

2235
To build the documentation of a given package, use the following command:

kit/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,4 @@ The generated html files and assets are in the `build` folder.
1313

1414
## Previewing the docs
1515

16-
Instead of `npm run build`, do `npm run dev`. Then go to http://localhost:3000/docs/transformers/master/en (or replace `master` with the correct prefix).
16+
Instead of `npm run build`, do `npm run dev`. Then go to http://localhost:3000 (or replace `master` with the correct prefix).

kit/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "kit",
33
"version": "0.0.1",
44
"scripts": {
5-
"dev": "svelte-kit dev",
5+
"dev": "svelte-kit dev -o",
66
"build": "rm -Rf src/routes/__pycache__ && svelte-kit build && ./postbuild.sh",
77
"package": "svelte-kit package",
88
"preview": "svelte-kit preview",

kit/svelte.config.js

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,18 @@ const config = {
3030
vite: {
3131
build: {
3232
sourcemap: Boolean(process.env.DOCS_SOURCEMAP)
33-
}
33+
},
3434
},
3535

3636
paths: {
37-
base:
38-
"/docs/" +
39-
(process.env.DOCS_LIBRARY || "transformers") +
40-
"/" +
41-
(process.env.DOCS_VERSION || "master") +
42-
"/" +
43-
(process.env.DOCS_LANGUAGE || "en")
37+
base: process.argv.includes("dev")
38+
? ""
39+
: "/docs/" +
40+
(process.env.DOCS_LIBRARY || "transformers") +
41+
"/" +
42+
(process.env.DOCS_VERSION || "master") +
43+
"/" +
44+
(process.env.DOCS_LANGUAGE || "en")
4445
}
4546
},
4647

src/doc_builder/autodoc.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,19 @@ def get_source_link(obj, page_info):
287287
return f"{base_link}{module}.py#L{line_number}"
288288

289289

290+
def get_source_path(object_name, package):
291+
"""
292+
Find a path to file in which given object was defined.
293+
294+
Args:
295+
- object_name (`str`): The name of the object to retrieve.
296+
- package (`types.ModuleType`): The package to look into.
297+
"""
298+
obj = obj = find_object_in_package(object_name=object_name, package=package)
299+
obj_path = inspect.getfile(obj)
300+
return obj_path
301+
302+
290303
def document_object(object_name, package, page_info, full_name=True):
291304
"""
292305
Writes the document of a function, class or method.

src/doc_builder/build_doc.py

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
import yaml
2525
from tqdm import tqdm
2626

27-
from .autodoc import autodoc, find_object_in_package, remove_example_tags, resolve_links_in_text
27+
from .autodoc import autodoc, find_object_in_package, get_source_path, remove_example_tags, resolve_links_in_text
2828
from .convert_md_to_mdx import convert_md_to_mdx
2929
from .convert_rst_to_mdx import convert_rst_to_mdx, find_indent, is_empty_line
3030
from .convert_to_notebook import generate_notebooks_from_file
@@ -87,6 +87,7 @@ def resolve_autodoc(content, package, return_anchors=False, page_info=None):
8787
is_inside_codeblock = False
8888
lines = content.split("\n")
8989
new_lines = []
90+
source_files = None
9091
if return_anchors:
9192
anchors = []
9293
errors = []
@@ -125,6 +126,8 @@ def resolve_autodoc(content, package, return_anchors=False, page_info=None):
125126
errors.extend(doc[2])
126127
doc = doc[0]
127128
new_lines.append(doc)
129+
130+
source_files = get_source_path(object_name, package)
128131
else:
129132
new_lines.append(lines[idx])
130133
if lines[idx].startswith("```"):
@@ -136,7 +139,7 @@ def resolve_autodoc(content, package, return_anchors=False, page_info=None):
136139
new_content = "\n".join(new_lines)
137140
new_content = remove_example_tags(new_content)
138141

139-
return (new_content, anchors, errors) if return_anchors else new_content
142+
return (new_content, anchors, source_files, errors) if return_anchors else new_content
140143

141144

142145
def build_mdx_files(package, doc_folder, output_dir, page_info):
@@ -153,6 +156,7 @@ def build_mdx_files(package, doc_folder, output_dir, page_info):
153156
output_dir = Path(output_dir)
154157
os.makedirs(output_dir, exist_ok=True)
155158
anchor_mapping = {}
159+
source_files_mapping = {}
156160

157161
if "package_name" not in page_info:
158162
page_info["package_name"] = package.__name__
@@ -171,9 +175,11 @@ def build_mdx_files(package, doc_folder, output_dir, page_info):
171175
content = reader.read()
172176
content = convert_md_to_mdx(content, page_info)
173177
content = resolve_open_in_colab(content, page_info)
174-
content, new_anchors, errors = resolve_autodoc(
178+
content, new_anchors, source_files, errors = resolve_autodoc(
175179
content, package, return_anchors=True, page_info=page_info
176180
)
181+
if source_files is not None:
182+
source_files_mapping[source_files] = str(file)
177183
with open(dest_file, "w", encoding="utf-8") as writer:
178184
writer.write(content)
179185
# Make sure we clean up for next page.
@@ -186,9 +192,11 @@ def build_mdx_files(package, doc_folder, output_dir, page_info):
186192
content = reader.read()
187193
content = convert_rst_to_mdx(content, page_info)
188194
content = resolve_open_in_colab(content, page_info)
189-
content, new_anchors, errors = resolve_autodoc(
195+
content, new_anchors, source_files, errors = resolve_autodoc(
190196
content, package, return_anchors=True, page_info=page_info
191197
)
198+
if source_files is not None:
199+
source_files_mapping[source_files] = str(file)
192200
with open(dest_file, "w", encoding="utf-8") as writer:
193201
writer.write(content)
194202
# Make sure we clean up for next page.
@@ -213,7 +221,7 @@ def build_mdx_files(package, doc_folder, output_dir, page_info):
213221
"The deployment of the documentation will fail because of the following errors:\n" + "\n".join(all_errors)
214222
)
215223

216-
return anchor_mapping
224+
return anchor_mapping, source_files_mapping
217225

218226

219227
def resolve_links(doc_folder, package, mapping, page_info):
@@ -351,6 +359,7 @@ def build_doc(
351359
language="en",
352360
notebook_dir=None,
353361
is_python_module=False,
362+
watch_mode=False,
354363
):
355364
"""
356365
Build the documentation of a package.
@@ -368,6 +377,9 @@ def build_doc(
368377
If provided, where to save the notebooks generated from the doc file with an [[open-in-colab]] marker.
369378
is_python_module (`bool`, *optional*, defaults to `False`):
370379
Whether the docs being built are for python module. (For example, HF Course is not a python module).
380+
watch_mode (`bool`, *optional*, default to `False`):
381+
If `True`, disables the toc tree check and sphinx objects.inv builds since they are not needed
382+
when this mode is active.
371383
"""
372384
page_info = {"version": version, "language": language, "package_name": package_name}
373385
if clean and Path(output_dir).exists():
@@ -376,11 +388,13 @@ def build_doc(
376388
read_doc_config(doc_folder)
377389

378390
package = importlib.import_module(package_name) if is_python_module else None
379-
anchors_mapping = build_mdx_files(package, doc_folder, output_dir, page_info)
380-
sphinx_refs = check_toc_integrity(doc_folder, output_dir)
381-
sphinx_refs.extend(convert_anchors_mapping_to_sphinx_format(anchors_mapping, package))
391+
anchors_mapping, source_files_mapping = build_mdx_files(package, doc_folder, output_dir, page_info)
392+
if not watch_mode:
393+
sphinx_refs = check_toc_integrity(doc_folder, output_dir)
394+
sphinx_refs.extend(convert_anchors_mapping_to_sphinx_format(anchors_mapping, package))
382395
if is_python_module:
383-
build_sphinx_objects_ref(sphinx_refs, output_dir, page_info)
396+
if not watch_mode:
397+
build_sphinx_objects_ref(sphinx_refs, output_dir, page_info)
384398
resolve_links(output_dir, package, anchors_mapping, page_info)
385399
generate_frontmatter(output_dir)
386400

@@ -390,6 +404,8 @@ def build_doc(
390404
os.remove(nb_file)
391405
build_notebooks(doc_folder, notebook_dir, package=package, mapping=anchors_mapping, page_info=page_info)
392406

407+
return source_files_mapping
408+
393409

394410
def check_toc_integrity(doc_folder, output_dir):
395411
"""

src/doc_builder/commands/doc_builder_cli.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
from doc_builder.commands.build import build_command_parser
2020
from doc_builder.commands.convert_doc_file import convert_command_parser
21+
from doc_builder.commands.preview import preview_command_parser
2122
from doc_builder.commands.style import style_command_parser
2223

2324

@@ -29,6 +30,7 @@ def main():
2930
convert_command_parser(subparsers=subparsers)
3031
build_command_parser(subparsers=subparsers)
3132
style_command_parser(subparsers=subparsers)
33+
preview_command_parser(subparsers=subparsers)
3234

3335
# Let's go
3436
args = parser.parse_args()

0 commit comments

Comments
 (0)