사용자:하늘/루아: 두 판 사이의 차이

편집 요약 없음
잔글 (Cerulean님이 사용자:Cerulean/루아/html 문서를 넘겨주기를 만들지 않고 사용자:Cerulean/루아 문서로 이동했습니다)
(차이 없음)

2021년 9월 11일 (토) 17:30 판

This is a list of Lua functions and the wikitext parser functions and magic words that they are comparable to.

It is slightly out of date for what wikitext parser functions can do.

Parser functions

Wikitext Lua
{{#if: x | not empty | empty}}
if x then
    'not empty'
else
    'empty'
end
{{#ifeq: x | y | identical | different}}
if x == y then
    'identical'
else
    'different'
end
{{#iferror: function | bad | good }}
 
if tostring(mw.getCurrentFrame():preprocess('function')):find('<strong class="error"') then
    'bad'
else
    'good'
end
{{#ifexpr: 1+2=3 |true|false}}
if 1 + 2 == 3 then
    'true'
else
    'false'
end
[Expensive]
{{#ifexist: namespace:title | exists | doesn't exist }}
[Expensive]
if mw.title.new( 'title', 'namespace' ).exists == true then
    "exists"
else
    "doesn't exist"
end
rel2abs
{{#switch: {{{1}}} | foo = Foo | Bar }}
local cases = {
    default = "Bar",
    foo = "Foo",
}

local pframe = frame:getParent()
local arg = pframe.args[1]
cases[arg] or cases["default"]
{{#time: format | timestamp }}
mw.getContentLanguage():formatDate( 'format', 'timestamp' )
{{#timel: format | timestamp }}
mw.getContentLanguage():formatDate( 'format', 'timestamp', 1 )
titleparts

Math

Unlike in wikicode, lua does not use functions like ifexpr and expr. Add the lua operators and functions straight into your module.

Grouping

Wikicode Lua
() ()

Numbers

Wikicode Lua
1234.5 1234.5
2e3 2e3
pi math.pi
e
2+2 2+2
2-1 2-1

Unary

Wikicode Lua
not not
ceil1.2 math.ceil(1.2)
trunc
floor1.2 math.floor(1.2)
abs-2 math.abs(-2)
exp43 math.exp(43)
ln2 math.log(2)
cos0.1 math.cos(0.1)
tan0.1 math.tan(0.1)
acos0.1 math.acos(0.1)
asin0.1 math.asin(0.1)
atan0.1 math.atan(0.1)

Binary

Wikicode Lua
2^3 2^3
2*3 2*3
2/3
2div3
2/3
30mod7 30%7
+2 2
-2 -2

Logic

Wikicode Data type Lua Data type
3.0=3 Integer (0 or 1) 3.0==3 Boolean (true or false)
3!=4
3<>4
Integer 3~=4 Boolean
4>3 Integer 4>3 Boolean
4>=3 Integer 4>=3 Boolean
3<=4 Integer 3<=4 Boolean
3<=4 Integer 3<=4 Boolean
and Integer and Boolean
or Integer or Boolean

틀:Clear

Magic words

Date and time

Wikicode Lua
{{CURRENTYEAR}}
os.date( "%Y" )
{{CURRENTMONTH}}
os.date( "%m" )
{{CURRENTMONTHNAME}}
mw.getContentLanguage():formatDate( 'F' )
{{CURRENTMONTHNAMEGEN}}
mw.getContentLanguage():formatDate( 'xg' )
{{CURRENTMONTHABBREV}}
mw.getContentLanguage():formatDate( 'M' )
{{CURRENTDAY}}
os.date( "%e" )
{{CURRENTDAY2}}
os.date( "%d" )
{{CURRENTDOW}}
os.date( "%w" )
{{CURRENTDAYNAME}}
mw.getContentLanguage():formatDate( 'l' )
{{CURRENTTIME}}
os.date( "%R" )
{{CURRENTHOUR}}
os.date( "%H" )
{{CURRENTWEEK}}
mw.getContentLanguage():formatDate( 'W' )
{{CURRENTTIMESTAMP}}

Technical metadata

Wikicode Lua
{{SITENAME}}
mw.site.siteName
{{SERVER}}
mw.site.server
{{SERVERNAME}}
{{DIRMARK}}
mw.language.getContentLanguage():getDirMark()
{{SCRIPTPATH}}
mw.site.scriptPath
{{STYLEPATH}}
mw.site.stylePath
{{CURRENTVERSION}}
mw.site.currentVersion
{{CONTENTLANGUAGE}}
mw.getContentLanguage():getCode()
{{PAGEID}}
mw.title.getCurrentTitle().id
{{PAGESIZE:page name}}
{{PROTECTIONLEVEL:edit}} [Expensive]
local protection = mw.title.getCurrentTitle().protectionLevels["edit"]
table.concat(protection)
{{REVISIONID}}
{{REVISIONDAY}}
{{REVISIONDAY}}
{{REVISIONMONTH}}
{{REVISIONMONTH1}}
{{REVISIONYEAR}}
{{REVISIONTIMESTAMP}}
{{REVISIONUSER}}
{{DISPLAYTITLE:title}}
{{DEFAULTSORT:sortkey}}

Statistics

Wikicode Lua
{{NUMBEROFPAGES}}
mw.site.stats.pages
{{NUMBEROFARTICLES}}
mw.site.stats.articles
{{NUMBEROFFILES}}
mw.site.stats.files
{{NUMBEROFEDITS}}
mw.site.stats.edits
{{NUMBEROFVIEWS}}
mw.site.stats.views
{{NUMBEROFUSERS}}
mw.site.stats.users
{{NUMBEROFADMINS}}
mw.site.stats.admins
{{NUMBEROFACTIVEUSERS}}
mw.site.stats.activeUsers
[Expensive]
{{PAGESINCATEGORY:categoryname}}
[Expensive]
mw.site.stats.pagesInCategory( 'categoryname' )
{{NUMBERINGROUP:groupname}}
mw.site.stats.usersInGroup( 'groupname' )

Page names

Wikicode Lua
{{FULLPAGENAME}}
mw.title.getCurrentTitle().prefixedText
{{PAGENAME}}
mw.title.getCurrentTitle().text
{{BASEPAGENAME}}
mw.title.getCurrentTitle().baseText
{{SUBPAGENAME}}
mw.title.getCurrentTitle().subpageText
{{SUBJECTPAGENAME}} [Expensive]
mw.title.getCurrentTitle().subjectPageTitle

or an non expensive alternative:

mw.title.getCurrentTitle().subjectNsText .. ":" .. mw.title.getCurrentTitle().text
{{TALKPAGENAME}} [Expensive]
mw.title.getCurrentTitle().talkPageTitle

Namespaces

Wikicode Lua
{{NAMESPACE}}
mw.title.getCurrentTitle().nsText
{{NAMESPACENUMBER}}
mw.title.getCurrentTitle().namespace
{{SUBJECTSPACE}}
mw.title.getCurrentTitle().subjectNsText
{{TALKSPACE}}

URL data

Wikicode Lua
{{localurl:page|query}}
mw.uri.localUrl( 'page', 'query' )
{{fullurl:page|query}}
mw.uri.fullUrl( 'page', 'query' )
{{canonicalurl:page|query}}
mw.uri.canonicalUrl( 'page', 'query' )
{{filepath:file name}}
{{urlencode:string|QUERY}}
mw.uri.encode( 'string', QUERY )
{{anchorencode:string}}
mw.uri.anchorEncode( 'string' )

Namespaces

Wikicode Lua
{{ns:0}}
mw.site.namespaces[0].name
{{ns:Project}}
mw.site.namespaces.Project.name

Formatting

Wikicode Lua
{{formatnum:number}}
mw.getContentLanguage():formatNum( number )
#dateformat
{{lc:string}}
mw.ustring.lower( 'string' )
{{lcfirst:string}}
mw.getContentLanguage():lcfirst( 'string' )
{{uc:string}}
mw.ustring.upper( 'string' )
{{ucfirst:string}}
mw.getContentLanguage():ucfirst( 'string' )
{{padleft:xyz|stringlength}}
{{padright:xyz|stringlength}}

Localisation

Wikicode Lua
{{plural:2|is|are}}
mw.getContentLanguage():plural( 2, 'is', 'are' )
{{grammar:N|noun}}
mw.getContentLanguage():grammar( 'N', 'noun' )
{{gender:username|male|female|neutral }}
mw.getContentLanguage():gender( 'username', { 'male', 'female', 'neutral' } )
{{int:message}}
mw.message.new( 'message' ):plain()
{{int:editsectionhint|MediaWiki}}
mw.message.new( 'editsectionhint', 'MediaWiki' ):plain()

Miscellaneous

Wikicode Lua
{{#language:code|inlanguage}}
mw.language.fetchLanguageName( 'code', 'inLanguage' )
{{#special:special page name}}
{{#speciale:special page name}}
{{#tag:tagname |some text |attribute1=value1 |attribute2=value2 }}
frame:extensionTag( 'tagname', 'some text', {
    attribute1 = 'value1', attribute2 = 'value2'
} )

HTML library

mw.html is a fluent interface for building complex HTML from Lua. A mw.html object can be created using [[<tvar|anchor>#mw.html.create</>|mw.html.create]]. </translate>

틀:Anchor <translate> Functions documented as mw.html.name are available on the global mw.html table; functions documented as mw.html:name and html:name are methods of an mw.html object (see [[<tvar|anchor>#mw.html.create</>|mw.html.create]]).

A basic example could look like this: </translate>

local div = mw.html.create( 'div' )
div
     :attr( 'id', 'testdiv' )
     :css( 'width', '100%' )
     :wikitext( 'Some text' )
     :tag( 'hr' )
return tostring( div )
-- Output: <div id="testdiv" style="width:100%;">Some text<hr /></div>

mw.html.create

태그 생성 <translate> mw.html.create( tagName, args )</translate>

<translate> Creates a new mw.html object containing a tagName html element. You can also pass an empty string or nil as tagName in order to create an empty mw.html object.

args can be a table with the following keys:

  • args.selfClosing: Force the current tag to be self-closing, even if mw.html doesn't recognize it as self-closing
  • args.parent: Parent of the current mw.html instance (intended for internal usage)

</translate>

mw.html:node

<translate> html:node( builder )</translate>

<translate> Appends a child mw.html (builder) node to the current mw.html instance. If a nil parameter is passed, this is a no-op. A (builder) node is a string representation of an html element. </translate>

mw.html:wikitext

텍스트 출력? html:wikitext( ... )

<translate> Appends an undetermined number of wikitext strings to the mw.html object.

Note that this stops at the first nil item. </translate>

mw.html:newline

새로운 행 html:newline()

<translate> Appends a newline to the mw.html object. </translate>

mw.html:tag

자식노드 삽입 <translate> html:tag( tagName, args )</translate>

<translate> Appends a new child node with the given tagName to the builder, and returns a mw.html instance representing that new node. The args parameter is identical to that of [[<tvar|anchor>#mw.html.create</>|mw.html.create]] </translate>

mw.html:attr

<translate> html:attr( name, value )</translate>
<translate> html:attr( table )</translate>

<translate> Set an HTML attribute with the given name and value on the node. Alternatively a table holding name->value pairs of attributes to set can be passed. In the first form, a value of nil causes any attribute with the given name to be unset if it was previously set. </translate>

mw.html:getAttr

<translate> html:getAttr( name )</translate>

<translate> Get the value of a html attribute previously set using [[<tvar|anchor>#mw.html:attr</>|html:attr()]] with the given name. </translate>

mw.html:addClass

<translate> html:addClass( class )</translate>

<translate> Adds a class name to the node's class attribute. If a nil parameter is passed, this is a no-op. </translate>

mw.html:css

<translate> html:css( name, value )</translate>
<translate> html:css( table )</translate>

<translate> Set a CSS property with the given name and value on the node. Alternatively a table holding name->value pairs of properties to set can be passed. In the first form, a value of nil causes any property with the given name to be unset if it was previously set. </translate>

mw.html:cssText

<translate> html:cssText( css )</translate>

<translate> Add some raw css to the node's style attribute. If a nil parameter is passed, this is a no-op. </translate>

mw.html:done

html:done()

<translate> Returns the parent node under which the current node was created. Like jQuery.end, this is a convenience function to allow the construction of several child nodes to be chained together into a single statement. </translate>

mw.html:allDone

html:allDone()

<translate> Like [[<tvar|anchor>#mw.html:done</>|html:done()]], but traverses all the way to the root node of the tree and returns it.