我在 MediaWiki 中安装了 Lua 支持和 Module:String 模块,我试图只获取模板中链接的后半部分。
模板:图标
{{Icon|Icon.png|mypage/Mysubpage}}
会返回子页面的链接,同时只显示“Mysubpage”而不是完整的链接“mypage/mysubpage”
普通模板是这样的:
[[File:{{{1}}}|{{{size|64px}}}|link={{{2}}}|{{{2}}}]]<br>{{#ifeq:{{#pos:{{lc:{{{2|}}}}}|http}}|0|[{{{2}}}]|[[{{{2}}}]]}}
但现在我添加了子页面,图标旁边显示了完整的链接路径。
我尝试将 {{{2}}} 替换为:
{{#invoke:String|sub|{{{2}}}| {{#invoke:String|find|{{{2}}}|/|1}}+1 |{{#invoke:String|len|{{{2}}} }} }}
但是好像不行。
我怎样才能做到这一点?
谢谢!
最佳答案
如果您有 ParserFunctions安装扩展,{{#titleparts}}
魔法词可能会有帮助。
docs说:
This function separates a page title into segments based on slashes, then returns some of those segments as output.
{{#titleparts: pagename | number of segments to return | first segment to return }}
If the number of segments to return parameter is not specified, it defaults to "0", which returns all the segments from the first segment to return (included).
…
Negative values are accepted for both values… Negative values for the first segment to return translates to "start with this segment counting from the right":
{{#titleparts: Talk:Foo/bar/baz/quok | | -1 }}
→quok
Returns last segment.
由于您只查找最后一段,因此您需要 {{#titleparts: {{{2}}} | | -1}}
。无论该模板是否用于子页面,这都将起作用。
关于mediawiki - 仅从 MediaWiki 中的子页面 url 获取页面名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52597504/