From c347b50330a18a9328c62b30aba3892de4a755db Mon Sep 17 00:00:00 2001 From: Takayuki Shimizukawa Date: Tue, 27 Nov 2012 17:30:08 +0900 Subject: [PATCH] fix #1044 refs #955: fix external/inline links with localization --- sphinx/environment.py | 14 ++++++++++ tests/root/contents.txt | 1 + tests/root/i18n_external_links.po | 32 ++++++++++++++++++++++ tests/root/i18n_external_links.txt | 13 +++++++++ tests/test_intl.py | 43 +++++++++++++++++++++++++++++- 5 files changed, 102 insertions(+), 1 deletion(-) create mode 100644 tests/root/i18n_external_links.po create mode 100644 tests/root/i18n_external_links.txt diff --git a/sphinx/environment.py b/sphinx/environment.py index 8fe52bb9a..d39270339 100644 --- a/sphinx/environment.py +++ b/sphinx/environment.py @@ -229,6 +229,8 @@ class Locale(Transform): footnote_refs = [r for r in node.children if isinstance(r, nodes.footnote_reference) and r.get('auto') == 1] + refs = [r for r in node.children if isinstance(r, nodes.reference)] + for i, child in enumerate(patch.children): # update leaves if isinstance(child, nodes.footnote_reference) \ and child.get('auto') == 1: @@ -238,6 +240,18 @@ class Locale(Transform): # Some duplicated footnote_reference in msgstr cause # IndexError by .pop(0). That is invalid msgstr. + elif isinstance(child, nodes.reference): + # reference use original 'refname'. + # * reference target ".. _Python: ..." is not translatable. + # * section refname is not translatable. + # * inline reference "`Python <...>`_" have no 'refname'. + if refs and 'refname' in refs[0]: + refname = child['refname'] = refs.pop(0)['refname'] + self.document.refnames.setdefault( + refname, []).append(child) + # if reference node count is not same before translation + # that offen generate unknown link target warning. + for child in patch.children: # update leaves child.parent = node node.children = patch.children diff --git a/tests/root/contents.txt b/tests/root/contents.txt index 3ba41eedb..8fcd4a9bf 100644 --- a/tests/root/contents.txt +++ b/tests/root/contents.txt @@ -29,6 +29,7 @@ Contents: versioning/index only i18n_footnote + i18n_external_links Python diff --git a/tests/root/i18n_external_links.po b/tests/root/i18n_external_links.po new file mode 100644 index 000000000..4cd19ddab --- /dev/null +++ b/tests/root/i18n_external_links.po @@ -0,0 +1,32 @@ +# SOME DESCRIPTIVE TITLE. +# Copyright (C) 2012, foof +# This file is distributed under the same license as the foo package. +# FIRST AUTHOR , 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 \n" +"Language-Team: LANGUAGE \n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: 8bit\n" + +msgid "i18n with external links" +msgstr "EXTERNAL LINKS" + +msgid "External link to Python_." +msgstr "EXTERNAL LINK TO Python_." + +msgid "Internal link to `i18n with external links`_." +msgstr "`EXTERNAL LINKS`_ IS INTERNAL LINK." + +msgid "Inline link by `Sphinx `_." +msgstr "INLINE LINK BY `SPHINX `_." + +msgid "Unnamed link__." +msgstr "UNNAMED LINK__." diff --git a/tests/root/i18n_external_links.txt b/tests/root/i18n_external_links.txt new file mode 100644 index 000000000..7ac1db02e --- /dev/null +++ b/tests/root/i18n_external_links.txt @@ -0,0 +1,13 @@ +:tocdepth: 2 + +i18n with external links +======================== +.. #1044 external-links-dont-work-in-localized-html + +* External link to Python_. +* Internal link to `i18n with external links`_. +* Inline link by `Sphinx `_. +* Unnamed link__. + +.. _Python: http://python.org +.. __: http://google.com diff --git a/tests/test_intl.py b/tests/test_intl.py index 19d5ab4a5..1c7df2543 100644 --- a/tests/test_intl.py +++ b/tests/test_intl.py @@ -11,6 +11,7 @@ """ from subprocess import Popen, PIPE +import re from util import * from util import SkipTest @@ -19,7 +20,7 @@ from util import SkipTest def setup_module(): (test_root / 'xx' / 'LC_MESSAGES').makedirs() # Compile all required catalogs into binary format (*.mo). - for catalog in 'bom', 'subdir', 'i18n_footnote': + for catalog in 'bom', 'subdir', 'i18n_footnote', 'i18n_external_links': try: p = Popen(['msgfmt', test_root / '%s.po' % catalog, '-o', test_root / 'xx' / 'LC_MESSAGES' / '%s.mo' % catalog], @@ -87,3 +88,43 @@ def test_i18n_footnote_regression(app): u"\n[ref] THIS IS A NAMED FOOTNOTE.\n" u"\n[100] THIS IS A NUMBERED FOOTNOTE.\n") assert result == expect + + +@with_app(buildername='html', + confoverrides={'language': 'xx', 'locale_dirs': ['.']}) +def test_i18n_keep_external_links(app): + """regression test for #1044""" + app.builder.build(['i18n_external_links']) + result = (app.outdir / 'i18n_external_links.html').text(encoding='utf-8') + + # external link check + expect_line = u"""
  • EXTERNAL LINK TO Python.
  • """ + matched = re.search('^
  • EXTERNAL LINK TO .*$', result, re.M) + matched_line = '' + if matched: + matched_line = matched.group() + assert expect_line == matched_line + + # internal link check + expect_line = u"""
  • EXTERNAL LINKS IS INTERNAL LINK.
  • """ + matched = re.search('^
  • $', result, re.M) + matched_line = '' + if matched: + matched_line = matched.group() + assert expect_line == matched_line + + # inline link check + expect_line = u"""
  • INLINE LINK BY SPHINX.
  • """ + matched = re.search('^
  • INLINE LINK BY .*$', result, re.M) + matched_line = '' + if matched: + matched_line = matched.group() + assert expect_line == matched_line + + # unnamed link check + expect_line = u"""
  • UNNAMED LINK.
  • """ + matched = re.search('^
  • UNNAMED .*$', result, re.M) + matched_line = '' + if matched: + matched_line = matched.group() + assert expect_line == matched_line