Example #1
0
    def test_translations_found(self, tdir):
        translations = unittest.mock.Mock()

        with unittest.mock.patch('mkdocs.localization.Translations.load', return_value=translations):
            install_translations(self.env, parse_locale('en'), [tdir])

        self.env.install_gettext_translations.assert_called_once_with(translations)
Example #2
0
 def test_simple_theme(self):
     theme = Theme(name='mkdocs')
     self.assertEqual(
         theme.dirs,
         [os.path.join(theme_dir, 'mkdocs'), mkdocs_templates_dir])
     self.assertEqual(theme.static_templates, {'404.html', 'sitemap.xml'})
     self.assertEqual(
         get_vars(theme), {
             'locale': parse_locale('en'),
             'include_search_page': False,
             'search_index_only': False,
             'analytics': {
                 'gtag': None
             },
             'highlightjs': True,
             'hljs_style': 'github',
             'hljs_languages': [],
             'navigation_depth': 2,
             'nav_style': 'primary',
             'shortcuts': {
                 'help': 191,
                 'next': 78,
                 'previous': 80,
                 'search': 83
             }
         })
Example #3
0
    def __init__(self, name=None, **user_config):
        self.name = name
        self._vars = {'locale': 'en'}

        # MkDocs provided static templates are always included
        package_dir = os.path.abspath(os.path.dirname(__file__))
        mkdocs_templates = os.path.join(package_dir, 'templates')
        self.static_templates = set(os.listdir(mkdocs_templates))

        # Build self.dirs from various sources in order of precedence
        self.dirs = []

        if 'custom_dir' in user_config:
            self.dirs.append(user_config.pop('custom_dir'))

        if self.name:
            self._load_theme_config(name)

        # Include templates provided directly by MkDocs (outside any theme)
        self.dirs.append(mkdocs_templates)

        # Handle remaining user configs. Override theme configs (if set)
        self.static_templates.update(user_config.pop('static_templates', []))
        self._vars.update(user_config)

        # Validate locale and convert to Locale object
        self._vars['locale'] = localization.parse_locale(self._vars['locale'])
Example #4
0
    def test_merge_translations(self, custom_dir, theme_dir):
        custom_dir_translations = unittest.mock.Mock()
        theme_dir_translations = unittest.mock.Mock()

        def side_effet(*args, **kwargs):
            dirname = args[0]
            if dirname.startswith(custom_dir):
                return custom_dir_translations
            elif dirname.startswith(theme_dir):
                return theme_dir_translations
            else:
                self.fail()

        with unittest.mock.patch('mkdocs.localization.Translations.load', side_effect=side_effet):
            install_translations(self.env, parse_locale('en'), [custom_dir, theme_dir])

        theme_dir_translations.merge.assert_called_once_with(custom_dir_translations)
Example #5
0
    def test_theme(self):
        with TemporaryDirectory() as mytheme, TemporaryDirectory() as custom:
            configs = [
                dict(),  # default theme
                {
                    "theme": "readthedocs"
                },  # builtin theme
                {
                    "theme": {
                        'name': 'readthedocs'
                    }
                },  # builtin as complex
                {
                    "theme": {
                        'name': None,
                        'custom_dir': mytheme
                    }
                },  # custom only as complex
                {
                    "theme": {
                        'name': 'readthedocs',
                        'custom_dir': custom
                    }
                },  # builtin and custom as complex
                {  # user defined variables
                    'theme': {
                        'name': 'mkdocs',
                        'locale': 'fr',
                        'static_templates': ['foo.html'],
                        'show_sidebar': False,
                        'some_var': 'bar'
                    }
                }
            ]

            mkdocs_dir = os.path.abspath(os.path.dirname(mkdocs.__file__))
            mkdocs_templates_dir = os.path.join(mkdocs_dir, 'templates')
            theme_dir = os.path.abspath(os.path.join(mkdocs_dir, 'themes'))

            results = ({
                'dirs':
                [os.path.join(theme_dir, 'mkdocs'), mkdocs_templates_dir],
                'static_templates': ['404.html', 'sitemap.xml'],
                'vars': {
                    'locale': parse_locale('en'),
                    'include_search_page': False,
                    'search_index_only': False,
                    'analytics': {
                        'gtag': None
                    },
                    'highlightjs': True,
                    'hljs_style': 'github',
                    'hljs_languages': [],
                    'navigation_depth': 2,
                    'nav_style': 'primary',
                    'shortcuts': {
                        'help': 191,
                        'next': 78,
                        'previous': 80,
                        'search': 83
                    }
                }
            }, {
                'dirs':
                [os.path.join(theme_dir, 'readthedocs'), mkdocs_templates_dir],
                'static_templates': ['404.html', 'sitemap.xml'],
                'vars': {
                    'locale': parse_locale('en'),
                    'include_search_page': True,
                    'search_index_only': False,
                    'analytics': {
                        'gtag': None
                    },
                    'highlightjs': True,
                    'hljs_languages': [],
                    'include_homepage_in_sidebar': True,
                    'prev_next_buttons_location': 'bottom',
                    'navigation_depth': 4,
                    'sticky_navigation': True,
                    'titles_only': False,
                    'collapse_navigation': True
                }
            }, {
                'dirs':
                [os.path.join(theme_dir, 'readthedocs'), mkdocs_templates_dir],
                'static_templates': ['404.html', 'sitemap.xml'],
                'vars': {
                    'locale': parse_locale('en'),
                    'include_search_page': True,
                    'search_index_only': False,
                    'analytics': {
                        'gtag': None
                    },
                    'highlightjs': True,
                    'hljs_languages': [],
                    'include_homepage_in_sidebar': True,
                    'prev_next_buttons_location': 'bottom',
                    'navigation_depth': 4,
                    'sticky_navigation': True,
                    'titles_only': False,
                    'collapse_navigation': True
                }
            }, {
                'dirs': [mytheme, mkdocs_templates_dir],
                'static_templates': ['sitemap.xml'],
                'vars': {
                    'locale': parse_locale('en')
                }
            }, {
                'dirs': [
                    custom,
                    os.path.join(theme_dir, 'readthedocs'),
                    mkdocs_templates_dir
                ],
                'static_templates': ['404.html', 'sitemap.xml'],
                'vars': {
                    'locale': parse_locale('en'),
                    'include_search_page': True,
                    'search_index_only': False,
                    'analytics': {
                        'gtag': None
                    },
                    'highlightjs': True,
                    'hljs_languages': [],
                    'include_homepage_in_sidebar': True,
                    'prev_next_buttons_location': 'bottom',
                    'navigation_depth': 4,
                    'sticky_navigation': True,
                    'titles_only': False,
                    'collapse_navigation': True
                }
            }, {
                'dirs':
                [os.path.join(theme_dir, 'mkdocs'), mkdocs_templates_dir],
                'static_templates': ['404.html', 'sitemap.xml', 'foo.html'],
                'vars': {
                    'locale': parse_locale('fr'),
                    'show_sidebar': False,
                    'some_var': 'bar',
                    'include_search_page': False,
                    'search_index_only': False,
                    'analytics': {
                        'gtag': None
                    },
                    'highlightjs': True,
                    'hljs_style': 'github',
                    'hljs_languages': [],
                    'navigation_depth': 2,
                    'nav_style': 'primary',
                    'shortcuts': {
                        'help': 191,
                        'next': 78,
                        'previous': 80,
                        'search': 83
                    }
                }
            })

            for config_contents, result in zip(configs, results):

                c = config.Config(schema=(('theme',
                                           config_options.Theme(
                                               default='mkdocs')), ))
                c.load_dict(config_contents)
                errors, warnings = c.validate()
                self.assertEqual(len(errors), 0)
                self.assertEqual(c['theme'].dirs, result['dirs'])
                self.assertEqual(c['theme'].static_templates,
                                 set(result['static_templates']))
                self.assertEqual({k: c['theme'][k]
                                  for k in iter(c['theme'])}, result['vars'])
 def test_no_translations_found(self, dir_without_translations):
     install_translations(self.env, parse_locale('fr_CA'),
                          [dir_without_translations])
     self.env.install_null_translations.assert_called_once()
 def test_valid_language_territory(self):
     locale = parse_locale('en_US')
     self.assertEqual(locale.language, 'en')
     self.assertEqual(locale.territory, 'US')
     self.assertEqual(str(locale), 'en_US')
 def test_valid_language(self):
     locale = parse_locale('en')
     self.assertEqual(locale.language, 'en')
 def test_jinja_extension_installed(self):
     install_translations(self.env, parse_locale('en'), [])
     self.env.add_extension.assert_called_once_with('jinja2.ext.i18n')