Parsing wiki templates calls with Javascript

2019-08-02 17:58发布

All that I need is to split the wiki template call to parameter parts. In the very basic scenario it is just splitting by | so {{template|unnamed_parameter|param1=value1}} would be split to {{template, unnamed_parameter, param1=value1 and }}.

But things are complicating when the pipe character is used for other purposes like for wikilinks [[link|title]] etc.

Any suggestions how to do this task in the easiest way? :)

Update: Sorry for possible misunderstanding but {{template|unnamed_parameter|param1=value1}} is just an example. For more information about wiki templates you can look at the following resource: http://www.mediawiki.org/wiki/Help:Templates

2条回答
戒情不戒烟
2楼-- · 2019-08-02 18:48

regex that assumes your wiki template has always 3 parts:
update to exclude false match to template {{template|[[link|name]]}}

regex:       \{\{(.+?)\|[^\[]{2}(.+?)\|(.+?)[^\]]{2\}\}
replacment:  $1,$2,$3
input:       {{template|unnamed_parameter|param1=value1}}
output:      template,unnamed_parameter,param1=value1

it's a simple regex using reluctant quantifiers and escaping the "special" meaning of {}| using \
by including \{\{ \}\} to the regex you avoid matches on [[ ]] pattern.

查看更多
Root(大扎)
3楼-- · 2019-08-02 18:49

Please look at this Q&A: How can I fix this wiki link parsing regular expression?

My answer (in Update section) there using perl regex is doing pretty much similar Wiki link parsing.

Update:

Alright here is the perl regex for your case:

echo "{{template|unnamed_parameter|param1=value1}}" |  \
perl -pe 's#(^|\b)((?![|\[]){{(.+?)\|(.+?)\|(.+?)}}(?![|\]]))($|\b)#{{$3, $4, $5 and }}#g'

Output: {{template, unnamed_parameter, param1=value1 and }}

Q: are you sure you need and here before closing }} otherwise just edit above regex:

And now checking above solution against string [[link|title]]

echo "[[link|title]]" |  \
perl -pe 's#(^|\b)((?![|\[]){{(.+?)\|(.+?)\|(.+?)}}(?![|\]]))($|\b)#{{$3, $4, $5 and }}#g'

Output: [[link|title]] # remains unchanged as per your requirements
查看更多
登录 后发表回答