I’m writing a simple Javascript to add a specific parameter to a specific template in article that is currently being edited.
Wikipedia Templates are structured in the following format:
{{Template name|unnamed parameter|named parameter=some value|another parameter=[[target article|article name]]|parameter={{another template|another tamplate's parameter}}}}
One template can also be over more lines, for example:
{{Template
|name=John
|surname=Smith
|pob=[[London|London, UK]]
}}
For further reference, please have a look at http://en.wikipedia.org/wiki/Help:Template
So firstly I’d like to match the entire template. I came over partial solution, that is:
document.editform.wpTextbox1.value.match(/\{\{template name((.|\n)*?)\}\}$/gmis)
However the problem is that it only matches text from the initial brackets till the closing brackets of the first nested template (first example).
In addition I’d like to fetch its parameters in an array form. So for the result, I’d like to get an array with parameters in specific order.
Array( value of paramter pob, value of paramter name, value of parameter surname, value of parameter pod (in this case empty, because it was unset) )
I’d use that to clean the unstandardised formatting in some articles and add some new parameters.
Thank you!
Write simple parser.
Solving this kind of problem by regexp is not right. It’s the same as matching brackets – difficult to do with regexp. Regexps are not suitable for nested expressions in general.
Try something like that:
This is just pseudo code, as I’m in rush now, will update this code to be working…
UPDATE (9th August 2011)
UNIT TESTS
Note: I’m using Jasmine’s syntax for these unit tests. You can easily run it using AngularJS which contains whole testing environment – check it out at http://angularjs.org