-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathzspace2.py
More file actions
89 lines (75 loc) · 2.86 KB
/
Copy pathzspace2.py
File metadata and controls
89 lines (75 loc) · 2.86 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
from __future__ import annotations
import pywikibot
from pywikibot import pagegenerators
from pywikibot.bot import (
AutomaticTWSummaryBot,
ConfigParserBot,
ExistingPageBot,
SingleSiteBot,
)
import mwparserfromhell
import re
docuReplacements = {'¶ms;': pagegenerators.parameterHelp} # noqa: N816
# 1) stray-newline: newline not followed by |, {, or }
newline_re = re.compile(r'\n(?!\s*[\|{}<\n])')
# 2) spaces: various non-breaking or thin spaces + tabs
space_re = re.compile(r'[\u00A0\u202F\u200A\u0009]')
# 3) soft hyphen → literal hyphen
hyphen_re = re.compile(r'\u00AD')
# 4) other invisibles to just drop
drop_re = re.compile(r'[\u200B-\u200D\uFEFF\uFFFD\u007F]')
class BasicBot(
SingleSiteBot,
ConfigParserBot,
ExistingPageBot,
AutomaticTWSummaryBot,
):
use_redirects = False
summary_key = 'basic-changing'
update_options = {
'replace': False,
'summary': "[[Вікіпедія:Завдання_для_ботів#Невидимі символи в шаблонах cite|Виправлення невидимих символів у шаблонах cite]]",
'text': 'Test',
'top': False,
}
def clean_ref_contents(self, text: str) -> str:
# 1: turn any stray newline into a space
text = newline_re.sub(' ', text)
# 2: normalize various spaces/tabs → regular space
text = space_re.sub(' ', text)
# 3: soft hyphens → hyphen-minus
text = hyphen_re.sub('-', text)
# 4: drop zero-width & other invisibles
text = drop_re.sub('', text)
return text
def treat_page(self) -> None:
page_text = self.current_page.text
parsed = mwparserfromhell.parse(page_text)
changed = False
for ref in parsed.filter_tags(matches=lambda t: t.tag.lower() == 'ref'):
orig = str(ref.contents)
cleaned = self.clean_ref_contents(orig)
if cleaned != orig:
ref.contents = cleaned
changed = True
if changed:
self.put_current(str(parsed), summary=self.opt.summary)
def main(*args: str) -> None:
options = {}
local_args = pywikibot.handle_args(args)
gen_factory = pagegenerators.GeneratorFactory()
local_args = gen_factory.handle_args(local_args)
for arg in local_args:
arg, _, val = arg.partition(':')
opt = arg[1:]
if opt in ('summary', 'text'):
if not val:
pywikibot.input(f'Please enter a value for {arg}')
options[opt] = val
else:
options[opt] = True
gen = gen_factory.getCombinedGenerator(preload=True)
if not pywikibot.bot.suggest_help(missing_generator=not gen):
BasicBot(generator=gen, **options).run()
if __name__ == '__main__':
main()