Skip to content

Commit 570b2b8

Browse files
MarkDaoustcopybara-github
authored andcommitted
Add injection sites for customizing page construction.
* For single-page changes, tag an object using `doc_controls.set_custom_page_builder_c lass(obj, cls) * For global changes pass a dict of `{ObjectType:Type[PageInfo]}` to the DocGenerator's `page_builder_classes` argument. * Switch generate2 to use the new customization pathways. PiperOrigin-RevId: 420801142
1 parent 786f63a commit 570b2b8

File tree

5 files changed

+46
-28
lines changed

5 files changed

+46
-28
lines changed

tools/tensorflow_docs/api_generator/doc_controls.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,17 +73,17 @@ def should_hide_from_search(obj) -> bool:
7373
return hasattr(obj, _NO_SEARCH_HINTS)
7474

7575

76-
_CUSTOM_PAGE_CONTENT = "_tf_docs_custom_page_content"
76+
_CUSTOM_PAGE_BUILDER_CLS = "_tf_docs_custom_page_builder_cls"
7777

7878

79-
def set_custom_page_content(obj, content):
79+
def set_custom_page_builder_cls(obj, cls):
8080
"""Replace most of the generated page with custom content."""
81-
setattr(obj, _CUSTOM_PAGE_CONTENT, content)
81+
setattr(obj, _CUSTOM_PAGE_BUILDER_CLS, cls)
8282

8383

84-
def get_custom_page_content(obj):
84+
def get_custom_page_builder_cls(obj):
8585
"""Gets custom page content if available."""
86-
return getattr(obj, _CUSTOM_PAGE_CONTENT, None)
86+
return getattr(obj, _CUSTOM_PAGE_BUILDER_CLS, None)
8787

8888

8989
_DO_NOT_DOC = "_tf_docs_do_not_document"

tools/tensorflow_docs/api_generator/generate_lib.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,7 @@ def write_docs(
470470
gen_redirects: bool = True,
471471
gen_report: bool = True,
472472
extra_docs: Optional[Dict[int, str]] = None,
473+
page_builder_classes: Optional[docs_for_object.PageBuilderDict] = None,
473474
):
474475
"""Write previously extracted docs to disk.
475476
@@ -498,6 +499,8 @@ def write_docs(
498499
extra_docs: To add docs for a particular object instance set it's __doc__
499500
attribute. For some classes (list, tuple, etc) __doc__ is not writable.
500501
Pass those docs like: `extra_docs={id(obj): "docs"}`
502+
page_builder_classes: A optional dict of `{ObjectType:Type[PageInfo]}` for
503+
overriding the default page builder classes.
501504
502505
Raises:
503506
ValueError: if `output_dir` is not an absolute path
@@ -564,7 +567,8 @@ def write_docs(
564567
# Generate docs for `py_object`, resolving references.
565568
try:
566569
page_info = docs_for_object.docs_for_object(full_name, py_object,
567-
parser_config, extra_docs)
570+
parser_config, extra_docs,
571+
page_builder_classes)
568572
if gen_report and not full_name.startswith(
569573
('tf.compat.v', 'tf.keras.backend', 'tf.numpy',
570574
'tf.experimental.numpy')):
@@ -731,6 +735,7 @@ def __init__(
731735
gen_redirects: bool = True,
732736
gen_report: bool = True,
733737
extra_docs: Optional[Dict[int, str]] = None,
738+
page_builder_classes: Optional[docs_for_object.PageBuilderDict] = None,
734739
):
735740
"""Creates a doc-generator.
736741
@@ -766,6 +771,8 @@ def __init__(
766771
extra_docs: To add docs for a particular object instance set it's __doc__
767772
attribute. For some classes (list, tuple, etc) __doc__ is not writable.
768773
Pass those docs like: `extra_docs={id(obj): "docs"}`
774+
page_builder_classes: An optional dict of `{ObjectType:Type[PageInfo]}`
775+
for overriding the default page builder classes.
769776
"""
770777
self._root_title = root_title
771778
self._py_modules = py_modules
@@ -807,6 +814,7 @@ def __init__(
807814
self._gen_redirects = gen_redirects
808815
self._gen_report = gen_report
809816
self._extra_docs = extra_docs
817+
self._page_builder_classes = page_builder_classes
810818

811819
def make_reference_resolver(self, visitor):
812820
return reference_resolver_lib.ReferenceResolver.from_visitor(
@@ -868,6 +876,7 @@ def build(self, output_dir):
868876
gen_redirects=self._gen_redirects,
869877
gen_report=self._gen_report,
870878
extra_docs=self._extra_docs,
879+
page_builder_classes=self._page_builder_classes,
871880
)
872881

873882
if self.api_cache:

tools/tensorflow_docs/api_generator/pretty_docs/base_page.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import textwrap
1919
from typing import Any, Callable, ClassVar, Dict, List, NamedTuple, Optional, Sequence, Tuple, Type
2020

21+
from tensorflow_docs.api_generator import config
2122
from tensorflow_docs.api_generator import parser
2223
from tensorflow_docs.api_generator import signature as signature_lib
2324

@@ -101,8 +102,13 @@ def __init__(
101102
self._aliases = None
102103
self._doc = None
103104

105+
def collect_docs(self, parser_config: config.ParserConfig):
106+
"""Collects additional information from the `config.ParserConfig`."""
107+
pass
108+
104109
def build(self) -> str:
105-
"""Builds the documentation"""
110+
"""Builds the documentation."""
111+
106112
cls = self.DEFAULT_BUILDER_CLASS
107113
return cls(self).build()
108114

tools/tensorflow_docs/api_generator/pretty_docs/docs_for_object.py

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,10 @@
1717
import os
1818
import posixpath
1919

20-
from typing import Any, Dict, Optional
20+
from typing import Any, Dict, Optional, Type
2121

2222
from tensorflow_docs.api_generator import config
23+
from tensorflow_docs.api_generator import doc_controls
2324
from tensorflow_docs.api_generator import obj_type as obj_type_lib
2425
from tensorflow_docs.api_generator import parser
2526
from tensorflow_docs.api_generator.pretty_docs import base_page
@@ -28,12 +29,22 @@
2829
from tensorflow_docs.api_generator.pretty_docs import module_page
2930
from tensorflow_docs.api_generator.pretty_docs import type_alias_page
3031

32+
_DEFAULT_PAGE_BUILDER_CLASSES = {
33+
obj_type_lib.ObjType.CLASS: class_page.ClassPageInfo,
34+
obj_type_lib.ObjType.CALLABLE: function_page.FunctionPageInfo,
35+
obj_type_lib.ObjType.MODULE: module_page.ModulePageInfo,
36+
obj_type_lib.ObjType.TYPE_ALIAS: type_alias_page.TypeAliasPageInfo,
37+
}
38+
39+
PageBuilderDict = Dict[obj_type_lib.ObjType, Type[base_page.PageInfo]]
40+
3141

3242
def docs_for_object(
3343
full_name: str,
3444
py_object: Any,
3545
parser_config: config.ParserConfig,
3646
extra_docs: Optional[Dict[int, str]] = None,
47+
page_builder_classes: Optional[PageBuilderDict] = None,
3748
) -> base_page.PageInfo:
3849
"""Return a PageInfo object describing a given object from the TF API.
3950
@@ -47,6 +58,8 @@ def docs_for_object(
4758
parser_config: A `config.ParserConfig` object.
4859
extra_docs: Extra docs for symbols like public constants(list, tuple, etc)
4960
that need to be added to the markdown pages created.
61+
page_builder_classes: An optional dict of `{ObjectType:Type[PageInfo]}` for
62+
overriding the default page builder classes.
5063
5164
Returns:
5265
Either a subclass of `pretty_docs.base_page.PageInfo` depending on the type
@@ -63,21 +76,16 @@ def docs_for_object(
6376
if main_name in duplicate_names:
6477
duplicate_names.remove(main_name)
6578

66-
obj_type = obj_type_lib.ObjType.get(py_object)
67-
if obj_type is obj_type_lib.ObjType.CLASS:
68-
page_info = class_page.ClassPageInfo(
69-
full_name=main_name, py_object=py_object, extra_docs=extra_docs)
70-
elif obj_type is obj_type_lib.ObjType.CALLABLE:
71-
page_info = function_page.FunctionPageInfo(
72-
full_name=main_name, py_object=py_object, extra_docs=extra_docs)
73-
elif obj_type is obj_type_lib.ObjType.MODULE:
74-
page_info = module_page.ModulePageInfo(
75-
full_name=main_name, py_object=py_object, extra_docs=extra_docs)
76-
elif obj_type is obj_type_lib.ObjType.TYPE_ALIAS:
77-
page_info = type_alias_page.TypeAliasPageInfo(
78-
full_name=main_name, py_object=py_object, extra_docs=extra_docs)
79-
else:
80-
raise RuntimeError('Cannot make docs for object {full_name}: {py_object!r}')
79+
if page_builder_classes is None:
80+
page_builder_classes = _DEFAULT_PAGE_BUILDER_CLASSES
81+
82+
page_info_class = doc_controls.get_custom_page_builder_cls(py_object)
83+
if page_info_class is None:
84+
obj_type = obj_type_lib.ObjType.get(py_object)
85+
page_info_class = page_builder_classes[obj_type]
86+
87+
page_info = page_info_class(
88+
full_name=main_name, py_object=py_object, extra_docs=extra_docs)
8189

8290
relative_path = os.path.relpath(
8391
path='.',

tools/tensorflow_docs/api_generator/pretty_docs/module_page.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,6 @@ def build(self) -> str:
5050

5151
parts.append('\n\n')
5252

53-
custom_content = doc_controls.get_custom_page_content(page_info.py_object)
54-
if custom_content is not None:
55-
parts.append(custom_content)
56-
return ''.join(parts)
57-
5853
if page_info.modules:
5954
parts.append('## Modules\n\n')
6055
parts.extend(

0 commit comments

Comments
 (0)