Esempio n. 1
0
def test_include_comment_without_value(command, command_aliases):
    expected_msg = (
        'You need to specify a message for the comment to include with the'
        f' command \'{command}\'.')
    with pytest.raises(ValueError, match=expected_msg):
        markdown_to_pofile(
            f'<!-- {command} -->',
            command_aliases=command_aliases,
        )
Esempio n. 2
0
def test_context_without_value(command, command_aliases):
    expected_msg = (
        'You need to specify a string for the context with the command'
        f' \'{command}\'.')
    with pytest.raises(ValueError, match=expected_msg):
        markdown_to_pofile(
            f'<!-- {command} -->',
            command_aliases=command_aliases,
        )
Esempio n. 3
0
def test_translator_command_without_value(command, command_aliases):
    content = f'''<!-- {command} -->
Some text that needs to be clarified
'''
    expected_msg = (
        'You need to specify a string for the extracted comment with the'
        f' command \'{command}\'.')
    with pytest.raises(ValueError, match=expected_msg):
        markdown_to_pofile(content, command_aliases=command_aliases)
Esempio n. 4
0
def test_extract_save(filename):
    filepath = os.path.join(EXAMPLES['plaintext']['dirpath'], filename)

    with tempfile.NamedTemporaryFile(suffix='.po') as save_file:

        markdown_to_pofile(
            filepath,
            plaintext=True,
            save=True,
            po_filepath=save_file.name,
            location=False,
        )

        with open(f'{filepath}.expect.po') as expect_file:
            assert save_file.read().decode('utf-8') == expect_file.read()
Esempio n. 5
0
def test_tcomment_obsolete_msgstr_fallback_with_found_tcomment(
    tmp_file,
    default_msgstr,
):
    """If a translated message is marked as obsolete and has a translator
    comment, and his msgid is found in markdown content and the found message
    has a translator comment, must be directly translated and the tcomment
    of the obsolete one is ignored, preserving the translator comment of the
    found message. This behaviour is preferred over default msgstr using
    ``msgstr`` parameter.
    """
    markdown_content = \
        '<!-- mdpo-translator Comment for translator -->\n# Hello'
    pofile_content = ('#\nmsgid ""\nmsgstr ""\n\n#. Other comment\n'
                      '#~ msgid "Hello"\n#~ msgstr "Hola"\n')

    with tmp_file(pofile_content, '.po') as po_filepath:
        expected_output = '''#
msgid ""
msgstr ""

#. Comment for translator
msgid "Hello"
msgstr "Hola"
'''

        output = str(
            markdown_to_pofile(
                markdown_content,
                po_filepath=po_filepath,
                msgstr=default_msgstr,
            ), )
    assert output == expected_output
Esempio n. 6
0
def test_obsolete_with_msgctxt_matching_msgstr_fallback(tmp_file):
    """If a translated message with msgctxt is marked as obsolete and his msgid
    with the same msgctxt is found in markdown content, must be directly
    translated.
    """
    markdown_content = '<!-- mdpo-context Context -->\n# Hello'
    pofile_content = ('#\nmsgid ""\nmsgstr ""\n\n#~ msgctxt "Context"\n'
                      '#~ msgid "Hello"\n#~ msgstr "Hola"\n')

    with tmp_file(pofile_content, '.po') as po_filepath:
        expected_output = '''#
msgid ""
msgstr ""

msgctxt "Context"
msgid "Hello"
msgstr "Hola"
'''

        output = str(
            markdown_to_pofile(
                markdown_content,
                po_filepath=po_filepath,
            ), )
    assert output == expected_output
Esempio n. 7
0
def test_obsolete_with_msgctxt_not_matching_msgstr_fallback(tmp_file):
    """If a translated message with msgctxt is marked as obsolete and his msgid
    with different msgctxt is found in markdown content, should not be
    translated and the other message (with msgctxt not found) must be marked
    as obsolete (if ``mark_not_found_as_obsolete`` is ``True``).
    """
    markdown_content = '<!-- mdpo-context First context -->\n# Hello'
    pofile_content = ('#\nmsgid ""\nmsgstr ""\n\n#~ msgctxt "Second context"\n'
                      '#~ msgid "Hello"\n#~ msgstr "Hola"\n')

    with tmp_file(pofile_content, '.po') as po_filepath:
        expected_output = '''#
msgid ""
msgstr ""

msgctxt "First context"
msgid "Hello"
msgstr ""

#~ msgctxt "Second context"
#~ msgid "Hello"
#~ msgstr "Hola"
'''

        output = str(
            markdown_to_pofile(
                markdown_content,
                po_filepath=po_filepath,
            ), )
    assert output == expected_output
Esempio n. 8
0
def test_fuzzy_obsolete_msgstr_fallback(tmp_file, default_msgstr):
    """If a translated message is marked as obsolete and fuzzy, and his msgid
    is found in markdown content, must be directly translated but needs to be
    marked as fuzzy like the obsolete one. This behaviour is preferred
    over default msgstr using ``msgstr`` parameter.
    """
    markdown_content = '# Hello'
    pofile_content = ('#\nmsgid ""\nmsgstr ""\n\n#, fuzzy\n'
                      '#~ msgid "Hello"\n#~ msgstr "Hola"\n')

    with tmp_file(pofile_content, '.po') as po_filepath:
        expected_output = '''#
msgid ""
msgstr ""

#, fuzzy
msgid "Hello"
msgstr "Hola"
'''

        output = str(
            markdown_to_pofile(
                markdown_content,
                po_filepath=po_filepath,
                msgstr=default_msgstr,
            ), )
    assert output == expected_output
Esempio n. 9
0
def test_msgid_event():
    def dont_save_hate_msgid(self, msgid, *args):
        if msgid == 'hate':
            return False

    content = '''<!-- mdpo-disable-next-line -->
hate

love

equilibrium
'''

    expected_output = '''#
msgid ""
msgstr ""

msgid "equilibrium"
msgstr ""
'''

    output = markdown_to_pofile(
        content,
        events={
            'msgid': dont_save_hate_msgid,
        },
    )

    assert str(output) == expected_output
Esempio n. 10
0
def test_location_file_independent():
    """Location block counters should be reset for each file."""

    with tempfile.TemporaryDirectory() as filesdir:
        foo_md_filepath = os.path.join(filesdir, 'foo.md')
        bar_md_filepath = os.path.join(filesdir, 'bar.md')
        with open(foo_md_filepath, 'w') as f:
            f.write('# Foo\n')
        with open(bar_md_filepath, 'w') as f:
            f.write('# Bar\n')

        expected_output = f'''#
msgid ""
msgstr ""

#: {bar_md_filepath}:block 1 (header)
msgid "Bar"
msgstr ""

#: {foo_md_filepath}:block 1 (header)
msgid "Foo"
msgstr ""
'''

        output = markdown_to_pofile(f'{filesdir}{os.sep}*.md')

    assert output == expected_output
Esempio n. 11
0
def test_location_code_blocks(tmp_file):
    markdown_content = '''<!-- mdpo-include-codeblock -->
```python
foo = "bar"
```

```javascript
var foo = "bar";
```

<!-- mdpo-include-codeblock -->

    int foo;

- Foo
   - Bar

      <!-- mdpo-include-codeblock -->
      ```python
      code_which_must_be_included = True
      ```

> <!-- mdpo-include-codeblock -->
> ```javascript
> var codeWhichMustBeIncluded = true;
> ```
'''

    with tmp_file(markdown_content, '.md') as md_filepath:
        expected_output = f'''#
msgid ""
msgstr ""

#: {md_filepath}:block 2 (code)
msgid "foo = \\"bar\\"\\n"
msgstr ""

#: {md_filepath}:block 5 (code)
msgid "int foo;\\n"
msgstr ""

#: {md_filepath}:block 6 (unordered list)
msgid "Foo"
msgstr ""

#: {md_filepath}:block 6 (unordered list)
msgid "Bar"
msgstr ""

#: {md_filepath}:block 6 (unordered list)
msgid "code_which_must_be_included = True\\n"
msgstr ""

#: {md_filepath}:block 7 (quote)
msgid "var codeWhichMustBeIncluded = true;\\n"
msgstr ""
'''

        output = markdown_to_pofile(md_filepath)
    assert str(output) == expected_output
Esempio n. 12
0
def test_include_comment(command, command_aliases):
    content = f'''<!-- {command} This comment must be included -->
Some text that needs to be clarified

Some text without comment
'''
    pofile = markdown_to_pofile(content, command_aliases=command_aliases)
    assert pofile == '''#
Esempio n. 13
0
def test_include_comment_with_extracted():
    content = '''<!-- mdpo-translator Comment for translator in comment -->
<!-- mdpo-include This comment must be included -->
Some text that needs to be clarified

Some text without comment
'''
    pofile = markdown_to_pofile(content)
    assert pofile == '''#
Esempio n. 14
0
def test_translator_command_paragraph(command, command_aliases):
    content = f'''<!-- {command} This is a comment for a translator -->
Some text that needs to be clarified

Some text without comment
'''

    pofile = markdown_to_pofile(content, command_aliases=command_aliases)
    assert pofile == '''#
Esempio n. 15
0
def test_context(command, command_aliases):
    content = f'''<!-- mdpo-context month -->
May

<!-- {command} might -->
May
'''
    pofile = markdown_to_pofile(content, command_aliases=command_aliases)
    assert pofile == '''#
Esempio n. 16
0
def test_location_headers(tmp_file):
    markdown_content = '''# Foo 1

- # Foo 2
   - # Foo 3

1. # Foo 4
   1. Foo 5

> # Foo 6
>
> - # Foo 7
>
> 1. # Foo 8
'''

    with tmp_file(markdown_content, '.md') as md_filepath:
        expected_output = f'''#
msgid ""
msgstr ""

#: {md_filepath}:block 1 (header)
msgid "Foo 1"
msgstr ""

#: {md_filepath}:block 2 (unordered list)
msgid "Foo 2"
msgstr ""

#: {md_filepath}:block 2 (unordered list)
msgid "Foo 3"
msgstr ""

#: {md_filepath}:block 3 (ordered list)
msgid "Foo 4"
msgstr ""

#: {md_filepath}:block 3 (ordered list)
msgid "Foo 5"
msgstr ""

#: {md_filepath}:block 4 (quote)
msgid "Foo 6"
msgstr ""

#: {md_filepath}:block 4 (quote)
msgid "Foo 7"
msgstr ""

#: {md_filepath}:block 4 (quote)
msgid "Foo 8"
msgstr ""
'''

        output = markdown_to_pofile(md_filepath)
    assert str(output) == expected_output
Esempio n. 17
0
def test_x_headers_included():
    markdown_content = '# Foo\n'

    extensions = DEFAULT_MD4C_GENERIC_PARSER_EXTENSIONS + ['underline']
    pofile = markdown_to_pofile(
        markdown_content,
        xheaders=True,
        plaintext=False,
        extensions=extensions,
    )
    assert pofile == '''#
Esempio n. 18
0
def test_extract_underline(filename):
    filepath = os.path.join(EXAMPLES['underline']['dirpath'], filename)
    pofile = markdown_to_pofile(
        filepath,
        plaintext=False,
        extensions=DEFAULT_MD4C_GENERIC_PARSER_EXTENSIONS + ['underline'],
        location=False,
    )

    with open(f'{filepath}.expect.po') as expect_file:
        assert str(pofile) == expect_file.read()
Esempio n. 19
0
def test_enter_leave_span_event(abort_event, expected_msgid):
    content = 'Hello `with` codespan'

    pofile = markdown_to_pofile(
        content,
        events={
            'enter_span': lambda *_: not abort_event,
            'leave_span': lambda *_: not abort_event,
        },
    )

    assert str(pofile).splitlines()[4].split('"')[1] == expected_msgid
Esempio n. 20
0
def test_disable_enable(commands, command_aliases):
    disable_command, enable_command = (commands['disable'], commands['enable'])

    content = f'''This must be included.

<!-- {disable_command} -->
This must be ignored

<!-- {enable_command} -->
This must be included also.
'''
    pofile = markdown_to_pofile(content, command_aliases=command_aliases)
    assert pofile == '''#
Esempio n. 21
0
def test_include_indented_codeblock(command, command_aliases):
    content = f'''
<!-- {command} -->

    var hello = "world";
    var hola = "mundo";

This must be included also.

    var thisCodeMustNotBeIncluded = undefined;
'''
    pofile = markdown_to_pofile(content, command_aliases=command_aliases)
    assert pofile == '''#
Esempio n. 22
0
def test_location_quotes(tmp_file):
    markdown_content = '''> Foo 1

- Foo 2
- Foo 3
   > Foo 4

1. # Foo 5

> > Foo 6
>
> > Foo 7
'''

    with tmp_file(markdown_content, '.md') as md_filepath:
        expected_output = f'''#
msgid ""
msgstr ""

#: {md_filepath}:block 1 (quote)
msgid "Foo 1"
msgstr ""

#: {md_filepath}:block 2 (unordered list)
msgid "Foo 2"
msgstr ""

#: {md_filepath}:block 2 (unordered list)
msgid "Foo 3"
msgstr ""

#: {md_filepath}:block 3 (quote)
msgid "Foo 4"
msgstr ""

#: {md_filepath}:block 4 (ordered list)
msgid "Foo 5"
msgstr ""

#: {md_filepath}:block 5 (quote)
msgid "Foo 6"
msgstr ""

#: {md_filepath}:block 5 (quote)
msgid "Foo 7"
msgstr ""
'''

        output = markdown_to_pofile(md_filepath)

    assert str(output) == expected_output
Esempio n. 23
0
def test_disable_enable_raw_inline():
    # enable command is part of the last item in the list
    content = '''This must be included.

<!-- mdpo-disable -->
- `config.development.yml`
- `config.staging.yml`
- `config.production.yml`
<!-- mdpo-enable -->

This must be included also.
'''
    pofile = markdown_to_pofile(content)
    assert pofile == '''#
Esempio n. 24
0
def test_disable_next_block(command, command_aliases):
    content = f'''<!-- mdpo-disable -->
This must be ignored.

<!-- mdpo-enable -->
This must be included.

<!-- {command} -->
This must be ignored also.

This must be included also.
'''
    pofile = markdown_to_pofile(content, command_aliases=command_aliases)
    assert pofile == '''#
Esempio n. 25
0
def test_include_fenced_codeblock():
    content = '''
<!-- mdpo-include-codeblock -->
```javascript
var hello = "world";
var hola = "mundo";
```

This must be included also.

```javascript
var thisCodeMustNotBeIncluded = undefined;
```
'''
    pofile = markdown_to_pofile(content)
    assert pofile == '''#
Esempio n. 26
0
def test_obsolete_not_equal_found(
    mark_not_found_as_obsolete,
    expected_output,
    tmp_file,
):
    markdown_content = '# Foo'
    pofile_content = '#\nmsgid ""\nmsgstr ""\n\nmsgid "Bar"\nmsgstr ""\n'

    with tmp_file(pofile_content, '.po') as po_filepath:
        output = str(
            markdown_to_pofile(
                markdown_content,
                po_filepath=po_filepath,
                mark_not_found_as_obsolete=mark_not_found_as_obsolete,
                location=False,
            ), )
    assert output == expected_output
Esempio n. 27
0
def test_link_reference_event():
    def process_footnotes(self, target, href, title):
        if re.match(r'^\^\d', target):
            return False

    content = '''This is a footnote[^1]. This is another[^2].

Here is a [link reference][foo].

[^1]: This is a footnote content.

[^2]: This is another footnote content.

[foo]: https://github.com/mondeja/mdpo
'''

    expected_output = '''#
msgid ""
msgstr ""

msgid "This is a footnote[^1]. This is another[^2]."
msgstr ""

msgid "Here is a [link reference][foo]."
msgstr ""

msgid "[^1]: This is a footnote content."
msgstr ""

msgid "[^2]: This is another footnote content."
msgstr ""

#, fuzzy
msgid "[foo]: https://github.com/mondeja/mdpo"
msgstr "[foo]: https://github.com/mondeja/mdpo"
'''

    output = markdown_to_pofile(
        content,
        events={
            'link_reference': process_footnotes,
        },
    )

    assert str(output) == expected_output
Esempio n. 28
0
def test_disable_next_codeblock(command, command_aliases):
    content = f'''
<!-- {command} -->

    var hello = "world";
    var hola = "mundo";

This must be included.

```javascript
var thisCodeMustBeIncluded = undefined;
```
'''
    pofile = markdown_to_pofile(
        content,
        command_aliases=command_aliases,
        include_codeblocks=True,
    )
    assert pofile == '''#
Esempio n. 29
0
def test_location_html(tmp_file):
    markdown_content = '''<!-- a comment -->

<!-- another comment -->

paragraph
'''

    with tmp_file(markdown_content, '.md') as md_filepath:
        expected_output = f'''#
msgid ""
msgstr ""

#: {md_filepath}:block 3 (paragraph)
msgid "paragraph"
msgstr ""
'''

        output = markdown_to_pofile(md_filepath)
    assert str(output) == expected_output
Esempio n. 30
0
def test_enable_next_block(command, command_aliases):
    content = f'''This must be included.

<!-- mdpo-disable -->

This must be ignored.

<!-- {command} -->
This must be included also.

This must be ignored also.

<!-- {command} -->
# This header must be included

Other line that must be ignored.

<!-- mdpo-enable -->

The last line also must be included.
'''

    pofile = markdown_to_pofile(content, command_aliases=command_aliases)
    assert pofile == '''#