jenkins-bot has submitted this change. ( 
https://gerrit.wikimedia.org/r/c/pywikibot/core/+/1132163?usp=email )

Change subject: [IMPR] Improve cleanUpLinks/handleOneLink
......................................................................

[IMPR] Improve cleanUpLinks/handleOneLink

Combine the result string with f-string. The performance
increases by 3 % (only)

Bug: T390068
Change-Id: Ib430f1c5aace753f84de7836d8d43b4c60869b02
---
M pywikibot/cosmetic_changes.py
1 file changed, 18 insertions(+), 23 deletions(-)

Approvals:
  Xqt: Looks good to me, approved
  jenkins-bot: Verified




diff --git a/pywikibot/cosmetic_changes.py b/pywikibot/cosmetic_changes.py
index 4bf1fdc..463e346 100644
--- a/pywikibot/cosmetic_changes.py
+++ b/pywikibot/cosmetic_changes.py
@@ -50,7 +50,7 @@
                                      'your_script_name_2']
 """
 #
-# (C) Pywikibot team, 2006-2024
+# (C) Pywikibot team, 2006-2025
 #
 # Distributed under the terms of the MIT license.
 #
@@ -538,11 +538,11 @@
         This function will:

         * Replace underscores with spaces
-        * Move leading and trailing spaces out of the wikilink and into the
-          surrounding text
+        * Move leading and trailing spaces out of the wikilink and into
+          the surrounding text
         * Convert URL-encoded characters into Unicode-encoded characters
-        * Move trailing characters out of the link and make the link without
-          using a pipe, if possible
+        * Move trailing characters out of the link and make the link
+          without using a pipe, if possible
         * Capitalize the article title of the link, if appropriate

         .. versionchanged:: 8.4
@@ -560,7 +560,7 @@
                                           encodings=self.site.encodings())
             label = match['label']
             trailingChars = match['linktrail']
-            newline = match['newline']
+            newline = match['newline'] or ''
             # entire link but convert URL-encoded text
             oldlink = url2string(match.group(),
                                  encodings=self.site.encodings())
@@ -582,16 +582,15 @@
             if not in_main_namespace:
                 return oldlink

-            # Replace underlines by spaces, also multiple underlines
-            titleWithSection = re.sub('_+', ' ', titleWithSection)
-            # Remove double spaces
-            titleWithSection = re.sub('  +', ' ', titleWithSection)
+            # Replace underlines by spaces, remove double spaces
+            titleWithSection = re.sub('[_ ]+', ' ', titleWithSection)
             # Remove unnecessary leading spaces from title,
             # but remember if we did this because we eventually want
             # to re-add it outside of the link later.
             titleLength = len(titleWithSection)
             titleWithSection = titleWithSection.lstrip()
-            hadLeadingSpaces = len(titleWithSection) != titleLength
+            hadLeadingSpaces = not newline \
+                and len(titleWithSection) != titleLength
             hadTrailingSpaces = False
             # Remove unnecessary trailing spaces from title,
             # but remember if we did this because it may affect
@@ -609,13 +608,15 @@
             # Remove unnecessary initial and final spaces from label.
             # Please note that some editors prefer spaces around pipes.
             # (See [[en:Wikipedia:Semi-bots]]). We remove them anyway.
-            if label is not None:
+            if label is None:
+                label = titleWithSection
+            else:
                 # Remove unnecessary leading spaces from label,
                 # but remember if we did this because we want
                 # to re-add it outside of the link later.
                 labelLength = len(label)
                 label = label.lstrip()
-                hadLeadingSpaces = len(label) != labelLength
+                hadLeadingSpaces = not newline and len(label) != labelLength
                 # Remove unnecessary trailing spaces from label,
                 # but remember if we did this because it affects
                 # the linktrail.
@@ -623,8 +624,7 @@
                     labelLength = len(label)
                     label = label.rstrip()
                     hadTrailingSpaces = len(label) != labelLength
-            else:
-                label = titleWithSection
+
             if trailingChars:
                 label += trailingChars

@@ -643,7 +643,6 @@
                   and trailR.sub('', label[len(titleWithSection):]) == ''):
                 newLink = (f'[[{label[:len(titleWithSection)]}]]'
                            f'{label[len(titleWithSection):]}')
-
             else:
                 # Try to capitalize the first letter of the title.
                 # Not useful for languages that don't capitalize nouns.
@@ -653,19 +652,15 @@
                 if self.site.sitename == 'wikipedia:de':
                     titleWithSection = first_upper(titleWithSection)
                 newLink = f'[[{titleWithSection}|{label}]]'
+
             # re-add spaces that were pulled out of the link.
             # Examples:
             #   text[[ title ]]text        -> text [[title]] text
             #   text[[ title | name ]]text -> text [[title|name]] text
             #   text[[ title |name]]text   -> text[[title|name]]text
             #   text[[title| name]]text    -> text [[title|name]]text
-            if hadLeadingSpaces and not newline:
-                newLink = ' ' + newLink
-            if hadTrailingSpaces:
-                newLink += ' '
-            if newline:
-                newLink = newline + newLink
-            return newLink
+            return f"{newline}{' ' if hadLeadingSpaces else ''}" \
+                   f"{newLink}{' ' if hadTrailingSpaces else ''}"

         trailR = re.compile(self.site.linktrail())
     # The regular expression which finds links. Results consist of four groups:

--
To view, visit 
https://gerrit.wikimedia.org/r/c/pywikibot/core/+/1132163?usp=email
To unsubscribe, or for help writing mail filters, visit 
https://gerrit.wikimedia.org/r/settings?usp=email

Gerrit-MessageType: merged
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Change-Id: Ib430f1c5aace753f84de7836d8d43b4c60869b02
Gerrit-Change-Number: 1132163
Gerrit-PatchSet: 1
Gerrit-Owner: Xqt <[email protected]>
Gerrit-Reviewer: Xqt <[email protected]>
Gerrit-Reviewer: jenkins-bot
_______________________________________________
Pywikibot-commits mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to