XZise has uploaded a new change for review.

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

Change subject: [IMPROV] cosmetic_changes: merge similar regexes
......................................................................

[IMPROV] cosmetic_changes: merge similar regexes

Instead of having two pretty similar regexes it's in most cases possible to
combine them easily. This is also fixing “dash” into “pipe” in the comment
explaining how the external link fixer works.

It also adds the site parameter to each changed call.

Change-Id: I8513e34f873730ee9c92abb75c499f67cb3d86fc
---
M pywikibot/cosmetic_changes.py
1 file changed, 14 insertions(+), 26 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/pywikibot/core 
refs/changes/28/244628/1

diff --git a/pywikibot/cosmetic_changes.py b/pywikibot/cosmetic_changes.py
index 169359f..ef8bfc3 100755
--- a/pywikibot/cosmetic_changes.py
+++ b/pywikibot/cosmetic_changes.py
@@ -706,17 +706,13 @@
 #                                     
r'\[https?://%s\.%s\.org/wiki/(?P<link>\S+)\s+(?P<title>.+?)\s?\]'
 #                                     % (self.site.code, 
self.site.family.name),
 #                                     r'[[\g<link>|\g<title>]]', exceptions)
-        # external link in double brackets
+        # external link in/starting with double brackets
         text = textlib.replaceExcept(
             text,
-            r'\[\[(?P<url>https?://[^\]]+?)\]\]',
-            r'[\g<url>]', exceptions)
-        # external link starting with double bracket
-        text = textlib.replaceExcept(text,
-                                     r'\[\[(?P<url>https?://.+?)\]',
-                                     r'[\g<url>]', exceptions)
-        # external link and description separated by a dash, with
-        # whitespace in front of the dash, so that it is clear that
+            r'\[\[(?P<url>https?://[^\]]+?)\]\]?',
+            r'[\g<url>]', exceptions, site=self.site)
+        # external link and description separated by a pipe, with
+        # whitespace in front of the pipe, so that it is clear that
         # the dash is not a legitimate part of the URL.
         text = textlib.replaceExcept(
             text,
@@ -739,14 +735,10 @@
         # Keep in mind that MediaWiki automatically converts <br> to <br />
         exceptions = ['nowiki', 'comment', 'math', 'pre', 'source',
                       'startspace']
-        text = textlib.replaceExcept(text, r'(?i)<b>(.*?)</b>', r"'''\1'''",
-                                     exceptions)
-        text = textlib.replaceExcept(text, r'(?i)<strong>(.*?)</strong>',
-                                     r"'''\1'''", exceptions)
-        text = textlib.replaceExcept(text, r'(?i)<i>(.*?)</i>', r"''\1''",
-                                     exceptions)
-        text = textlib.replaceExcept(text, r'(?i)<em>(.*?)</em>', r"''\1''",
-                                     exceptions)
+        text = textlib.replaceExcept(text, r'(?i)<(b|strong)>(.*?)</\1>',
+                                     r"'''\2'''", exceptions, site=self.site)
+        text = textlib.replaceExcept(text, r'(?i)<(i|em)>(.*?)</\1>',
+                                     r"''\2''", exceptions, site=self.site)
         # horizontal line without attributes in a single line
         text = textlib.replaceExcept(text, r'(?i)([\r\n])<hr[ /]*>([\r\n])',
                                      r'\1----\2', exceptions)
@@ -797,19 +789,15 @@
         exceptions = ['nowiki', 'comment', 'math', 'pre', 'source',
                       'startspace', 'gallery', 'hyperlink', 'interwiki', 
'link']
         # change <number> ccm -> <number> cm³
-        text = textlib.replaceExcept(text, r'(\d)\s*&nbsp;ccm',
-                                     r'\1&nbsp;' + u'cm³', exceptions)
-        text = textlib.replaceExcept(text,
-                                     r'(\d)\s*ccm', r'\1&nbsp;' + u'cm³',
-                                     exceptions)
+        text = textlib.replaceExcept(text, r'(\d)\s*(?:&nbsp;)?ccm',
+                                     r'\1&nbsp;cm³', exceptions,
+                                     site=self.site)
         # Solve wrong Nº sign with °C or °F
         # additional exception requested on fr-wiki for this stuff
         pattern = re.compile(u'«.*?»', re.UNICODE)
         exceptions.append(pattern)
-        text = textlib.replaceExcept(text, r'(\d)\s*&nbsp;' + u'[º°]([CF])',
-                                     r'\1&nbsp;' + u'°' + r'\2', exceptions)
-        text = textlib.replaceExcept(text, r'(\d)\s*' + u'[º°]([CF])',
-                                     r'\1&nbsp;' + u'°' + r'\2', exceptions)
+        text = textlib.replaceExcept(text, r'(\d)\s*(?:&nbsp;)?[º°]([CF])',
+                                     r'\1&nbsp;°\2', exceptions, 
site=self.site)
         text = textlib.replaceExcept(text, u'º([CF])', u'°' + r'\1',
                                      exceptions)
         return text

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I8513e34f873730ee9c92abb75c499f67cb3d86fc
Gerrit-PatchSet: 1
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Owner: XZise <commodorefabia...@gmx.de>

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

Reply via email to