def test_get_is_descendant(self): a = CWNode('a') root = CWRootNode([a]) tree = CWTree(root) self.assertTrue(tree.get_is_descendant(a, root)) self.assertFalse(tree.get_is_descendant(a, a)) self.assertFalse(tree.get_is_descendant(root, a))
def test_make_toc(self): tree = CWTree(CWRootNode([ CWDocumentNode('doc 1', [ CWTagNode('table-of-contents', {}, []), CWTagNode('h1', {}, [ CWTextNode('Header 1 text') ]), CWTagNode('h2', {}, [ CWTextNode('Subheader 1 text') ]), CWTagNode('h2', {}, [ CWTextNode('Subheader 2 text') ]), ]), CWDocumentNode('doc 2', [ CWTagNode('h1', {}, [ CWTextNode('Header 2 text') ]), ]), ])) tree.apply_library(self.library) self.assertEqual(tree.root.get_string_for_test_comparison(), self.strip(""" Root() Document(path='doc 1') div(kwargs={'class': 'table-of-contents-wrapper'}) h1(kwargs={'class': 'table-of-contents-title'}) 'Table of Contents' nav(kwargs={'class': 'table-of-contents'}) ol(kwargs={}) li(kwargs={}) Link(ref_id='Header-1-text') 'Header 1 text' ol(kwargs={}) li(kwargs={}) Link(ref_id='Subheader-1-text') 'Subheader 1 text' li(kwargs={}) Link(ref_id='Subheader-2-text') 'Subheader 2 text' li(kwargs={}) Link(ref_id='Header-2-text') 'Header 2 text' Anchor(ref_id='Header-1-text', kwargs={'class': 'header-anchor'}) h1(kwargs={}) 'Header 1 text' Anchor(ref_id='Subheader-1-text', kwargs={'class': 'header-anchor'}) h2(kwargs={}) 'Subheader 1 text' Anchor(ref_id='Subheader-2-text', kwargs={'class': 'header-anchor'}) h2(kwargs={}) 'Subheader 2 text' Document(path='doc 2') Anchor(ref_id='Header-2-text', kwargs={'class': 'header-anchor'}) h1(kwargs={}) 'Header 2 text' """))
def test_add_root_child_fails(self): tree = CWTree(CWRootNode([ CWDocumentNode('doc', [ CWNode('a'), CWNode('b'), CWNode('add_root_child') ]) ])) library = LibraryForTesting() @library.processor('add_root_child') def add_root_child(tree, node): tree.insert_subtree(tree.root, 0, CWNode('a_child')) with self.assertRaises(CWTreeConsistencyError): tree.apply_library(library)
def test_mark_dirty(self): tree = CWTree(CWRootNode([ CWDocumentNode('doc', [ CWNode('a'), CWNode('b'), CWNode('dirty_a') ]) ])) library = LibraryForTesting() tree.apply_library(library) self.assertTreeIsConsistent(tree.root) self.assertEqual(library.visit_history, [ 'a', 'b', 'dirty_a', 'Document', 'Root', 'a' ])
def test_preorder(self): tree = CWTree(CWRootNode([ CWDocumentNode('doc', [ CWNode('a'), CWNode('b', [ CWNode('x'), CWNode('y'), ]), CWNode('c'), ]) ])) self.assertEqual( [node.name for node in tree.preorder_traversal()], ['Root', 'Document', 'a', 'b', 'x', 'y', 'c'])
def test_postorder_2(self): header1 = CWTagNode('h1', {}, [ CWTextNode('Header 1 text') ]) header2 = CWTagNode('h1', {}, [ CWTextNode('Header 2 text') ]) tree = CWTree(CWRootNode([ CWDocumentNode('doc 1', [header1]), CWDocumentNode('doc 2', [header2]), ])) self.assertEqual( [node.name for node in tree.postorder_traversal_allowing_ancestor_mutations()], ['Text', 'h1', 'Document', 'Text', 'h1', 'Document', 'Root'])
def run(): p = argparse.ArgumentParser() p.add_argument('--conf', default="conf.json", type=argparse.FileType('r')) p.add_argument('--debug', default=False, action='store_true') p.add_argument('--writer', default='html', action='store') args = p.parse_args() config_json = json.load(args.conf) plugin_names = DEFAULT_CONFIG['plugins'] + config_json.get('plugins', []) plugins = list(_get_plugins(plugin_names)) more_defaults = [ {plugin.CONFIG_NAMESPACE: plugin.get_default_config()} for plugin in plugins if plugin.CONFIG_NAMESPACE is not None ] config = dict( DictCascade(*([DEFAULT_CONFIG] + more_defaults + [config_json]))) files_root = pathlib.Path(args.conf.name).parent.resolve() config['root_dir'] = files_root output_root = pathlib.Path(files_root) / pathlib.Path(config['output_dir']) if not output_root.exists(): output_root.mkdir() for plugin in plugins: plugin.postprocess_config(config) for plugin in plugins: plugin.add_processors(stdlib) doc_tree, document_nodes = read_doc_tree( files_root, config['file_hierarchy'], _get_cfm_reader(stdlib)) tree = CWTree(CWRootNode(document_nodes), { 'doc_tree': doc_tree, 'output_dir': output_root, 'config': config }) if args.debug: print(tree.root.get_string_for_test_comparison()) tree.apply_library(stdlib) writers = { plugin.WRITER_NAME: plugin for plugin in plugins if plugin.WRITER_NAME is not None } writers[args.writer].write(config, files_root, output_root, stdlib, tree)
def test_aliases(self): tree = CWTree(CWRootNode([ CWDocumentNode('doc 1', [ CWTagNode('strike', {}, []), CWTagNode('b', {}, []), CWTagNode('tt', {}, []), ]), ])) tree.apply_library(LibraryForTesting()) self.assertEqual(tree.root.get_string_for_test_comparison(), self.strip(""" Root() Document(path='doc 1') s(kwargs={}) strong(kwargs={}) code(kwargs={}) """))
def test_postorder(self): tree = CWTree(CWRootNode([ CWDocumentNode('doc', [ CWNode('a'), CWNode('b', [ CWNode('x'), CWNode('y'), ]), CWNode('c'), ]) ])) self.assertEqual( [node.name for node in tree.postorder_traversal()], ['a', 'x', 'y', 'b', 'c', 'Document', 'Root']) self.assertEqual( [node.name for node in tree.postorder_traversal_allowing_ancestor_mutations()], ['a', 'x', 'y', 'b', 'c', 'Document', 'Root'])
def test_replace_self_subtree(self): tree = CWTree(CWRootNode([ CWDocumentNode('doc', [ CWNode('replace_self_subtree', [ CWNode('a') ]) ]) ])) library = LibraryForTesting() tree.apply_library(library) self.assertEqual(library.visit_history, [ 'a', 'replace_self_subtree', 'replacement', 'Document', 'Root', 'replacement']) self.assertEqual(tree.root.get_string_for_test_comparison(), self.strip(""" Root() Document(path='doc') replacement() replacement() """))
def test_wrap_self(self): tree = CWTree(CWRootNode([ CWDocumentNode('doc', [ CWNode('a'), CWNode('b'), CWNode('wrap_self') ]) ])) library = LibraryForTesting() tree.apply_library(library) self.assertEqual(library.visit_history, [ 'a', 'b', 'wrap_self', 'wrapper', 'Document', 'Root']) self.assertEqual(tree.root.get_string_for_test_comparison(), self.strip(""" Root() Document(path='doc') a() b() wrapper() wrap_self() """))
def test_collect_entries(self): header1 = CWTagNode('h1', {}, [ CWTextNode('Header 1 text') ]) header2 = CWTagNode('h1', {}, [ CWTextNode('Header 2 text') ]) tree = CWTree(CWRootNode([ CWDocumentNode('doc 1', [header1]), CWDocumentNode('doc 2', [header2]), ])) tree.apply_library(self.library) self.assertEqual(header1.data, { 'toc_entry': TOCEntry( level=1, heading_node=header1, ref_id='Header-1-text') }) self.assertEqual(header2.data, { 'toc_entry': TOCEntry( level=1, heading_node=header2, ref_id='Header-2-text') })
def test_add_own_child(self): tree = CWTree(CWRootNode([ CWDocumentNode('doc', [ CWNode('a'), CWNode('b'), CWNode('add_own_child') ]) ])) library = LibraryForTesting() tree.apply_library(library) self.assertTreeIsConsistent(tree.root) self.assertEqual(library.visit_history, [ 'a', 'b', 'add_own_child', 'Document', 'Root', 'a_child']) self.assertEqual(tree.root.get_string_for_test_comparison(), self.strip(""" Root() Document(path='doc') a() b() add_own_child() a_child() """))