Skip to content

Commit

Permalink
Preserve HTML blocks in po2md output (#246)
Browse files Browse the repository at this point in the history
* Preserve HTML blocks in po2md output
  • Loading branch information
mondeja committed Sep 20, 2022
1 parent a32dce6 commit eedb0b5
Show file tree
Hide file tree
Showing 10 changed files with 70 additions and 15 deletions.
1 change: 1 addition & 0 deletions docs/cli.rst
Expand Up @@ -51,6 +51,7 @@ The output produced by :ref:`cli:po2md` is compatible with the following
{
"no-blanks-blockquote": false,
"no-bare-urls": false,
"no-inline-html": false,
"ul-indent": {
"indent": 3
}
Expand Down
16 changes: 8 additions & 8 deletions docs/pre-commit-hooks.rst
Expand Up @@ -19,7 +19,7 @@ so you don't need to specify them.
.. code-block:: yaml
- repo: https://github.com/mondeja/mdpo
rev: v1.0.2
rev: v1.0.3
hooks:
- id: md2po
args:
Expand All @@ -32,7 +32,7 @@ so you don't need to specify them.
.. code-block:: yaml
- repo: https://github.com/mondeja/mdpo
rev: v1.0.2
rev: v1.0.3
hooks:
- id: md2po
files: ^README\.md
Expand All @@ -53,7 +53,7 @@ po2md
.. code-block:: yaml
- repo: https://github.com/mondeja/mdpo
rev: v1.0.2
rev: v1.0.3
hooks:
- id: po2md
args:
Expand All @@ -68,7 +68,7 @@ po2md
.. code-block:: yaml
- repo: https://github.com/mondeja/mdpo
rev: v1.0.2
rev: v1.0.3
hooks:
- id: po2md
files: ^README\.md
Expand All @@ -91,7 +91,7 @@ md2po2md
.. code-block:: yaml
- repo: https://github.com/mondeja/mdpo
rev: v1.0.2
rev: v1.0.3
hooks:
- id: md2po2md
args:
Expand All @@ -107,7 +107,7 @@ md2po2md
.. code-block:: yaml
- repo: https://github.com/mondeja/mdpo
rev: v1.0.2
rev: v1.0.3
hooks:
- id: md2po2md
files: ^README\.md
Expand All @@ -126,7 +126,7 @@ mdpo2html
.. code-block:: yaml
- repo: https://github.com/mondeja/mdpo
rev: v1.0.2
rev: v1.0.3
hooks:
- id: mdpo2html
args:
Expand All @@ -141,7 +141,7 @@ mdpo2html
.. code-block:: yaml
- repo: https://github.com/mondeja/mdpo
rev: v1.0.2
rev: v1.0.3
hooks:
- id: mdpo2html
files: ^README\.html
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
@@ -1,6 +1,6 @@
[tool.poetry]
name = "mdpo"
version = "1.0.2"
version = "1.0.3"
description = "Markdown files translation using PO files."
readme = "README.md"
license = "BSD-3-Clause"
Expand Down
24 changes: 18 additions & 6 deletions src/mdpo/po2md/__init__.py
Expand Up @@ -247,7 +247,10 @@ def __init__(self, pofiles, ignore=frozenset(), po_encoding=None, **kwargs):
self._leavespan_replacer[md4c.SpanType.WIKILINK.value] = \
self.wikilink_end_string

self._inside_htmlblock = False
self._inside_htmlblock = [
False, # the parser is inside an HTML block
None, # a mdpo command has been found in the HTML block
]
self._inside_codeblock = False
self._inside_indented_codeblock = False
self._inside_hblock = False
Expand Down Expand Up @@ -326,7 +329,9 @@ def command(self, mdpo_command, comment, original_command):
def _process_command(self, text):
original_command, comment = parse_mdpo_html_command(text)
if original_command is None:
return
return False

self._inside_htmlblock[1] = True

try:
command = self.command_aliases[original_command]
Expand All @@ -336,6 +341,8 @@ def _process_command(self, text):
# process solved command
self.command(command, comment, original_command)

return True

def _escape_translation(self, text):
if self._aimg_title_inside_current_msgid:
# escape '"' characters inside links and image titles
Expand Down Expand Up @@ -561,7 +568,7 @@ def enter_block(self, block, details):
if self._current_list_type and not self._inside_quoteblock:
self._save_current_line()
elif block is md4c.BlockType.HTML:
self._inside_htmlblock = True
self._inside_htmlblock = [True, False]

def leave_block(self, block, details):
# raise 'leave_block' event
Expand Down Expand Up @@ -681,7 +688,11 @@ def leave_block(self, block, details):
self._save_current_line()
self._current_thead_aligns = []
elif block is md4c.BlockType.HTML:
self._inside_htmlblock = False
if not self._inside_htmlblock[1]:
self.current_line += '\n'
else:
self.current_line = self.current_line.rstrip('\n')
self._inside_htmlblock[0] = False

def enter_span(self, span, details):
# raise 'enter_span' event
Expand Down Expand Up @@ -851,7 +862,7 @@ def text(self, block, text):
):
return

if not self._inside_htmlblock:
if not self._inside_htmlblock[0]:
if not self._inside_codeblock:
if self._inside_liblock and text == '\n':
text = ' '
Expand Down Expand Up @@ -903,7 +914,8 @@ def text(self, block, text):
self.current_line += indent
self.current_msgid += text
else:
self._process_command(text)
if not self._process_command(text):
self.current_line += text

def _append_link_references(self):
if self.link_references:
Expand Down
@@ -0,0 +1,7 @@
Some text

<div id="my-special-div">
content
</div>

More text
@@ -0,0 +1,9 @@
#
msgid ""
msgstr ""

msgid "Some text"
msgstr ""

msgid "More text"
msgstr ""
3 changes: 3 additions & 0 deletions tests/test_unit/test_po2md/.markdownlint-cli2.yaml
Expand Up @@ -41,3 +41,6 @@ config:
# Link fragments could not be valid in certain tests:
# https://github.com/DavidAnson/markdownlint/blob/main/doc/Rules.md#md051
link-fragments: false

# HTML blocks are preserved in output
no-inline-html: false
7 changes: 7 additions & 0 deletions tests/test_unit/test_po2md/translate-examples/html.md
@@ -0,0 +1,7 @@
Some text

<div id="my-special-div">
content
</div>

More text
@@ -0,0 +1,7 @@
Algo de texto

<div id="my-special-div">
content
</div>

Más texto
9 changes: 9 additions & 0 deletions tests/test_unit/test_po2md/translate-examples/html.po
@@ -0,0 +1,9 @@
#
msgid ""
msgstr ""

msgid "Some text"
msgstr "Algo de texto"

msgid "More text"
msgstr "Más texto"

0 comments on commit eedb0b5

Please sign in to comment.