Пример #1
0
    def examine(self):
        """
        Investigate directories for new files and add them to the tree if found.

        TODO: Remove nodes if page is deleted.
        TODO: Handle !include (see extensions.include.py for more information).
        """
        for node in anytree.PreOrderIter(self._translator.root):

            # Only perform check on valid directories
            if not isinstance(node, page.DirectoryNode) or not os.path.exists(node.source):
                continue

            # Build map of child pages for the directory
            children = {child.name:child for child in node.children \
                        if isinstance(child, page.FileNode)}

            # Compare the list of files in the directory with those tracked by MooseDocs
            for filename in os.listdir(node.source):  #pylint: disable=no-member
                if filename.endswith(MooseDocs.FILE_EXT) and not filename.startswith('.'):
                    if filename not in children:
                        source = os.path.join(node.source, filename)
                        if filename.endswith('.md'):
                            new = page.MarkdownNode(node, source=source)
                        else:
                            new = page.FileNode(node, source=source) #pylint: disable=redefined-variable-type
                        new.base = self._options.destination
                        self.watch(new.source, new.build, delay=2) #pylint: disable=no-member
                        new.init(self._translator)
                        new.build()

        return super(MooseDocsWatcher, self).examine()
Пример #2
0
    def testFromFile(self, mock):
        filename = tempfile.mkstemp('.md')[-1]
        with open(filename, 'w') as fid:
            fid.write(self.content())

        node = page.MarkdownNode(None, source=filename)
        node.read()
        self.ast(node.content)

        self.assertEqual(mock.call_count, 2)

        calls = mock.call_args_list
        args, kwargs = calls[0]
        msg = '\n'.join(args)
        self.assertIn(
            "The following command combination is unknown: 'unkown command'",
            msg)

        args, kwargs = calls[1]
        msg = '\n'.join(args)
        self.assertIn(
            "The following command combination is unknown: 'this too'", msg)

        if os.path.exists(filename):
            os.remove(filename)
Пример #3
0
def create_file_node(parent, name, filename):
    """
    Create the correct node object for the given extension.
    """
    _, ext = os.path.splitext(filename)
    if ext == '.md':
        return page.MarkdownNode(parent, name=name, source=filename)
    else:
        return page.FileNode(parent, name=name, source=filename)
Пример #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
Пример #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