-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor plugin, fix minor bugs, add more tests...
- Loading branch information
Showing
16 changed files
with
591 additions
and
286 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
"""mkdocs-mdpo-plugin package""" | ||
|
||
__version__ = '0.0.14' | ||
__version__ = '0.0.15' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
"""mdpo utilities""" | ||
|
||
import re | ||
|
||
from mdpo.command import COMMAND_SEARCH_RE | ||
|
||
|
||
COMMAND_SEARCH_RE_AT_LINE_START = re.compile( | ||
r'^(\s{2,})?[^\\]' + COMMAND_SEARCH_RE.pattern + r'\n?', | ||
re.M, | ||
) | ||
COMMAND_SEARCH_RE_ESCAPER = re.compile( | ||
( | ||
r'\\(' + COMMAND_SEARCH_RE.pattern[:20] + ')(' | ||
+ COMMAND_SEARCH_RE.pattern[20:38] + '?=' | ||
+ COMMAND_SEARCH_RE.pattern[38:] + ')' | ||
), | ||
re.M, | ||
) | ||
|
||
|
||
def remove_mdpo_commands_preserving_escaped(text): | ||
return re.sub( | ||
# restore escaped commands | ||
'<!-- mdpo-0', | ||
'<!-- mdpo', | ||
re.sub( | ||
# remove commands | ||
COMMAND_SEARCH_RE_AT_LINE_START, | ||
'', | ||
# preserve escaped commands | ||
re.sub( | ||
COMMAND_SEARCH_RE_ESCAPER, | ||
r'\g<1>0-\g<2>', | ||
text, | ||
), | ||
), | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
"""Mkdocs utilities""" | ||
|
||
from mkdocs import __version__ as __mkdocs_version__ | ||
|
||
from mkdocs_mdpo_plugin.io import remove_file_and_parent_dir_if_empty | ||
|
||
|
||
MKDOCS_MINOR_VERSION_INFO = tuple( | ||
int(n) for n in __mkdocs_version__.split('.')[:2] | ||
) | ||
|
||
|
||
class MkdocsBuild: | ||
"""Represents the Mkdocs build process. | ||
Is a singleton, so only accepts one build. Should be initialized using | ||
the `instance` class method, which accepts an instance of the plugin class. | ||
""" | ||
_instance = None | ||
|
||
@classmethod | ||
def instance(cls, mdpo_plugin): | ||
if cls._instance is None: | ||
cls._instance = cls.__new__(cls) | ||
cls.mdpo_plugin = mdpo_plugin | ||
return cls._instance | ||
|
||
|
||
def __on_build_error(_self): | ||
for filepath in _self._temp_pages_to_remove: | ||
try: | ||
remove_file_and_parent_dir_if_empty(filepath) | ||
except FileNotFoundError: | ||
pass | ||
|
||
MkdocsBuild._instance = None | ||
|
||
|
||
def set_on_build_error_event(MdpoPlugin): | ||
"""mkdocs>=1.2.0 includes a ``build_error`` event executed when the build | ||
triggers a exception. | ||
This function patch provides the same cleanup functionality if the | ||
`build_error` event is not supported. | ||
""" | ||
if MKDOCS_MINOR_VERSION_INFO >= (1, 2): | ||
if not hasattr(MdpoPlugin, 'on_build_error'): | ||
def _on_build_error(self, error): | ||
return __on_build_error(self) | ||
|
||
MdpoPlugin.on_build_error = _on_build_error | ||
else: | ||
import atexit | ||
|
||
def _on_build_error(): # pragma: no cover | ||
build_instance = MkdocsBuild() | ||
if hasattr(build_instance, 'mdpo_plugin'): | ||
return __on_build_error(build_instance.mdpo_plugin) | ||
|
||
atexit.unregister(_on_build_error) | ||
atexit.register(_on_build_error) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
"""Configuration event for 'mkdocs-mdpo-plugin'.""" | ||
|
||
import mkdocs | ||
|
||
|
||
def on_config_event(plugin, config, **kwargs): | ||
"""Configuration event for 'mkdocs-mdpo-plugin'. | ||
* Define properly `lc_messages`, `languages` and `locale_dir` | ||
configuration settings. | ||
* Loads `mkdocs.mdpo` extension. | ||
* Configures md4c extensions accordingly to Python-Markdown extensions. | ||
* Stores the Markdown extensions used in the build in the | ||
``_markdown_extensions`` property of the plugin instance. | ||
* Creates the persistent files cache for the project. | ||
""" | ||
if plugin.config['lc_messages'] is True: | ||
plugin.config['lc_messages'] = 'LC_MESSAGES' | ||
elif not plugin.config['lc_messages']: | ||
plugin.config['lc_messages'] = '' | ||
|
||
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(): | ||
msg = ( | ||
'You must define the languages you will translate the' | ||
' content into using' | ||
f"{' either' if _using_material_theme else ' the'}" | ||
" 'plugins.mdpo.languages'" | ||
) | ||
if _using_material_theme: | ||
msg += " or 'extra.alternate'" | ||
msg += ( | ||
' configuration setting' | ||
f"{'s' if _using_material_theme else ''}." | ||
) | ||
return mkdocs.config.base.ValidationError(msg) | ||
|
||
languages = plugin.config.get('languages') | ||
if not languages: | ||
if _using_material_theme: | ||
if 'extra' not in config: | ||
raise _languages_required() | ||
alternate = config['extra'].get('alternate') | ||
if not alternate: | ||
raise _languages_required() | ||
plugin.config['languages'] = [alt['lang'] for alt in alternate] | ||
else: | ||
raise _languages_required() | ||
|
||
default_language = plugin.config.get('default_language') | ||
if not default_language: | ||
# use mkdocs>=v1.2.0 theme localization setting | ||
theme_locale = ( | ||
None if 'theme' not in config | ||
else config['theme']._vars.get('locale') | ||
) | ||
if theme_locale and theme_locale in plugin.config['languages']: | ||
plugin.config['default_language'] = theme_locale | ||
else: | ||
if ( | ||
not isinstance(plugin.config['languages'], list) | ||
or not plugin.config['languages'] | ||
): | ||
raise _languages_required() | ||
|
||
plugin.config['default_language'] = plugin.config['languages'][0] | ||
|
||
# ---------------------------------------------------------- | ||
|
||
# extensions configuration | ||
markdown_extensions = config.get('markdown_extensions') | ||
|
||
# configure MD4C extensions | ||
if markdown_extensions: | ||
if 'tables' not in markdown_extensions: | ||
if 'tables' in plugin._md4c_extensions: | ||
plugin._md4c_extensions.remove('tables') | ||
else: | ||
if 'tables' not in plugin._md4c_extensions: | ||
plugin._md4c_extensions.append('tables') | ||
if 'wikilinks' not in markdown_extensions: | ||
if 'wikilinks' in plugin._md4c_extensions: | ||
plugin._md4c_extensions.remove('wikilinks') | ||
else: | ||
if 'wikilinks' not in plugin._md4c_extensions: | ||
plugin._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 plugin._md4c_extensions: | ||
plugin._md4c_extensions.remove('permissive_atx_headers') | ||
else: | ||
if 'permissive_atx_headers' not in plugin._md4c_extensions: | ||
plugin._md4c_extensions.append('permissive_atx_headers') | ||
|
||
# 'pymdownx.tasklist' enables 'tasklists' MD4C extentsion | ||
if 'pymdownx.tasklist' in markdown_extensions: | ||
if 'tasklists' not in plugin._md4c_extensions: | ||
plugin._md4c_extensions.append('tasklists') | ||
else: | ||
if 'tasklists' in plugin._md4c_extensions: | ||
plugin._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 plugin._md4c_extensions: | ||
plugin._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') | ||
|
||
# store reference in plugin to markdown_extensions for later usage | ||
plugin._markdown_extensions = markdown_extensions | ||
|
||
# ---------------------------------------------------------- |
Oops, something went wrong.