Ejemplo n.º 1
0
def doc_tree(items):
    """
    Create a tree of files for processing.

    Inputs:
        inputs: [list[dict(),...] A list of dict items, each dict entry must contain the 'root_dir'
                and 'content' fields that are passed to the doc_import function.
    """
    # Error checking
    if not isinstance(items, list) or any(not isinstance(x, dict)
                                          for x in items):
        LOG.error(
            'The supplied items must be a list of dict items, each with a "root_dir" and '
            'optionally a "content" entry.')
        return None

    # Define a dict for storing nodes by path
    nodes = dict()

    # Create the root node
    nodes[()] = page.DirectoryNode(source='')

    # Create the file tree
    for value in items:

        if 'root_dir' not in value:
            LOG.error(
                'The supplied items must be a list of dict items, each with a "root_dir" and '
                'optionally a "content" entry.')

        root = mooseutils.eval_path(value['root_dir'])
        if not os.path.isabs(root):
            root = os.path.join(MooseDocs.ROOT_DIR, root)

        # Update the project files
        MooseDocs.PROJECT_FILES.update(
            mooseutils.git_ls_files(mooseutils.git_root_dir(root)))

        files = doc_import(root, content=value.get('content', None))
        for filename in files:
            key = tuple(filename.replace(root, '').strip('/').split('/'))

            # Create directory nodes if they don't exist
            for i in range(1, len(key)):
                dir_key = key[:i]
                if dir_key not in nodes:
                    nodes[dir_key] = page.DirectoryNode(nodes[key[:i - 1]],
                                                        name=key[i - 1],
                                                        source=os.path.join(
                                                            root, *dir_key))

            # Create the file node, if it doesn't already exist. This enforces that the first
            # item in the supplied content lists is the page that is rendered.
            if key not in nodes:
                nodes[key] = create_file_node(nodes[key[:-1]], key[-1],
                                              filename)

    return nodes[()]
Ejemplo n.º 2
0
    def testErrors(self):
        MooseDocs.LOG_LEVEL = logging.DEBUG
        root = page.DirectoryNode(source='docs')
        page.FileNode(root, source='docs/index.md')
        page.FileNode(root, source='docs/core.md')
        sub = page.DirectoryNode(root, source='docs/sub')
        page.FileNode(sub, source='docs/sub/core.md')

        with self.assertRaises(exceptions.MooseDocsException) as e:
            root.findall('foo', maxcount=2.2)
        self.assertIn("The argument 'maxcount' must be", str(e.exception))

        with self.assertRaises(exceptions.MooseDocsException) as e:
            root.findall('foo', mincount=2.2)
        self.assertIn("The argument 'mincount' must be", str(e.exception))
Ejemplo n.º 3
0
    def testBasic(self):
        root = page.DirectoryNode(None, source='docs')
        page.FileNode(root, source='docs/index.md')
        page.FileNode(root, source='docs/core.md')
        sub = page.DirectoryNode(root, source='docs/sub')
        page.FileNode(sub, source='docs/sub/core.md')
        page.FileNode(sub, source='docs/sub/full_core.md')

        nodes = root.findall('core.md', maxcount=None)
        self.assertEqual(len(nodes), 3)

        nodes = root.findall('/core.md', maxcount=None)
        self.assertEqual(len(nodes), 2)

        nodes = root.findall('docs/core.md', maxcount=None)
        self.assertEqual(len(nodes), 1)
Ejemplo n.º 4
0
    def setUpContent(self):
        self.loc = tempfile.mkdtemp()
        self.files = [
            os.path.join(self.loc, 'file0.md'),
            os.path.join(self.loc, 'file1.md')
        ]

        with open(self.files[0], 'w') as fid:
            fid.write(
                '# Page 0\n\n[Foo](file1.md)\n\n[Bar](file1.md#sub)\n\n[file1.md]\n\n[file1.md#sub]'
            )
        with open(self.files[1], 'w') as fid:
            fid.write('# Page 1\n\nparagraph\n\n## Sub id=sub')

        self.root = page.DirectoryNode(None, source=self.loc)
        page.MarkdownNode(self.root,
                          base=os.path.dirname(self.loc),
                          source=self.files[0])
        page.MarkdownNode(self.root,
                          base=os.path.dirname(self.loc),
                          source=self.files[1])
        return self.root
Ejemplo n.º 5
0
    def setUpContent(self):
        self.loc = tempfile.mkdtemp()
        self.files = [
            os.path.join(self.loc, 'file0.md'),
            os.path.join(self.loc, 'file1.md'),
            os.path.join(self.loc, 'file2.md'),
            os.path.join(self.loc, 'file3.md')
        ]

        with open(self.files[0], 'w') as fid:
            fid.write('File 0\n\n!include {}'.format(
                os.path.basename(self.files[1])))
        with open(self.files[1], 'w') as fid:
            fid.write('File 1\n\n!include {}'.format(
                os.path.basename(self.files[2])))
        with open(self.files[2], 'w') as fid:
            fid.write('File 2')
        with open(self.files[3], 'w') as fid:
            fid.write('File 3\n\n!include {}'.format(
                os.path.basename(self.files[2])))

        self.root = page.DirectoryNode(None, source=self.loc)
        page.MarkdownNode(self.root,
                          base=os.path.dirname(self.loc),
                          source=self.files[0])
        page.MarkdownNode(self.root,
                          base=os.path.dirname(self.loc),
                          source=self.files[1])
        page.MarkdownNode(self.root,
                          base=os.path.dirname(self.loc),
                          source=self.files[2])
        page.MarkdownNode(self.root,
                          base=os.path.dirname(self.loc),
                          source=self.files[3])

        return self.root
Ejemplo n.º 6
0
 def testDirectoryNode(self):
     node = page.DirectoryNode(source='foo')
     self.assertEqual(node.source, 'foo')
     self.assertEqual(node.COLOR, 'CYAN')