Wikipedia:Lua
Lua係隻程式語言,喺維基百科道透過MediaWiki擴充功能Scribunto用到。Lua代碼可以透過Scribunto嘅「{{#invoke:}}」功能嵌入維基模道。呢個擴充功能支援Lua 5.1(2012年版)。
啲Lua源碼儲存喺啲叫做「模組」嘅版度(譬如係模組:Example)。呢啲獨立嘅模組可以被呼叫(用{{#invoke:<模組名>|<函數名>|(可選) 參數1 | 參數2...}}
)。例子:
Wikitext | 結果 |
---|---|
{{#invoke:Example|hello}}
|
Hello World! |
簡介
編輯Lua模組用法
編輯用#invoke來調用Lua模組,具體寫法:
{{#invoke:模組名|模組入面嘅函數名|第一參數|第二參數|有名嘅參數個名 = 對應嘅值}}
例如模組:BaseConvert可以轉換數值嘅進制:
{{#invoke:BaseConvert|convert|n=9|base=2}}
→ 1001 (將十進制9轉成二進制){{#invoke:BaseConvert|convert|n=30|base=16}}
→ 1E(將十進制30轉成十六進制)
點整一個Lua模組
編輯Lua語言基本語法同埋維基上面Lua庫嘅用法都可以去Scribunto Lua 参考手册道睇,亦可以參考模組:HelloWorld,入面有詳細解釋。
注意:模組名分大小寫。
運行模組
編輯模組可以透過#invoke解析器函數喺普通wiki版面度運行。#invoke嘅句法同模相近,但亦有啲唔同。最重要嘅分別係你要指明函數名。一個函數係一組指示,接收輸入值,處理輸入,然後返回一個輸出值。[1] This is much like what a template does: you give it arguments, it processes them, and you get a result. However, you can define many functions in one Lua module, whereas you can only define one template on one page.
Furthermore, you can't just run a Lua module directly – you can only run one of the module's functions. The module is just a container for the functions, and doesn't do anything by itself. So there are two reasons that we need to input a function name: we can't run a module by itself, and without specifying a function name, Lua will not know which function it is we want to run.
The simplest way to run a module from a wiki page is like this:
{{#invoke:module name|function name}}
For example, we can run Module:Example in this way, which has a function named "hello".
{{#invoke:Example|hello}}
→ Hello World!
用參數
編輯參數(argument)are passed to modules in the same way that they are passed to templates. Note, however, that the text after the first pipe character is always the function name; the first positional argument is the text after the second pipe.
{{#invoke:module name|function name|first positional argument|second positional argument|named argument = value}}
In Module:Example, the "hello_to" function greets different people depending on the first positional argument. It works like this:
{{#invoke:Example|hello_to|Kate}}
→ Hello, Kate!{{#invoke:Example|hello_to|Fred}}
→ Hello, Fred!
A third function in Module:Example, named has "count_fruit" uses the named arguments bananas
and apples
to count the number of bananas and apples we have. It can be run like this:
{{#invoke:Example|count_fruit|apples=3|bananas=4}}
→ I have 4 bananas and 3 apples{{#invoke:Example|count_fruit|bananas=5|apples=2}}
→ I have 5 bananas and 2 apples
唔少模組都有解說版,講解邊啲參數可以用,同埋啲參數嘅作用。
歷史
編輯Sordid history. {{qif}}, ParserFunctions, Lua extension, wiki scripting language debated (JavaScript v. Lua), mw:Extension:WikiScripts, Tim writes Scribunto with initial support for Lua.
Discussed for years, Lua was installed in 2012 for testing on test2.wikipedia.org, with open invitation to all editors to experiment with developing Lua modules. Lua was installed on the English Wikipedia in February 2013, after testing on mediawiki.org and Wikimedia test wikis.
關於Lua
編輯Lua is a scripting language which can be used to analyze data, calculate expressions, and format results using functions or object-oriented programming. Although some Lua scripts can be kept simple, for easy understanding, Lua allows complex structures including tables, dynamic functions, and associative arrays where index subscripts can be words as well as index numbers. Lua also supports recursion of re-nested functions, so care should be taken to avoid excessive complexity where other users would not understand how to maintain a Lua module. The following is the source code of the module used for the examples above.
<strong class="error"><span class="scribunto-error mw-scribunto-error-959cb5cb">Lua error in package.lua at line 80: module 'Module:CallAssert' not found.</span></strong>
A sample of Lua is highlighted by tag "<syntaxhighlight lang="lua">...</syntaxhighlight>" placed around the Lua source code. To view some more complex examples of Lua, see article: "Lua (programming language)".
For instructions on how to use Lua within MediaWiki (and hence Wikipedia), see mw:Extension:Scribunto/Lua reference manual.
Unit testing
編輯A few unit testing frameworks are available for Lua scripts on Wikipedia. These allow an editor to execute the module with a given set of inputs and verify that the expected outputs are produced. They are useful for rapidly detecting software regressions, where modifications to a script introduce new (or identify old) problems.
By convention, unit tests for a module like Module:Example are placed in Module:Example/testcases, and are executed on Module talk:Example/testcases.
Module:ScribuntoUnit and Module:UnitTests are the more-used test frameworks. Category:Modules for test tools has a few other to review which may be interesting.
Wikipedia-specific features
編輯Overall: Lua can only get input as text strings passed to the {{#invoke:}}
and what can be fetched via mw.title.new(...):getContent() and frame:expandTemplate(). Lua output will not be preprocessed unless frame:preprocess() is explicitly called, meaning that template calls, parser functions, etc. in the output will not work correctly. Also, all Lua in the page is limited to 10 seconds CPU time (you can look in the source code of a rendered page to see how long a template or module took to parse). And relative to standard Lua, Scribunto's Lua lacks all sorts of functions (see mw:Extension:Scribunto/Lua reference manual § Differences from standard Lua).
Lua input limitations
編輯Lua code in Scribunto is only run when the page is being parsed. Therefore, the only user input that Lua can receive is by page editing – it cannot create a box that calculates the square root of a number you type in, or recalculate a piece of the Mandelbrot set depending on which part of the parent set you click on. The input Lua can receive includes any transcludeable text page on Wikipedia. This does not include graphics files (not even .SVG files, although they are actually text, unless you cut and paste it onto a Wiki text page), the list of pages listed in a category, nor the contents of non-transcludeable special pages.
Wikitext
編輯Transcluded Wikipedia headers frequently contain a hidden code such as "UNIQ5ae8f2aa414ff233-h-3--QINU" which may need to be stripped out in order for them to be parsed effectively.
Wikilinks using the Pipe trick [[Wikipedia:Help| ]] won't work if returned as output – they need to be written explicitly as [[Wikipedia:Help|Help]]. Other pre-save transforms, such as replacing ~~~~ with signatures, will also fail to be processed. Template transclusions, parser function calls, and variable substitutions (i.e. anything with a {{...}}
) will not be processed, nor will tags such as <ref>
or <nowiki>
.
Labeling converted templates
編輯呢個模用到Lua語言: |
Please place the {{lua}} template on the documentation subpage of all templates that use Lua. It will help to better communicate Lua usage and template conversions.
睇埋
編輯- Special:PrefixIndex/Module: – tracking of Lua modules can be done by using Special:PrefixIndex
- Help:Lua debugging – a how-to guide about debugging Lua modules
- Module:Sandbox provides a pseudo-namespace for experimenting with Lua modules
- WP:Lua requests – requests for Lua-based templates or tasks
- Category:Lua-based templates – groups of Lua-based templates
- Category:Lua metamodules
- Manual:Coding conventions/Lua – standards to improve the readability of code through consistency
- mw:Extension:Scribunto/Lua reference manual and all Modules.
- Wikipedia:Advanced template coding
備註
編輯- ↑ You can also have multiple output values, but functions that do this are not normally meant to be accessed from wiki pages.