Skip to content

Commit b0901e7

Browse files
MarkDaoustcopybara-github
authored andcommitted
Swap the TypeAliasPageBuilder to use a template.
No output changes. PiperOrigin-RevId: 416268003
1 parent fba766b commit b0901e7

File tree

5 files changed

+43
-38
lines changed

5 files changed

+43
-38
lines changed

tools/tensorflow_docs/api_generator/parser.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1908,6 +1908,7 @@ class ClassPageInfo(PageInfo):
19081908
defined inside the class object (mostly enum style fields).
19091909
attr_block: A `TitleBlock` containing information about the Attributes of
19101910
the class.
1911+
inheritable_header: A header that may be placed on a base-class.
19111912
"""
19121913

19131914
def __init__(self, *, full_name, py_object, **kwargs):
@@ -1939,6 +1940,13 @@ def bases(self):
19391940
"""Returns a list of `MemberInfo` objects pointing to the class' parents."""
19401941
return self._bases
19411942

1943+
@property
1944+
def inheritable_header(self) -> Optional[str]:
1945+
header = doc_controls.get_inheritable_header(self.py_object)
1946+
if header is not None:
1947+
header = textwrap.dedent(header)
1948+
return header
1949+
19421950
def set_attr_block(self, attr_block):
19431951
assert self.attr_block is None
19441952
self.attr_block = attr_block

tools/tensorflow_docs/api_generator/pretty_docs.py

Lines changed: 6 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -142,43 +142,19 @@ def format_docstring_part(self, part):
142142
return _format_docstring(part, table_title_template=ttt)
143143

144144
def build_signature(self):
145-
if self.page_info.signature:
146-
return _build_signature(self.page_info)
145+
return _build_signature(self.page_info)
147146

148147

149-
class TypeAliasPageBuilder(PageBuilder):
148+
class TypeAliasPageBuilder(TemplatePageBuilder):
150149
"""Builds a markdown page from a `TypeAliasPageBuilder` object."""
150+
TEMPLATE = 'templates/type_alias.jinja'
151151

152152
def __init__(self, page_info: parser.TypeAliasPageInfo):
153153
super().__init__(page_info)
154154

155-
def build(self) -> str:
156-
"""Builds and returns a markdown page."""
157-
page_info = self.page_info
158-
159-
parts = [f'# {page_info.full_name}\n\n']
160-
161-
parts.append('<!-- Insert buttons and diff -->\n')
162-
163-
parts.append('This symbol is a **type alias**.\n\n')
164-
parts.append(page_info.doc.brief)
165-
parts.append('\n\n')
166-
167-
if page_info.signature is not None:
168-
parts.append('#### Source:\n\n')
169-
parts.append(
170-
_build_signature(
171-
page_info, obj_name=page_info.short_name, type_alias=True))
172-
parts.append('\n\n')
173-
174-
parts.append('<!-- Placeholder for "Used in" -->\n')
175-
176-
for item in page_info.doc.docstring_parts:
177-
parts.append(
178-
_format_docstring(
179-
item, table_title_template='<h2 class="add-link">{title}</h2>'))
180-
181-
return ''.join(parts)
155+
def build_signature(self):
156+
return _build_signature(
157+
self.page_info, obj_name=self.page_info.short_name, type_alias=True)
182158

183159

184160
class Methods(NamedTuple):
@@ -307,13 +283,6 @@ def __init__(self, page_info: parser.ClassPageInfo):
307283
# Split the methods into constructor and other methods.
308284
self.methods = split_methods(page_info.methods)
309285

310-
def build_inheritable_header(self):
311-
header = doc_controls.get_inheritable_header(self.page_info.py_object)
312-
if header is None:
313-
return ''
314-
else:
315-
return '\n\n' + textwrap.dedent(header)
316-
317286
def build_bases(self):
318287
page_info = self.page_info
319288
# If a class is a child class, add which classes it inherits from.

tools/tensorflow_docs/api_generator/templates/class.jinja

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,13 @@
66
{{ builder.top_source_link() }}
77

88
{{ page_info.doc.brief -}}
9-
{{ builder.build_inheritable_header() }}
9+
10+
{% if page_info.inheritable_header %}
11+
12+
13+
{{ page_info.inheritable_header -}}
14+
{% endif %}
15+
1016
{{ builder.build_bases() }}
1117
{{ builder.build_collapsable_aliases() -}}
1218
{{ builder.build_constructor() -}}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
{% extends "templates/page.jinja" %}
22

33
{% block body %}
4+
{% if page_info.signature %}
45
{{ builder.build_signature() }}
6+
{% endif %}
57
{{ super() -}}
68
{% endblock %}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{% extends "templates/page.jinja" %}
2+
3+
4+
{% block metadata %}
5+
This symbol is a **type alias**.
6+
7+
{{ page_info.doc.brief }}
8+
9+
{% endblock metadata %}
10+
11+
{#----------------------------------------------------------------------------#}
12+
13+
{% block body %}
14+
{% if page_info.signature %}
15+
#### Source:
16+
17+
{{ builder.build_signature() }}
18+
{% endif %}
19+
{{ super() -}}
20+
{% endblock %}

0 commit comments

Comments
 (0)