Skip to content

Commit c868b3a

Browse files
authored
👌 IMPROVE: Store rawtext in AST nodes (for gettext) (#301)
In this commit we ensure that the raw text is propagated from the Markdown tokens to the Sphinx AST. In particular, this is used by the `gettext` builder, to generate translation POT templates.
1 parent 86df2ea commit c868b3a

File tree

5 files changed

+139
-4
lines changed

5 files changed

+139
-4
lines changed

myst_parser/docutils_renderer.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ def renderInlineAsText(self, tokens: List[Token]) -> str:
325325
# ### render methods for commonmark tokens
326326

327327
def render_paragraph_open(self, token: NestedTokens):
328-
para = nodes.paragraph("")
328+
para = nodes.paragraph(token.children[0].content if token.children else "")
329329
self.add_line_and_source_path(para, token)
330330
with self.current_node_context(para, append=True):
331331
self.render_children(token)
@@ -449,7 +449,7 @@ def render_heading_open(self, token: NestedTokens):
449449
if self.is_section_level(level, self.current_node):
450450
self.current_node = cast(nodes.Element, self.current_node.parent)
451451

452-
title_node = nodes.title()
452+
title_node = nodes.title(token.children[0].content if token.children else "")
453453
self.add_line_and_source_path(title_node, token)
454454

455455
new_section = nodes.section()
@@ -719,7 +719,9 @@ def render_table_row(self, token: Token):
719719
with self.current_node_context(row, append=True):
720720
for child in token.children or []:
721721
entry = nodes.entry()
722-
para = nodes.paragraph("")
722+
para = nodes.paragraph(
723+
child.children[0].content if child.children else ""
724+
)
723725
style = child.attrGet("style") # i.e. the alignment when using e.g. :--
724726
if style:
725727
entry["classes"].append(style)
@@ -865,7 +867,9 @@ def render_dl_open(self, token: NestedTokens):
865867
item = nodes.definition_list_item()
866868
self.add_line_and_source_path(item, child)
867869
with self.current_node_context(item, append=True):
868-
term = nodes.term()
870+
term = nodes.term(
871+
child.children[0].content if child.children else ""
872+
)
869873
self.add_line_and_source_path(term, child)
870874
with self.current_node_context(term, append=True):
871875
self.render_children(child)
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
extensions = ["myst_parser"]
2+
exclude_patterns = ["_build"]
3+
copyright = "2020, Executable Book Project"
4+
myst_enable_extensions = ["deflist"]
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# **bold** text 1
2+
3+
**bold** text 2
4+
5+
> **bold** text 3
6+
7+
```{eval-rst}
8+
.. note::
9+
10+
**bold** text 4
11+
```
12+
13+
* **bold** text 5
14+
15+
1. **bold** text 6
16+
17+
**bold** text 7
18+
: **bold** text 8
19+
20+
| **bold** text 9 |
21+
| ---------------- |
22+
| **bold** text 10 |
23+
24+
<div markdown=1>
25+
26+
**bold** text 11
27+
28+
</div>
29+
30+
**skip** text
31+
32+
```
33+
**skip** text
34+
```
35+
36+
```json
37+
{
38+
"skip": "text"
39+
}
40+
```
41+
42+
<h3>**skip** text</h3>

tests/test_sphinx/test_sphinx_builds.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
see conftest.py for fixture usage
44
"""
55
import os
6+
import re
67

78
import pytest
89

@@ -272,3 +273,26 @@ def test_substitutions(
272273
)
273274
finally:
274275
get_sphinx_app_output(app, filename="index.html", regress_html=True)
276+
277+
278+
@pytest.mark.sphinx(
279+
buildername="gettext", srcdir=os.path.join(SOURCE_DIR, "gettext"), freshenv=True
280+
)
281+
def test_gettext(
282+
app,
283+
status,
284+
warning,
285+
get_sphinx_app_output,
286+
remove_sphinx_builds,
287+
file_regression,
288+
):
289+
"""Test gettext message extraction."""
290+
app.build()
291+
assert "build succeeded" in status.getvalue() # Build succeeded
292+
warnings = warning.getvalue().strip()
293+
assert warnings == ""
294+
295+
output = get_sphinx_app_output(app, filename="index.pot", buildername="gettext")
296+
output = re.sub(r"POT-Creation-Date: [0-9: +-]+", "POT-Creation-Date: ", output)
297+
298+
file_regression.check(output, extension=".pot")
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# SOME DESCRIPTIVE TITLE.
2+
# Copyright (C) 2020, Executable Book Project
3+
# This file is distributed under the same license as the Python package.
4+
# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
5+
#
6+
#, fuzzy
7+
msgid ""
8+
msgstr ""
9+
"Project-Id-Version: Python \n"
10+
"Report-Msgid-Bugs-To: \n"
11+
"POT-Creation-Date: \n"
12+
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
13+
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
14+
"Language-Team: LANGUAGE <LL@li.org>\n"
15+
"MIME-Version: 1.0\n"
16+
"Content-Type: text/plain; charset=UTF-8\n"
17+
"Content-Transfer-Encoding: 8bit\n"
18+
19+
#: ../../index.md:1
20+
msgid "**bold** text 1"
21+
msgstr ""
22+
23+
#: ../../index.md:3
24+
msgid "**bold** text 2"
25+
msgstr ""
26+
27+
#: ../../index.md:5
28+
msgid "**bold** text 3"
29+
msgstr ""
30+
31+
#: ../../index.md:10
32+
msgid "**bold** text 4"
33+
msgstr ""
34+
35+
#: ../../index.md:13
36+
msgid "**bold** text 5"
37+
msgstr ""
38+
39+
#: ../../index.md:15
40+
msgid "**bold** text 6"
41+
msgstr ""
42+
43+
#: ../../index.md:17
44+
msgid "**bold** text 7"
45+
msgstr ""
46+
47+
#: ../../index.md:18
48+
msgid "**bold** text 8"
49+
msgstr ""
50+
51+
#: ../../index.md:0
52+
msgid "**bold** text 9"
53+
msgstr ""
54+
55+
#: ../../index.md:0
56+
msgid "**bold** text 10"
57+
msgstr ""
58+
59+
#: ../../index.md:26
60+
msgid "**bold** text 11"
61+
msgstr ""

0 commit comments

Comments
 (0)