Skip to content

Commit

Permalink
Allow to pass languages with -l es fr de to md2po2md (#224)
Browse files Browse the repository at this point in the history
* Allow to pass languages with `-l es fr de` to `md2po2md`
  • Loading branch information
mondeja authored Feb 28, 2022
1 parent d7ca502 commit c6b9ae0
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 29 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.3.84
current_version = 0.3.85

[bumpversion:file:mdpo/__init__.py]

Expand Down
25 changes: 9 additions & 16 deletions docs/pre-commit-hooks.rst
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ so you don't need to specify them.
.. code-block:: yaml
- repo: https:/mondeja/mdpo
rev: v0.3.84
rev: v0.3.85
hooks:
- id: md2po
args:
Expand All @@ -32,7 +32,7 @@ so you don't need to specify them.
.. code-block:: yaml
- repo: https:/mondeja/mdpo
rev: v0.3.84
rev: v0.3.85
hooks:
- id: md2po
files: ^README\.md
Expand All @@ -53,7 +53,7 @@ po2md
.. code-block:: yaml
- repo: https:/mondeja/mdpo
rev: v0.3.84
rev: v0.3.85
hooks:
- id: po2md
args:
Expand All @@ -68,7 +68,7 @@ po2md
.. code-block:: yaml
- repo: https:/mondeja/mdpo
rev: v0.3.84
rev: v0.3.85
hooks:
- id: po2md
files: ^README\.md
Expand All @@ -91,14 +91,13 @@ md2po2md
.. code-block:: yaml
- repo: https:/mondeja/mdpo
rev: v0.3.84
rev: v0.3.85
hooks:
- id: md2po2md
args:
- README.md
- -l
- es
- -l
- fr
- -o
- locale/{lang}
Expand All @@ -108,17 +107,11 @@ md2po2md
.. code-block:: yaml
- repo: https:/mondeja/mdpo
rev: v0.3.84
rev: v0.3.85
hooks:
- id: md2po2md
files: ^README\.md
args:
- -l
- es
- -l
- fr
- -o
- locale/{lang}
args: ['-l', 'es', 'fr', '-o', 'locale/{lang}']
.. seealso::
* :ref:`md2po2md CLI<cli:md2po2md>`
Expand All @@ -133,7 +126,7 @@ mdpo2html
.. code-block:: yaml
- repo: https:/mondeja/mdpo
rev: v0.3.84
rev: v0.3.85
hooks:
- id: mdpo2html
args:
Expand All @@ -148,7 +141,7 @@ mdpo2html
.. code-block:: yaml
- repo: https:/mondeja/mdpo
rev: v0.3.84
rev: v0.3.85
hooks:
- id: mdpo2html
files: ^README\.html
Expand Down
8 changes: 4 additions & 4 deletions docs/tutorial.rst
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@ Just use :ref:`md2po2md CLI<cli:md2po2md>`:

.. code-block:: bash
md2po2md README.md -l es -l fr -o "locale/{lang}"
md2po2md README.md -l es fr -o "locale/{lang}"
Define the languages to translate into using the ``-l`` option.
Define the languages to translate into using the ``--lang``/``-l`` option.

You also can use the next snippet to include links for the translations:

Expand All @@ -94,10 +94,10 @@ This will be the output after the previous command:
├── locale
│   ├── es
│   | ├── README.md
│   | └── readme.po 
│   | └── readme.po
| └── fr
│   ├── README.md
│   └── readme.po 
│   └── readme.po
└── README.md
.. seealso::
Expand Down
2 changes: 1 addition & 1 deletion mdpo/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

__description__ = ('Markdown files translation using PO files.')
__title__ = 'mdpo'
__version__ = '0.3.84'
__version__ = '0.3.85'
__all__ = [
'__description__',
'__title__',
Expand Down
11 changes: 9 additions & 2 deletions mdpo/md2po2md/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"""

import argparse
import itertools
import sys

from mdpo.cli import (
Expand All @@ -18,6 +19,7 @@
add_nolocation_option,
add_pre_commit_option,
add_wrapwidth_argument,
cli_codespan,
parse_command_aliases_cli_arguments,
)
from mdpo.context import environ
Expand All @@ -40,9 +42,12 @@ def build_parser():
' If not provided, will be read from STDIN.',
)
parser.add_argument(
'-l', '--lang', dest='langs', default=[], action='append',
'-l', '--lang', dest='langs', default=[], nargs='*',
action='append',
help='Language codes used to create the output directories.'
' This argument can be passed multiple times.',
' This argument can be passed multiple times. Also, all'
' languages can be defined after this argument with'
f" {cli_codespan('-l es_ES fr_FR de_DE')}.",
metavar='LANG', required=True,
)

Expand Down Expand Up @@ -100,6 +105,8 @@ def parse_options(args=[]):
sys.exit(1)
opts.input_paths_glob = input_paths_glob

opts.langs = set(itertools.chain(*opts.langs)) # flatten

if opts.extensions is None:
opts.extensions = DEFAULT_MD4C_GENERIC_PARSER_EXTENSIONS

Expand Down
6 changes: 3 additions & 3 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[metadata]
name = mdpo
version = 0.3.84
version = 0.3.85
description = Markdown files translation using PO files.
long_description = file: README.md
long_description_content_type = text/markdown
Expand Down Expand Up @@ -135,7 +135,7 @@ sections = STDLIB,THIRDPARTY,FIRSTPARTY,TESTS,LOCALFOLDER

[build_sphinx]
project = mdpo
version = 0.3.84
release = 0.3.84
version = 0.3.85
release = 0.3.85
source-dir = docs
build-dir = docs/_build
36 changes: 34 additions & 2 deletions test/test_unit/test_md2po2md/test_md2po2md_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

@pytest.mark.parametrize('output_arg', ('-o', '--output'))
@pytest.mark.parametrize('langs_arg', ('-l', '--lang'))
@pytest.mark.parametrize('all_langs_in_same_arg', (True, False))
@pytest.mark.parametrize(
(
'langs',
Expand Down Expand Up @@ -120,11 +121,36 @@
FileNotFoundError,
id='no-files-matching-input-glob',
),
pytest.param(
('es', 'fr', 'de_DE'),
'README.md',
'locale/{lang}',
{'README.md': 'Foo\n\nBar\n'},
{
'locale/es/README.md.po': (
'#\nmsgid ""\nmsgstr ""\n\nmsgid "Foo"\nmsgstr ""\n\n'
'msgid "Bar"\nmsgstr ""\n'
),
'locale/es/README.md': 'Foo\n\nBar\n',
'locale/fr/README.md.po': (
'#\nmsgid ""\nmsgstr ""\n\nmsgid "Foo"\nmsgstr ""\n\n'
'msgid "Bar"\nmsgstr ""\n'
),
'locale/fr/README.md': 'Foo\n\nBar\n',
'locale/de_DE/README.md.po': (
'#\nmsgid ""\nmsgstr ""\n\nmsgid "Foo"\nmsgstr ""\n\n'
'msgid "Bar"\nmsgstr ""\n'
),
'locale/de_DE/README.md': 'Foo\n\nBar\n',
},
id='README-es,fr-locale/{lang}',
),
),
)
def test_md2po2md_arguments(
output_arg,
langs_arg,
all_langs_in_same_arg,
langs,
input_paths_glob,
output,
Expand All @@ -151,8 +177,14 @@ def cleanup():
os.path.join(basedir, input_paths_glob),
output_arg, os.path.join(basedir, output),
]
for lang in langs:
cmd.extend([langs_arg, lang])

# if all languages are passed in the same `--lang`/`-l` argument
if all_langs_in_same_arg:
cmd.extend([langs_arg, *langs])
else:
for lang in langs:
cmd.extend([langs_arg, lang])

cmd.append('--no-location')

if hasattr(expected_files_content, '__traceback__'):
Expand Down

0 comments on commit c6b9ae0

Please sign in to comment.