Skip to content

Commit 8451d31

Browse files
swegnercopybara-github
authored andcommitted
Add wiring for self-links in generated docs.
PiperOrigin-RevId: 515056793
1 parent f6affa0 commit 8451d31

File tree

3 files changed

+52
-17
lines changed

3 files changed

+52
-17
lines changed

tools/tensorflow_docs/api_generator/config.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,21 @@
1717
class ParserConfig(object):
1818
"""Stores all indexes required to parse the docs."""
1919

20-
def __init__(self, *, reference_resolver, duplicates, duplicate_of, tree,
21-
index, reverse_index, path_tree, api_tree, base_dir,
22-
code_url_prefix):
20+
def __init__(
21+
self,
22+
*,
23+
reference_resolver,
24+
duplicates,
25+
duplicate_of,
26+
tree,
27+
index,
28+
reverse_index,
29+
path_tree,
30+
api_tree,
31+
base_dir,
32+
code_url_prefix,
33+
self_link_base
34+
):
2335
"""Object with the common config for docs_for_object() calls.
2436
2537
Args:
@@ -38,6 +50,8 @@ def __init__(self, *, reference_resolver, duplicates, duplicate_of, tree,
3850
base_dir: A base path that is stripped from file locations written to the
3951
docs.
4052
code_url_prefix: A Url to pre-pend to the links to file locations.
53+
self_link_base: A Url to pre-pend to self-links to the generated docs
54+
pages.
4155
"""
4256
self.reference_resolver = reference_resolver
4357
self.duplicates = duplicates
@@ -49,6 +63,7 @@ def __init__(self, *, reference_resolver, duplicates, duplicate_of, tree,
4963
self.api_tree = api_tree
5064
self.base_dir = base_dir
5165
self.code_url_prefix = code_url_prefix
66+
self.self_link_base = self_link_base
5267

5368
def py_name_to_object(self, full_name):
5469
"""Return the Python object for a Python symbol name."""

tools/tensorflow_docs/api_generator/generate_lib.py

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ def write_docs(
9797
attribute. For some classes (list, tuple, etc) __doc__ is not writable.
9898
Pass those docs like: `extra_docs={id(obj): "docs"}`
9999
page_builder_classes: A optional dict of `{ObjectType:Type[PageInfo]}` for
100-
overriding the default page builder classes.
100+
overriding the default page builder classes.
101101
102102
Raises:
103103
ValueError: if `output_dir` is not an absolute path
@@ -168,12 +168,13 @@ def write_docs(
168168
if api_report is not None:
169169
api_report.write(output_dir / root_module_name / 'api_report.pb')
170170

171-
172171
if num_docs_output <= 1:
173-
raise ValueError('The `DocGenerator` failed to generate any docs. Verify '
174-
'your arguments (`base_dir` and `callbacks`). '
175-
'Everything you want documented should be within '
176-
'`base_dir`.')
172+
raise ValueError(
173+
'The `DocGenerator` failed to generate any docs. Verify '
174+
'your arguments (`base_dir` and `callbacks`). '
175+
'Everything you want documented should be within '
176+
'`base_dir`.'
177+
)
177178

178179
if yaml_toc:
179180
if isinstance(yaml_toc, bool):
@@ -286,15 +287,18 @@ class DocGenerator:
286287

287288
def __init__(
288289
self,
290+
*,
289291
root_title: str,
290292
py_modules: Sequence[Tuple[str, Any]],
291293
base_dir: Optional[Sequence[Union[str, pathlib.Path]]] = None,
292294
code_url_prefix: Union[Optional[str], Sequence[Optional[str]]] = (),
295+
self_link_base: Optional[str] = None,
293296
search_hints: bool = True,
294297
site_path: str = 'api_docs/python',
295298
private_map: Optional[Dict[str, str]] = None,
296299
visitor_cls: Type[doc_generator_visitor.DocGeneratorVisitor] = (
297-
doc_generator_visitor.DocGeneratorVisitor),
300+
doc_generator_visitor.DocGeneratorVisitor
301+
),
298302
api_cache: bool = True,
299303
callbacks: Optional[List[public_api.ApiFilter]] = None,
300304
yaml_toc: Union[bool, Type[toc_lib.TocBuilder]] = True,
@@ -315,12 +319,15 @@ def __init__(
315319
in" paths. These are zipped with `base-dir`, to set the `defined_in`
316320
path for each file. The defined in link for `{base_dir}/path/to/file` is
317321
set to `{code_url_prefix}/path/to/file`.
322+
self_link_base: A string. A URL prefix pre-pend to self-links to the
323+
generated docs pages. Optional, if no `self_link_base` is supplied, no
324+
self-link will be added.
318325
search_hints: Bool. Include metadata search hints at the top of each file.
319326
site_path: Path prefix in the "_toc.yaml"
320327
private_map: DEPRECATED. Use `api_generator.doc_controls`, or pass a
321-
filter to the `callbacks` argument. A
322-
`{"module.path.to.object": ["names"]}` dictionary. Specific
323-
aliases that should not be shown in the resulting docs.
328+
filter to the `callbacks` argument. A `{"module.path.to.object":
329+
["names"]}` dictionary. Specific aliases that should not be shown in the
330+
resulting docs.
324331
visitor_cls: An option to override the default visitor class
325332
`doc_generator_visitor.DocGeneratorVisitor`.
326333
api_cache: Bool. Generate an api_cache file. This is used to easily add
@@ -364,10 +371,13 @@ def __init__(
364371
raise ValueError('`code_url_prefix` cannot be empty')
365372

366373
if len(self._code_url_prefix) != len(base_dir):
367-
raise ValueError('The `base_dir` list should have the same number of '
368-
'elements as the `code_url_prefix` list (they get '
369-
'zipped together).')
374+
raise ValueError(
375+
'The `base_dir` list should have the same number of '
376+
'elements as the `code_url_prefix` list (they get '
377+
'zipped together).'
378+
)
370379

380+
self._self_link_base = self_link_base
371381
self._search_hints = search_hints
372382
self._site_path = site_path
373383
self._private_map = private_map or {}
@@ -399,7 +409,9 @@ def make_parser_config(self,
399409
path_tree=visitor.path_tree,
400410
api_tree=visitor.api_tree,
401411
base_dir=self._base_dir,
402-
code_url_prefix=self._code_url_prefix)
412+
code_url_prefix=self._code_url_prefix,
413+
self_link_base=self._self_link_base,
414+
)
403415

404416
def run_extraction(self):
405417
"""Walks the module contents, returns an index of all visited objects.

tools/tensorflow_docs/api_generator/pretty_docs/base_page.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,14 @@ def set_defined_in(self, defined_in):
205205
assert self.defined_in is None
206206
self._defined_in = defined_in
207207

208+
@property
209+
def self_link(self):
210+
if not self.parser_config.self_link_base:
211+
return None
212+
rel_path = parser.documentation_path(self.full_name)
213+
rel_path = rel_path[: -1 * len('.md')] # strip suffix
214+
return f'{self.parser_config.self_link_base}/{rel_path}'
215+
208216
@property
209217
def aliases(self):
210218
"""Returns a list of all full names for the documented object."""

0 commit comments

Comments
 (0)