Skip to content

Commit

Permalink
Simplify mdpo commands stripping
Browse files Browse the repository at this point in the history
  • Loading branch information
mondeja committed Feb 27, 2022
1 parent ebd6809 commit c563956
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 16 deletions.
23 changes: 23 additions & 0 deletions docs/locale/es/_compendium.po
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,26 @@ msgstr "Instalar"

msgid "[mkdocs-material]: https://squidfunk.github.io/mkdocs-material/"
msgstr "[mkdocs-material]: https://squidfunk.github.io/mkdocs-material/"

msgid "Tip"
msgstr "Consejo"

msgid "See [Code blocks extraction][mdpo-codeblocks-extraction]."
msgstr ""
"Ver [Extracción de bloques de código (inglés)][mdpo-codeblocks-extraction]."

msgid ""
"Code blocks are not translated by default, but you can include a [`<!-- "
"mdpo-include-codeblock -->`][mdpo-codeblocks-extraction] HTML comment before"
" each code block that you want translate."
msgstr ""
"Los bloques de código no son traducidos por defecto, pero puedes incluir un "
"comentario HTML [`<!-- mdpo-include-codeblock -->`][mdpo-codeblocks-"
"extraction] antes de cada bloque de código que quieres traducir."

msgid ""
"[mdpo-codeblocks-extraction]: "
"https://mdpo.readthedocs.io/en/master/commands.html#code-blocks-extraction"
msgstr ""
"[mdpo-codeblocks-extraction]: "
"https://mdpo.readthedocs.io/en/master/commands.html#code-blocks-extraction"
3 changes: 0 additions & 3 deletions docs/locale/es/index.md.po
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,6 @@ msgstr ""
"[xml.etree.ElementTree]: "
"https://docs.python.org/3/library/xml.etree.elementtree.html"

msgid "Tip"
msgstr "Consejo"

msgid ""
"See also [the traditional approach for Markdown translations][mdpo-"
"traditional-approach-docs] and [mdpo approach][mdpo-approach-docs]."
Expand Down
2 changes: 1 addition & 1 deletion docs/src/assets/javascripts/extensions-support.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ var removePyMdownxSnippetsMarkdownDemoEscapeChar = function() {
var markdownCode = pyMdownSnippetsSection.nextElementSibling.children[5].children[0].children[0];
for (let i=0; i<markdownCode.children.length; i++) {
let child = markdownCode.children[i];
if (child.tagName === 'CODE') {
if (child.tagName === 'CODE' && child.innerText.includes('--8<--')) {
child.innerHTML = child.innerHTML.replace('\\', '');
break;
}
Expand Down
5 changes: 5 additions & 0 deletions docs/src/extensions-support/official.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,10 @@ Code blocks are not translated by default, but you can include a
[`<!-- mdpo-include-codeblock -->`](https://mdpo.readthedocs.io/en/master/commands.html#code-blocks-extraction)
HTML comment before each code block that you want translate.

!!! tip

See [Code blocks extraction][mdpo-codeblocks-extraction].

=== "Output"

```javascript
Expand Down Expand Up @@ -247,3 +251,4 @@ You must always let one newline between each footnote content:
```

[officially-supported-extensions]: https://python-markdown.github.io/extensions/#officially-supported-extensions
[mdpo-codeblocks-extraction]: https://mdpo.readthedocs.io/en/master/commands.html#code-blocks-extraction
5 changes: 5 additions & 0 deletions docs/src/extensions-support/pymdownx.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ Code blocks are not translated by default, but you can include a
[`<!-- mdpo-include-codeblock -->`](https://mdpo.readthedocs.io/en/master/commands.html#code-blocks-extraction)
HTML comment before each code block that you want translate.

!!! tip

See [Code blocks extraction][mdpo-codeblocks-extraction].

=== "Output"

```python
Expand Down Expand Up @@ -411,3 +415,4 @@ You must let one blank line between each progress bar.
```

[pymdown-extensions]: https://facelessuser.github.io/pymdown-extensions/extensions
[mdpo-codeblocks-extraction]: https://mdpo.readthedocs.io/en/master/commands.html#code-blocks-extraction
21 changes: 9 additions & 12 deletions mkdocs_mdpo_plugin/mdpo_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,26 @@
from mdpo.command import COMMAND_SEARCH_REGEX


COMMAND_SEARCH_REGEX_ESCAPER = re.compile(
(
r'\\(' + COMMAND_SEARCH_REGEX[:20] + ')('
+ COMMAND_SEARCH_REGEX[20:38] + '?='
+ COMMAND_SEARCH_REGEX[38:] + ')'
),
re.M,
)
STRIP_COMMAND_REGEX = re.compile(r'[^\\]' + COMMAND_SEARCH_REGEX)


def remove_mdpo_commands_preserving_escaped(text):
# preserve escaped commands
text = re.sub(
COMMAND_SEARCH_REGEX_ESCAPER,
r'\g<1>0-\g<2>',
r'(\\<!--\s*mdpo)',
r'\g<1>-0',
text,
)

text = re.sub(
COMMAND_SEARCH_REGEX,
STRIP_COMMAND_REGEX,
'',
text,
)

# restore escaped commands
return text.replace('<!-- mdpo-0', '<!-- mdpo')
return re.sub(
r'\\<!--\s*mdpo-0',
'<!-- mdpo',
text,
)

0 comments on commit c563956

Please sign in to comment.