Calc
天下维客,你可以修改的网络知识库
Calc扩展允许经由简单的MediaWiki扩展去实现简单的数学公式。
[编辑]
安装
在"extensions"文件夹下建立一个文件,命名为"Calc.php"。之后添加下列代码:
<?php
$wgExtensionFunctions[] = "wfCalcExtension";
function wfCalcExtension() {
global $wgParser;
# register the extension with the WikiText parser
# the first parameter is the name of the new tag.
# In this case it defines the tag <calc> ... </calc>
# the second parameter is the callback function for
# processing the text between the tags
$wgParser->setHook( "calc", "doMath" );
}
# The callback function for converting the input text to HTML output
function doMath( $input ) {
global $wgOut;
# Parser
$input = $wgOut->parse($input,false);
$array = explode(' ', $input);
if($array[0]=="incr")
return $array[1]+1;
if($array[0]=="decr")
return $array[1]-1;
if($array[0]=="add")
return $array[1]+$array[2];
if($array[0]=="sub")
return $array[1]-$array[2];
return <i>;</i>
}
?>
然后再在LocalSettings.php的末尾添加这段代码:
include("extensions/calc.php");
然后就可以使用了,你可以在沙盒中测试。
[编辑]
用法
| 当你输入了... | 结果就会是... |
|---|---|
| <calc>incr 999</calc> | 1000 |
| <calc>decr 1001</calc> | 1000 |
| <calc>add 1001 -1</calc> | 1000 |
| <calc>sub 1001 +1</calc> | 1000 |
| <calc>incr </calc> | 1 |
| <calc>decr </calc> | -1 |
| <calc>add 1001 </calc> | 1001 |
| <calc>sub 1001 </calc> | 1001 |
| <calc>add </calc> | 0 |
| <calc>sub </calc> | 0 |
| <calc>incr kill</calc> | 1 |
| <calc>decr kill</calc> | -1 |
| <calc>add kill -1</calc> | -1 |
| <calc>sub kill +1</calc> | -1 |
| <calc>add 1001 kill</calc> | 1001 |
| <calc>sub 1001 kill</calc> | 1001 |
| <calc>add kill </calc> | 0 |
| <calc>sub kill </calc> | 0 |
| <calc>add </calc> | 0 |
| <calc>sub </calc> | 0 |
| <calc></calc> | |
| <calc>add 45 92</calc> | 137 |
| <calc>sub 1190 190</calc> | 1000 |
| <calc>add {{CURRENTMONTH}} 99</calc> | 111 |
[编辑]


