0

I am a contributor to Wikipedia and I would like to make a script with AutoHotKey that could format the wikicode of infoboxes and other similar templates.

Infoboxes are templates that displays a box on the side of articles and shows the values of the parameters entered (they are numerous and they differ in number, lenght and type of characters used depending on the infobox).

Parameters are always preceded by a pipe (|) and end with an equal sign (=). On rare occasions, multiple parameters can be put on the same line, but I can sort this manually before running the script.

A typical infobox will be like this:

{{Infobox XYZ
 | first parameter  = foo
 | second_parameter = 
 | 3rd parameter    = bar
 | 4th              = bazzzzz
 | 5th              = 
 | etc.             = 
}}

But sometime, (lazy) contributors put them like this:

{{Infobox XYZ
|first parameter=foo
|second_parameter= 
|3rd parameter=bar
|4th=bazzzzz
|5th= 
|etc.= 
}}

Which isn't very easy to read and modify.

I would like to know if it is possible to make a regex (or a serie of regexes) that would transform the second example into the first.

The lines should start with a space, then a pipe, then another space, then the parameter name, then any number of spaces (to match the other lines lenght), then an equal sign, then another space, and if present, the parameter value.

I try some things using multiple capturing groups, but I'm going nowhere... (I'm even ashamed to show my tries as they really don't work).

Would someone have an idea on how to make it work?

Thank you for your time.

vvvvv
  • 25,404
  • 19
  • 49
  • 81
oli_vi_er
  • 57
  • 8
  • 1
    If you're looking a script to do this I'd recommend this one https://en.wikipedia.org/wiki/User:Taavi/Aligner.js It doesn't use regex but it does do what you're trying to do. – Taazar Nov 15 '22 at 15:58
  • @Taazar thank you, but I am using Vector 2010 and I don't think it works with it, or maybe it is because I am on wp.fr (I just added it to my common.js, cleared my cache, but don't know how to make it work, ther is nothing in my "More" menu) – oli_vi_er Nov 16 '22 at 16:17

3 Answers3

0

The lines should start with a space, then a pipe, then another space, then the parameter name, then a space, then an equal sign, then another space, and if present, the parameter value.

First the selection, it's relatively trivial:

^\s*\|\s*([^=]*?)\s*=(.*)$

Then the replacement, literally your description of what you want (note the space at the beginning):

 | $1 = $2

See it in action here.

Blindy
  • 65,249
  • 10
  • 91
  • 131
  • Yea, but I want the equal signs to be aligned, not just add "space/pipe/space" at the beggining and "space/equal/space" after the parameter name. Sorry I had to edit my question to clarify what I wanted... – oli_vi_er Nov 15 '22 at 15:29
  • No, it will preserve new lines. Again, click the link. – Blindy Nov 15 '22 at 15:30
  • sorry I edited my comment... – oli_vi_er Nov 15 '22 at 15:31
  • I would like the result be like the 1st example, with all equal signs aligned vertically. By the way, the "\s" are useless, it is always normal spaces used in wikipedia code. – oli_vi_er Nov 15 '22 at 15:32
  • It's never useless to write proper regexes. And what you want with the aligned block isn't possible to output with a regex in one step, you need to do it in two steps: first select all results to get the max size, then in the replace function you need to use a lambda to calculate the number of spaces to insert based on the max size. – Blindy Nov 15 '22 at 15:36
  • I managed to make something with 20 or less characters before the equal sign, but I can't get the "missing" characters (ie the ones that would make the line be 20 characters long) to be replaced by a space. – oli_vi_er Nov 15 '22 at 15:52
0

@Blindy:

The best code I have found so far is the following : https://regex101.com/r/GunrUg/1

The problem is it doesn't align the equal signs vertically...

oli_vi_er
  • 57
  • 8
0

I got an answer on AutoHotKey forums:

^i::
out := ""
Send, ^x
regex := "O)\s*\|\s*(.*?)\s*=\s*(.*)", width := 1
Loop, Parse, Clipboard, `n, `r
    If RegExMatch(A_LoopField, regex, _)
        width := Max(width, StrLen(_[1]))
Loop, Parse, Clipboard, `n, `r
    If RegExMatch(A_LoopField, regex, _)
        out .= Format(" | {:-" width "} = {2}", _[1],_[2]) "`n"
else
    out .= A_LoopField "`n"
Clipboard := out
Send, ^v
Return

With this script, pressing Ctrl+i formats the infobox code just right (I guess a simple regex isn't enough to do the job).

oli_vi_er
  • 57
  • 8