aboutsummaryrefslogtreecommitdiffhomepage
path: root/tensorflow/tools/docs
diff options
context:
space:
mode:
authorGravatar Mark Daoust <markdaoust@google.com>2018-07-19 11:50:09 -0700
committerGravatar TensorFlower Gardener <gardener@tensorflow.org>2018-07-19 11:56:21 -0700
commitcb11b60da0e2d8e2730e9bb096f40aa2ed1f2b56 (patch)
tree63582b6ff48b271f3e892711d7d8a3af271612b3 /tensorflow/tools/docs
parent3d6c8b8aae8433b16af61097641b9958e679fb3d (diff)
Add redirects to point api duplicates to the canonical doc location.
PiperOrigin-RevId: 205276722
Diffstat (limited to 'tensorflow/tools/docs')
-rw-r--r--tensorflow/tools/docs/generate.py5
-rw-r--r--tensorflow/tools/docs/generate_lib.py30
-rw-r--r--tensorflow/tools/docs/generate_lib_test.py13
3 files changed, 45 insertions, 3 deletions
diff --git a/tensorflow/tools/docs/generate.py b/tensorflow/tools/docs/generate.py
index fc93085e3e..f96887e4c7 100644
--- a/tensorflow/tools/docs/generate.py
+++ b/tensorflow/tools/docs/generate.py
@@ -31,6 +31,11 @@ if __name__ == '__main__':
doc_generator = generate_lib.DocGenerator()
doc_generator.add_output_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
# at tensorflow/tools/docs, and all code is defined somewhere inside
diff --git a/tensorflow/tools/docs/generate_lib.py b/tensorflow/tools/docs/generate_lib.py
index e7634cd5dc..4f70a69364 100644
--- a/tensorflow/tools/docs/generate_lib.py
+++ b/tensorflow/tools/docs/generate_lib.py
@@ -55,7 +55,8 @@ def write_docs(output_dir,
parser_config,
yaml_toc,
root_title='TensorFlow',
- search_hints=True):
+ search_hints=True,
+ site_api_path=None):
"""Write previously extracted docs to disk.
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.
search_hints: (bool) include meta-data search hints at the top of each
output file.
+ site_api_path: Used to write the api-duplicates _redirects.yaml file. if
+ None (the default) the file is not generated.
Raises:
ValueError: if `output_dir` is not an absolute path
@@ -92,6 +95,9 @@ def write_docs(output_dir,
# - symbol name(string):pathname (string)
symbol_to_file = {}
+ # Collect redirects for an api _redirects.yaml file.
+ redirects = ['redirects:\n']
+
# Parse and write Markdown pages, resolving cross-links (@{symbol}).
for full_name, py_object in six.iteritems(parser_config.index):
parser_config.reference_resolver.current_doc_full_name = full_name
@@ -150,6 +156,25 @@ def write_docs(output_dir,
raise OSError(
'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:
# Generate table of contents
@@ -608,7 +633,8 @@ class DocGenerator(object):
parser_config,
yaml_toc=self.yaml_toc,
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_refs(flags.src_dir, flags.output_dir, reference_resolver, '*.md')
diff --git a/tensorflow/tools/docs/generate_lib_test.py b/tensorflow/tools/docs/generate_lib_test.py
index 7a6f9fd9f7..de18b13254 100644
--- a/tensorflow/tools/docs/generate_lib_test.py
+++ b/tensorflow/tools/docs/generate_lib_test.py
@@ -107,7 +107,18 @@ class GenerateTest(googletest.TestCase):
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.
self.assertTrue(os.path.exists(os.path.join(output_dir, 'index.md')))