Xqt has submitted this change and it was merged. (
https://gerrit.wikimedia.org/r/337288
)
Change subject: Make code flat
......................................................................
Make code flat
Change-Id: I843c91fc18efc29947a98e35a6f2d537e86567c8
---
M pywikibot/cosmetic_changes.py
1 file changed, 99 insertions(+), 101 deletions(-)
Approvals:
Xqt: Looks good to me, approved
jenkins-bot: Verified
diff --git a/pywikibot/cosmetic_changes.py b/pywikibot/cosmetic_changes.py
index 7d35834..90847d1 100755
--- a/pywikibot/cosmetic_changes.py
+++ b/pywikibot/cosmetic_changes.py
@@ -53,7 +53,7 @@
"""
#
# (C) xqt, 2009-2018
-# (C) Pywikibot team, 2006-2018
+# (C) Pywikibot team, 2006-2019
#
# Distributed under the terms of the MIT license.
#
@@ -71,6 +71,7 @@
import pywikibot
from pywikibot import config, textlib
+from pywikibot.page import url2unicode
from pywikibot.textlib import (_MultiTemplateMatchBuilder, FILE_LINK_REGEX,
_get_regexes)
from pywikibot.tools import deprecated_args, first_lower, first_upper
@@ -511,112 +512,109 @@
except ValueError: # T111513
is_interwiki = True
- if not is_interwiki:
- # The link looks like this:
- # [[page_title|link_text]]trailing_chars
- # We only work on namespace 0 because pipes and linktrails work
- # differently for images and categories.
- page = pywikibot.Page(pywikibot.Link(titleWithSection,
- self.site))
- try:
- namespace = page.namespace()
- except pywikibot.InvalidTitle:
- return match.group()
- if namespace == 0:
- # Replace underlines by spaces, also multiple underlines
- titleWithSection = re.sub('_+', ' ',
titleWithSection)
- # 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)
- hadTrailingSpaces = False
- # Remove unnecessary trailing spaces from title,
- # but remember if we did this because it may affect
- # the linktrail and because we eventually want to
- # re-add it outside of the link later.
- if not trailingChars:
- titleLength = len(titleWithSection)
- titleWithSection = titleWithSection.rstrip()
- hadTrailingSpaces = (len(titleWithSection)
- != titleLength)
+ if is_interwiki:
+ return match.group()
- # Convert URL-encoded characters to unicode
- from pywikibot.page import url2unicode
- titleWithSection = url2unicode(titleWithSection,
- encodings=self.site)
+ # The link looks like this:
+ # [[page_title|link_text]]trailing_chars
+ # We only work on namespace 0 because pipes and linktrails work
+ # differently for images and categories.
+ page = pywikibot.Page(pywikibot.Link(titleWithSection, self.site))
+ try:
+ in_main_namespace = page.namespace() == 0
+ except pywikibot.InvalidTitle:
+ in_main_namespace = False
+ if not in_main_namespace:
+ return match.group()
- if titleWithSection == '':
- # just skip empty links.
- return match.group()
+ # Replace underlines by spaces, also multiple underlines
+ titleWithSection = re.sub('_+', ' ', titleWithSection)
+ # 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
+ hadTrailingSpaces = False
+ # Remove unnecessary trailing spaces from title,
+ # but remember if we did this because it may affect
+ # the linktrail and because we eventually want to
+ # re-add it outside of the link later.
+ if not trailingChars:
+ titleLength = len(titleWithSection)
+ titleWithSection = titleWithSection.rstrip()
+ hadTrailingSpaces = len(titleWithSection) != titleLength
- # 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:
- # 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)
- # Remove unnecessary trailing spaces from label,
- # but remember if we did this because it affects
- # the linktrail.
- if not trailingChars:
- labelLength = len(label)
- label = label.rstrip()
- hadTrailingSpaces = (len(label) != labelLength)
- else:
- label = titleWithSection
- if trailingChars:
- label += trailingChars
+ # Convert URL-encoded characters to unicode
+ titleWithSection = url2unicode(titleWithSection,
+ encodings=self.site)
- if self.site.siteinfo['case'] == 'first-letter':
- firstcase_title = first_lower(titleWithSection)
- firstcase_label = first_lower(label)
- else:
- firstcase_title = titleWithSection
- firstcase_label = label
+ if not titleWithSection:
+ # just skip empty links.
+ return match.group()
- if firstcase_label == firstcase_title:
- newLink = '[[%s]]' % label
- # Check if we can create a link with trailing characters
- # instead of a pipelink
- elif (firstcase_label.startswith(firstcase_title)
- and trailR.sub('',
- label[len(titleWithSection):]) == ''):
- newLink = '[[%s]]%s' % (
- label[:len(titleWithSection)],
- label[len(titleWithSection):])
+ # 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:
+ # 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
+ # Remove unnecessary trailing spaces from label,
+ # but remember if we did this because it affects
+ # the linktrail.
+ if not trailingChars:
+ labelLength = len(label)
+ label = label.rstrip()
+ hadTrailingSpaces = len(label) != labelLength
+ else:
+ label = titleWithSection
+ if trailingChars:
+ label += trailingChars
- else:
- # Try to capitalize the first letter of the title.
- # Not useful for languages that don't capitalize nouns.
- # TODO: Add a configuration variable for each site,
- # which determines if the link target is written in
- # uppercase
- if self.site.sitename == 'wikipedia:de':
- titleWithSection = first_upper(titleWithSection)
- newLink = '[[%s|%s]]' % (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 = newLink + ' '
- if newline:
- newLink = newline + newLink
- return newLink
- # don't change anything
- return match.group()
+ if self.site.siteinfo['case'] == 'first-letter':
+ firstcase_title = first_lower(titleWithSection)
+ firstcase_label = first_lower(label)
+ else:
+ firstcase_title = titleWithSection
+ firstcase_label = label
+
+ if firstcase_label == firstcase_title:
+ newLink = '[[%s]]' % label
+ # Check if we can create a link with trailing characters
+ # instead of a pipelink
+ elif (firstcase_label.startswith(firstcase_title)
+ and trailR.sub('', label[len(titleWithSection):]) ==
''):
+ newLink = '[[%s]]%s' % (label[:len(titleWithSection)],
+ label[len(titleWithSection):])
+
+ else:
+ # Try to capitalize the first letter of the title.
+ # Not useful for languages that don't capitalize nouns.
+ # TODO: Add a configuration variable for each site,
+ # which determines if the link target is written in
+ # uppercase
+ if self.site.sitename == 'wikipedia:de':
+ titleWithSection = first_upper(titleWithSection)
+ newLink = '[[%s|%s]]' % (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 = newLink + ' '
+ if newline:
+ newLink = newline + newLink
+ return newLink
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/337288
To unsubscribe, or for help writing mail filters, visit
https://gerrit.wikimedia.org/r/settings
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-MessageType: merged
Gerrit-Change-Id: I843c91fc18efc29947a98e35a6f2d537e86567c8
Gerrit-Change-Number: 337288
Gerrit-PatchSet: 6
Gerrit-Owner: Matěj Suchánek <matejsuchanek97(a)gmail.com>
Gerrit-Reviewer: Xqt <info(a)gno.de>
Gerrit-Reviewer: jenkins-bot (75)