Esempio n. 1
0
 def test_copy_file_dirty_not_modified(self, src_dir, dest_dir):
     file = File('test.txt', src_dir, dest_dir, use_directory_urls=False)
     file.is_modified = mock.Mock(return_value=False)
     dest_path = os.path.join(dest_dir, 'test.txt')
     file.copy_file(dirty=True)
     self.assertPathIsFile(dest_path)
     with io.open(dest_path, 'r', encoding='utf-8') as f:
         self.assertEqual(f.read(), 'destination content')
Esempio n. 2
0
 def test_md_file_use_directory_urls(self):
     f = File('foo.md',
              '/path/to/docs',
              '/path/to/site',
              use_directory_urls=True)
     self.assertPathsEqual(f.src_path, 'foo.md')
     self.assertPathsEqual(f.abs_src_path, '/path/to/docs/foo.md')
     self.assertPathsEqual(f.dest_path, 'foo/index.html')
     self.assertPathsEqual(f.abs_dest_path, '/path/to/site/foo/index.html')
     self.assertEqual(f.url, 'foo/')
     self.assertEqual(f.name, 'foo')
     self.assertTrue(f.is_documentation_page())
     self.assertFalse(f.is_static_page())
     self.assertFalse(f.is_media_file())
     self.assertFalse(f.is_javascript())
     self.assertFalse(f.is_css())
Esempio n. 3
0
 def test_css_file(self):
     f = File('foo/bar.css',
              '/path/to/docs',
              '/path/to/site',
              use_directory_urls=False)
     self.assertPathsEqual(f.src_path, 'foo/bar.css')
     self.assertPathsEqual(f.abs_src_path, '/path/to/docs/foo/bar.css')
     self.assertPathsEqual(f.dest_path, 'foo/bar.css')
     self.assertPathsEqual(f.abs_dest_path, '/path/to/site/foo/bar.css')
     self.assertEqual(f.url, 'foo/bar.css')
     self.assertEqual(f.name, 'bar')
     self.assertFalse(f.is_documentation_page())
     self.assertFalse(f.is_static_page())
     self.assertTrue(f.is_media_file())
     self.assertFalse(f.is_javascript())
     self.assertTrue(f.is_css())
Esempio n. 4
0
 def test_page(title, filename, config):
     test_page = Page(
         title,
         File(filename, config['docs_dir'], config['site_dir'],
              config['use_directory_urls']), config)
     test_page.content = """
         <h1 id="heading-1">Heading 1</h1>
         <p>Content 1</p>
         <h2 id="heading-2">Heading 2</h1>
         <p>Content 2</p>
         <h3 id="heading-3">Heading 3</h1>
         <p>Content 3</p>"""
     test_page.markdown = dedent("""
         # Heading 1
         ## Heading 2
         ### Heading 3""")
     test_page.toc = get_toc(get_markdown_toc(test_page.markdown))
     return test_page
Esempio n. 5
0
    def on_files(self, files, config, **kwargs):
        """Generate DAG as png and add it to mkdocs files."""
        path_to_snakemake = Path.cwd() / self.config["path_to_snakefile"]
        path_to_png = Path.cwd() / self.config[
            "path_to_src_dir"] / self.config["path_to_png_relative_to_site"]
        path_to_png.parent.mkdir(parents=True, exist_ok=True)
        graph_string = io.StringIO()
        with redirect_stdout(graph_string):
            snakemake.snakemake(path_to_snakemake, printrulegraph=True)
        graph = pydot.graph_from_dot_data(graph_string.getvalue())[0]
        graph.write_png(path_to_png)

        dag_file = File(path=self.config["path_to_png_relative_to_site"],
                        src_dir=self.config["path_to_src_dir"],
                        dest_dir=config["site_dir"],
                        use_directory_urls=config["use_directory_urls"])
        files.append(dag_file)
        return files
Esempio n. 6
0
 def test_nav_no_directory_urls(self):
     nav_cfg = [{'Home': 'index.md'}, {'About': 'about.md'}]
     expected = dedent("""
     Page(title='Home', url='/index.html')
     Page(title='About', url='/about.html')
     """)
     cfg = load_config(nav=nav_cfg,
                       use_directory_urls=False,
                       site_url='http://example.com/')
     files = Files([
         File(
             list(item.values())[0], cfg['docs_dir'], cfg['site_dir'],
             cfg['use_directory_urls']) for item in nav_cfg
     ])
     site_navigation = get_navigation(files, cfg)
     self.assertEqual(str(site_navigation).strip(), expected)
     self.assertEqual(len(site_navigation.items), 2)
     self.assertEqual(len(site_navigation.pages), 2)
Esempio n. 7
0
 def test_css_file(self):
     f = File('foo/bar.css', '/path/to/docs', '/path/to/site', use_directory_urls=False)
     self.assertPathsEqual(f.src_path, 'foo/bar.css')
     self.assertPathsEqual(f.abs_src_path, '/path/to/docs/foo/bar.css')
     self.assertPathsEqual(f.dest_path, 'foo/bar.css')
     self.assertPathsEqual(f.abs_dest_path, '/path/to/site/foo/bar.css')
     self.assertEqual(f.url, 'foo/bar.css')
     self.assertEqual(f.name, 'bar')
     self.assertFalse(f.is_documentation_page())
     self.assertFalse(f.is_static_page())
     self.assertTrue(f.is_media_file())
     self.assertFalse(f.is_javascript())
     self.assertTrue(f.is_css())
Esempio n. 8
0
 def test_md_file_nested_use_directory_urls(self):
     f = File('foo/bar.md', '/path/to/docs', '/path/to/site', use_directory_urls=True)
     self.assertPathsEqual(f.src_path, 'foo/bar.md')
     self.assertPathsEqual(f.abs_src_path, '/path/to/docs/foo/bar.md')
     self.assertPathsEqual(f.dest_path, 'foo/bar/index.html')
     self.assertPathsEqual(f.abs_dest_path, '/path/to/site/foo/bar/index.html')
     self.assertEqual(f.url, 'foo/bar/')
     self.assertEqual(f.name, 'bar')
     self.assertTrue(f.is_documentation_page())
     self.assertFalse(f.is_static_page())
     self.assertFalse(f.is_media_file())
     self.assertFalse(f.is_javascript())
     self.assertFalse(f.is_css())
Esempio n. 9
0
 def test_md_index_file(self):
     f = File('index.md', '/path/to/docs', '/path/to/site', use_directory_urls=False)
     self.assertPathsEqual(f.src_path, 'index.md')
     self.assertPathsEqual(f.abs_src_path, '/path/to/docs/index.md')
     self.assertPathsEqual(f.dest_path, 'index.html')
     self.assertPathsEqual(f.abs_dest_path, '/path/to/site/index.html')
     self.assertEqual(f.url, 'index.html')
     self.assertEqual(f.name, 'index')
     self.assertTrue(f.is_documentation_page())
     self.assertFalse(f.is_static_page())
     self.assertFalse(f.is_media_file())
     self.assertFalse(f.is_javascript())
     self.assertFalse(f.is_css())
Esempio n. 10
0
 def test_build_page_error(self, site_dir, mock_write_file):
     cfg = load_config(site_dir=site_dir, nav=['test.md'], plugins=[])
     files = Files([
         File('test.md', cfg['docs_dir'], cfg['site_dir'],
              cfg['use_directory_urls'])
     ])
     nav = get_navigation(files, cfg)
     page = files.documentation_pages()[0].page
     # Fake populate page
     page.title = 'Title'
     page.markdown = 'page content'
     page.content = '<p>page content</p>'
     with self.assertLogs('mkdocs', level='ERROR') as cm:
         self.assertRaises(IOError, build._build_page, page, cfg, files,
                           nav, cfg['theme'].get_env())
     self.assertEqual(cm.output, [
         "ERROR:mkdocs.commands.build:Error building page 'test.md': Error message."
     ])
     mock_write_file.assert_called_once()
Esempio n. 11
0
    def test_nested_ungrouped_nav_no_titles(self):
        nav_cfg = ['index.md', 'about/contact.md', 'about/sub/license.md']
        expected = dedent("""
        Page(title=[blank], url='/')
        Page(title=[blank], url='/about/contact/')
        Page(title=[blank], url='/about/sub/license/')
        """)

        cfg = load_config(nav=nav_cfg, site_url='http://example.com/')
        files = Files([
            File(item, cfg['docs_dir'], cfg['site_dir'],
                 cfg['use_directory_urls']) for item in nav_cfg
        ])
        site_navigation = get_navigation(files, cfg)
        self.assertEqual(str(site_navigation).strip(), expected)
        self.assertEqual(len(site_navigation.items), 3)
        self.assertEqual(len(site_navigation.pages), 3)
        self.assertEqual(repr(site_navigation.homepage),
                         "Page(title=[blank], url='/')")
    def on_page_markdown(self, markdown, page, config, files):

        match = TOKEN.search(markdown)

        if match is None:
            return markdown

        pre_token = markdown[:match.start()]
        post_token = markdown[match.end():]

        def _error(message):
            return (pre_token +
                    escape(ERROR_TEMPLATE.substitute(error=message)) +
                    post_token)

        path = match.group("path")

        if path is None:
            return _error(USAGE_MSG)

        try:
            api_file = Path(page.file.abs_src_path).with_name(path)
        except ValueError as exc:
            return _error(f"Invalid path. {exc.args[0]}")

        if not api_file.exists():
            return _error(f"File {path} not found.")

        src_dir = api_file.parent
        dest_dir = Path(page.file.abs_dest_path).parent

        new_file = File(api_file.name, src_dir, dest_dir, False)
        files.append(new_file)
        url = Path(new_file.abs_dest_path).name

        lib = swagger_lib(config)

        markdown = pre_token + TEMPLATE.substitute(
            path=url, swagger_lib_js=lib['js'],
            swagger_lib_css=lib['css']) + post_token

        # If multiple swaggers exist.
        return self.on_page_markdown(markdown, page, config, files)
Esempio n. 13
0
 def test_page_render(self):
     cfg = load_config()
     fl = File('testing.md', cfg['docs_dir'], cfg['site_dir'],
               cfg['use_directory_urls'])
     pg = Page('Foo', fl, cfg)
     pg.read_source(cfg)
     self.assertEqual(pg.content, None)
     self.assertEqual(pg.toc, [])
     pg.render(cfg, [fl])
     self.assertTrue(
         pg.content.startswith(
             '<h1 id="welcome-to-mkdocs">Welcome to MkDocs</h1>\n'))
     self.assertEqual(
         str(pg.toc).strip(),
         dedent("""
         Welcome to MkDocs - #welcome-to-mkdocs
             Commands - #commands
             Project layout - #project-layout
     """))
Esempio n. 14
0
 def test_build_page_dirty_not_modified(self, site_dir, mock_write_file):
     cfg = load_config(site_dir=site_dir, nav=['test.md'], plugins=[])
     files = Files([
         File('testing.md', cfg['docs_dir'], cfg['site_dir'],
              cfg['use_directory_urls'])
     ])
     nav = get_navigation(files, cfg)
     page = files.documentation_pages()[0].page
     # Fake populate page
     page.title = 'Title'
     page.markdown = 'page content'
     page.content = '<p>page content</p>'
     build._build_page(page,
                       cfg,
                       files,
                       nav,
                       cfg['theme'].get_env(),
                       dirty=True)
     mock_write_file.assert_called_once()
Esempio n. 15
0
 def test_nested_ungrouped_nav(self):
     nav_cfg = [
         {'Home': 'test.md'},
         {'Contact': 'about/contact.md'},
         {'License Title': 'about/sub/license.md'},
     ]
     expected = dedent("""
     Page(title='Home', url='/')
     Page(title='Contact', url='/about/contact/')
     Page(title='License Title', url='/about/sub/license/')
     """)
     cfg = load_config(nav=nav_cfg, site_url='http://example.com/')
     files = Files(
         [File(list(item.values())[0], cfg['docs_dir'], cfg['site_dir'], cfg['use_directory_urls'])
          for item in nav_cfg]
     )
     site_navigation = get_navigation(files, cfg)
     self.assertEqual(str(site_navigation).strip(), expected)
     self.assertEqual(len(site_navigation.items), 3)
     self.assertEqual(len(site_navigation.pages), 3)
Esempio n. 16
0
 def test_files_append_remove_src_paths(self):
     fs = [
         File('index.md',
              '/path/to/docs',
              '/path/to/site',
              use_directory_urls=True),
         File('foo/bar.md',
              '/path/to/docs',
              '/path/to/site',
              use_directory_urls=True),
         File('foo/bar.html',
              '/path/to/docs',
              '/path/to/site',
              use_directory_urls=True),
         File('foo/bar.jpg',
              '/path/to/docs',
              '/path/to/site',
              use_directory_urls=True),
         File('foo/bar.js',
              '/path/to/docs',
              '/path/to/site',
              use_directory_urls=True),
         File('foo/bar.css',
              '/path/to/docs',
              '/path/to/site',
              use_directory_urls=True)
     ]
     files = Files(fs)
     self.assertEqual(len(files), 6)
     self.assertEqual(len(files.src_paths), 6)
     extra_file = File('extra.md',
                       '/path/to/docs',
                       '/path/to/site',
                       use_directory_urls=True)
     self.assertFalse(extra_file.src_path in files)
     files.append(extra_file)
     self.assertEqual(len(files), 7)
     self.assertEqual(len(files.src_paths), 7)
     self.assertTrue(extra_file.src_path in files)
     files.remove(extra_file)
     self.assertEqual(len(files), 6)
     self.assertEqual(len(files.src_paths), 6)
     self.assertFalse(extra_file.src_path in files)
Esempio n. 17
0
    def on_files(self, files, config):
        # Scan the list of files to extract tags from meta
        for f in files:
            if not f.src_path.endswith(".md"):
                continue

            # One-time extract of all the YAML metadata from the file
            self.metadata.append(
                get_metadata(f.src_path, config["docs_dir"],
                             self.tags_encoding))

        # Create new file with tags
        for tagname in self.tags_names:
            newfilename = self.generate_tags_file(tagname)

            # New file to add to the build
            newfile = File(path=str(newfilename),
                           src_dir=str(self.tags_folder),
                           dest_dir=config["site_dir"],
                           use_directory_urls=False)
            files.append(newfile)
    def on_files(self, files, config):
        # Scan the list of files to extract tags from meta
        for f in files:
            if not f.src_path.endswith(".md"):
                continue
            self.metadata.append(get_metadata(f.src_path, config["docs_dir"]))

        self.update_tags_dict(config)

        # Create new file with tags
        if self.tags_create_target:
            self.generate_tags_file()

            # New file to add to the build
            if self.tags_add_target:
                newfile = File(path=str(self.tags_filename),
                               src_dir=str(self.tags_folder),
                               dest_dir=config["site_dir"] /
                               Path(self.tags_target_folder),
                               use_directory_urls=False)
                files.append(newfile)
Esempio n. 19
0
 def test_page_canonical_url_nested_no_slash(self):
     cfg = load_config(site_url='http://example.com/foo')
     fl = File('testing.md', cfg['docs_dir'], cfg['site_dir'], cfg['use_directory_urls'])
     pg = Page('Foo', fl, cfg)
     self.assertEqual(pg.url, 'testing/')
     self.assertEqual(pg.abs_url, '/foo/testing/')
     self.assertEqual(pg.canonical_url, 'http://example.com/foo/testing/')
     self.assertEqual(pg.edit_url, None)
     self.assertEqual(pg.file, fl)
     self.assertEqual(pg.content, None)
     self.assertFalse(pg.is_homepage)
     self.assertFalse(pg.is_index)
     self.assertTrue(pg.is_page)
     self.assertFalse(pg.is_section)
     self.assertTrue(pg.is_top_level)
     self.assertEqual(pg.markdown, None)
     self.assertEqual(pg.meta, {})
     self.assertEqual(pg.next_page, None)
     self.assertEqual(pg.parent, None)
     self.assertEqual(pg.previous_page, None)
     self.assertEqual(pg.title, 'Foo')
     self.assertEqual(pg.toc, [])
Esempio n. 20
0
 def test_page_no_directory_url(self):
     cfg = load_config(use_directory_urls=False)
     fl = File('testing.md', cfg['docs_dir'], cfg['site_dir'], cfg['use_directory_urls'])
     pg = Page('Foo', fl, cfg)
     self.assertEqual(pg.url, 'testing.html')
     self.assertEqual(pg.abs_url, None)
     self.assertEqual(pg.canonical_url, None)
     self.assertEqual(pg.edit_url, None)
     self.assertEqual(pg.file, fl)
     self.assertEqual(pg.content, None)
     self.assertFalse(pg.is_homepage)
     self.assertFalse(pg.is_index)
     self.assertTrue(pg.is_page)
     self.assertFalse(pg.is_section)
     self.assertTrue(pg.is_top_level)
     self.assertEqual(pg.markdown, None)
     self.assertEqual(pg.meta, {})
     self.assertEqual(pg.next_page, None)
     self.assertEqual(pg.parent, None)
     self.assertEqual(pg.previous_page, None)
     self.assertEqual(pg.title, 'Foo')
     self.assertEqual(pg.toc, [])
Esempio n. 21
0
 def test_nav_external_links(self):
     nav_cfg = [{
         'Home': 'index.md'
     }, {
         'Local': '/local.html'
     }, {
         'External': 'http://example.com/external.html'
     }]
     expected = dedent("""
     Page(title='Home', url='/')
     Link(title='Local', url='/local.html')
     Link(title='External', url='http://example.com/external.html')
     """)
     cfg = load_config(nav=nav_cfg, site_url='http://example.com/')
     files = Files([
         File('index.md', cfg['docs_dir'], cfg['site_dir'],
              cfg['use_directory_urls'])
     ])
     site_navigation = get_navigation(files, cfg)
     self.assertEqual(str(site_navigation).strip(), expected)
     self.assertEqual(len(site_navigation.items), 3)
     self.assertEqual(len(site_navigation.pages), 1)
Esempio n. 22
0
    def on_files(self, files, config, **kwargs):
        """Generate overview over configuration schema and add to mkdoc's files."""
        path_to_schema = Path.cwd() / self.config["path_to_schema"]
        path_to_md = Path.cwd() / self.config["path_to_src_dir"] / self.config[
            "path_to_md_relative_to_site"]
        path_to_md.parent.mkdir(parents=True, exist_ok=True)

        parser = jsonschema2md.Parser()
        parser.tab_size = 4
        with path_to_schema.open("r") as f_schema:
            schema = yaml.safe_load(f_schema)
        with path_to_md.open("w") as f_md:
            lines = parser.parse_schema(schema)
            lines = SchemaPlugin.customise_markdown(lines)
            f_md.writelines(lines)

        schema_md_file = File(path=self.config["path_to_md_relative_to_site"],
                              src_dir=self.config["path_to_src_dir"],
                              dest_dir=config["site_dir"],
                              use_directory_urls=config["use_directory_urls"])
        files.append(schema_md_file)
        return files
Esempio n. 23
0
 def test_nested_index_page_no_parent_no_directory_urls(self):
     cfg = load_config(docs_dir=self.DOCS_DIR, use_directory_urls=False)
     fl = File('sub1/index.md', cfg['docs_dir'], cfg['site_dir'], cfg['use_directory_urls'])
     pg = Page('Foo', fl, cfg)
     pg.parent = None  # non-homepage at nav root level; see #1919.
     self.assertEqual(pg.url, 'sub1/index.html')
     self.assertEqual(pg.abs_url, None)
     self.assertEqual(pg.canonical_url, None)
     self.assertEqual(pg.edit_url, None)
     self.assertEqual(pg.file, fl)
     self.assertEqual(pg.content, None)
     self.assertFalse(pg.is_homepage)
     self.assertTrue(pg.is_index)
     self.assertTrue(pg.is_page)
     self.assertFalse(pg.is_section)
     self.assertTrue(pg.is_top_level)
     self.assertEqual(pg.markdown, None)
     self.assertEqual(pg.meta, {})
     self.assertEqual(pg.next_page, None)
     self.assertEqual(pg.parent, None)
     self.assertEqual(pg.previous_page, None)
     self.assertEqual(pg.title, 'Foo')
     self.assertEqual(pg.toc, [])
Esempio n. 24
0
 def test_page_title_from_homepage_filename(self):
     cfg = load_config(docs_dir=self.DOCS_DIR)
     fl = File('index.md', cfg['docs_dir'], cfg['site_dir'], cfg['use_directory_urls'])
     pg = Page(None, fl, cfg)
     pg.read_source(cfg)
     self.assertEqual(pg.url, '')
     self.assertEqual(pg.abs_url, None)
     self.assertEqual(pg.canonical_url, None)
     self.assertEqual(pg.edit_url, None)
     self.assertEqual(pg.file, fl)
     self.assertEqual(pg.content, None)
     self.assertTrue(pg.is_homepage)
     self.assertTrue(pg.is_index)
     self.assertTrue(pg.is_page)
     self.assertFalse(pg.is_section)
     self.assertTrue(pg.is_top_level)
     self.assertTrue(pg.markdown.startswith('## Test'))
     self.assertEqual(pg.meta, {})
     self.assertEqual(pg.next_page, None)
     self.assertEqual(pg.parent, None)
     self.assertEqual(pg.previous_page, None)
     self.assertEqual(pg.title, 'Home')
     self.assertEqual(pg.toc, [])
 def test_build_page_dirty_modified(self, site_dir, docs_dir,
                                    mock_write_file):
     cfg = load_config(docs_dir=docs_dir,
                       site_dir=site_dir,
                       nav=['index.md'],
                       plugins=[])
     files = Files([
         File('index.md', cfg['docs_dir'], cfg['site_dir'],
              cfg['use_directory_urls'])
     ])
     nav = get_navigation(files, cfg)
     page = files.documentation_pages()[0].page
     # Fake populate page
     page.title = 'Title'
     page.markdown = 'new page content'
     page.content = '<p>new page content</p>'
     build._build_page(page,
                       cfg,
                       files,
                       nav,
                       self._get_env_with_null_translations(cfg),
                       dirty=True)
     mock_write_file.assert_not_called()
Esempio n. 26
0
 def test_page_title_from_markdown(self):
     cfg = load_config()
     fl = File('testing.md', cfg['docs_dir'], cfg['site_dir'], cfg['use_directory_urls'])
     pg = Page(None, fl, cfg)
     pg.read_source(cfg)
     self.assertEqual(pg.url, 'testing/')
     self.assertEqual(pg.abs_url, None)
     self.assertEqual(pg.canonical_url, None)
     self.assertEqual(pg.edit_url, None)
     self.assertEqual(pg.file, fl)
     self.assertEqual(pg.content, None)
     self.assertFalse(pg.is_homepage)
     self.assertFalse(pg.is_index)
     self.assertTrue(pg.is_page)
     self.assertFalse(pg.is_section)
     self.assertTrue(pg.is_top_level)
     self.assertTrue(pg.markdown.startswith('# Welcome to MkDocs\n'))
     self.assertEqual(pg.meta, {})
     self.assertEqual(pg.next_page, None)
     self.assertEqual(pg.parent, None)
     self.assertEqual(pg.previous_page, None)
     self.assertEqual(pg.title, 'Welcome to MkDocs')
     self.assertEqual(pg.toc, [])
Esempio n. 27
0
def test_pages_sort() -> None:
    """Test i18pages sort correctness."""
    plugin = Translator()
    test_lang = "test"
    index_page = File("/tmp/theme/index.md", "/tmp", "/tmp", use_directory_urls=False)
    index_page.url = "lang/theme/"

    second = File("/tmp/theme/b.md", "/tmp", "/tmp", use_directory_urls=False)
    second.url = "lang/theme/b/"
    plugin.i18pages[test_lang].append(
        second,
    )
    plugin.i18pages[test_lang].append(
        index_page,
    )
    plugin._sort_translated_files()
    doc_pages = plugin.i18pages[test_lang].documentation_pages()
    assert doc_pages[0] == index_page
Esempio n. 28
0
    def _get_file(self, name: str, new: bool = False) -> str:
        new_f = File(
            name,
            src_dir=self.directory,
            dest_dir=self.config["site_dir"],
            use_directory_urls=self.config["use_directory_urls"],
        )
        normname = _normpath(name)

        if new or normname not in self._files:
            os.makedirs(os.path.dirname(new_f.abs_src_path), exist_ok=True)
            self._files[normname] = new_f
            self.edit_paths.setdefault(normname, None)
            return new_f.abs_src_path

        f = self._files[normname]
        if f.abs_src_path != new_f.abs_src_path:
            os.makedirs(os.path.dirname(new_f.abs_src_path), exist_ok=True)
            self._files[normname] = new_f
            self.edit_paths.setdefault(normname, None)
            shutil.copyfile(f.abs_src_path, new_f.abs_src_path)
            return new_f.abs_src_path

        return f.abs_src_path
Esempio n. 29
0
 def test_homepage(self):
     cfg = load_config(docs_dir=self.DOCS_DIR)
     fl = File('index.md', cfg['docs_dir'], cfg['site_dir'], cfg['use_directory_urls'])
     self.assertIsNone(fl.page)
     pg = Page('Foo', fl, cfg)
     self.assertEqual(fl.page, pg)
     self.assertEqual(pg.url, '')
     self.assertEqual(pg.abs_url, None)
     self.assertEqual(pg.canonical_url, None)
     self.assertEqual(pg.edit_url, None)
     self.assertEqual(pg.file, fl)
     self.assertEqual(pg.content, None)
     self.assertTrue(pg.is_homepage)
     self.assertTrue(pg.is_index)
     self.assertTrue(pg.is_page)
     self.assertFalse(pg.is_section)
     self.assertTrue(pg.is_top_level)
     self.assertEqual(pg.markdown, None)
     self.assertEqual(pg.meta, {})
     self.assertEqual(pg.next_page, None)
     self.assertEqual(pg.parent, None)
     self.assertEqual(pg.previous_page, None)
     self.assertEqual(pg.title, 'Foo')
     self.assertEqual(pg.toc, [])
Esempio n. 30
0
 def test_page_defaults(self):
     cfg = load_config()
     fl = File('testing.md', cfg['docs_dir'], cfg['site_dir'],
               cfg['use_directory_urls'])
     pg = Page('Foo', fl, cfg)
     self.assertRegex(pg.update_date, r'\d{4}-\d{2}-\d{2}')
     self.assertEqual(pg.url, 'testing/')
     self.assertEqual(pg.abs_url, '/testing/')
     self.assertEqual(pg.canonical_url, 'https://example.com/testing/')
     self.assertEqual(pg.edit_url, None)
     self.assertEqual(pg.file, fl)
     self.assertEqual(pg.content, None)
     self.assertFalse(pg.is_homepage)
     self.assertFalse(pg.is_index)
     self.assertTrue(pg.is_page)
     self.assertFalse(pg.is_section)
     self.assertTrue(pg.is_top_level)
     self.assertEqual(pg.markdown, None)
     self.assertEqual(pg.meta, {})
     self.assertEqual(pg.next_page, None)
     self.assertEqual(pg.parent, None)
     self.assertEqual(pg.previous_page, None)
     self.assertEqual(pg.title, 'Foo')
     self.assertEqual(pg.toc, [])
Esempio n. 31
0
 def test_page_title_from_meta(self):
     cfg = load_config(docs_dir=self.DOCS_DIR)
     fl = File('metadata.md', cfg['docs_dir'], cfg['site_dir'],
               cfg['use_directory_urls'])
     pg = Page(None, fl, cfg)
     pg.read_source(cfg)
     self.assertEqual(pg.url, 'metadata/')
     self.assertEqual(pg.abs_url, '/metadata/')
     self.assertEqual(pg.canonical_url, 'https://example.com/metadata/')
     self.assertEqual(pg.edit_url, None)
     self.assertEqual(pg.file, fl)
     self.assertEqual(pg.content, None)
     self.assertFalse(pg.is_homepage)
     self.assertFalse(pg.is_index)
     self.assertTrue(pg.is_page)
     self.assertFalse(pg.is_section)
     self.assertTrue(pg.is_top_level)
     self.assertTrue(pg.markdown.startswith('# Welcome to MkDocs\n'))
     self.assertEqual(pg.meta, {'title': 'A Page Title'})
     self.assertEqual(pg.next_page, None)
     self.assertEqual(pg.parent, None)
     self.assertEqual(pg.previous_page, None)
     self.assertEqual(pg.title, 'A Page Title')
     self.assertEqual(pg.toc, [])
Esempio n. 32
0
 def test_nested_index_page(self):
     cfg = load_config(docs_dir=self.DOCS_DIR)
     fl = File('sub1/index.md', cfg['docs_dir'], cfg['site_dir'],
               cfg['use_directory_urls'])
     pg = Page('Foo', fl, cfg)
     pg.parent = 'foo'
     self.assertEqual(pg.url, 'sub1/')
     self.assertEqual(pg.abs_url, '/sub1/')
     self.assertEqual(pg.canonical_url, 'https://example.com/sub1/')
     self.assertEqual(pg.edit_url, None)
     self.assertEqual(pg.file, fl)
     self.assertEqual(pg.content, None)
     self.assertFalse(pg.is_homepage)
     self.assertTrue(pg.is_index)
     self.assertTrue(pg.is_page)
     self.assertFalse(pg.is_section)
     self.assertFalse(pg.is_top_level)
     self.assertEqual(pg.markdown, None)
     self.assertEqual(pg.meta, {})
     self.assertEqual(pg.next_page, None)
     self.assertEqual(pg.parent, 'foo')
     self.assertEqual(pg.previous_page, None)
     self.assertEqual(pg.title, 'Foo')
     self.assertEqual(pg.toc, [])
Esempio n. 33
0
 def test_nav_from_nested_files(self):
     expected = dedent("""
     Page(title=[blank], url='/')
     Section(title='About')
         Page(title=[blank], url='/about/license/')
         Page(title=[blank], url='/about/release-notes/')
     Section(title='Api guide')
         Page(title=[blank], url='/api-guide/debugging/')
         Page(title=[blank], url='/api-guide/running/')
         Page(title=[blank], url='/api-guide/testing/')
         Section(title='Advanced')
             Page(title=[blank], url='/api-guide/advanced/part-1/')
     """)
     cfg = load_config(site_url='http://example.com/')
     files = Files([
         File('index.md', cfg['docs_dir'], cfg['site_dir'],
              cfg['use_directory_urls']),
         File('about/license.md', cfg['docs_dir'], cfg['site_dir'],
              cfg['use_directory_urls']),
         File('about/release-notes.md', cfg['docs_dir'], cfg['site_dir'],
              cfg['use_directory_urls']),
         File('api-guide/debugging.md', cfg['docs_dir'], cfg['site_dir'],
              cfg['use_directory_urls']),
         File('api-guide/running.md', cfg['docs_dir'], cfg['site_dir'],
              cfg['use_directory_urls']),
         File('api-guide/testing.md', cfg['docs_dir'], cfg['site_dir'],
              cfg['use_directory_urls']),
         File('api-guide/advanced/part-1.md', cfg['docs_dir'],
              cfg['site_dir'], cfg['use_directory_urls']),
     ])
     site_navigation = get_navigation(files, cfg)
     self.assertEqual(str(site_navigation).strip(), expected)
     self.assertEqual(len(site_navigation.items), 3)
     self.assertEqual(len(site_navigation.pages), 7)
     self.assertEqual(repr(site_navigation.homepage),
                      "Page(title=[blank], url='/')")
Esempio n. 34
0
 def test_copy_file(self, src_dir, dest_dir):
     file = File('test.txt', src_dir, dest_dir, use_directory_urls=False)
     dest_path = os.path.join(dest_dir, 'test.txt')
     self.assertPathNotExists(dest_path)
     file.copy_file()
     self.assertPathIsFile(dest_path)
Esempio n. 35
0
 def test_copy_file(self, src_dir, dest_dir):
     file = File('test.txt', src_dir, dest_dir, use_directory_urls=False)
     dest_path = os.path.join(dest_dir, 'test.txt')
     self.assertPathNotExists(dest_path)
     file.copy_file()
     self.assertPathIsFile(dest_path)
Esempio n. 36
0
    def test_get_relative_url(self):
        to_files = [
            'index.md',
            'foo/index.md',
            'foo/bar/index.md',
            'foo/bar/baz/index.md',
            'foo.md',
            'foo/bar.md',
            'foo/bar/baz.md'
        ]

        to_file_urls = [
            'index.html',
            'foo/index.html',
            'foo/bar/index.html',
            'foo/bar/baz/index.html',
            'foo.html',
            'foo/bar.html',
            'foo/bar/baz.html'
        ]

        from_file = File('img.jpg', '/path/to/docs', '/path/to/site', use_directory_urls=False)
        expected = [
            'img.jpg',           # img.jpg relative to .
            '../img.jpg',        # img.jpg relative to foo/
            '../../img.jpg',     # img.jpg relative to foo/bar/
            '../../../img.jpg',  # img.jpg relative to foo/bar/baz/
            'img.jpg',           # img.jpg relative to foo.html
            '../img.jpg',        # img.jpg relative to foo/bar.html
            '../../img.jpg'      # img.jpg relative to foo/bar/baz.html
        ]

        for i, filename in enumerate(to_files):
            file = File(filename, '/path/to/docs', '/path/to/site', use_directory_urls=False)
            self.assertEqual(from_file.url, 'img.jpg')
            self.assertEqual(file.url, to_file_urls[i])
            self.assertEqual(from_file.url_relative_to(file.url), expected[i])
            self.assertEqual(from_file.url_relative_to(file), expected[i])

        from_file = File('foo/img.jpg', '/path/to/docs', '/path/to/site', use_directory_urls=False)
        expected = [
            'foo/img.jpg',    # foo/img.jpg relative to .
            'img.jpg',        # foo/img.jpg relative to foo/
            '../img.jpg',     # foo/img.jpg relative to foo/bar/
            '../../img.jpg',  # foo/img.jpg relative to foo/bar/baz/
            'foo/img.jpg',    # foo/img.jpg relative to foo.html
            'img.jpg',        # foo/img.jpg relative to foo/bar.html
            '../img.jpg'      # foo/img.jpg relative to foo/bar/baz.html
        ]

        for i, filename in enumerate(to_files):
            file = File(filename, '/path/to/docs', '/path/to/site', use_directory_urls=False)
            self.assertEqual(from_file.url, 'foo/img.jpg')
            self.assertEqual(file.url, to_file_urls[i])
            self.assertEqual(from_file.url_relative_to(file.url), expected[i])
            self.assertEqual(from_file.url_relative_to(file), expected[i])

        from_file = File('index.html', '/path/to/docs', '/path/to/site', use_directory_urls=False)
        expected = [
            'index.html',           # index.html relative to .
            '../index.html',        # index.html relative to foo/
            '../../index.html',     # index.html relative to foo/bar/
            '../../../index.html',  # index.html relative to foo/bar/baz/
            'index.html',           # index.html relative to foo.html
            '../index.html',        # index.html relative to foo/bar.html
            '../../index.html'      # index.html relative to foo/bar/baz.html
        ]

        for i, filename in enumerate(to_files):
            file = File(filename, '/path/to/docs', '/path/to/site', use_directory_urls=False)
            self.assertEqual(from_file.url, 'index.html')
            self.assertEqual(file.url, to_file_urls[i])
            self.assertEqual(from_file.url_relative_to(file.url), expected[i])
            self.assertEqual(from_file.url_relative_to(file), expected[i])

        from_file = File('file.html', '/path/to/docs', '/path/to/site', use_directory_urls=False)
        expected = [
            'file.html',           # file.html relative to .
            '../file.html',        # file.html relative to foo/
            '../../file.html',     # file.html relative to foo/bar/
            '../../../file.html',  # file.html relative to foo/bar/baz/
            'file.html',           # file.html relative to foo.html
            '../file.html',        # file.html relative to foo/bar.html
            '../../file.html'      # file.html relative to foo/bar/baz.html
        ]

        for i, filename in enumerate(to_files):
            file = File(filename, '/path/to/docs', '/path/to/site', use_directory_urls=False)
            self.assertEqual(from_file.url, 'file.html')
            self.assertEqual(file.url, to_file_urls[i])
            self.assertEqual(from_file.url_relative_to(file.url), expected[i])
            self.assertEqual(from_file.url_relative_to(file), expected[i])