Skip to content

Commit af0b684

Browse files
MarkDaoustcopybara-github
authored andcommitted
Test headers.
PiperOrigin-RevId: 358035270
1 parent a8a2778 commit af0b684

File tree

2 files changed

+80
-14
lines changed

2 files changed

+80
-14
lines changed

tools/tensorflow_docs/api_generator/generate_lib.py

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,26 @@ def generate(self) -> Dict[str, Any]:
411411
return {'toc': toc}
412412

413413

414+
def _get_headers(page_info: parser.PageInfo, search_hints: bool) -> List[str]:
415+
"""Returns the list of header lines for this page."""
416+
hidden = doc_controls.should_hide_from_search(page_info.py_object)
417+
brief_no_backticks = page_info.doc.brief.replace('`', '').strip()
418+
headers = []
419+
if brief_no_backticks:
420+
headers.append(f'description: {brief_no_backticks}')
421+
422+
# It's easier to read if there's a blank line after the `name:value` headers.
423+
if search_hints and not hidden:
424+
if headers:
425+
headers.append('')
426+
headers.append(page_info.get_metadata_html())
427+
else:
428+
headers.append('robots: noindex')
429+
headers.append('')
430+
431+
return headers
432+
433+
414434
def write_docs(
415435
*,
416436
output_dir: Union[str, pathlib.Path],
@@ -526,22 +546,12 @@ def write_docs(
526546
continue
527547

528548
path = output_dir / parser.documentation_path(full_name)
549+
550+
content = _get_headers(page_info, search_hints)
551+
content.append(pretty_docs.build_md_page(page_info))
552+
text = '\n'.join(content)
529553
try:
530554
path.parent.mkdir(exist_ok=True, parents=True)
531-
# This function returns unicode in PY3.
532-
hidden = doc_controls.should_hide_from_search(page_info.py_object)
533-
brief_no_backticks = page_info.doc.brief.replace('`', '').strip()
534-
content = []
535-
if brief_no_backticks:
536-
content.append(f'description: {brief_no_backticks}\n')
537-
538-
if search_hints and not hidden:
539-
content.append(page_info.get_metadata_html())
540-
else:
541-
content.append('robots: noindex\n')
542-
543-
content.append(pretty_docs.build_md_page(page_info))
544-
text = '\n'.join(content)
545555
path.write_text(text, encoding='utf-8')
546556
except OSError:
547557
raise OSError('Cannot write documentation for '

tools/tensorflow_docs/api_generator/generate_lib_test.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,12 @@
1919
import pathlib
2020
import sys
2121
import tempfile
22+
import textwrap
2223

2324
from absl import flags
2425
from absl.testing import absltest
2526

27+
from tensorflow_docs.api_generator import doc_controls
2628
from tensorflow_docs.api_generator import generate_lib
2729
from tensorflow_docs.api_generator import parser
2830

@@ -244,6 +246,60 @@ def test_replace_refes(self):
244246
with open(os.path.join(test_out_dir, 'b/OWNERS')) as f:
245247
content = f.read()
246248

249+
def _get_test_page_info(self):
250+
page_info = parser.FunctionPageInfo(
251+
full_name='abc', py_object=test_function)
252+
docstring_info = parser._DocstringInfo(
253+
brief='hello `tensorflow`',
254+
docstring_parts=['line1', 'line2'],
255+
compatibility={})
256+
page_info.set_doc(docstring_info)
257+
return page_info
258+
259+
def test_get_headers_global_hints(self):
260+
page_info = self._get_test_page_info()
261+
result = '\n'.join(generate_lib._get_headers(page_info, search_hints=True))
262+
263+
expected = textwrap.dedent("""\
264+
description: hello tensorflow
265+
266+
<div itemscope itemtype="http://developers.google.com/ReferenceObject">
267+
<meta itemprop="name" content="abc" />
268+
<meta itemprop="path" content="Stable" />
269+
</div>
270+
""")
271+
272+
self.assertEqual(expected, result)
273+
274+
def test_get_headers_global_no_hints(self):
275+
page_info = self._get_test_page_info()
276+
result = '\n'.join(generate_lib._get_headers(page_info, search_hints=False))
277+
278+
expected = textwrap.dedent("""\
279+
description: hello tensorflow
280+
robots: noindex
281+
""")
282+
283+
self.assertEqual(expected, result)
284+
285+
def test_get_headers_local_no_hints(self):
286+
page_info = self._get_test_page_info()
287+
288+
@doc_controls.hide_from_search
289+
def py_object():
290+
pass
291+
292+
page_info.py_object = py_object
293+
294+
result = '\n'.join(generate_lib._get_headers(page_info, search_hints=True))
295+
296+
expected = textwrap.dedent("""\
297+
description: hello tensorflow
298+
robots: noindex
299+
""")
300+
301+
self.assertEqual(expected, result)
302+
247303

248304
if __name__ == '__main__':
249305
absltest.main()

0 commit comments

Comments
 (0)