def test_references_replaced_in_generated_markdown(self): index = { 'test_function_for_markdown_reference': test_function_for_markdown_reference } tree = {'': ['test_function_for_markdown_reference']} docs = parser.generate_markdown( full_name='test_function_for_markdown_reference', py_object=test_function_for_markdown_reference, duplicate_of={}, duplicates={}, index=index, tree=tree, reverse_index={}, doc_index={}, guide_index={}, base_dir='/') # Make sure docstring shows up and is properly processed. expected_docs = parser.replace_references( inspect.getdoc(test_function_for_markdown_reference), relative_path_to_root='.', duplicate_of={}, doc_index={}, index={}) self.assertTrue(expected_docs in docs)
def test_replace_references(self): string = 'A @{reference}, another @{reference}, and a @{third}.' duplicate_of = {'third': 'fourth'} result = parser.replace_references(string, '../..', duplicate_of) self.assertEqual( 'A [`reference`](../../reference.md), another ' '[`reference`](../../reference.md), ' 'and a [`third`](../../fourth.md).', result)
def test_replace_references(self): string = 'A @{reference}, another @{reference}, and a @{third}.' duplicate_of = {'third': 'fourth'} result = parser.replace_references(string, '../..', duplicate_of) self.assertEqual( 'A [`reference`](../../reference.md), another ' '[`reference`](../../reference.md), ' 'and a [`third`](../../fourth.md).', result)
def other_docs(src_dir, output_dir, visitor, doc_index): """Convert all the files in `src_dir` and write results to `output_dir`.""" header = '<!-- DO NOT EDIT! Automatically generated file. -->\n' # Iterate through all the source files and process them. tag_updater = UpdateTags() for dirpath, _, filenames in os.walk(src_dir): # How to get from `dirpath` to api_docs/python/ relative_path_to_root = os.path.relpath(path=os.path.join( src_dir, 'api_docs/python'), start=dirpath) # Make the directory under output_dir. new_dir = os.path.join(output_dir, os.path.relpath(path=dirpath, start=src_dir)) try: if not os.path.exists(new_dir): os.makedirs(new_dir) except OSError as e: print('Creating output dir "%s" failed: %s' % (new_dir, e)) raise for base_name in filenames: full_in_path = os.path.join(dirpath, base_name) suffix = os.path.relpath(path=full_in_path, start=src_dir) full_out_path = os.path.join(output_dir, suffix) if not base_name.endswith('.md'): print('Copying non-md file %s...' % suffix) open(full_out_path, 'w').write(open(full_in_path).read()) continue if dirpath.endswith('/api_guides/python'): print('Processing Python guide %s...' % base_name) md_string = tag_updater.process(full_in_path) else: print('Processing doc %s...' % suffix) md_string = open(full_in_path).read() output = parser.replace_references(md_string, relative_path_to_root, visitor.duplicate_of, doc_index=doc_index, index=visitor.index) with open(full_out_path, 'w') as f: f.write(header + output) print('Done.')
def test_doc_replace_references(self): string = '@{$doc1} @{$doc1#abc} @{$doc1$link} @{$doc1#def$zelda} @{$do/c2}' class DocInfo(object): pass doc1 = DocInfo() doc1.title = 'Title1' doc1.url = 'URL1' doc2 = DocInfo() doc2.title = 'Two words' doc2.url = 'somewhere/else' doc_index = {'doc1': doc1, 'do/c2': doc2} result = parser.replace_references(string, 'python', {}, doc_index=doc_index, index={}) self.assertEqual( '[Title1](../URL1) [Title1](../URL1#abc) [link](../URL1) ' '[zelda](../URL1#def) [Two words](../somewhere/else)', result)
def test_doc_replace_references(self): string = '@{$doc1} @{$doc1#abc} @{$doc1$link} @{$doc1#def$zelda} @{$do/c2}' class DocInfo(object): pass doc1 = DocInfo() doc1.title = 'Title1' doc1.url = 'URL1' doc2 = DocInfo() doc2.title = 'Two words' doc2.url = 'somewhere/else' doc_index = {'doc1': doc1, 'do/c2': doc2} result = parser.replace_references(string, '..', {}, doc_index=doc_index, index={}) self.assertEqual( '[Title1](../URL1) [Title1](../URL1#abc) [link](../URL1) ' '[zelda](../URL1#def) [Two words](../somewhere/else)', result)
def other_docs(src_dir, output_dir, visitor, doc_index): """Convert all the files in `src_dir` and write results to `output_dir`.""" header = '<!-- DO NOT EDIT! Automatically generated file. -->\n' # Iterate through all the source files and process them. tag_updater = UpdateTags() for dirpath, _, filenames in os.walk(src_dir): # How to get from `dirpath` to api_docs/python/ relative_path_to_root = os.path.relpath( path=os.path.join(src_dir, 'api_docs/python'), start=dirpath) # Make the directory under output_dir. new_dir = os.path.join(output_dir, os.path.relpath(path=dirpath, start=src_dir)) try: if not os.path.exists(new_dir): os.makedirs(new_dir) except OSError as e: print('Creating output dir "%s" failed: %s' % (new_dir, e)) raise for base_name in filenames: full_in_path = os.path.join(dirpath, base_name) suffix = os.path.relpath(path=full_in_path, start=src_dir) full_out_path = os.path.join(output_dir, suffix) if not base_name.endswith('.md'): print('Copying non-md file %s...' % suffix) open(full_out_path, 'w').write(open(full_in_path).read()) continue if dirpath.endswith('/api_guides/python'): print('Processing Python guide %s...' % base_name) md_string = tag_updater.process(full_in_path) else: print('Processing doc %s...' % suffix) md_string = open(full_in_path).read() output = parser.replace_references( md_string, relative_path_to_root, visitor.duplicate_of, doc_index=doc_index, index=visitor.index) with open(full_out_path, 'w') as f: f.write(header + output) print('Done.')
def test_replace_references(self): class HasOneMember(object): def foo(self): pass string = ('A @{tf.reference}, another @{tf.reference}, ' 'a member @{tf.reference.foo}, and a @{tf.third}.') duplicate_of = {'tf.third': 'tf.fourth'} index = {'tf.reference': HasOneMember, 'tf.reference.foo': HasOneMember.foo, 'tf.third': HasOneMember, 'tf.fourth': HasOneMember} result = parser.replace_references( string, '../..', duplicate_of, doc_index={}, index=index) self.assertEqual( 'A [`tf.reference`](../../tf/reference.md), another ' '[`tf.reference`](../../tf/reference.md), ' 'a member [`tf.reference.foo`](../../tf/reference.md#foo), ' 'and a [`tf.third`](../../tf/fourth.md).', result)
def test_replace_references(self): class HasOneMember(object): def foo(self): pass string = ('A @{tf.reference}, another @{tf.reference}, ' 'a member @{tf.reference.foo}, and a @{tf.third}.') duplicate_of = {'tf.third': 'tf.fourth'} index = {'tf.reference': HasOneMember, 'tf.reference.foo': HasOneMember.foo, 'tf.third': HasOneMember, 'tf.fourth': HasOneMember} result = parser.replace_references( string, '../..', duplicate_of, doc_index={}, index=index) self.assertEqual( 'A [`tf.reference`](../../tf/reference.md), another ' '[`tf.reference`](../../tf/reference.md), ' 'a member [`tf.reference.foo`](../../tf/reference.md#foo), ' 'and a [`tf.third`](../../tf/fourth.md).', result)
def test_references_replaced_in_generated_markdown(self): index = { 'test_function_for_markdown_reference': test_function_for_markdown_reference } tree = { '': ['test_function_for_markdown_reference'] } docs = parser.generate_markdown( full_name='test_function_for_markdown_reference', py_object=test_function_for_markdown_reference, duplicate_of={}, duplicates={}, index=index, tree=tree, reverse_index={}, doc_index={}, guide_index={}, base_dir='/') # Make sure docstring shows up and is properly processed. expected_docs = parser.replace_references( inspect.getdoc(test_function_for_markdown_reference), relative_path_to_root='.', duplicate_of={}, doc_index={}, index={}) self.assertTrue(expected_docs in docs)
def _main(input_dir, output_dir): """Convert all the files in `input_dir` and write results to `output_dir`.""" visitor = generate.extract() # Make output_dir. try: if not os.path.exists(output_dir): os.makedirs(output_dir) except OSError as e: print('Creating output dir "%s" failed: %s' % (output_dir, e)) raise # How to get from api_guides/python/ to api_docs/python/ relative_path_to_root = '../../api_docs/python/' # Iterate through all the source files and process them. for full_path, base_name in _md_files_in_dir(input_dir): print('Processing %s...' % base_name) md_string = open(full_path).read() output = parser.replace_references(md_string, relative_path_to_root, visitor.duplicate_of) open(os.path.join(output_dir, base_name), 'w').write(output) print('Done.')
def _main(input_dir, output_dir): """Convert all the files in `input_dir` and write results to `output_dir`.""" visitor = generate.extract() # Make output_dir. try: if not os.path.exists(output_dir): os.makedirs(output_dir) except OSError as e: print('Creating output dir "%s" failed: %s' % (output_dir, e)) raise # How to get from api_guides/python/ to api_docs/python/ relative_path_to_root = '../../api_docs/python/' # Iterate through all the source files and process them. for full_path, base_name in _md_files_in_dir(input_dir): print('Processing %s...' % base_name) md_string = open(full_path).read() output = parser.replace_references( md_string, relative_path_to_root, visitor.duplicate_of) open(os.path.join(output_dir, base_name), 'w').write(output) print('Done.')