Skip to content

Commit

Permalink
Add 'ignore_msgids' setting
Browse files Browse the repository at this point in the history
  • Loading branch information
mondeja committed Jul 5, 2021
1 parent 805bb3c commit 5dc80c3
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 19 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.15
current_version = 0.0.16

[bumpversion:file:mkdocs_mdpo_plugin/__init__.py]

Expand Down
5 changes: 5 additions & 0 deletions docs/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -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']`.

<!-- mdpo-disable-next-line -->
### **`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
Expand Down
7 changes: 7 additions & 0 deletions docs/es/config.md.po
Original file line number Diff line number Diff line change
Expand Up @@ -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."
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.15'
__version__ = '0.0.16'
19 changes: 17 additions & 2 deletions mkdocs_mdpo_plugin/on_config.py → mkdocs_mdpo_plugin/config.py
Original file line number Diff line number Diff line change
@@ -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):
Expand Down Expand Up @@ -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:
Expand Down
18 changes: 4 additions & 14 deletions mkdocs_mdpo_plugin/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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()

Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -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
Expand Down
28 changes: 28 additions & 0 deletions tests/test_plugin_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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': [
'<p>foo</p>',
'<p>bar</p>',
'<p>baz</p>',
],
'es/index.html': [
'<p>foo es</p>',
'<p>bar</p>',
'<p>baz</p>',
],
},
id='ignore_msgids',
),
)


Expand Down

0 comments on commit 5dc80c3

Please sign in to comment.