Skip to content

Commit

Permalink
Add support for Mkdocs > 1.2
Browse files Browse the repository at this point in the history
  • Loading branch information
mondeja committed Jun 11, 2021
1 parent 1761511 commit 0740776
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 68 deletions.
2 changes: 1 addition & 1 deletion .bumpversion.cfg
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[bumpversion]
current_version = 0.0.13
current_version = 0.0.14

[bumpversion:file:mkdocs_mdpo_plugin/__init__.py]

Expand Down
5 changes: 4 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ jobs:
- 3.7
- 3.8
- 3.9
mkdocs-version:
- 1.1.2
- 1.2.1
steps:
- uses: actions/checkout@v2
- name: Set up Python v${{ matrix.python-version }}
Expand All @@ -29,7 +32,7 @@ jobs:
- name: Install dependencies
run: |
python -m pip install --upgrade pip wheel coveralls
python -m pip install .[test]
python -m pip install .[test] mkdocs==${{ matrix.mkdocs-version }}
pip list
- name: Test with pytest
run: pytest -svv --cov=mkdocs_mdpo_plugin --cov-config=setup.cfg
Expand Down
2 changes: 1 addition & 1 deletion mkdocs_mdpo_plugin/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
"""mkdocs-mdpo-plugin package"""

__version__ = '0.0.13'
__version__ = '0.0.14'
99 changes: 57 additions & 42 deletions mkdocs_mdpo_plugin/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,10 @@ def on_config(self, config, **kwargs):
elif not self.config['lc_messages']:
self.config['lc_messages'] = ''

_using_material_theme = config['theme'].name == 'material'
try:
_using_material_theme = config['theme'].name == 'material'
except KeyError:
_using_material_theme = None

# load language selection settings from material or mdpo configuration
def _languages_required():
Expand Down Expand Up @@ -197,56 +200,68 @@ def _default_language_required():
default_language = self.config.get('default_language')
if not default_language:
# use mkdocs>=v1.2.0 theme localization setting
if hasattr(config['theme'], 'locale') and config['theme'].locale:
if (
config.get('theme')
and hasattr(config['theme'], 'locale')
and config['theme'].locale
):
self.config['default_language'] = config['theme'].locale
else:
if (
not isinstance(self.config['languages'], list)
or not self.config['languages']
):
raise _languages_required()

self.config['default_language'] = self.config['languages'][0]

# ----------------------------------------------------------

# extensions configuration
markdown_extensions = config.get('markdown_extensions')

# configure MD4C extensions
if 'tables' not in config['markdown_extensions']:
if 'tables' in self._md4c_extensions:
self._md4c_extensions.remove('tables')
else:
if 'tables' not in self._md4c_extensions:
self._md4c_extensions.append('tables')
if 'wikilinks' not in config['markdown_extensions']:
if 'wikilinks' in self._md4c_extensions:
self._md4c_extensions.remove('wikilinks')
else:
if 'wikilinks' not in self._md4c_extensions:
self._md4c_extensions.append('wikilinks')

# spaces after '#' are optional in Python-Markdown for headers,
# but the extension 'pymdownx.saneheaders' makes them mandatory
if 'pymdownx.saneheaders' in config['markdown_extensions']:
if 'permissive_atx_headers' in self._md4c_extensions:
self._md4c_extensions.remove('permissive_atx_headers')
else:
if 'permissive_atx_headers' not in self._md4c_extensions:
self._md4c_extensions.append('permissive_atx_headers')

# 'pymdownx.tasklist' enables 'tasklists' MD4C extentsion
if 'pymdownx.tasklist' in config['markdown_extensions']:
if 'tasklists' not in self._md4c_extensions:
self._md4c_extensions.append('tasklists')
else:
if 'tasklists' in self._md4c_extensions:
self._md4c_extensions.remove('tasklists')

# 'pymdownx.tilde' enables strikethrough syntax, but only works
# if the MD4C extension is disabled
if 'pymdownx.tilde' in config['markdown_extensions']:
if 'strikethrough' in self._md4c_extensions:
self._md4c_extensions.remove('strikethrough')

# configure internal 'mkdocs.mdpo' extension
if 'mkdocs.mdpo' in config['markdown_extensions']: # pragma: no cover
config['markdown_extensions'].remove('mkdocs.mdpo')
config['markdown_extensions'].append('mkdocs.mdpo')
if markdown_extensions:
if 'tables' not in markdown_extensions:
if 'tables' in self._md4c_extensions:
self._md4c_extensions.remove('tables')
else:
if 'tables' not in self._md4c_extensions:
self._md4c_extensions.append('tables')
if 'wikilinks' not in markdown_extensions:
if 'wikilinks' in self._md4c_extensions:
self._md4c_extensions.remove('wikilinks')
else:
if 'wikilinks' not in self._md4c_extensions:
self._md4c_extensions.append('wikilinks')

# spaces after '#' are optional in Python-Markdown for headers,
# but the extension 'pymdownx.saneheaders' makes them mandatory
if 'pymdownx.saneheaders' in markdown_extensions:
if 'permissive_atx_headers' in self._md4c_extensions:
self._md4c_extensions.remove('permissive_atx_headers')
else:
if 'permissive_atx_headers' not in self._md4c_extensions:
self._md4c_extensions.append('permissive_atx_headers')

# 'pymdownx.tasklist' enables 'tasklists' MD4C extentsion
if 'pymdownx.tasklist' in markdown_extensions:
if 'tasklists' not in self._md4c_extensions:
self._md4c_extensions.append('tasklists')
else:
if 'tasklists' in self._md4c_extensions:
self._md4c_extensions.remove('tasklists')

# 'pymdownx.tilde' enables strikethrough syntax, but only works
# if the MD4C extension is disabled
if 'pymdownx.tilde' in markdown_extensions:
if 'strikethrough' in self._md4c_extensions:
self._md4c_extensions.remove('strikethrough')

# configure internal 'mkdocs.mdpo' extension
if 'mkdocs.mdpo' in markdown_extensions: # pragma: no cover
config['markdown_extensions'].remove('mkdocs.mdpo')
config['markdown_extensions'].append('mkdocs.mdpo')

# ----------------------------------------------------------

Expand Down
13 changes: 6 additions & 7 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[metadata]
name = mkdocs_mdpo_plugin
version = 0.0.13
version = 0.0.14
description = Mkdocs plugin for translations using PO files.
long_description = file: README.md
long_description_content_type = text/markdown
Expand Down Expand Up @@ -44,25 +44,25 @@ dev =
flake8-implicit-str-concat==0.2.0
flake8-print==4.0.0
isort==5.8.0
mkdocs==1.1.2
mkdocs==1.2.1
mkdocs-exclude==1.0.2
mkdocs-include-markdown-plugin==3.1.0
mkdocs-material==7.1.2
mkdocs-material-relative-language-selector==1.1.0
mkdocstrings==0.15.1
mkdocstrings==0.15.2
pre-commit==2.12.1
pymdown-extensions==8.1.1
pytest==6.2.3
pytest-cov==2.11.1
yamllint==1.26.1
doc =
mkdocs==1.1.2
mkdocs==1.2.1
mkdocs-exclude==1.0.2
mkdocs-include-markdown-plugin==3.1.0
mkdocs-material==7.1.2
mkdocs-material-relative-language-selector==1.1.0
mkdocs-minify-plugin==0.4.0
mkdocstrings==0.15.1
mkdocstrings==0.15.2
pymdown-extensions==8.1.1
lint =
flake8==3.9.1
Expand All @@ -71,10 +71,9 @@ lint =
isort==5.8.0
yamllint==1.26.1
test =
mkdocs==1.1.2
mkdocs-exclude==1.0.2
mkdocs-material==7.1.2
mkdocstrings==0.15.1
mkdocstrings==0.15.2
pymdown-extensions==8.1.1
pytest==6.2.3
pytest-cov==2.11.1
Expand Down
1 change: 1 addition & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ def _mkdocs_build(

mkdocs_config = {
'site_name': 'My site',
'site_url': 'https://foo.bar',
'docs_dir': docs_dir,
'site_dir': site_dir,
'plugins': [],
Expand Down
21 changes: 5 additions & 16 deletions tests/test_plugin_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,26 +286,14 @@ def test_config(
'languages': 5,
},
None,
mkdocs.exceptions.ConfigurationError,
mkdocs.config.base.ValidationError,
(
"Plugin value: 'languages'. Error: Expected type:"
" <class 'list'> but received: <class 'int'>"
'You must define the languages you will translate the content'
" into using the 'plugins.mdpo.languages' configuration"
' setting.'
),
id='languages:<int>',
),
pytest.param(
{
'languages': ['en', 'es'],
'default_language': 5,
},
None,
mkdocs.exceptions.ConfigurationError,
(
"Plugin value: 'default_language'. Error: Expected type:"
" <class 'str'> but received: <class 'int'>"
),
id='default_language:<int>',
),
pytest.param(
{
'default_language': 'es',
Expand Down Expand Up @@ -412,6 +400,7 @@ def test_plugin_config_errors(

mkdocs_config = {
'site_name': 'My site',
'site_url': 'https://foo.bar',
'docs_dir': docs_dir,
'site_dir': site_dir,
'plugins': [
Expand Down

0 comments on commit 0740776

Please sign in to comment.