User:CX Zoom AWB/Task 2
{{User:CX Zoom AWB/infobox
| task = 2
| desc = Implementing behavior change for {{PresFoot}}, compliance with MOS:CHRONO
| status = brfa
| freq = non-rec
| type = AWB, Python, Manual
| editnum =
| editlink =
| lastrun =
| lastdiff =
}}
= Background =
{{main|Special:Permalink/1296419942#Behavior change for {{PresFoot}}}}
{{tl|PresFoot}} currently accepts parameters for the last row, as well as closes the table. This behaviour is undesirable. Hence, {{tl|PresFoot}} is to be converted to a pure table closer, and the last row being replaced by the standard {{tl|PresRow}} template.
= Process =
- If {{tl|PresFoot}} is present, change all existing cases of PresFoot(with parameters) to PresRow(with parameters). (2950 cases)
- Add {{tl|PresFoot}} with no parameters to a new line after final {{tl|PresRow}}. (same articles)
- While doing these changes it makes sense to bring these tables' usage into compliance with WP:CHRONO (earliest to latest).
- Remove stray table close in articles that have a PresFoot followed by an unnecessary stray table closer.
= Resources =
- Special:Permalink/1293822995#How to pass an article into Python code in AWB (using external script in AWB; by Qwerfjkl)
= Python code =
import re
f = "
with open(f, "r", encoding = 'cp850') as file:
text = file.read()
- Change last row to {{PresRow}} and end with {{PresFoot}}
pf_match = re.finditer(r"\{\{PresFoot\|(.*?)\}\}", text)
for i in pf_match:
params = i.group(1)
s = i.start()
e = i.end()
textpf = text[:s] + "{{PresRow|" + params + "}}\n{{PresFoot}}" + text[e:]
- Reorder {{PresRow}} chronologically
dic = {}
pr_match = re.finditer(r"\{\{PresRow\|(\d{4})\|(.*?)\}\}", textpf)
for i in pr_match:
dic[i.group(1)] = i.group(2)
s = i.start()
e = i.end()
break
for i in pr_match:
dic[i.group(1)] = i.group(2)
e = i.end()
dicl = sorted(dic.items(), key=lambda x: x[0])
textpr = textpf[:s]
for i in dicl:
textpr += "{{PresRow|" + i[0] + "|" + i[1] + "}}\n"
textpr += textpf[e+1:]
balancer
tprl = len(textpr)
textfin = ""
stack = []
i = 0
while i < tprl:
if textpr[i:i+2] == '
':
stack.append(i) textfin += textpr[i:i+2] i += 2 elif textpr[i:i+2] == ' |
if stack:
if textpr[stack[-1]:stack[-1]+2] == '{|':
stack.pop()
textfin += textpr[i:i+2]
i += 2
else:
textfin += textpr[i]
i += 1
textfin = textfin + (("\n" + ",".join(map(str, stack))) if len(stack) > 0 else "")
- Write the file
with open(f, "w", encoding = 'cp850') as file:
file.write(textfin)