Skip to content

Commit 9adea2a

Browse files
MarkDaoustcopybara-github
authored andcommitted
Swap the ModulePageBuilder to use a template.
No output changes. PiperOrigin-RevId: 421012042
1 parent 7241ee5 commit 9adea2a

File tree

4 files changed

+118
-66
lines changed

4 files changed

+118
-66
lines changed

tools/tensorflow_docs/api_generator/pretty_docs/base_page.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ def build_other_members(other_members: List[MemberInfo], title: str):
229229
description='\n'.join(description),
230230
))
231231
return '\n' + parser.TABLE_TEMPLATE.format(
232-
title=title, text='', items=''.join(items)) + '\n'
232+
title=title, text='', items=''.join(items))
233233

234234

235235
DECORATOR_ALLOWLIST = frozenset({

tools/tensorflow_docs/api_generator/pretty_docs/module_page.py

Lines changed: 14 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -22,70 +22,21 @@
2222
from tensorflow_docs.api_generator.pretty_docs import base_page
2323

2424

25-
class ModulePageBuilder(base_page.PageBuilder):
25+
class ModulePageBuilder(base_page.TemplatePageBuilder):
2626
"""Builds a markdown page from a `ModulePageInfo` instance."""
27-
28-
def build(self) -> str:
29-
"""Build the page."""
30-
page_info = self.page_info
31-
parts = [f'# Module: {page_info.full_name}\n\n']
32-
33-
parts.append('<!-- Insert buttons and diff -->\n')
34-
35-
parts.append(base_page.top_source_link(page_info.defined_in))
36-
parts.append('\n\n')
37-
38-
# First line of the docstring i.e. a brief introduction about the symbol.
39-
parts.append(page_info.doc.brief + '\n\n')
40-
41-
parts.append(base_page.build_collapsable_aliases(page_info.aliases))
42-
43-
parts.append(base_page.build_top_compat(page_info, h_level=2))
44-
45-
# All lines in the docstring, expect the brief introduction.
46-
for item in page_info.doc.docstring_parts:
47-
parts.append(base_page.format_docstring(item, table_title_template=None))
48-
49-
parts.append(base_page.build_bottom_compat(page_info, h_level=2))
50-
51-
parts.append('\n\n')
52-
53-
if page_info.modules:
54-
parts.append('## Modules\n\n')
55-
parts.extend(
56-
_build_module_parts(
57-
module_parts=page_info.modules,
58-
template='[`{short_name}`]({url}) module'))
59-
60-
if page_info.classes:
61-
parts.append('## Classes\n\n')
62-
parts.extend(
63-
_build_module_parts(
64-
module_parts=page_info.classes,
65-
template='[`class {short_name}`]({url})'))
66-
67-
if page_info.functions:
68-
parts.append('## Functions\n\n')
69-
parts.extend(
70-
_build_module_parts(
71-
module_parts=page_info.functions,
72-
template='[`{short_name}(...)`]({url})'))
73-
74-
if page_info.type_alias:
75-
parts.append('## Type Aliases\n\n')
76-
parts.extend(
77-
_build_module_parts(
78-
module_parts=page_info.type_alias,
79-
template='[`{short_name}`]({url})'))
80-
81-
if page_info.other_members:
82-
parts.append(
83-
base_page.build_other_members(
84-
page_info.other_members,
85-
title='<h2 class="add-link">Other Members</h2>',
86-
))
87-
88-
return ''.join(parts)
27+
TEMPLATE = 'templates/module.jinja'
28+
29+
def __init__(self, page_info: 'ModulePageInfo'):
30+
super().__init__(page_info)
31+
32+
def build_other_member_section(self):
33+
if self.page_info.other_members:
34+
return base_page.build_other_members(
35+
self.page_info.other_members,
36+
title='<h2 class="add-link">Other Members</h2>',
37+
)
38+
else:
39+
return ''
8940

9041

9142
class ModulePageInfo(base_page.PageInfo):

tools/tensorflow_docs/api_generator/pretty_docs/templates/class.jinja

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
{{- self.child_classes() -}}
3030
{{- self.methods() -}}
3131

32-
{{- builder.build_other_member_section() -}}
32+
{{- self.other_members() -}}
3333

3434
{{- builder.bottom_compat() -}}
3535
{% endblock body %}
@@ -63,5 +63,7 @@
6363
{#----------------------------------------------------------------------------#}
6464

6565
{% block other_members %}
66-
{{ builder.build_other_member_section() -}}
66+
{%- if page_info.other_members -%}
67+
{{ builder.build_other_member_section() }}
68+
{% endif %}
6769
{% endblock other_members%}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
{% extends "templates/page.jinja" %}
2+
3+
{#----------------------------------------------------------------------------#}
4+
5+
{% block header %}
6+
# Module: {{page_info.full_name}}
7+
8+
<!-- Insert buttons and diff -->
9+
{% endblock header%}
10+
11+
{#----------------------------------------------------------------------------#}
12+
13+
{% block body %}
14+
{{ builder.top_compat() -}}
15+
16+
{% for part in page_info.doc.docstring_parts %}
17+
{{- builder.format_docstring_part(part) -}}
18+
{% endfor %}
19+
{{ builder.bottom_compat() }}
20+
21+
{{ self.modules() }}
22+
{{- self.classes() }}
23+
{{- self.functions() }}
24+
{{- self.type_aliases() }}
25+
{{- self.other_members() -}}
26+
{% endblock body %}
27+
28+
{#----------------------------------------------------------------------------#}
29+
30+
{% block modules %}
31+
{%- if page_info.modules %}
32+
## Modules
33+
34+
{% for module in page_info.modules %}
35+
{%if module.doc.brief%}
36+
[`{{module.short_name}}`]({{module.url}}) module: {{module.doc.brief}}
37+
{%else%}
38+
[`{{module.short_name}}`]({{module.url}}) module
39+
{%endif%}
40+
41+
{% endfor %}
42+
{% endif -%}
43+
{% endblock modules %}
44+
45+
{#----------------------------------------------------------------------------#}
46+
47+
{% block classes %}
48+
{%- if page_info.classes %}
49+
## Classes
50+
51+
{% for cls in page_info.classes %}
52+
{%if cls.doc.brief%}
53+
[`class {{cls.short_name}}`]({{cls.url}}): {{cls.doc.brief}}
54+
{%else%}
55+
[`class {{cls.short_name}}`]({{cls.url}})
56+
{%endif%}
57+
58+
{% endfor %}
59+
{% endif -%}
60+
{% endblock classes%}
61+
62+
{#----------------------------------------------------------------------------#}
63+
64+
{% block functions %}
65+
{%- if page_info.functions -%}
66+
## Functions
67+
68+
{% for fun in page_info.functions %}
69+
{%if fun.doc.brief%}
70+
[`{{fun.short_name}}(...)`]({{fun.url}}): {{fun.doc.brief}}
71+
{%else%}
72+
[`{{fun.short_name}}(...)`]({{fun.url}})
73+
{%endif%}
74+
75+
{% endfor %}
76+
{% endif -%}
77+
{% endblock functions%}
78+
79+
{#----------------------------------------------------------------------------#}
80+
81+
{% block type_aliases %}
82+
{%- if page_info.type_alias -%}
83+
## Type Aliases
84+
85+
{% for alias in page_info.type_alias %}
86+
[`{{alias.short_name}}`]({{alias.url}})
87+
88+
{% endfor %}
89+
{% endif -%}
90+
{% endblock type_aliases%}
91+
92+
{#----------------------------------------------------------------------------#}
93+
94+
{% block other_members %}
95+
{%- if page_info.other_members -%}
96+
{{ builder.build_other_member_section() }}
97+
{% endif %}
98+
{% endblock other_members%}
99+

0 commit comments

Comments
 (0)