diff --git a/.bumpversion.cfg b/.bumpversion.cfg index c4bd620..ea3cafc 100644 --- a/.bumpversion.cfg +++ b/.bumpversion.cfg @@ -1,5 +1,5 @@ [bumpversion] -current_version = 0.0.15 +current_version = 0.0.16 [bumpversion:file:mkdocs_mdpo_plugin/__init__.py] diff --git a/docs/config.md b/docs/config.md index f685f32..6cc5144 100644 --- a/docs/config.md +++ b/docs/config.md @@ -191,6 +191,11 @@ The context for the template includes: File extensions that are ignored from being added to site directory, defaults to `['.po', '.pot', '.mo']`. + +### **`ignore_msgids`** (*list*) + +You can ignore certain messages from being dumped into PO files adding them to +this list. [iso-369]: https://en.wikipedia.org/wiki/ISO_639 [mkdocs-material]: https://squidfunk.github.io/mkdocs-material diff --git a/docs/es/config.md.po b/docs/es/config.md.po index 461a4a0..1380b9f 100644 --- a/docs/es/config.md.po +++ b/docs/es/config.md.po @@ -181,3 +181,10 @@ msgstr "" msgid "Configuration" msgstr "Configuración" + +msgid "" +"You can ignore certain messages from being dumped into PO files adding them " +"to this list." +msgstr "" +"Puedes ignorar ciertos mensajes de ser volcados a los archivos PO añadiéndolos" +" a esta lista." diff --git a/mkdocs_mdpo_plugin/__init__.py b/mkdocs_mdpo_plugin/__init__.py index ebda8c3..b59cb02 100644 --- a/mkdocs_mdpo_plugin/__init__.py +++ b/mkdocs_mdpo_plugin/__init__.py @@ -1,3 +1,3 @@ """mkdocs-mdpo-plugin package""" -__version__ = '0.0.15' +__version__ = '0.0.16' diff --git a/mkdocs_mdpo_plugin/on_config.py b/mkdocs_mdpo_plugin/config.py similarity index 89% rename from mkdocs_mdpo_plugin/on_config.py rename to mkdocs_mdpo_plugin/config.py index fc654cf..8aa972c 100644 --- a/mkdocs_mdpo_plugin/on_config.py +++ b/mkdocs_mdpo_plugin/config.py @@ -1,6 +1,21 @@ """Configuration event for 'mkdocs-mdpo-plugin'.""" -import mkdocs +from mkdocs.config.base import ValidationError +from mkdocs.config.config_options import Type + + +CONFIG_SCHEME = ( + ('locale_dir', Type(str, default='')), + ('default_language', Type(str, required=False)), + ('languages', Type(list, required=False)), + ('lc_messages', Type((str, bool), default='')), + ( + 'dest_filename_template', + Type(str, default='{{language}}/{{page.file.dest_path}}'), + ), + ('ignore_extensions', Type(list, default=['.po', '.pot', '.mo'])), + ('ignore_msgids', Type(list, default=[])), +) def on_config_event(plugin, config, **kwargs): @@ -38,7 +53,7 @@ def _languages_required(): ' configuration setting' f"{'s' if _using_material_theme else ''}." ) - return mkdocs.config.base.ValidationError(msg) + return ValidationError(msg) languages = plugin.config.get('languages') if not languages: diff --git a/mkdocs_mdpo_plugin/plugin.py b/mkdocs_mdpo_plugin/plugin.py index 2a0de2a..2eb1dbd 100644 --- a/mkdocs_mdpo_plugin/plugin.py +++ b/mkdocs_mdpo_plugin/plugin.py @@ -11,8 +11,8 @@ from mdpo.md2po import Md2Po from mdpo.md4c import DEFAULT_MD4C_GENERIC_PARSER_EXTENSIONS from mdpo.po2md import Po2Md -from mkdocs.config.config_options import Type +from mkdocs_mdpo_plugin.config import CONFIG_SCHEME, on_config_event from mkdocs_mdpo_plugin.io import ( remove_empty_directories_from_dirtree, remove_file_and_parent_dir_if_empty, @@ -28,21 +28,10 @@ MkdocsBuild, set_on_build_error_event, ) -from mkdocs_mdpo_plugin.on_config import on_config_event class MdpoPlugin(mkdocs.plugins.BasePlugin): - config_scheme = ( - ('locale_dir', Type(str, default='')), - ('default_language', Type(str, required=False)), - ('languages', Type(list, required=False)), - ('lc_messages', Type((str, bool), default='')), - ( - 'dest_filename_template', - Type(str, default='{{language}}/{{page.file.dest_path}}'), - ), - ('ignore_extensions', Type(list, default=['.po', '.pot', '.mo'])), - ) + config_scheme = CONFIG_SCHEME def __init__(self, *args, **kwargs): # temporal translated pages created by the plugin at runtime @@ -99,7 +88,6 @@ def on_config(self, config, **kwargs): def on_pre_build(self, config): """Create locales folders inside documentation directory.""" - for language in self._non_default_languages(): os.makedirs( os.path.join( @@ -217,6 +205,7 @@ def on_page_markdown(self, markdown, page, config, files): events=build_md2po_events(self._markdown_extensions), mark_not_found_as_obsolete=False, location=False, + ignore_msgids=self.config['ignore_msgids'], ) original_po = md2po.extract() @@ -354,6 +343,7 @@ def on_page_markdown(self, markdown, page, config, files): new_page._disabled_msgids = [ entry.msgid for entry in po2md.disabled_entries ] + new_page._disabled_msgids.extend(self.config['ignore_msgids']) for entry in po2md.translated_entries: new_page._translated_entries_msgstrs.append(entry.msgstr) new_page._translated_entries_msgids.append(entry.msgid) diff --git a/setup.cfg b/setup.cfg index 1dfb279..7934ae8 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,6 +1,6 @@ [metadata] name = mkdocs_mdpo_plugin -version = 0.0.15 +version = 0.0.16 description = Mkdocs plugin for translations using PO files. long_description = file: README.md long_description_content_type = text/markdown diff --git a/tests/test_plugin_config.py b/tests/test_plugin_config.py index 0afb7a8..5204a8c 100644 --- a/tests/test_plugin_config.py +++ b/tests/test_plugin_config.py @@ -242,6 +242,34 @@ }, id='nav-title-translation', ), + pytest.param( # ignore msgids + { + 'index.md': 'foo\n\nbar\n\nbaz\n', + }, + { + 'es/index.md.po': { + 'foo': 'foo es', + }, + }, + { + 'languages': ['en', 'es'], + 'ignore_msgids': ['bar', 'baz'], + }, + None, + { + 'index.html': [ + '

foo

', + '

bar

', + '

baz

', + ], + 'es/index.html': [ + '

foo es

', + '

bar

', + '

baz

', + ], + }, + id='ignore_msgids', + ), )