ParserFunctions

来自KlniuWiki
跳转到: 导航, 搜索

ParserFunctions(解析器函数)是一个MediaWiki扩展,包含多个解析函数解释器。

ParserFunctions 的用法都遵循下面的格式:

{{#函数名: 参数1 | 参数2 | 参数 3 …}}

目前有预定义的函数:exprififeqifexprswitchrand暂时被废除)。

各函数名都对大小写不敏感。

语句中的空格、换行等空白字符将被省略。

目录

1 函数

1.1 expr

expr函数,计算数学表达式。语法为:

{{ #expr: 表达式 }}

表达式支持的运算符有:

运算符 名称 优先级 元数 结合性 样例
+ 9 1 {{#expr: + 7}} = 7
- 9 1 {{#expr: - 7}} = -7
not 逻辑非 9 1 {{#expr: not 7}} = 0
* 8 2 {{#expr: 30 * 7}} = 210
/ 8 2 {{#expr: 30 / 7}} = 4.28571428571
div 8 2 {{#expr: 30 div 7}} = 4.28571428571
mod 8 2 {{#expr: 30 mod 7}} = 2
+ 6 2 {{#expr: 30 + 7}} = 37
- 6 2 {{#expr: 30 - 7}} = 23
round 舍入(最大9) 5 2 {{#expr: 30 / 7 round 7}} = 4.2857143
= 等于 4 2 {{#expr: 30 = 7}} = 0
< 小于 4 2 {{#expr: 30 < 7}} = 0
> 大于 4 2 {{#expr: 30 > 7}} = 1
<= 小于等于 4 2 {{#expr: 30 <= 7}} = 0
>= 大于等于 4 2 {{#expr: 30 >= 7}} = 1
<> 不等于 4 2 {{#expr: 30 <> 7}} = 1
 != 不等于 4 2 {{#expr: 30 != 7}} = 1
and 逻辑与 3 2 {{#expr: 30 and 7}} = 1
or 逻辑或 2 2 {{#expr: 30 or 7}} = 1

round运算对运算数正负,位数正负都有不同的表现,参见下例。

逻辑运算符把假映射为0,把真映射为非0,且返回值只有0或1。

同一表达式中先计算高优先级运算。括号优先级高于一切。

1.2 if

if函数是一个if-then-else结构。语法是:

{{#if: <判断字符串> | <then字符串> | <else字符串> }}
{{#if: <判断字符串> | <then字符串> |}}

若判断字符串为非空字符串(忽略前导或后缀空格),则函数返回then字符串,否则函数返回else字符串。else字符可被省略而不会造成错误,但函数在判断字符串为空时便会返回空字符串。

1.3 #ifeq

#ifeq比较两个字符串,返回比较结果。语法为:

{{#ifeq: <字符串1> | <字符串2> | <相等时返回的字符串> | <不相等时返回的字符串> }}

注意:两个空字符串是相等的。

示例:

假设模板Ifeq test的内容是

{{#ifeq:{{{lang}}}|zh|我使用中文|I don't use Chinese}}

则{{Ifeq test|lang=zh}}的结果是我使用中文;而{{Ifeq test|lang=en|I prefer English}}的结果是I don't use Chinese

1.4 #iferror

This function takes an input string and returns one of two results; the function evaluates to true if the input string contains an HTML object with class="error", as generated by other parser functions such as #expr, #time and #rel2abs, template errors such as loops and recursions, and other "failsoft" parser errors.

{{#iferror: test string | value if error | value if correct }}

One or both of the return strings can be omitted. If the correct string is omitted, the test string is returned if it is not erroneous. If the error string is also omitted, an empty string is returned on an error:

{{#iferror: {{#expr: 1 + 2 }} | error | correct }}correct
{{#iferror: {{#expr: 1 + X }} | error | correct }}error
{{#iferror: {{#expr: 1 + 2 }} | error }}3
{{#iferror: {{#expr: 1 + X }} | error }}error
{{#iferror: {{#expr: 1 + 2 }} }}3
{{#iferror: {{#expr: 1 + X }} }} → '
{{#iferror: <strong class="error">a</strong> | error | correct }}error

1.5 ifexist

ifexist根据指定名称的页面是否存在,返回两个参数中的一个。用法:

{{#ifexist: <待测页面标题> | <存在文字> | <不存在文字> }}

示例:

  • {{#ifexist:MediaWiki|有MediaWiki页面|无MediaWiki页面}} 得到有MediaWiki页面
  • {{#ifexist:User:Klniu|该用户存在|该用户不存在}} 得到 该用户不存在
  • {{#ifexist:首页||×}} 得到

1.6 ifexpr

ifexpr计算数学表达式,并根据计算结果返回字符串。

{{ #ifexpr: <表达式> | <then字符串> | <else字符串> }}

若表达式经计算不为0,则函数返回then字符串,否则函数返回else字符串。表达式语法与expr<tt>相同。

1.7 #rel2abs

This function converts a relative file path into an absolute filepath.

<tt>{{#rel2abs: path }}
{{#rel2abs: path | base path }}

Within the path input, the following syntax is valid:

  • . → the current level
  • .. → "go up one level"
  • /foo → "go down one level into the subdirectory /foo"

If the base path is not specified, the full page name of the page will be used instead:

{{#rel2abs: /quok | Help:Foo/bar/baz }}Help:Foo/bar/baz/quok
{{#rel2abs: ./quok | Help:Foo/bar/baz }}Help:Foo/bar/baz/quok
{{#rel2abs: ../quok | Help:Foo/bar/baz }}Help:Foo/bar/quok
{{#rel2abs: ../. | Help:Foo/bar/baz }}Help:Foo/bar

Invalid syntax, such as /. or /./, is ignored. Since no more than two consecutive full stops are permitted, sequences such as these can be used to separate successive statements:

{{#rel2abs: ../quok/. | Help:Foo/bar/baz }}Help:Foo/bar/quok
{{#rel2abs: ../../quok | Help:Foo/bar/baz }}Help:Foo/quok
{{#rel2abs: ../../../quok | Help:Foo/bar/baz }}quok
{{#rel2abs: ../../../../quok | Help:Foo/bar/baz }}错误:无效路径深度:“Help:Foo/bar/baz/../../../../quok”(尝试访问根节点以上节点)

1.8 switch

switch将一个值与多个预设值比较,若有匹配时则返回指定字符串,即双射。语法是:

{{ #switch: <比较值>
| <预设值1> [= <结果1>]
| <预设值2> [= <结果2>]
| ...
| <预设值n> [= <结果n>]
| [#default = ]<缺省结果> 
}}

switch将从左往右逐一尝试,直到出现匹配。函数将返回第一个匹配值对应的结果,而忽略后面的匹配值。如果没有匹配,函数将返回缺省结果。如果缺省结果没有设置,函数将返回空串。

注意:“缺省结果”是最后一个没有等号的预设值或“#default”预设值对应的结果;如果期望把一个包含“=”号的字符串作为缺省结果,则必须采用“#default”预设值形式。例如:

#default = <span style="color:red;">red</span>

switch也可用作满射(多对一,避免重复设置结果)。即某预设值后未设置结果,这样如果该预设值与比较值匹配,则函数返回第一个有结果的预设值的结果。例如:

{{ #switch: <比较值>
| <预设值1>
| <预设值2>
| <预设值3> = <结果3>
| <缺省结果>
}}

如果比较值与预设值1或预设值2匹配,都将返回结果3。注意:“#default”后必须有“=”,但其他预设值可以使用“#default”的结果。

1.9 time

time是一个时间日期格式化函数,它的语法为:

{{ #time: 格式参数 }}

或者

{{ #time: 格式参数 | 时间参数 }}

如果时间参数未指定,则使用该条目被转换为HTML的时间(值)。注意到由于缓存的缘故,这与条目被浏览的时间可能会有高达1星期的偏差。因此可能需要手工更新,方法是加上action=purge参数访问页面。

格式参数是一种格式字符,与在PHP的date中的用法相似。

下列格式代码与在PHP中的意义一样。所不同的是...

如果时间未被指定,则显示文章最后一次被转换成HTML的时间。由于缓存的关系,此时间和你浏览文章的时间可能有最长一个星期的差别。所以有时可能需要人工更新数据,方法是编辑文章但不做任何修改即保存。

参数format是表示格式的字符串,类似于PHP的时间格式.

以下格式代码和PHP中date()函数意义相同。除了国际化(主要是语言)造成的差别以外,所有和PHP的不同点都应当作为软件的错误进行报告。其中所有的数字输出都会被替换成当地语言的时间格式,可以使用xn(见下文)恢复成显示原来的数字。

代码 描述 输出(示例)
d 一个月中的第 n 天,不足两位补充0 04
D 星期的缩写,通常不国际化
j 一个月中的第 n 天,不足两位不补0 3
l 星期的全称,通常不国际化 星期四
F 月份的的全称,通常需要国际化 5月
m 数字表示的月份,不足两位补充0 01-12
M 月份的的缩写,通常需要国际化 5月
n 数字表示的月份,不足两位不补0 1-12
Y 四位年份 2006
y 二位年份 06
H 小时,不足两位补充0 00-23
i 分钟,不足两位补充0 00-59
s 秒,不足两位补充0 00-59

以下代码是对PHP作出的扩展:

代码 描述
xn 将接下来的数字代码恢复成ASCII中的阿拉伯数字例如,在印地语中,{{ #time:H, xnH}}输出०६, 06。
xr 将接下来的数字代码显示成罗马数字
xg 输出月份名字的属格,只针对那些区分主格和属格的语言。
xx 输出"x"

任何其他字符都将不做处理直接输出。你也可以用引号来输出未经处理的字符串。

  • 引号中的字符直接输出(但不输出引号),没有配对的引号也直接输出。例如:
    • {{; #time: "现在是" F}} → 现在是 5月
    • {{ #time:i's"}} → 20'11"
  • 像PHP的date()函数一样的反斜杠转义也是支持的。 \H 直接输出 H , \" 直接输出 " 。

未来可能会增加更多格式代码,可能是完善PHP中已有功能,也可能是增加新功能。

参数time的格式参照PHP的strtotime()函数。它同时支持相对时间,如"+10 hours",用来表示时区转换。更多信息参见the GNU tar manual

下表以北京时间2008年4月6日(星期日)10时02分05秒(UTC时间2008-04-06 02:03:05)为例说明各格式参数的作用。

格式参数 说明 显示结果
A 显示AM或PM AM
a 显示am或pm am
c 显示长日期 2008-04-06T02:03:05+00:00
D 星期数,以一个汉字显示
d 日期日数,有0补齐, 06
F或M 月份 4月
G或g 当前UTC时间小时数,1位或2位数字 2
H或h 小时数,2位数字 02
i 分钟数,2位数字 03
j 日数,2位数字 6
L 日期星期数,1位数字,星期日为1,星期六为7 1
l 日期星期数,1位汉字 星期日
m 月份数,2位数字 04
N 星期数,星期一为1,星期日为7 7
n 月份数,1位或2位数字 4
r 英文长日期格式 Sun, 06 Apr 2008 02:03:05 +0000
s 秒数 05
t 该月天数 30
U 1970-1-1 0:0:1为1 1207447385
W 日期周数,显示日期为当年第几周 14
w 星期数,星期日为0,星期六为6 0
Y 日期年份,4位数字 2008
y 日期年份,2位数字 08
z 显示日期为当年第几周 96

系统默认的时间参数为当前UTC+0时间,可以使用{{#time:参数|+8 hours}}得到当前北京时间(UTC+8时间)。

时间参数可以使用绝对时间,如“2008-12-31 23:59:59”,也可以使用相对时间,如“+7 days”或者“-5 hours”得到默认时间7日之后或默认时间5小时之前的时间。也可以二者混合使用,比如{{#time:Y-m-d H:i:s|2001-2-3 04:05:06 +1 year +2 months +3 days +4 hours +5 minutes +6 seconds}}返回 2002-04-06 08:10:12

使用xr可以在其后显示罗马数字,如{{#time:xrY年xrm月xrd日|2008-12-31}}显示为MMVIII年XII月XXXI日

1.10 #titleparts

This function separates a pagetitle 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 parameter is not specified, it defaults to "0", which returns all the segments. If the first segment parameter is not specified or is "0", it defaults to "1":

{{#titleparts: Talk:Foo/bar/baz/quok }}Talk:Foo/bar/baz/quok
{{#titleparts: Talk:Foo/bar/baz/quok | 1 }}Talk:Foo
{{#titleparts: Talk:Foo/bar/baz/quok | 2 }}Talk:Foo/bar
{{#titleparts: Talk:Foo/bar/baz/quok | 2 | 2 }}bar/baz

Negative values are accepted for both values. Negative values for number of segments effectively 'strips' segments from the end of the string. Negative values for first segment translates to "add this value to the total number of segments", loosely equivalent to "count from the right":

{{#titleparts: Talk:Foo/bar/baz/quok | -1 }}Talk:Foo/bar/baz
{{#titleparts: Talk:Foo/bar/baz/quok | | -1 }} quok
{{#titleparts: Talk:Foo/bar/baz/quok | -1 | 2 }} bar/baz Strips one segment from the end of the string, then returns the second segment and beyond

The string is split a maximum of 25 times; further slashes are ignored. The string is also limited to 255 characters, as it is treated as a page title:

{{#titleparts: a/b/c/d/e/f/g/h/i/j/k/l/m/n/o/p/q/r/s/t/u/v/w/x/y/z/aa/bb/cc/dd/ee | 1 | 25 }}y/z/aa/bb/cc/dd/ee
Warning: {{{1}}}

You can use #titleparts as a small "string parser & converter", but consider that it returns the first substring capitalized. If lower case is needed, use lc: function to control output.

{{#titleparts: one/two/three/four|1|1 }}One
{{#titleparts: one/two/three/four|1|2 }}two
{{lc: {{#titleparts: one/two/three/four|1|1 }} }}one

1.11 language

#language得到指定语言代码的该语言名称。

{{#language:de}} 得到 Deutsch
{{#language:en}} 得到 English
{{#language:ja}} 得到 日本语
{{#language:nl}} 得到 Nederlands
{{#language:zh}} 得到 中文
{{#language:zh-cn}} 得到 ‪中文(中国大陆)‬
{{#language:zh-tw}} 得到 ‪中文(台湾)‬
{{#language:zh-hk}} 得到 ‪中文(香港)‬
{{#language:zh-sg}} 得到 ‪中文(新加坡)‬

2 subst

应用subst:到解析器函数,必须在subst:和#之间不能有空格,才可以正常工作。

3 表格

解析器函数中由于使用了“|”管道符做参数分隔符,所以不能包括表格所需要的“|”符。要想在输出中包含表格,可以通过以下两个办法达到:

  1. 通过嵌套模板来达到隐藏“|”的目的。
  2. 使用HTML语法。
个人工具
分类
化学
[×] 國學
学佛
[×] 数学
物理
生活
[×] 英语
读书
辞典
廣告