-
Notifications
You must be signed in to change notification settings - Fork 0
/
citationTemplates.py
executable file
·50 lines (42 loc) · 1.96 KB
/
citationTemplates.py
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
#!/usr/bin/env python3
#-*- coding: utf-8 -*-
import toolforge
import json
import re
import pywikibot
import mwparserfromhell
site = pywikibot.Site()
conn = toolforge.connect('cswiki')
templates = json.loads(pywikibot.Page(site, "Wikipedie:Citace/Anglické citační šablony.json").text)
del templates['dokumentace']
template_names = []
template_names_query = []
for template in templates:
template_names.append(template.replace('_', ' ').lower())
template_names_query.append(template.replace(' ', '_')[0].upper() + template.replace(' ', '_')[1:].lower())
with conn.cursor() as cur:
cur.execute('select page_title from templatelinks join page on tl_from=page_id where tl_title in (%s) and page_namespace=0 limit 1', ", ".join(template_names_query))
data = cur.fetchall()
for row in data:
page = pywikibot.Page(site, row[0].decode('utf-8'))
# Preprocessing - merge parameters that shall be merged
code = mwparserfromhell.parse(page.text)
for template in code.filter_templates():
template_name = template.name.lower().strip()
if template_name in template_names:
for param_to_merge in templates[template_name]['parameters_to_merge']:
to_merge = []
for param_name in param_to_merge['order']:
for param in template.params:
if param_name == param.name.strip():
to_merge.append(str(param.value))
template.remove(param.name)
if to_merge:
template.add(param_to_merge['order'][0], param_to_merge['delimiter'].join(to_merge))
page.text = str(code)
# Change the citation templates parameters blindly
for template in templates:
for parameter in templates[template]['parameters']:
page.text = re.sub(r"(\{\{%s[^}]*\| *)%s( *)=" % (template, parameter), r"\1%s\2=" % templates[template]['parameters'][parameter], page.text, flags=re.IGNORECASE)
page.text = re.sub(r"\{\{%s( *(\}\}|\|))" % template, r"{{%s\1" % templates[template]['local'], page.text, flags=re.IGNORECASE)
page.save('Robot: Nahrazení anglických citačních šablon za české')