https://de.touhouwiki.net/index.php?title=Modul:Common&feed=atom&action=historyModul:Common - Versionsgeschichte2024-03-29T05:26:10ZVersionsgeschichte dieser Seite in Touhou WikiMediaWiki 1.39.3https://de.touhouwiki.net/index.php?title=Modul:Common&diff=19733&oldid=prevSatorioshi: Die Seite wurde neu angelegt: „--[[ Library for all the functions that are going to be shared by other scripts. To use all the functions NOT defined as local, add at the beginning of script…“2016-09-01T19:39:26Z<p>Die Seite wurde neu angelegt: „--[[ Library for all the functions that are going to be shared by other scripts. To use all the functions NOT defined as local, add at the beginning of script…“</p>
<p><b>Neue Seite</b></p><div>--[[<br />
Library for all the functions that are going to be shared by other scripts.<br />
To use all the functions NOT defined as local, add at the beginning of script the following:<br />
require("Module:Common")<br />
<br />
The names aren't being sorted, because this could break the function dependencies.<br />
--]]<br />
<br />
-- create global object "common" for most of exported functions<br />
local common = {}<br />
<br />
-- == constants ==<br />
common.romanizable = {'zh-hans', 'zh-hant', 'zh-cn', 'zh-tw', 'zh-hk', 'zh-sg', 'zh-mo', 'zh-my', 'ja', 'ko', 'vi'}<br />
-- Note: There's a full-width space to space conversion. Don't remove it!<br />
common.normalization_table = {[' '] = ' ', ['~'] = '~', ['!'] = '!', ['?'] = '?'}<br />
<br />
-- == helper functions ==<br />
-- checks if string is set and if it's non-empty<br />
function common.isset(target)<br />
return target ~= nil and target ~= ""<br />
end<br />
<br />
--[[ simulates the (a ? b : c) notation of C/C++ and PHP languages.<br />
Similar to {{#if:{{{a|}}}|{{{b|}}}|{{{c|}}}}} from parser functions. ]]<br />
function common.cv(a, b, c)<br />
if a then return b else return c end<br />
end<br />
<br />
--slices a table to return another table containing values within a certain range<br />
--source: http://snippets.luacode.org/snippets/Table_Slice_116<br />
function common.sliceTable (values,i1,i2)<br />
local res = {}<br />
local n = #values<br />
-- default values for range<br />
i1 = i1 or 1<br />
i2 = i2 or n<br />
if i2 < 0 then<br />
i2 = n + i2 + 1<br />
elseif i2 > n then<br />
i2 = n<br />
end<br />
if i1 < 1 or i1 > n then<br />
return {}<br />
end<br />
local k = 1<br />
for i = i1,i2 do<br />
res[k] = values[i]<br />
k = k + 1<br />
end<br />
return res<br />
end<br />
<br />
-- checks if a given page exists<br />
function common.exists(page)<br />
if not common.isset(page) then return false end<br />
return mw.getCurrentFrame():preprocess('{{#ifexist:' .. page .. '|1|0}}') == '1'<br />
end<br />
<br />
--[[ Tries to get contents of a page with given name.<br />
If the function fails it returns nil (page doesn't exist or can't be loaded)<br />
On success it returns the contents of page (it can be be partially preprocessed, so watch out for parser markers). ]]<br />
function common.getPage(name)<br />
if not common.isset(name) then return nil end<br />
<br />
local frame = mw.getCurrentFrame()<br />
<br />
-- do a safe call to catch possible errors, like "template doesn't exist"<br />
local stat,page = pcall(frame.expandTemplate, frame, {title = ':' .. name})<br />
<br />
if not stat then<br />
-- TODO: 'page' contains the error message. Do some debugging?<br />
return nil<br />
end<br />
<br />
return page<br />
end<br />
<br />
function common.stripTags(text)<br />
if common.isset(text) then<br />
local tmp<br />
repeat<br />
tmp = text<br />
-- a pair of tags, like <td style="">...</td><br />
text = string.gsub(text, '<%s*(%w+).->(.-)<%s*/%s*%1%s*>', '%2')<br />
-- closed tag, like <br/><br />
text = string.gsub(text, '<%s*%w+%s*/%s*>', '')<br />
until tmp == text<br />
end<br />
return text<br />
end<br />
<br />
--[[ Sort table and remove repeating elements.<br />
Since tbl is passed as reference, any changes in it will affect the passed table. ]]<br />
function common.trunkTable(tbl)<br />
table.sort(tbl)<br />
local last<br />
local redo<br />
repeat<br />
redo = false<br />
last = nil<br />
for k,v in pairs(tbl) do<br />
if v ~= last then<br />
last = v<br />
else<br />
table.remove(tbl, k)<br />
redo = true<br />
break<br />
end<br />
end<br />
until not redo<br />
end<br />
<br />
--[[ Checks if a given value is among the elements of a table and returns its index.<br />
Returns nil if it can't find it. ]]<br />
function common.isInTable(tbl, val)<br />
for k,v in pairs(tbl) do<br />
if v == val then return k end<br />
end<br />
return nil<br />
end<br />
<br />
--[[ Compare 'n' elements in two tables, starting from 's1' in first table and 's2' in second table. ]]<br />
function common.partialTableCompare(t1, t2, s1, s2, n)<br />
if n < 1 then return true end -- basically there's nothing to compare, so no differences were found<br />
<br />
for i = 0,(n-1) do<br />
-- Note that nil values are also valid.<br />
if t1[s1+i] ~= t2[s2+i] then return false end<br />
end<br />
<br />
return true<br />
end<br />
<br />
return common</div>Satorioshi