Exemplo n.º 1
0
    def on_files(self, files: Files, config: Config):
        """
        From `https://www.mkdocs.org/user-guide/plugins/#on_files`:

        The files event is called after the files collection is populated from
        the docs_dir. Use this event to add, remove, or alter files in the
        collection. Note that Page objects have not yet been associated with
        the file objects in the collection.
        Use Page Events to manipulate page specific data.

        Parameters:
        files: global files collection
        config: global configuration object

        Returns:
        global files collection
        """
        _posts_by_date = dict()
        self.config.update(
            dict(docs_dir=config['docs_dir'], site_dir=config['site_dir']))

        for self._blog_md_file in self.get_blog_md():
            _posts_by_date.update(self.build_blog_dest())

        for post in sorted(_posts_by_date):
            post = _posts_by_date[post]

            blogpost = File(path=post['path'],
                            src_dir=post['src_dir'],
                            dest_dir=post['dest_dir'],
                            use_directory_urls=config['use_directory_urls'])
            blogpost.abs_src_path = post['src_dir']
            files.append(blogpost)

        return files
Exemplo n.º 2
0
 def test_files(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([f for f in files], fs)
     self.assertEqual(len(files), 6)
     self.assertEqual(files.documentation_pages(), [fs[0], fs[1]])
     self.assertEqual(files.static_pages(), [fs[2]])
     self.assertEqual(files.media_files(), [fs[3], fs[4], fs[5]])
     self.assertEqual(files.javascript_files(), [fs[4]])
     self.assertEqual(files.css_files(), [fs[5]])
     self.assertEqual(files.get_file_from_path('foo/bar.jpg'), fs[3])
     self.assertEqual(files.get_file_from_path('foo/bar.jpg'), fs[3])
     self.assertEqual(files.get_file_from_path('missing.jpg'), None)
     self.assertTrue(fs[2].src_path in files)
     self.assertTrue(fs[2].src_path in files)
     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.assertTrue(extra_file.src_path in files)
     self.assertEqual(files.documentation_pages(), [fs[0], fs[1], extra_file])
Exemplo n.º 3
0
    def on_files(self, files: Files, config: Config) -> Files:  # noqa: WPS210
        """
        Files event handler.

        It's called when MkDocs discovers all files.
        Here we filter all default pages and pages for translation.

        :param files: discovered files.
        :param config: mkdocs global config.
        :return: files for default language.
        """
        default_language = self.config.get("default_language")
        all_languages = set([default_language] + list(self.config["languages"]))

        # Idk we we need to process main_files separate from
        # translations, but it doesn't work without it.
        main_files = Files([])

        # Aux files, such as js or css.
        for aux_file in files:
            if aux_file not in files.documentation_pages():
                main_files.append(aux_file)
                for language in all_languages:
                    self.i18pages[language].append(aux_file)

        for page in files.documentation_pages():
            page_lang = self._get_lang(page)
            if page_lang == default_language:
                main_files.append(page)
            self.i18pages[page_lang].append(
                self.translate_page(page, Path(config.get("site_dir"))),
            )
        self._sort_translated_files()

        return main_files
Exemplo n.º 4
0
 def on_files(self, files: Files, config: Config):
     json_dir = "api"
     (pathlib.Path(config['docs_dir']).resolve() / json_dir).mkdir(
         parents=True, exist_ok=True)
     (pathlib.Path(config['site_dir']).resolve() / json_dir).mkdir(
         parents=True, exist_ok=True)
     all_json_file = File(f"{json_dir}/codes.json", config["docs_dir"],
                          config["site_dir"], config["use_directory_urls"])
     with open(all_json_file.abs_src_path, "w") as file:
         json.dump(self.codes, file, indent=2)
     with open(all_json_file.abs_dest_path, "w") as file:
         json.dump(self.codes, file, indent=2)
     for category, codes in self.codes_by_category.items():
         dir = parameterize(f"{category}-responses")
         for name, emojicode in codes.items():
             md_file = File(f"{dir}/{name}.md", config["docs_dir"],
                            config["site_dir"],
                            config["use_directory_urls"])
             files.append(md_file)
             json_file = File(f"{json_dir}/{name}.json", config["docs_dir"],
                              config["site_dir"],
                              config["use_directory_urls"])
             with open(json_file.abs_src_path, "w") as file:
                 json.dump(emojicode, file, indent=2)
             files.append(json_file)
Exemplo n.º 5
0
 def test_files(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([f for f in files], fs)
     self.assertEqual(len(files), 6)
     self.assertEqual(files.documentation_pages(), [fs[0], fs[1]])
     self.assertEqual(files.static_pages(), [fs[2]])
     self.assertEqual(files.media_files(), [fs[3], fs[4], fs[5]])
     self.assertEqual(files.javascript_files(), [fs[4]])
     self.assertEqual(files.css_files(), [fs[5]])
     self.assertEqual(files.get_file_from_path('foo/bar.jpg'), fs[3])
     self.assertEqual(files.get_file_from_path('foo/bar.jpg'), fs[3])
     self.assertEqual(files.get_file_from_path('missing.jpg'), None)
     self.assertTrue(fs[2].src_path in files)
     self.assertTrue(fs[2].src_path in files)
     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.assertTrue(extra_file.src_path in files)
     self.assertEqual(files.documentation_pages(), [fs[0], fs[1], extra_file])
Exemplo n.º 6
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)
Exemplo n.º 7
0
    def on_files(self, files: Files, config: Config, **kwargs) -> Files:
        """
        Add the coverage page to the navigation.

        Hook for the [`on_files` event](https://www.mkdocs.org/user-guide/plugins/#on_files).
        This hook is used to add the coverage page to the navigation, using a temporary file.

        Arguments:
            files: The files collection.
            config: The MkDocs config object.
            kwargs: Additional arguments passed by MkDocs.

        Returns:
            The modified files collection.
        """
        if config["use_directory_urls"]:
            covindex = "covindex.html"
        else:
            covindex = f"{self.config['page_name']}/covindex.html"

        style = textwrap.dedent(  # noqa: WPS462
            """
            <style>
            .md-content {
                max-width: none !important;
            }
            article h1, article > a {
                display: none;
            }
            </style>
            """)

        iframe = textwrap.dedent(  # noqa: WPS462
            f"""
            <iframe
                id="coviframe"
                src="{covindex}"
                frameborder="0"
                scrolling="no"
                onload="resizeIframe();"
                width="100%">
            </iframe>
            """)

        script = textwrap.dedent(  # noqa: WPS462
            """
            <script>
            var coviframe = document.getElementById("coviframe");

            function resizeIframe() {
                coviframe.style.height = coviframe.contentWindow.document.documentElement.offsetHeight + 'px';
            }

            coviframe.contentWindow.document.body.onclick = function() {
                coviframe.contentWindow.location.reload();
            }
            </script>

            """, )
        page_contents = style + iframe + script
        tempdir = mkdtemp()
        page_name = self.config["page_name"] + ".md"
        tempfile = Path(tempdir) / page_name
        with tempfile.open("w") as fp:
            fp.write(page_contents)
        files.append(
            File(
                page_name,
                str(tempfile.parent),
                config["site_dir"],
                config["use_directory_urls"],
            ), )
        return files