Add redirects to point api duplicates to the canonical doc location.

PiperOrigin-RevId: 205276722
This commit is contained in:
Mark Daoust 2018-07-19 11:50:09 -07:00 committed by TensorFlower Gardener
parent 3d6c8b8aae
commit cb11b60da0
3 changed files with 45 additions and 3 deletions

View File

@ -31,6 +31,11 @@ if __name__ == '__main__':
doc_generator = generate_lib.DocGenerator() doc_generator = generate_lib.DocGenerator()
doc_generator.add_output_dir_argument() doc_generator.add_output_dir_argument()
doc_generator.add_src_dir_argument() doc_generator.add_src_dir_argument()
doc_generator.argument_parser.add_argument(
'--site_api_path',
type=str, default='api_docs/python',
help='The path from the site-root to api_docs'
'directory for this project')
# This doc generator works on the TensorFlow codebase. Since this script lives # This doc generator works on the TensorFlow codebase. Since this script lives
# at tensorflow/tools/docs, and all code is defined somewhere inside # at tensorflow/tools/docs, and all code is defined somewhere inside

View File

@ -55,7 +55,8 @@ def write_docs(output_dir,
parser_config, parser_config,
yaml_toc, yaml_toc,
root_title='TensorFlow', root_title='TensorFlow',
search_hints=True): search_hints=True,
site_api_path=None):
"""Write previously extracted docs to disk. """Write previously extracted docs to disk.
Write a docs page for each symbol included in the indices of parser_config to Write a docs page for each symbol included in the indices of parser_config to
@ -73,6 +74,8 @@ def write_docs(output_dir,
root_title: The title name for the root level index.md. root_title: The title name for the root level index.md.
search_hints: (bool) include meta-data search hints at the top of each search_hints: (bool) include meta-data search hints at the top of each
output file. output file.
site_api_path: Used to write the api-duplicates _redirects.yaml file. if
None (the default) the file is not generated.
Raises: Raises:
ValueError: if `output_dir` is not an absolute path ValueError: if `output_dir` is not an absolute path
@ -92,6 +95,9 @@ def write_docs(output_dir,
# - symbol name(string):pathname (string) # - symbol name(string):pathname (string)
symbol_to_file = {} symbol_to_file = {}
# Collect redirects for an api _redirects.yaml file.
redirects = ['redirects:\n']
# Parse and write Markdown pages, resolving cross-links (@{symbol}). # Parse and write Markdown pages, resolving cross-links (@{symbol}).
for full_name, py_object in six.iteritems(parser_config.index): for full_name, py_object in six.iteritems(parser_config.index):
parser_config.reference_resolver.current_doc_full_name = full_name parser_config.reference_resolver.current_doc_full_name = full_name
@ -150,6 +156,25 @@ def write_docs(output_dir,
raise OSError( raise OSError(
'Cannot write documentation for %s to %s' % (full_name, directory)) 'Cannot write documentation for %s to %s' % (full_name, directory))
if site_api_path:
duplicates = parser_config.duplicates.get(full_name, [])
if not duplicates:
continue
duplicates = [item for item in duplicates if item != full_name]
template = ('- from: /{}\n'
' to: /{}\n')
for dup in duplicates:
from_path = os.path.join(site_api_path, dup.replace('.', '/'))
to_path = os.path.join(site_api_path, full_name.replace('.', '/'))
redirects.append(
template.format(from_path, to_path))
if site_api_path:
api_redirects_path = os.path.join(output_dir, '_redirects.yaml')
with open(api_redirects_path, 'w') as redirect_file:
redirect_file.write(''.join(redirects))
if yaml_toc: if yaml_toc:
# Generate table of contents # Generate table of contents
@ -608,7 +633,8 @@ class DocGenerator(object):
parser_config, parser_config,
yaml_toc=self.yaml_toc, yaml_toc=self.yaml_toc,
root_title=root_title, root_title=root_title,
search_hints=getattr(flags, 'search_hints', True)) search_hints=getattr(flags, 'search_hints', True),
site_api_path=getattr(flags, 'site_api_path', None))
# Replace all the @{} references in files under `FLAGS.src_dir` # Replace all the @{} references in files under `FLAGS.src_dir`
replace_refs(flags.src_dir, flags.output_dir, reference_resolver, '*.md') replace_refs(flags.src_dir, flags.output_dir, reference_resolver, '*.md')

View File

@ -107,7 +107,18 @@ class GenerateTest(googletest.TestCase):
output_dir = googletest.GetTempDir() output_dir = googletest.GetTempDir()
generate_lib.write_docs(output_dir, parser_config, yaml_toc=True) generate_lib.write_docs(output_dir, parser_config, yaml_toc=True,
site_api_path='api_docs/python')
# Check redirects
redirects_file = os.path.join(output_dir, '_redirects.yaml')
self.assertTrue(os.path.exists(redirects_file))
with open(redirects_file) as f:
redirects = f.read()
self.assertEqual(redirects.split(), [
'redirects:', '-', 'from:', '/api_docs/python/tf/test_function', 'to:',
'/api_docs/python/tf/TestModule/test_function'
])
# Make sure that the right files are written to disk. # Make sure that the right files are written to disk.
self.assertTrue(os.path.exists(os.path.join(output_dir, 'index.md'))) self.assertTrue(os.path.exists(os.path.join(output_dir, 'index.md')))