Module:Sandbox/Frietjes: Difference between revisions

Content deleted Content added
No edit summary
Tag: Reverted
No edit summary
 
(39 intermediate revisions by the same user not shown)
Line 1:
local p = {}
 
local math_module = require("Module:Math")
 
local function rnd(num, digits)
-- This function implements {{rnd}}
return math_module._precision_format(tostring(num), digits)
end
 
function p.ifexists(frame)
page = frame.args[1]
if not page then return (frame.args['no'] or '') end
if mw.title.new(page).exists then return (frame.args['yes'] or 'yes') end
return (frame.args['no'] or '')
end
 
function p.lists(frame)
local s = '\n' .. (frame.args[1] or '') .. '\n'
s = mw.ustring.gsub(s, '([\r\n])%*([^\r\n]*)', '%1<ul><li>%2</li></ul>')
s = mw.ustring.gsub(s, '([\r\n])#([^\r\n]*)', '%1<ol><li>%2</li></ol>')
s = mw.ustring.gsub(s, '</ol>%s*([\r\n]*)<ol>', '%1')
s = mw.ustring.gsub(s, '</ul>%s*([\r\n]*)<ul>', '%1')
s = mw.ustring.gsub(s, '^[\r\n](.*)[\r\n]$', '%1')
return s
end
 
function p.precision(frame)
return math_module._precision(frame.args[1])
end
 
function p.wpct(frame)
local w = tonumber(frame.args[1]) or 0
local l = tonumber(frame.args[2]) or 0
local pct = '–'
if (w + l) > 0 then
pct = rnd(w / (w + l), 3):gsub('^0', '')
end
return pct
end
 
function p.extractcolor(frame)
local str = frame.args[1] or ''
local color = mw.ustring.match(';' .. str .. ';', '.*;%s*([Cc][Oo][Ll][Oo][Rr]%s*:%s*.-)%s*;')
return color or 'NO MATCH'
end
 
local function getBestStatement(item_id, property_id)
Line 18 ⟶ 62:
function p.hasOSM(frame)
return getBestStatement(mw.wikibase.getEntityIdForCurrentPage(), 'P402') and 'yes' or 'no'
end
 
function p.chart(frame)
local chart = require('Module:Chart')['bar-chart']
return chart(frame)
end
 
Line 96 ⟶ 146:
 
return res
end
 
-- This module performs validation checks for [[WP:DYK]] hooks
local validationPatternGroups = {
{
-- Check that hooks start with three periods, followed by an acceptable
-- follow-on word.
"^ *%.%.%. *that",
"^ *%.%.%. *about",
},
{
-- Check that hooks end with a question mark, or another acceptable
-- phrase.
[[.%?%]*'*"? *$]],
[[.&#63;</span>%]*'*"? *$]],
"[Yy]ou probably did%.+ *$",
}
}
 
function p._isValidHook(hook)
-- Whether the given hook is valid.
-- We use the patterns in the validationPatternGroups table to find whether
-- a hook is valid or not. Hooks are treated as valid if they match at least
-- one pattern from each group.
for _, patternGroup in ipairs(validationPatternGroups) do
local found = false
for _, pattern in ipairs(patternGroup) do
if mw.ustring.find(hook, pattern) then
found = true
break
end
end
if not found then
return false
end
end
return true
end
 
function p.isValidHook(frame)
local hook = frame.args[1]
if not hook then
error("No hook specified")
end
hook = hook:match("^%s*(.*?)%s*$") -- Trim whitespace
return hook
-- if p._isValidHook(hook) then
-- return "yes"
-- else
-- return ""
-- end
end