Skip to content

Commit

Permalink
Add config setting to disable cross language search (#32)
Browse files Browse the repository at this point in the history
* Add config setting to disable cross language search

* Bump version

* Continue work...

* Add support for readthedocs theme

* Fix test

* Update docs

* Fix error removing suffixes

* Add tests for search indexes

* Test config

* Improve documentation for the new config setting
  • Loading branch information
mondeja authored Feb 26, 2022
1 parent 5afc215 commit c0b2f6e
Show file tree
Hide file tree
Showing 24 changed files with 794 additions and 10 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.20
current_version = 0.0.21

[bumpversion:file:mkdocs_mdpo_plugin/__init__.py]

Expand Down
18 changes: 18 additions & 0 deletions docs/locale/es/config.md.po
Original file line number Diff line number Diff line change
Expand Up @@ -188,3 +188,21 @@ msgstr ""

msgid "Content"
msgstr "Contenido"

msgid ""
"It configures if the search plugin of the theme will search through all "
"languages. By default is enabled. You can disable it to restrict the search "
"to the active language."
msgstr ""
"Configura si el plugin de búsqueda del tema buscará a través de todos los "
"idiomas. Por defecto está habilitado. Puedes deshabilitarlo para restringir "
"la búsqueda al idioma activo."

msgid ""
"The support for this feature currently includes the [mkdocs-material] theme,"
" the Mkdocs theme, the Readthedocs theme and all themes which use the "
"builtin Mkdocs search plugin."
msgstr ""
"El soporte de esta característica actualmente incluye el tema [mkdocs-"
"material], el tema por defecto de Mkdocs, el tema de Readthedocs y todos los "
"temas que usen el plugin de búsqueda incluido en Mkdocs."
10 changes: 10 additions & 0 deletions docs/src/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,16 @@ File extensions that are ignored from being added to site directory, defaults to
You can ignore certain messages from being dumped into PO files adding them to
this list.

<!-- mdpo-disable-next-line -->
### **`cross_language_search`** (*bool*)

It configures if the search plugin of the theme will search through all
languages. By default is enabled. You can disable it to restrict the search to the active language.

The support for this feature currently includes the [mkdocs-material] theme,
the Mkdocs theme, the Readthedocs theme and all themes which use the builtin
Mkdocs search plugin.

[iso-369]: https://en.wikipedia.org/wiki/ISO_639
[mkdocs-material]: https://squidfunk.github.io/mkdocs-material
[mkdocs-material-site-language]: https://squidfunk.github.io/mkdocs-material/setup/changing-the-language/#site-language
Expand Down
3 changes: 2 additions & 1 deletion examples/material/mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ nav:

plugins:
- search
- mdpo
- mdpo:
cross_language_search: false

extra:
alternate:
Expand Down
12 changes: 12 additions & 0 deletions examples/mkdocs-theme/docs/locale/es/index.md.po
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#
msgid ""
msgstr ""

msgid "Home"
msgstr "Inicio"

msgid "Welcome to MkDocs"
msgstr "Bienvenido a Mkdocs"

msgid "Some content"
msgstr "Algo de contenido"
12 changes: 12 additions & 0 deletions examples/mkdocs-theme/docs/locale/fr/index.md.po
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#
msgid ""
msgstr ""

msgid "Home"
msgstr "Accueil"

msgid "Welcome to MkDocs"
msgstr "Bienvenue sur mkdocs"

msgid "Some content"
msgstr "Du contenu"
3 changes: 3 additions & 0 deletions examples/mkdocs-theme/docs/src/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Welcome to MkDocs

Some content
16 changes: 16 additions & 0 deletions examples/mkdocs-theme/mkdocs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
site_name: mkdocs-mdpo-plugin Mkdocs theme example
site_url: https://mkdocs-mdpo.ga
docs_dir: docs/src

nav:
- Home: index.md

plugins:
- search
- mdpo:
languages:
- en
- es
- fr
cross_language_search: false
locale_dir: ../locale
1 change: 1 addition & 0 deletions examples/po-outside-docs/mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ plugins:
- search
- mdpo:
locale_dir: ../locale
cross_language_search: false

extra:
alternate:
Expand Down
12 changes: 12 additions & 0 deletions examples/readthedocs-theme/docs/locale/es/index.md.po
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#
msgid ""
msgstr ""

msgid "Home"
msgstr "Inicio"

msgid "Welcome to MkDocs"
msgstr "Bienvenido a Mkdocs"

msgid "Some content"
msgstr "Algo de contenido"
12 changes: 12 additions & 0 deletions examples/readthedocs-theme/docs/locale/fr/index.md.po
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#
msgid ""
msgstr ""

msgid "Home"
msgstr "Accueil"

msgid "Welcome to MkDocs"
msgstr "Bienvenue sur mkdocs"

msgid "Some content"
msgstr "Du contenu"
3 changes: 3 additions & 0 deletions examples/readthedocs-theme/docs/src/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Welcome to MkDocs

Some content
19 changes: 19 additions & 0 deletions examples/readthedocs-theme/mkdocs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
site_name: mkdocs-mdpo-plugin Mkdocs theme example
site_url: https://mkdocs-mdpo.ga
docs_dir: docs/src

nav:
- Home: index.md

theme:
name: readthedocs

plugins:
- search
- mdpo:
languages:
- en
- es
- fr
cross_language_search: false
locale_dir: ../locale
1 change: 1 addition & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ plugins:
root_domain: true
- mdpo:
locale_dir: ../locale
cross_language_search: false
- minify:
minify_html: true

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.20'
__version__ = '0.0.21'
4 changes: 4 additions & 0 deletions mkdocs_mdpo_plugin/compat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
def removesuffix(s, suf):
if suf and s.endswith(suf):
return s[:-len(suf)]
return s
9 changes: 9 additions & 0 deletions mkdocs_mdpo_plugin/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
),
('ignore_extensions', Type(list, default=['.po', '.pot', '.mo'])),
('ignore_msgids', Type(list, default=[])),
('cross_language_search', Type(bool, default=True)),
)


Expand Down Expand Up @@ -146,6 +147,14 @@ def _languages_required():
)
config['theme'].dirs.insert(0, custom_sitemap_dir)

# check that cross language search configuration is valid
if plugin.config.get('cross_language_search') is False:
if 'search' not in config['plugins']:
raise ValidationError(
'"cross_language_search" setting is disabled but'
' no "search" plugin has been added to "plugins"',
)

# store reference in plugin to markdown_extensions for later usage
plugin.extensions.markdown = markdown_extensions

Expand Down
41 changes: 36 additions & 5 deletions mkdocs_mdpo_plugin/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from mdpo.md2po import Md2Po
from mdpo.po2md import Po2Md

from mkdocs_mdpo_plugin.compat import removesuffix
from mkdocs_mdpo_plugin.config import CONFIG_SCHEME, on_config_event
from mkdocs_mdpo_plugin.extensions import Extensions
from mkdocs_mdpo_plugin.mdpo_events import (
Expand All @@ -24,6 +25,7 @@
MkdocsBuild,
set_on_build_error_event,
)
from mkdocs_mdpo_plugin.search_indexes import TranslationsSearchPatcher
from mkdocs_mdpo_plugin.translations import Translation, Translations


Expand Down Expand Up @@ -97,7 +99,7 @@ def on_files(self, files, config):
dest_path = Template(
self.config['dest_filename_template'],
).render(**context)
src_path = f"{dest_path.rstrip('.html')}.md"
src_path = f"{removesuffix(dest_path, '.html')}.md"

self.translations.files[file.src_path][language] = (
os.path.join(
Expand Down Expand Up @@ -329,6 +331,7 @@ def on_page_markdown(self, markdown, page, config, files):
_translated_entries_msgstrs.append(entry.msgstr)
_translated_entries_msgids.append(entry.msgid)

# create translation object
translation = Translation(
language,
po,
Expand All @@ -341,14 +344,15 @@ def on_page_markdown(self, markdown, page, config, files):
self.translations.current = translation

# change file url
url = new_page.file.url.rstrip('.md') + '.html'
url = removesuffix(new_page.file.url, '.md') + '.html'
if config['use_directory_urls']:
url = url.rstrip('index.html')
url = removesuffix(url, 'index.html')
new_page.file.url = url

self.translations.nav[page.title][language] = [
translated_page_title, new_page.file.url,
]

mkdocs.commands.build._populate_page(
new_page,
config,
Expand Down Expand Up @@ -391,13 +395,40 @@ def on_post_page(self, output, page, config):
elif not render_path.endswith('.html'):
render_path += '.html'

# save locations of records with languages for search indexes usage
location = os.path.relpath(
removesuffix(render_path, 'index.html'),
config['site_dir'],
) + '/'
self.translations.locations[location] = page.file._mdpo_language

with open(render_path, 'w') as f:
f.write(output)
return output

def on_post_build(self, config):
self.translations.tempdir.cleanup()

if not self.config['cross_language_search']:
# cross language search is disabled, so build indexes
# for each language and patch the 'site_dir' directory
search_patcher = TranslationsSearchPatcher(
config['site_dir'],
self.config['languages'],
self.config['default_language'],
# use mkdocs 'search' plugin if the theme
# has not its own implementation
(
config['theme'].name
if config['theme'].name
in TranslationsSearchPatcher.supported_themes
else 'mkdocs'
),
self.translations.locations,
)
search_patcher.patch_site_dir()

# save PO files
for translations in self.translations.all.values():
for translation in translations:
translation.po.save(translation.po_filepath)
Expand Down Expand Up @@ -468,14 +499,14 @@ def on_post_build(self, config):
# reset mkdocs build instance
MkdocsBuild._instance = None

def on_serve(self, server, builder, **kwargs):
def on_serve(self, *args, **kwargs): # pragma: no cover
"""When serving with livereload server, prevent a infinite loop
if the user edits a PO file if is placed inside documentation
directory.
"""
if '..' not in self.config['locale_dir']:
sys.stderr.write(
'ERROR - '
'ERROR [mdpo] - '
"You need to set 'locale_dir' configuration setting"
' pointing to a directory placed outside'
" the documentation directory ('docs_dir') in order to"
Expand Down
Loading

0 comments on commit c0b2f6e

Please sign in to comment.