-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathurlspaces.py
More file actions
113 lines (93 loc) · 4.16 KB
/
Copy pathurlspaces.py
File metadata and controls
113 lines (93 loc) · 4.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
from __future__ import annotations
import pywikibot
from pywikibot import pagegenerators
from pywikibot.bot import (
AutomaticTWSummaryBot,
ConfigParserBot,
ExistingPageBot,
SingleSiteBot,
)
import mwparserfromhell
# Help text for -help output
docuReplacements = {'¶ms;': pagegenerators.parameterHelp} # noqa: N816
class RemoveLinkSpacesBot(
SingleSiteBot, # work on a single site only
ConfigParserBot, # read options from scripts.ini
ExistingPageBot, # skip non‐existing pages
AutomaticTWSummaryBot, # auto‐generate edit summaries
):
summary_key = 'remove-link-spaces'
update_options = {
'summary': '[[Обговорення_користувача:MonAx#http:_//_->_http://|Прибирання зайвих пробілів у посиланнях]]',
'always': False,
}
@staticmethod
def _clean_url_preserve_ws(val_str: str) -> str:
# Preserve leading/trailing whitespace, clean spaces inside core URL
if len(val_str.strip()) == 0: return val_str
leading = val_str[:len(val_str) - len(val_str.lstrip())]
trailing = val_str[len(val_str.rstrip()):]
core = val_str.strip()
cleaned_core = core.replace(' ', '')
return f"{leading}{cleaned_core}{trailing}"
def treat_page(self) -> None:
text = self.current_page.text
parsed = mwparserfromhell.parse(text)
changed = False
# 1) Clean external link URLs across the page
for ext in parsed.filter_external_links():
url_str = str(ext.url)
new_url = url_str.replace(' ', '')
if new_url != url_str:
ext.url = new_url
changed = True
# 2) Clean URL params in templates (preserve surrounding whitespace)
for tmpl in parsed.filter_templates():
for param in tmpl.params:
name = str(param.name).strip().lower()
if name in ['url', 'посилання', "ссылка", "archiveurl"]:
val_str = str(param.value)
new_val = self._clean_url_preserve_ws(val_str)
if new_val != val_str:
param.value = new_val
changed = True
# 3) Within <ref> tags, also clean external link URLs and template URL params
for tag in parsed.filter_tags(matches=lambda t: t.tag.lower() == 'ref'):
inner = str(tag.contents)
parsed_ref = mwparserfromhell.parse(inner)
sub_changed = False
# clean external links
for ext in parsed_ref.filter_external_links():
url_str = str(ext.url)
new_url = url_str.replace(' ', '')
if new_url != url_str:
ext.url = new_url
sub_changed = True
# clean templates URL params inside refs
for tmpl in parsed_ref.filter_templates():
for param in tmpl.params:
if str(param.name).strip().lower() == 'url':
val_str = str(param.value)
new_val = self._clean_url_preserve_ws(val_str)
if new_val != val_str:
param.value = new_val
sub_changed = True
if sub_changed:
tag.contents = parsed_ref
changed = True
if changed or self.opt.always:
self.put_current(str(parsed), summary=self.opt.summary)
def main(*args: str) -> None:
options: dict[str, bool] = {}
local_args = pywikibot.handle_args(args)
gen_factory = pagegenerators.GeneratorFactory()
local_args = gen_factory.handle_args(local_args)
for arg in local_args:
if arg.startswith('-always'):
options['always'] = True
gen = gen_factory.getCombinedGenerator(preload=True)
if not pywikibot.bot.suggest_help(missing_generator=not gen):
bot = RemoveLinkSpacesBot(generator=gen, **options)
bot.run()
if __name__ == '__main__':
main()