Takayuki Shimizukawa avatar Takayuki Shimizukawa committed 0493ec8

fixed #955: footnote i18n translation cause KeyError and 'Only update text nodes in translation' change at b7b808e46851 break translation.

Comments (0)

Files changed (5)

sphinx/environment.py

             if not isinstance(patch, nodes.paragraph):
                 continue # skip for now
 
-            # copy text children
-            for i, child in enumerate(patch.children):
-                if isinstance(child, nodes.Text):
-                    child.parent = node
-                    node.children[i] = child
+            footnote_refs = [r for r in node.children
+                             if isinstance(r, nodes.footnote_reference)
+                             and r.get('auto') == 1]
+            for i, child in enumerate(patch.children): # update leaves
+                if isinstance(child, nodes.footnote_reference):
+                    # use original 'footnote_reference' object.
+                    # this object already registered in self.document.autofootnote_refs
+                    patch.children[i] = footnote_refs.pop(0)
+                    # Some duplicated footnote_reference in msgstr cause
+                    # IndexError by .pop(0). That is invalid msgstr.
+
+            for child in patch.children: # update leaves
+                child.parent = node
+            node.children = patch.children
 
 
 class SphinxStandaloneReader(standalone.Reader):

tests/root/contents.txt

    extensions
    versioning/index
    only
+   i18n_footnote
 
    Python <http://python.org/>
 

tests/root/i18n_footnote.po

+# SOME DESCRIPTIVE TITLE.
+# Copyright (C) 2012, foof
+# This file is distributed under the same license as the foo package.
+# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
+#
+#, fuzzy
+msgid ""
+msgstr ""
+"Project-Id-Version: sphinx 1.0\n"
+"Report-Msgid-Bugs-To: \n"
+"POT-Creation-Date: 2012-11-22 08:28\n"
+"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
+"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
+"Language-Team: LANGUAGE <LL@li.org>\n"
+"MIME-Version: 1.0\n"
+"Content-Type: text/plain; charset=UTF-8\n"
+"Content-Transfer-Encoding: 8bit\n"
+
+msgid "i18n with Footnote"
+msgstr "I18N WITH FOOTNOTE"
+
+msgid "Contents [#]_ for `i18n with Footnote`_ [ref]_"
+msgstr "`I18N WITH FOOTNOTE`_ INCLUDE THIS CONTENTS [ref]_ [#]_"
+
+msgid "This is a auto numbered footnote."
+msgstr "THIS IS A AUTO NUMBERED FOOTNOTE."
+
+msgid "This is a named footnote."
+msgstr "THIS IS A NAMED FOOTNOTE."
+

tests/root/i18n_footnote.txt

+:tocdepth: 2
+
+i18n with Footnote
+==================
+.. #955 cant-build-html-with-footnotes-when-using
+
+Contents [#]_ for `i18n with Footnote`_ [ref]_
+
+.. [#] This is a auto numbered footnote.
+.. [ref] This is a named footnote.

tests/test_intl.py

 def setup_module():
     (test_root / 'xx' / 'LC_MESSAGES').makedirs()
     # Compile all required catalogs into binary format (*.mo).
-    for catalog in 'bom', 'subdir':
+    for catalog in 'bom', 'subdir', 'i18n_footnote':
         try:
             p = Popen(['msgfmt', test_root / '%s.po' % catalog, '-o',
                 test_root / 'xx' / 'LC_MESSAGES' / '%s.mo' % catalog],
     app.builder.build(['subdir/includes'])
     result = (app.outdir / 'subdir' / 'includes.txt').text(encoding='utf-8')
     assert result.startswith(u"\ntranslation\n***********\n\n")
+
+
+@with_app(buildername='html',
+          confoverrides={'language': 'xx', 'locale_dirs': ['.']})
+def test_i18n_footnote_break_refid(app):
+    """test for #955 cant-build-html-with-footnotes-when-using"""
+    app.builder.build(['i18n_footnote'])
+    result = (app.outdir / 'i18n_footnote.html').text(encoding='utf-8')
+    # expect no error by build
+
+
+@with_app(buildername='text',
+          confoverrides={'language': 'xx', 'locale_dirs': ['.']})
+def test_i18n_footnote_regression(app):
+    """regression test for fix #955"""
+    app.builder.build(['i18n_footnote'])
+    result = (app.outdir / 'i18n_footnote.txt').text(encoding='utf-8')
+    expect = (u"\nI18N WITH FOOTNOTE"
+              u"\n******************\n"  # underline matches new translation
+              u"\nI18N WITH FOOTNOTE INCLUDE THIS CONTENTS [ref] [1]\n"
+              u"\n[1] THIS IS A AUTO NUMBERED FOOTNOTE.\n"
+              u"\n[ref] THIS IS A NAMED FOOTNOTE.\n")
+    assert result == expect
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.