Parsing wiki templates calls with Javascript

﹥>﹥吖頭↗ 提交于 2020-02-06 04:19:24

问题


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


回答1:


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



回答2:


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.



来源:https://stackoverflow.com/questions/5460500/parsing-wiki-templates-calls-with-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!