예제 #1
0
 def test_event_empty_item(self):
     collection = plugins.PluginCollection()
     plugin = DummyPlugin()
     plugin.load_config({'foo': 'new'})
     collection['foo'] = plugin
     self.assertEqual(collection.run_event('page_read_source'),
                      'new source')
예제 #2
0
 def test_set_multiple_plugins_on_collection(self):
     collection = plugins.PluginCollection()
     plugin1 = DummyPlugin()
     collection['foo'] = plugin1
     plugin2 = DummyPlugin()
     collection['bar'] = plugin2
     self.assertEqual([(k, v) for k, v in collection.items()], [('foo', plugin1), ('bar', plugin2)])
예제 #3
0
 def test_run_event_on_collection(self):
     collection = plugins.PluginCollection()
     plugin = DummyPlugin()
     plugin.load_config({'foo': 'new'})
     collection['foo'] = plugin
     self.assertEqual(collection.run_event('pre_page', 'page content'),
                      'new page content')
예제 #4
0
 def setUp(self):
     self.techdocscore = TechDocsCore()
     self.plugin_collection = plugins.PluginCollection()
     plugin = DummyTechDocsCorePlugin()
     self.plugin_collection["techdocs-core"] = plugin
     self.mkdocs_yaml_config = {"plugins": self.plugin_collection}
     # Note: in reality, config["theme"] is always an instance of Theme
     self.mkdocs_yaml_config["theme"] = get_default_theme()
예제 #5
0
 def test_run_event_twice_on_collection(self):
     collection = plugins.PluginCollection()
     plugin1 = DummyPlugin()
     plugin1.load_config({'foo': 'new'})
     collection['foo'] = plugin1
     plugin2 = DummyPlugin()
     plugin2.load_config({'foo': 'second'})
     collection['bar'] = plugin2
     self.assertEqual(collection.run_event('pre_page', 'page content'),
                      'second new page content')
예제 #6
0
    def _collect_tags_and_pages_info(self, nav: nav.Navigation,
                                     config: config.Config) -> str:
        """Collects unsorted tags and pages info from markdown files.

        Args:
            nav: the `mkdocs.structure.nav.Navigation` instance of the site
            config: the `mkdocs.config.Config` instance of the site

        Returns:
            a str containing title of the tag page, to be used in
            `_set_header_ids()`
        """
        config_copy = copy.copy(config)
        config_copy["plugins"] = plugins.PluginCollection()
        # Remove plugins in `config`. Otherwise,
        # `page_copy.read_source()` will call `on_page_read_source()`
        # for each plugin
        for page in nav.pages:
            page_copy = copy.copy(page)
            page_copy.read_source(config_copy)  # Read meta data
            # Collect the title of the tag page
            if page_copy.file.src_path == self._tag_page_md_path:
                tag_page_title: str = page_copy.title
            # Collect tag and page info
            page_info = _PageInfo(page=page_copy,
                                  tag_page_md_path=self._tag_page_md_path)
            self._page_info_of_abs_path[page_info.abs_path] = page_info
            if _TAGS_META_ENTRY not in page_copy.meta:
                continue
            tag_names = page_copy.meta[_TAGS_META_ENTRY]
            if not isinstance(tag_names, list):
                continue
            for tag_name in tag_names:
                if not isinstance(tag_name, str):
                    continue
                if tag_name in self._tag_info_of_name:
                    tag = self._tag_info_of_name[tag_name]
                else:
                    tag = _TagInfo(name=tag_name)
                    self._tag_info_of_name[tag_name] = tag
                page_info.tags.append(tag)
                if tag not in self._pages_under_tag:
                    self._pages_under_tag[tag] = []
                self._pages_under_tag[tag].append(page_info)
        return tag_page_title
예제 #7
0
 def run_validation(self, value):
     if not isinstance(value, (list, tuple, dict)):
         raise ValidationError('Invalid Plugins configuration. Expected a list or dict.')
     plgins = plugins.PluginCollection()
     if isinstance(value, dict):
         for name, cfg in value.items():
             plgins[name] = self.load_plugin(name, cfg)
     else:
         for item in value:
             if isinstance(item, dict):
                 if len(item) > 1:
                     raise ValidationError('Invalid Plugins configuration')
                 name, cfg = item.popitem()
                 item = name
             else:
                 cfg = {}
             plgins[item] = self.load_plugin(item, cfg)
     return plgins
예제 #8
0
 def run_validation(self, value):
     if not isinstance(value, (list, tuple)):
         raise ValidationError(
             'Invalid Plugins configuration. Expected a list of plugins')
     plgins = plugins.PluginCollection()
     for item in value:
         if isinstance(item, dict):
             if len(item) > 1:
                 raise ValidationError('Invalid Plugins configuration')
             name, cfg = item.popitem()
             cfg = cfg or {}  # Users may define a null (None) config
             if not isinstance(cfg, dict):
                 raise ValidationError('Invalid config options for '
                                       'the "{0}" plugin.'.format(name))
             plgins[name] = self.load_plugin(name, cfg)
         elif isinstance(item, utils.string_types):
             plgins[item] = self.load_plugin(item, {})
         else:
             raise ValidationError('Invalid Plugins configuration')
     return plgins
예제 #9
0
    def run_validation(self, value):
        if not isinstance(value, (list, tuple)):
            raise ValidationError('Invalid Plugins configuration. Expected a list of plugins')
        plgins = plugins.PluginCollection()
        for item in value:
            if isinstance(item, dict):
                if len(item) > 1:
                    raise ValidationError('Invalid Plugins configuration')
                name, cfg = item.popitem()
                cfg = cfg or {}  # Users may define a null (None) config
                if not isinstance(cfg, dict):
                    raise ValidationError(f'Invalid config options for the "{name}" plugin.')
                item = name
            else:
                cfg = {}

            if not isinstance(item, str):
                raise ValidationError('Invalid Plugins configuration')

            plgins[item] = self.load_plugin(item, cfg)

        return plgins
예제 #10
0
 def test_set_plugin_on_collection(self):
     collection = plugins.PluginCollection()
     plugin = DummyPlugin()
     collection['foo'] = plugin
     self.assertEqual([(k, v) for k, v in collection.items()], [('foo', plugin)])
예제 #11
0
 def test_run_unknown_event_on_collection(self):
     collection = plugins.PluginCollection()
     self.assertRaises(KeyError, collection.run_event, 'unknown', 'page content')
예제 #12
0
 def test_run_undefined_event_on_collection(self):
     collection = plugins.PluginCollection()
     self.assertEqual(collection.run_event('pre_page', 'page content'), 'page content')
예제 #13
0
 def test_event_returns_None(self):
     collection = plugins.PluginCollection()
     plugin = DummyPlugin()
     plugin.load_config({'foo': 'new'})
     collection['foo'] = plugin
     self.assertEqual(collection.run_event('nav', 'nav item'), 'nav item')
예제 #14
0
 def test_event_empty_item_returns_None(self):
     collection = plugins.PluginCollection()
     plugin = DummyPlugin()
     plugin.load_config({'foo': 'new'})
     collection['foo'] = plugin
     self.assertEqual(collection.run_event('pre_build'), None)
예제 #15
0
 def setUp(self):
     self.techdocscore = TechDocsCore()
     self.plugin_collection = plugins.PluginCollection()
     plugin = DummyTechDocsCorePlugin()
     self.plugin_collection["techdocs-core"] = plugin
     self.mkdocs_yaml_config = {"plugins": self.plugin_collection}