John Vandenberg has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/246802

Change subject: Move cc 'file' exception into textlib
......................................................................

Move cc 'file' exception into textlib

The custom 'file' exception is slightly more advanced
than the 'file' and 'category' exceptions in textlib.

Change-Id: If2a0827df1a0f83afd3c23e57bb841929ed1399c
---
M pywikibot/cosmetic_changes.py
M pywikibot/textlib.py
M tests/textlib_tests.py
3 files changed, 39 insertions(+), 8 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/pywikibot/core 
refs/changes/02/246802/1

diff --git a/pywikibot/cosmetic_changes.py b/pywikibot/cosmetic_changes.py
index 3010ef3..af76391 100755
--- a/pywikibot/cosmetic_changes.py
+++ b/pywikibot/cosmetic_changes.py
@@ -833,6 +833,7 @@
             return
         exceptions = [
             'gallery',
+            'file,
             'hyperlink',
             'interwiki',
             # FIXME: but changes letters inside wikilinks
@@ -856,16 +857,9 @@
         new = digits.pop(self.site.code)
         # This only works if there are only two items in digits dict
         old = digits[digits.keys()[0]]
-        # do not change inside file links
-        namespaces = list(self.site.namespace(6, all=True))
-        pattern = re.compile(
-            u'\\[\\[(%s):.+?\\.\\w+? *(\\|((\\[\\[.*?\\]\\])|.)*)?\\]\\]'
-            % u'|'.join(namespaces),
-            re.UNICODE)
         # not to let bot edits in latin content
         exceptions.append(re.compile(u"[^%(fa)s] *?\"*? *?, *?[^%(fa)s]"
                                      % {'fa': faChrs}))
-        exceptions.append(pattern)
         text = textlib.replaceExcept(text, u',', u'،', exceptions)
         if self.site.code == 'ckb':
             text = textlib.replaceExcept(text,
diff --git a/pywikibot/textlib.py b/pywikibot/textlib.py
index 54d20eb..a45923c 100644
--- a/pywikibot/textlib.py
+++ b/pywikibot/textlib.py
@@ -180,7 +180,7 @@
         'category':     ('\[\[ *(?:%s)\s*:.*?\]\]',
                          lambda site: '|'.join(site.namespaces[14])),
         # files
-        'file':         ('\[\[ *(?:%s)\s*:.*?\]\]',
+        'file':         ('\[\[ *(?:%s)\s*:.*? *(\|((\[\[.*?\]\])|.)*)?\]\]',
                          lambda site: '|'.join(site.namespaces[6])),
     })
 
diff --git a/tests/textlib_tests.py b/tests/textlib_tests.py
index f450455..43cf7d9 100644
--- a/tests/textlib_tests.py
+++ b/tests/textlib_tests.py
@@ -1122,17 +1122,54 @@
         self.assertEqual(textlib.replaceExcept('{{#invoke:x}}', 'x', 'y',
                                                ['invoke'], site=self.site),
                          '{{#invoke:x}}')
+
+    def test_replace_tags_category(self):
+        """Test replacing not inside category namespace links."""
         for ns_name in self.site.namespaces[14]:
             self.assertEqual(textlib.replaceExcept('[[%s:x]]' % ns_name,
                                                    'x', 'y', ['category'],
                                                    site=self.site),
                              '[[%s:x]]' % ns_name)
+
+    def test_replace_tags_file(self):
+        """Test replacing not inside category file links."""
         for ns_name in self.site.namespaces[6]:
             self.assertEqual(textlib.replaceExcept('[[%s:x]]' % ns_name,
                                                    'x', 'y', ['file'],
                                                    site=self.site),
                              '[[%s:x]]' % ns_name)
 
+        self.assertEqual(textlib.replaceExcept('[[File:x|]][[File:x|foo]]',
+                                               'x', 'y', ['file'],
+                                               site=self.site),
+                         '[[File:x|]][[File:x|foo]]')
+
+        self.assertEqual(textlib.replaceExcept('[[NonFile:x]]',
+                                               'x', 'y', ['file'],
+                                               site=self.site),
+                         '[[NonFile:y]]')
+
+        self.assertEqual(textlib.replaceExcept('[[File:]]',
+                                               'File:', 'NonFile:', ['file'],
+                                               site=self.site),
+                         '[[File:]]')
+
+        self.assertEqual(textlib.replaceExcept('[[File:x|[[foo]].]]',
+                                               'x', 'y', ['file'],
+                                               site=self.site),
+                         '[[File:x|[[foo]].]]')
+
+        # ensure only links inside file are captured
+        self.assertEqual(textlib.replaceExcept('[[File:a|[[f]].x]][[x]]',
+                                               'x', 'y', ['file'],
+                                               site=self.site),
+                         '[[File:a|[[f]].x]][[y]]')
+
+        self.assertEqual(textlib.replaceExcept('[[File:a|[[f]][[b]].x]][[x]]',
+                                               'x', 'y', ['file'],
+                                               site=self.site),
+                         '[[File:a|[[f]][[b]].x]][[y]]')
+
     def test_replace_tags_interwiki(self):
         """Test replacing not inside interwiki links."""
         if 'es' not in self.site.family.langs or 'ey' in 
self.site.family.langs:

-- 
To view, visit https://gerrit.wikimedia.org/r/246802
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: If2a0827df1a0f83afd3c23e57bb841929ed1399c
Gerrit-PatchSet: 1
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Owner: John Vandenberg <jay...@gmail.com>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to