Skip to content

Commit 5d97de5

Browse files
MarkDaoustcopybara-github
authored andcommitted
Move the page headers to the templates.
PiperOrigin-RevId: 421556385
1 parent 82e0ad2 commit 5d97de5

File tree

7 files changed

+102
-82
lines changed

7 files changed

+102
-82
lines changed

tools/tensorflow_docs/api_generator/generate_lib.py

Lines changed: 10 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -437,27 +437,6 @@ def generate(self) -> Dict[str, Any]:
437437
return {'toc': toc}
438438

439439

440-
def _get_headers(page_info: base_page.PageInfo,
441-
search_hints: bool) -> List[str]:
442-
"""Returns the list of header lines for this page."""
443-
hidden = doc_controls.should_hide_from_search(page_info.py_object)
444-
brief_no_backticks = page_info.doc.brief.replace('`', '').strip()
445-
headers = []
446-
if brief_no_backticks:
447-
headers.append(f'description: {brief_no_backticks}')
448-
449-
# It's easier to read if there's a blank line after the `name:value` headers.
450-
if search_hints and not hidden:
451-
if headers:
452-
headers.append('')
453-
headers.append(page_info.get_metadata_html())
454-
else:
455-
headers.append('robots: noindex')
456-
headers.append('')
457-
458-
return headers
459-
460-
461440
def write_docs(
462441
*,
463442
output_dir: Union[str, pathlib.Path],
@@ -566,9 +545,14 @@ def write_docs(
566545

567546
# Generate docs for `py_object`, resolving references.
568547
try:
569-
page_info = docs_for_object.docs_for_object(full_name, py_object,
570-
parser_config, extra_docs,
571-
page_builder_classes)
548+
page_info = docs_for_object.docs_for_object(
549+
full_name=full_name,
550+
py_object=py_object,
551+
parser_config=parser_config,
552+
extra_docs=extra_docs,
553+
search_hints=search_hints,
554+
page_builder_classes=page_builder_classes)
555+
572556
if gen_report and not full_name.startswith(
573557
('tf.compat.v', 'tf.keras.backend', 'tf.numpy',
574558
'tf.experimental.numpy')):
@@ -579,9 +563,8 @@ def write_docs(
579563

580564
path = output_dir / parser.documentation_path(full_name)
581565

582-
content = _get_headers(page_info, search_hints)
583-
content.append(page_info.build())
584-
text = '\n'.join(content)
566+
text = page_info.build()
567+
585568
try:
586569
path.parent.mkdir(exist_ok=True, parents=True)
587570
path.write_text(text, encoding='utf-8')

tools/tensorflow_docs/api_generator/generate_lib_test.py

Lines changed: 0 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -187,60 +187,6 @@ def test_write(self):
187187
# Make sure that duplicates are not written
188188
self.assertTrue((output_dir / 'tf/TestModule/test_function.md').exists())
189189

190-
def _get_test_page_info(self):
191-
page_info = function_page.FunctionPageInfo(
192-
full_name='abc', py_object=test_function)
193-
docstring_info = parser.DocstringInfo(
194-
brief='hello `tensorflow`',
195-
docstring_parts=['line1', 'line2'],
196-
compatibility={})
197-
page_info.set_doc(docstring_info)
198-
return page_info
199-
200-
def test_get_headers_global_hints(self):
201-
page_info = self._get_test_page_info()
202-
result = '\n'.join(generate_lib._get_headers(page_info, search_hints=True))
203-
204-
expected = textwrap.dedent("""\
205-
description: hello tensorflow
206-
207-
<div itemscope itemtype="http://developers.google.com/ReferenceObject">
208-
<meta itemprop="name" content="abc" />
209-
<meta itemprop="path" content="Stable" />
210-
</div>
211-
""")
212-
213-
self.assertEqual(expected, result)
214-
215-
def test_get_headers_global_no_hints(self):
216-
page_info = self._get_test_page_info()
217-
result = '\n'.join(generate_lib._get_headers(page_info, search_hints=False))
218-
219-
expected = textwrap.dedent("""\
220-
description: hello tensorflow
221-
robots: noindex
222-
""")
223-
224-
self.assertEqual(expected, result)
225-
226-
def test_get_headers_local_no_hints(self):
227-
page_info = self._get_test_page_info()
228-
229-
@doc_controls.hide_from_search
230-
def py_object():
231-
pass
232-
233-
page_info.py_object = py_object
234-
235-
result = '\n'.join(generate_lib._get_headers(page_info, search_hints=True))
236-
237-
expected = textwrap.dedent("""\
238-
description: hello tensorflow
239-
robots: noindex
240-
""")
241-
242-
self.assertEqual(expected, result)
243-
244190

245191
if __name__ == '__main__':
246192
absltest.main()

tools/tensorflow_docs/api_generator/pretty_docs/base_page.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
from typing import Any, Callable, ClassVar, Dict, List, NamedTuple, Optional, Sequence, Tuple, Type
2020

2121
from tensorflow_docs.api_generator import config
22+
from tensorflow_docs.api_generator import doc_controls
2223
from tensorflow_docs.api_generator import parser
2324
from tensorflow_docs.api_generator import signature as signature_lib
2425

@@ -63,6 +64,25 @@ def bottom_compat(self):
6364
def format_docstring_part(self, part):
6465
return str(part)
6566

67+
def get_devsite_headers(self):
68+
"""Returns the list of header lines for this page."""
69+
hidden = doc_controls.should_hide_from_search(self.page_info.py_object)
70+
brief_no_backticks = self.page_info.doc.brief.replace('`', '').strip()
71+
headers = []
72+
if brief_no_backticks:
73+
headers.append(f'description: {brief_no_backticks}')
74+
75+
if self.page_info.search_hints and not hidden:
76+
if headers:
77+
headers.append('')
78+
headers.append(self.page_info.get_metadata_html())
79+
else:
80+
headers.append('robots: noindex')
81+
headers.append('')
82+
83+
result = '\n'.join(headers)
84+
return result
85+
6686

6787
class PageInfo:
6888
"""Base-class for api_pages objects.
@@ -85,6 +105,7 @@ def __init__(
85105
full_name: str,
86106
py_object: Any,
87107
extra_docs: Optional[Dict[int, str]] = None,
108+
search_hints: bool = True,
88109
):
89110
"""Initialize a PageInfo.
90111
@@ -97,6 +118,7 @@ def __init__(
97118
self.full_name = full_name
98119
self.py_object = py_object
99120
self._extra_docs = extra_docs
121+
self.search_hints = search_hints
100122

101123
self._defined_in = None
102124
self._aliases = None

tools/tensorflow_docs/api_generator/pretty_docs/docs_for_object.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,12 @@
4040

4141

4242
def docs_for_object(
43+
*,
4344
full_name: str,
4445
py_object: Any,
4546
parser_config: config.ParserConfig,
4647
extra_docs: Optional[Dict[int, str]] = None,
48+
search_hints: bool = True,
4749
page_builder_classes: Optional[PageBuilderDict] = None,
4850
) -> base_page.PageInfo:
4951
"""Return a PageInfo object describing a given object from the TF API.
@@ -85,7 +87,10 @@ def docs_for_object(
8587
page_info_class = page_builder_classes[obj_type]
8688

8789
page_info = page_info_class(
88-
full_name=main_name, py_object=py_object, extra_docs=extra_docs)
90+
full_name=main_name,
91+
py_object=py_object,
92+
search_hints=search_hints,
93+
extra_docs=extra_docs)
8994

9095
relative_path = os.path.relpath(
9196
path='.',

tools/tensorflow_docs/api_generator/pretty_docs/pretty_docs_test.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@
1919

2020
from absl.testing import absltest
2121

22+
from tensorflow_docs.api_generator import doc_controls
2223
from tensorflow_docs.api_generator import parser
2324
from tensorflow_docs.api_generator.pretty_docs import base_page
25+
from tensorflow_docs.api_generator.pretty_docs import function_page
2426

2527

2628
class ParserTest(absltest.TestCase):
@@ -70,6 +72,66 @@ def test_no_source_link(self):
7072
""")
7173
self.assertEqual(expected, table)
7274

75+
def _get_test_page_builder(self, search_hints):
76+
77+
def test_function():
78+
pass
79+
80+
page_info = function_page.FunctionPageInfo(
81+
full_name='abc', py_object=test_function, search_hints=search_hints)
82+
docstring_info = parser.DocstringInfo(
83+
brief='hello `tensorflow`',
84+
docstring_parts=['line1', 'line2'],
85+
compatibility={})
86+
page_info.set_doc(docstring_info)
87+
page_builder = function_page.FunctionPageBuilder(page_info)
88+
return page_builder
89+
90+
def test_get_headers_global_hints(self):
91+
page_builder = self._get_test_page_builder(search_hints=True)
92+
result = page_builder.get_devsite_headers()
93+
94+
expected = textwrap.dedent("""\
95+
description: hello tensorflow
96+
97+
<div itemscope itemtype="http://developers.google.com/ReferenceObject">
98+
<meta itemprop="name" content="abc" />
99+
<meta itemprop="path" content="Stable" />
100+
</div>
101+
""")
102+
103+
self.assertEqual(expected, result)
104+
105+
def test_get_headers_global_no_hints(self):
106+
page_builder = self._get_test_page_builder(search_hints=False)
107+
result = page_builder.get_devsite_headers()
108+
109+
expected = textwrap.dedent("""\
110+
description: hello tensorflow
111+
robots: noindex
112+
""")
113+
114+
self.assertEqual(expected, result)
115+
116+
def test_get_headers_local_no_hints(self):
117+
page_builder = self._get_test_page_builder(search_hints=True)
118+
result = page_builder.get_devsite_headers()
119+
120+
@doc_controls.hide_from_search
121+
def py_object():
122+
pass
123+
124+
page_builder.page_info.py_object = py_object
125+
126+
result = page_builder.get_devsite_headers()
127+
128+
expected = textwrap.dedent("""\
129+
description: hello tensorflow
130+
robots: noindex
131+
""")
132+
133+
self.assertEqual(expected, result)
134+
73135

74136
if __name__ == "__main__":
75137
absltest.main()

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
{#----------------------------------------------------------------------------#}
44

55
{% block header %}
6+
{{ builder.get_devsite_headers() }}
67
# Module: {{page_info.full_name}}
78

89
<!-- Insert buttons and diff -->

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
{#----------------------------------------------------------------------------#}
1212

1313
{% block header %}
14+
{{ builder.get_devsite_headers() }}
1415
# {{page_info.full_name}}
1516

1617
<!-- Insert buttons and diff -->

0 commit comments

Comments
 (0)