Module:Module sandbox/sandbox: Difference between revisions
Appearance
Content deleted Content added
No edit summary |
No edit summary Tags: Reverted Mobile edit Mobile web edit Advanced mobile edit |
||
Line 1: | Line 1: | ||
local p = {} |
|||
-- Note: Originally written on English Wikipedia as [[w:en:Module:Attached_KML]] |
|||
local m={} |
|||
-- ##### Localisation (L10n) settings ##### |
|||
local |
local currentYear = tonumber(os.date("%Y")) |
||
-- Template parameter names |
|||
-- (replace values in quotes with local parameter names) |
|||
para = { |
|||
display = "display", |
|||
from = "from", |
|||
header = "header", |
|||
title = "title", |
|||
wikidata = "wikidata", |
|||
demo = "demo", |
|||
}, |
|||
-- Other configuration settings |
|||
config = { |
|||
-- controls the format used for inline display, can be set to "box" (default) or "line" |
|||
-- "box" example: https://en.wikipedia.org/wiki/Template:Attached_KML |
|||
-- "line" example: https://sv.wikipedia.org/wiki/Mall:KML |
|||
inline_format = "box", |
|||
}, |
|||
-- Other strings |
|||
str = { |
|||
inline = "inline", -- used with display parameter: (|display=inline) or (|display=title) or (|display=inline,title) or (|display=title,inline) |
|||
title = "title", -- (as above) |
|||
dsep = ",", -- separator between inline and title (comma in the example above) |
|||
kml_prefix = "Template:Attached KML/", -- local KML files are stored as subpages of this location |
|||
default_title = "Route map", -- default title for links at top of page, when title parameter not used in transclusion |
|||
default_header = "", -- default header for links in inline box, when header parameter not used in transclusion |
|||
kml_file = "KML file", -- text to display for link to raw KML file |
|||
edit = "edit", -- text to display for link to edit KML file |
|||
help = "help", -- text to display for help page link |
|||
help_location = "Help:Attached KML", -- page to link to for help page link |
|||
err_prepend = "Attached KML", -- text to prepend to the error messages, when shown at top of page (display=title) |
|||
cat = { -- tracking categories: full wikimarkup required, or set to the empty string ("") to not to track the condition |
|||
wikidata_kml = "[[Category:Articles using KML from Wikidata]]", -- tracks mainspace articles using KML from Wikidata |
|||
local_kml = "[[Category:Articles using KML not from Wikidata]]", -- tracks mainspace articles not using KML from Wikidata |
|||
error_mqid = "[[Category:Attached KML errors|M]]", -- tracks malformed_qid error |
|||
error_badqid = "[[Category:Attached KML errors|W]]", -- tracks bad_qid error |
|||
error_noitem = "[[Category:Attached KML errors|N]]", -- tracks no_item error |
|||
error_from = "[[Category:Attached KML errors|F]]", -- tracks bad_from error |
|||
error_nokml = "[[Category:Attached KML errors|K]]", -- tracks no_kml error |
|||
}, |
|||
line = { -- these strings are only needed if using 'inline_format = "line"' configuration |
|||
start = "", -- wikitext to display at start of line, may include image markup, should start with a space |
|||
separator = "", -- text to display between links to external mapping providers, should include spaces |
|||
}, |
|||
} |
|||
} |
|||
local function calculateAge(dateString) |
|||
L10n.str.err = { -- error messages |
|||
local year = tonumber(dateString:match("%d+")) -- Extract the year from the date string |
|||
malformed_qid = "Error: malformed item id in <code><nowiki>|" .. L10n.para.wikidata .. "=</nowiki></code>", -- item id doesn't match pattern (number with Q prefix) |
|||
if year then |
|||
bad_qid = "Error: item specified on Wikidata, or in <code><nowiki>|" .. L10n.para.wikidata .. "=</nowiki></code>, is not a KML file <small>(P31→Q26267864 not found)</small>", -- item doesn't have a P31→Q26267864 statement |
|||
return currentYear - year .. " years old" |
|||
no_item = "Error: item specified in <code><nowiki>|" .. L10n.para.wikidata .. "=</nowiki></code> not found on Wikidata", -- item not found on wikidata |
|||
end |
|||
bad_from = "Error: KML file not found, check <code><nowiki>|" .. L10n.para.from .. "=</nowiki></code>", -- KML specified by from parameter doesn't exist |
|||
no_kml = "Error: KML file not found", -- no KML file found |
|||
} |
|||
local success, parsedDate = pcall(function() return os.date("%Y", os.time{year=tonumber(dateString)}) end) |
|||
-- Masks for external mapping providers, in the form: |
|||
if success then |
|||
-- externalLinkMasks[index-number] = { short = "short-label", long = "long-label", link = "url" }' |
|||
return currentYear - parsedDate .. " years old" |
|||
-- The short label is used for the title links; the long label is used for the inline links |
|||
end |
|||
-- Links in the output will be ordered by index-number |
|||
-- Instead of kml file's raw url or encoded raw url, use __KML_URL__ or __KML_URL_E__ |
|||
local externalLinks = {} |
|||
externalLinks[1] = { |
|||
short = "Bing", |
|||
long = "Display on Bing Maps", |
|||
link = "http://www.bing.com/maps/?mapurl=__KML_URL__" |
|||
} |
|||
return dateString |
|||
-- #### End of L10n settings #### |
|||
end |
|||
--❗ |
|||
-- Volume conversions |
|||
local function litersToGallons(liters) |
|||
local litersValue = tonumber(liters:match("%d+")) |
|||
if litersValue then |
|||
local gallons = litersValue * 0.264172 |
|||
return string.format("%d liters (%.2f gallons)", litersValue, gallons) |
|||
end |
|||
return liters |
|||
end |
|||
local function gallonsToLiters(gallons) |
|||
-- Table of available wikis, in the order that they are to be searched for kml files |
|||
local gallonsValue = tonumber(gallons:match("%d+")) |
|||
-- (once a kml file is found, further sites are not checked) |
|||
if gallonsValue then |
|||
local sites = { |
|||
local liters = gallonsValue * 3.78541 |
|||
{ |
|||
return string.format("%d gallons (%.2f liters)", gallonsValue, liters) |
|||
mw.ustring.match( mw.site.server, "%w+" ) .. mw.ustring.gsub( mw.ustring.lower(mw.site.siteName), "[mp]edia", ""), |
|||
end |
|||
mw.ustring.sub(mw.site.server, 3), |
|||
return gallons |
|||
"" |
|||
end |
|||
}, -- local wiki (listed first so local files can override files on other wikis) |
|||
{ "commonswiki", "commons.wikimedia.org", "c:" }, -- Commons would be a logical central repository for KML files (but has no files as of August 2016) |
|||
{ "enwiki", "en.wikipedia.org", "w:en:" }, -- largest source of KML files (as of August 2016) |
|||
{ "bnwiki", "bn.wikipedia.org", "w:bn:" }, -- other sites with a KML template, listed in alphabetical order |
|||
{ "cswiki", "cs.wikipedia.org", "w:cs:" }, |
|||
{ "fawiki", "fa.wikipedia.org", "w:fa:" }, |
|||
{ "frwiki", "fr.wikipedia.org", "w:fr:" }, |
|||
{ "jawiki", "ja.wikipedia.org", "w:ja:" }, |
|||
{ "mlwiki", "ml.wikipedia.org", "w:ml:" }, |
|||
{ "svwiki", "sv.wikipedia.org", "w:sv:" }, |
|||
{ "zhwiki", "zh.wikipedia.org", "w:zh:" }, |
|||
} |
|||
local function cubicMetersToCubicFeet(cubicMeters) |
|||
local p, q = {}, {} |
|||
local cubicMetersValue = tonumber(cubicMeters:match("%d+")) |
|||
if cubicMetersValue then |
|||
local cubicFeet = cubicMetersValue * 35.3147 |
|||
return string.format("%d cubic meters (%.2f cubic feet)", cubicMetersValue, cubicFeet) |
|||
end |
|||
return cubicMeters |
|||
end |
|||
local function |
local function cubicFeetToCubicMeters(cubicFeet) |
||
local cubicFeetValue = tonumber(cubicFeet:match("%d+")) |
|||
local cleanArgs = {} |
|||
if cubicFeetValue then |
|||
for key, val in pairs(argsTable) do |
|||
local cubicMeters = cubicFeetValue * 0.0283168 |
|||
if type(val) == 'string' then |
|||
return string.format("%d cubic feet (%.2f cubic meters)", cubicFeetValue, cubicMeters) |
|||
val = val:match('^%s*(.-)%s*$') |
|||
end |
|||
if val ~= '' then |
|||
return cubicFeet |
|||
cleanArgs[key] = val |
|||
end |
|||
else |
|||
cleanArgs[key] = val |
|||
end |
|||
end |
|||
return cleanArgs |
|||
end |
end |
||
-- Area conversions |
|||
local function safeReplace(string, pattern, replacement) |
|||
local function squareMetersToSquareFeet(squareMeters) |
|||
-- avoids "Lua error: invalid capture index" that occurs with string.gsub when the replacement contains one or more literal % character |
|||
local squareMetersValue = tonumber(squareMeters:match("%d+")) |
|||
local nonpattern_parts = mw.text.split( string, pattern ) |
|||
if squareMetersValue then |
|||
return table.concat(nonpattern_parts, replacement) |
|||
local squareFeet = squareMetersValue * 10.7639 |
|||
return string.format("%d square meters (%.2f square feet)", squareMetersValue, squareFeet) |
|||
end |
|||
return squareMeters |
|||
end |
end |
||
local function |
local function squareFeetToSquareMeters(squareFeet) |
||
local squareFeetValue = tonumber(squareFeet:match("%d+")) |
|||
if err and L10n.str.err_prepend then |
|||
if squareFeetValue then |
|||
err = mw.ustring.gsub( err, ">", ">" .. L10n.str.err_prepend .. " ", 1 ) |
|||
local squareMeters = squareFeetValue * 0.092903 |
|||
end |
|||
return string.format("%d square feet (%.2f square meters)", squareFeetValue, squareMeters) |
|||
end |
|||
return squareFeet |
|||
end |
|||
local function hectaresToAcres(hectares) |
|||
local titleLinks = {} |
|||
local hectaresValue = tonumber(hectares:match("%d+")) |
|||
for i, v in ipairs( externalLinks ) do |
|||
if hectaresValue then |
|||
titleLinks[i] = mw.ustring.format( "[%s %s]", v.link , v.short) |
|||
local acres = hectaresValue * 2.47105 |
|||
end |
|||
return string.format("%d hectares (%.2f acres)", hectaresValue, acres) |
|||
return mw.getCurrentFrame():extensionTag{ |
|||
end |
|||
name = 'indicator', |
|||
return hectares |
|||
args = { name = 'attached-kml' }, |
|||
content = mw.ustring.format( |
|||
"<span id=\"coordinates\">\'\'\'%s\'\'\': %s</span>", |
|||
titletext, |
|||
err or table.concat(titleLinks, " / ") |
|||
) |
|||
} |
|||
end |
end |
||
local function |
local function acresToHectares(acres) |
||
local acresValue = tonumber(acres:match("%d+")) |
|||
local inlineLinks = {} |
|||
if acresValue then |
|||
for i, v in ipairs( externalLinks ) do |
|||
local hectares = acresValue * 0.404686 |
|||
inlineLinks[i] = mw.ustring.format("[%s %s]", v.link , v.long) |
|||
return string.format("%d acres (%.2f hectares)", acresValue, hectares) |
|||
end |
|||
end |
|||
local editUrl = mw.ustring.gsub( url, "action=raw", "action=edit" ) |
|||
return acres |
|||
local wiki_link_class |
|||
end |
|||
if mw.ustring.find( editUrl, mw.site.server, 1, true ) then |
|||
wiki_link_class = "plainlinks" |
|||
else |
|||
wiki_link_class = "" |
|||
end |
|||
-- Time conversions |
|||
if L10n.config.inline_format == "line" then |
|||
local function hoursToMinutes(hours) |
|||
return mw.ustring.format( |
|||
local hoursValue = tonumber(hours:match("%d+")) |
|||
"<li>%s%s%s (<span class=\"%s\">[%s %s] <span style=\"font-size:85%%;\">([%s %s] • [[%s|%s]])</span></span>)</li>", |
|||
if hoursValue then |
|||
headertext, L10n.str.line.start, |
|||
local minutes = hoursValue * 60 |
|||
err or table.concat(inlineLinks, L10n.str.line.separator), |
|||
return string.format("%d hours (%d minutes)", hoursValue, minutes) |
|||
wiki_link_class, url, L10n.str.kml_file, editUrl, L10n.str.edit, |
|||
end |
|||
L10n.str.help_location, L10n.str.help |
|||
return hours |
|||
) |
|||
end |
|||
local text = mw.ustring.format( |
|||
'%s<span class="%s">\'\'\'[%s %s]\'\'\' ([%s %s] • [[%s|%s]])</span>', |
|||
headertext, wiki_link_class, url, L10n.str.kml_file, editUrl, |
|||
L10n.str.edit, L10n.str.help_location, L10n.str.help |
|||
) |
|||
if err or #inlineLinks > 0 then |
|||
text = mw.ustring.format( |
|||
"%s<ul><li>%s</li></ul>", |
|||
text, |
|||
err or table.concat(inlineLinks, "</li><li>") |
|||
) |
|||
end |
|||
return require('Module:Side box')._main({ |
|||
class = 'attached-kml', |
|||
text = text |
|||
}) |
|||
end |
end |
||
local function |
local function minutesToHours(minutes) |
||
local minutesValue = tonumber(minutes:match("%d+")) |
|||
return mw.ustring.format( |
|||
if minutesValue then |
|||
'<div class="kmldata" data-server="%s" title="%s">[[%s%s]]</div>', |
|||
local hours = minutesValue / 60 |
|||
sites[s_index][2], link, sites[s_index][3], link |
|||
return string.format("%d minutes (%.2f hours)", minutesValue, hours) |
|||
) |
|||
end |
|||
return minutes |
|||
end |
end |
||
local function |
local function daysToHours(days) |
||
local daysValue = tonumber(days:match("%d+")) |
|||
return mw.ustring.format( |
|||
if daysValue then |
|||
'<strong class="attached-kml-error">%s</strong>%s', |
|||
local hours = daysValue * 24 |
|||
msg, |
|||
return string.format("%d days (%d hours)", daysValue, hours) |
|||
mw.title.getCurrentTitle():inNamespaces(0, 118) and cat or '' |
|||
end |
|||
) |
|||
return days |
|||
end |
end |
||
local function |
local function hoursToDays(hours) |
||
local hoursValue = tonumber(hours:match("%d+")) |
|||
local pcall_result, kml_entity = pcall(mw.wikibase.getEntity, kml_qid) |
|||
if hoursValue then |
|||
if not pcall_result then return nil, nil, nil, makeError(L10n.str.err.no_item, L10n.str.cat.error_noitem) end -- Error if entity doesn't exist |
|||
local days = hoursValue / 24 |
|||
return string.format("%d hours (%.2f days)", hoursValue, days) |
|||
end |
|||
return hours |
|||
end |
|||
-- Energy conversions |
|||
local p31_claim = kml_entity:getBestStatements("P31") -- P31 is property "instance of" |
|||
local function joulesToCalories(joules) |
|||
local has_good_p31 |
|||
local joulesValue = tonumber(joules:match("%d+")) |
|||
for k, v in pairs( p31_claim ) do |
|||
if joulesValue then |
|||
if (p31_claim[k] and p31_claim[k].mainsnak.snaktype == "value" and |
|||
local calories = joulesValue * 0.000239006 |
|||
p31_claim[k].mainsnak.datavalue.type == "wikibase-entityid" and |
|||
return string.format("%d joules (%.6f calories)", joulesValue, calories) |
|||
p31_claim[k].mainsnak.datavalue.value["numeric-id"] == 26267864) then |
|||
end |
|||
has_good_p31 = true |
|||
return joules |
|||
end |
|||
end |
|||
if not (has_good_p31) then -- Error if item isn't a kml file |
|||
return nil, nil, nil, makeError(L10n.str.err.bad_qid, L10n.str.cat.error_badqid) |
|||
end |
|||
local function caloriesToJoules(calories) |
|||
local kml_sitelink |
|||
local caloriesValue = tonumber(calories:match("%d+")) |
|||
local kml_siteindex |
|||
if caloriesValue then |
|||
local kml_url |
|||
local joules = caloriesValue / 0.000239006 |
|||
for i, v in ipairs( sites ) do |
|||
return string.format("%d calories (%.2f joules)", caloriesValue, joules) |
|||
kml_sitelink = kml_entity:getSitelink( v[1] ) |
|||
end |
|||
if kml_sitelink then |
|||
return calories |
|||
kml_url = "https://" .. v[2] .. "/w/index.php?title=" .. mw.uri.encode( kml_sitelink, "WIKI" ) .. "&action=raw" |
|||
kml_siteindex = i |
|||
end |
|||
if kml_url then break end |
|||
end |
|||
return kml_url or nil, kml_sitelink or nil, kml_siteindex or nil, nil |
|||
end |
end |
||
local function kilowattHoursToJoules(kWh) |
|||
-- Attempts to get url from linked wikidata items, will return nil if it can't |
|||
local kWhValue = tonumber(kWh:match("%d+")) |
|||
local function getUrlFromWikidata() |
|||
if kWhValue then |
|||
local entity = mw.wikibase.getEntityObject() |
|||
local joules = kWhValue * 3600000 |
|||
if not entity then return nil end |
|||
return string.format("%d kWh (%.2e joules)", kWhValue, joules) |
|||
end |
|||
return kWh |
|||
end |
|||
local function joulesToKilowattHours(joules) |
|||
local kml_claim = entity:getBestStatements("P3096") -- P3096 is property "KML file" |
|||
local joulesValue = tonumber(joules:match("%d+")) |
|||
if joulesValue then |
|||
local kWh = joulesValue / 3600000 |
|||
-- get the QID of the first value of the property |
|||
return string.format("%.2e joules (%d kWh)", joulesValue, kWh) |
|||
if (kml_claim[1] and kml_claim[1].mainsnak.snaktype == "value" and kml_claim[1].mainsnak.datavalue.type == "wikibase-entityid") then |
|||
end |
|||
local kml_qid = "Q" .. kml_claim[1].mainsnak.datavalue.value["numeric-id"] |
|||
return joules |
|||
return getUrlFromQid( kml_qid ) |
|||
else |
|||
return nil -- TODO: error message |
|||
end |
|||
else |
|||
return nil -- TODO: error message |
|||
end |
|||
end |
end |
||
-- Pressure conversions |
|||
function p.main(frame) |
|||
local function pascalsToPSI(pascals) |
|||
local parent = frame |
|||
local pascalsValue = tonumber(pascals:match("%d+")) |
|||
local Args = setCleanArgs(frame.args) |
|||
if pascalsValue then |
|||
local psi = pascalsValue * 0.000145038 |
|||
return string.format("%d pascals (%.6f PSI)", pascalsValue, psi) |
|||
end |
|||
return pascals |
|||
end |
|||
local function psiToPascals(psi) |
|||
local qid = Args[L10n.para.wikidata] or nil |
|||
local psiValue = tonumber(psi:match("%d+")) |
|||
if psiValue then |
|||
local pascals = psiValue / 0.000145038 |
|||
return string.format("%.6f PSI (%d pascals)", psiValue, pascals) |
|||
end |
|||
return psi |
|||
end |
|||
local function atmospheresToPascals(atmospheres) |
|||
-- get KML file url |
|||
local atmospheresValue = tonumber(atmospheres:match("%d+")) |
|||
local wikiUrl, wikiTitle, wikiLink, trackingWikitext, kmlError |
|||
if atmospheresValue then |
|||
if not (Args[L10n.para.from]) then |
|||
local pascals = atmospheresValue * 101325 |
|||
if not qid then |
|||
return string.format("%d atmospheres (%d pascals)", atmospheresValue, pascals) |
|||
wikiUrl, wikiLink, siteindex, kmlError = getUrlFromWikidata() |
|||
end |
|||
elseif mw.ustring.find( qid, "^Q%d+" ) then |
|||
return atmospheres |
|||
wikiUrl, wikiLink, siteindex, kmlError = getUrlFromQid(qid) |
|||
end |
|||
else |
|||
kmlError = makeError(L10n.str.err.malformed_qid, L10n.str.cat.error_mqid) |
|||
end |
|||
end |
|||
if not (wikiUrl) then |
|||
-- FIXME? this smells bad. shouldn't need to make a new title of a to_string |
|||
-- from the current title and then turn it into text form |
|||
wikiLink = Args[L10n.para.from] or mw.title.new(tostring(mw.title.getCurrentTitle())).text |
|||
wikiLink = L10n.str.kml_prefix .. wikiLink |
|||
wikiTitle = mw.title.new( wikiLink ) |
|||
local function pascalsToAtmospheres(pascals) |
|||
if not (wikiTitle.exists) and not (kmlError) then |
|||
local pascalsValue = tonumber(pascals:match("%d+")) |
|||
if Args[L10n.para.from] then |
|||
if pascalsValue then |
|||
kmlError = makeError(L10n.str.err.bad_from, L10n.str.cat.error_from) |
|||
local atmospheres = pascalsValue / 101325 |
|||
else |
|||
return string.format("%d pascals (%.6f atmospheres)", pascalsValue, atmospheres) |
|||
kmlError = makeError(L10n.str.err.no_kml, L10n.str.cat.error_nokml) |
|||
end |
|||
return pascals |
|||
end |
|||
end |
|||
wikiUrl = wikiTitle:fullUrl("action=raw", "https") |
|||
siteindex = 1 |
|||
trackingWikitext = mw.ustring.format( |
|||
'<div title="KML & Wikidata" class="attached-kml-wikidata">KML is not from Wikidata</div>%s', |
|||
mw.title.getCurrentTitle():inNamespace(0) and L10n.str.cat.local_kml or '' |
|||
) |
|||
else |
|||
trackingWikitext = mw.ustring.format( |
|||
'<div title="KML & Wikidata" class="attached-kml-wikidata">KML is from Wikidata</div>%s', |
|||
mw.title.getCurrentTitle():inNamespace(0) and L10n.str.cat.wikidata_kml or '' |
|||
) |
|||
end |
|||
wikiTitle = mw.title.new( wikiLink ) |
|||
if wikiTitle.exists then |
|||
local transclusion = wikiTitle:getContent() -- hack to register the template as transcluded. |
|||
end |
|||
-- replace __KML_URL__ or __KML_URL_E__ with actual values |
|||
local encodedWikiUrl = mw.uri.encode(wikiUrl, "PATH") |
|||
for i, v in ipairs( externalLinks ) do |
|||
local el1 = safeReplace( v.link, "__KML_URL__", wikiUrl ) |
|||
local el2 = safeReplace( el1, "__KML_URL_E__", encodedWikiUrl ) |
|||
externalLinks[i]["link"] = el2 |
|||
end |
|||
-- Frequency conversions |
|||
-- suppress errors and categories if demo parameter is set |
|||
local function hertzToRPM(hertz) |
|||
if Args[L10n.para.demo] then |
|||
local hertzValue = tonumber(hertz:match("%d+")) |
|||
kmlError = nil |
|||
if hertzValue then |
|||
trackingWikitext = "" |
|||
local rpm = hertzValue * 60 |
|||
end |
|||
return string.format("%d Hertz (%d RPM)", hertzValue, rpm) |
|||
end |
|||
return hertz |
|||
end |
|||
local function rpmToHertz(rpm) |
|||
local rpmValue = tonumber(rpm:match("%d+")) |
|||
if Args[L10n.para.display] then |
|||
if rpmValue then |
|||
local display = mw.text.split(Args[L10n.para.display], '%s*' .. L10n.str.dsep .. '%s*') |
|||
local hertz = rpmValue / 60 |
|||
if display[1] == L10n.str.title or display[2] == L10n.str.title then |
|||
return string.format("%d RPM (%.2f Hertz)", rpmValue, hertz) |
|||
wikitext = makeTitleWikitext(Args[L10n.para.title] or L10n.str.default_title, kmlError) |
|||
end |
|||
return rpm |
|||
if display[1] == L10n.str.inline or display[2] == L10n.str.inline or (display[1] ~= L10n.str.title and display[2] ~= L10n.str.title) then |
|||
end |
|||
local inlineWikitext = makeInlineWikitext(Args[L10n.para.header] or L10n.str.default_header, wikiUrl, kmlError) |
|||
wikitext = wikitext .. inlineWikitext |
|||
end |
|||
else |
|||
wikitext = makeInlineWikitext(Args[L10n.para.header] or L10n.str.default_header, wikiUrl, kmlError) |
|||
end |
|||
wikitext = wikitext .. makeKmldataDiv(wikiLink, siteindex) .. trackingWikitext |
|||
local function kilohertzToHertz(kHz) |
|||
return frame:extensionTag{ |
|||
local kHzValue = tonumber(kHz:match("%d+")) |
|||
name = 'templatestyles', args = { src = 'Module:Attached KML/styles.css' } |
|||
if kHzValue then |
|||
} .. frame:preprocess( wikitext ) |
|||
local hertz = kHzValue * 1000 |
|||
return string.format("%d kHz (%d Hertz)", kHzValue, hertz) |
|||
end |
|||
return kHz |
|||
end |
|||
local function hertzToKilohertz(hertz) |
|||
local hertzValue = tonumber(hertz:match("%d+")) |
|||
if hertzValue then |
|||
local kHz = hertzValue / 1000 |
|||
return string.format("%d Hertz (%.2f kHz)", hertzValue, kHz) |
|||
end |
|||
return hertz |
|||
end |
end |
||
-- Power conversions |
|||
function q.main(frame) |
|||
local function wattsToHorsepower(watts) |
|||
local entity = mw.wikibase.getEntity(frame.args.Q) |
|||
local |
local wattsValue = tonumber(watts:match("%d+")) |
||
if wattsValue then |
|||
local horsepower = wattsValue * 0.00134102 |
|||
return string.format("%d watts (%.6f horsepower)", wattsValue, horsepower) |
|||
end |
|||
return watts |
|||
end |
|||
local function horsepowerToWatts(horsepower) |
|||
local kml = entity:getBestStatements("P3096") |
|||
local horsepowerValue = tonumber(horsepower:match("%d+")) |
|||
if horsepowerValue then |
|||
local watts = horsepowerValue / 0.00134102 |
|||
return string.format("%.6f horsepower (%d watts)", horsepowerValue, watts) |
|||
end |
|||
return horsepower |
|||
end |
|||
local function megawattsToHorsepower(megawatts) |
|||
if entity==nil then |
|||
local megawattsValue = tonumber(megawatts:match("%d+")) |
|||
return frame:preprocess("Hello".."Bye") |
|||
if megawattsValue then |
|||
else |
|||
local horsepower = megawattsValue * 1341.02 |
|||
return frame:preprocess("Hello"..tostring(kml[1]).."Bye") |
|||
return string.format("%d megawatts (%.2f horsepower)", megawattsValue, horsepower) |
|||
end |
|||
end |
|||
return megawatts |
|||
end |
end |
||
local function horsepowerToMegawatts(horsepower) |
|||
return q |
|||
local horsepowerValue = tonumber(horsepower:match("%d+")) |
|||
if horsepowerValue then |
|||
local megawatts = horsepowerValue / 1341.02 |
|||
return string.format("%.2f horsepower (%d megawatts)", horsepowerValue, megawatts) |
|||
end |
|||
return horsepower |
|||
end |
|||
-- Add these functions to the p dynamic function to integrate them into the infobox |
|||
--❗ |
|||
local function cmToFeetAndInches(cm) |
|||
local cmValue = tonumber(cm:match("%d+")) |
|||
if cmValue then |
|||
local totalInches = cmValue / 2.54 |
|||
local feet = math.floor(totalInches / 12) |
|||
local inches = math.floor(totalInches % 12) |
|||
return string.format("%d centimeters (%d feet %d inches)", cmValue, feet, inches) |
|||
end |
|||
return cm |
|||
end |
|||
local function mphToKmh(mph) |
|||
local mphValue = tonumber(mph:match("%d+")) |
|||
if mphValue then |
|||
local kmh = mphValue * 1.60934 |
|||
return string.format("%d mph (%.2f km/h)", mphValue, kmh) |
|||
end |
|||
return mph |
|||
end |
|||
local function kmhToMph(kmh) |
|||
local kmhValue = tonumber(kmh:match("%d+")) |
|||
if kmhValue then |
|||
local mph = kmhValue / 1.60934 |
|||
return string.format("%d km/h (%.2f mph)", kmhValue, mph) |
|||
end |
|||
return kmh |
|||
end |
|||
local function kmToMi(km) |
|||
local kmValue = tonumber(km:match("%d+")) |
|||
if kmValue then |
|||
local miles = kmValue * 0.621371 |
|||
return string.format("%d kilometers (%.2f mi)", kmValue, miles) |
|||
end |
|||
return km |
|||
end |
|||
local function miToKm(mi) |
|||
local miValue = tonumber(mi:match("%d+")) |
|||
if miValue then |
|||
local km = miValue / 0.621371 |
|||
return string.format("%d miles (%.2f km)", miValue, km) |
|||
end |
|||
return mi |
|||
end |
|||
local function hpToKw(hp) |
|||
local hpValue = tonumber(hp:match("%d+")) |
|||
if hpValue then |
|||
local kw = hpValue * 0.735499 |
|||
return string.format("%d horsepower (%.2f kW)", hpValue, kw) |
|||
end |
|||
return hp |
|||
end |
|||
local function kwToHp(kw) |
|||
local kwValue = tonumber(kw:match("%d+")) |
|||
if kwValue then |
|||
local hp = kwValue / 0.735499 |
|||
return string.format("%d Kilowatt(%.2f hp)", kwValue, hp) |
|||
end |
|||
return kw |
|||
end |
|||
local function ftToCm(ft) |
|||
local ftValue = tonumber(ft:match("%d+")) |
|||
if ftValue then |
|||
local cm = ftValue * 30.48 |
|||
return string.format("%d ft (%.2f cm)", ftValue, cm) |
|||
end |
|||
return ft |
|||
end |
|||
local function lbToKg(lb) |
|||
local lbValue = tonumber(lb:match("%d+")) |
|||
if lbValue then |
|||
local kg = lbValue * 0.453592 |
|||
return string.format("%d lb (%.2f kg)", lbValue, kg) |
|||
end |
|||
return lb |
|||
end |
|||
local function kgToPounds(kg) |
|||
local kgValue = tonumber(kg:match("%d+")) |
|||
if kgValue then |
|||
local pounds = kgValue * 2.20462 |
|||
return string.format("%d kg (%.2f pounds)", kgValue, pounds) |
|||
end |
|||
return kg |
|||
end |
|||
local function mmToInches(mm) |
|||
local mmValue = tonumber(mm:match("%d+")) |
|||
if mmValue then |
|||
local inches = mmValue * 0.0393701 |
|||
return string.format("%d mm (%.2f inches)", mmValue, inches) |
|||
end |
|||
return mm |
|||
end |
|||
local function inchesToMm(inches) |
|||
local inchesValue = tonumber(inches:match("%d+")) |
|||
if inchesValue then |
|||
local mm = inchesValue / 0.0393701 |
|||
return string.format("%d inches (%.2f mm)", inchesValue, mm) |
|||
end |
|||
return inches |
|||
end |
|||
local function msToFts(ms) |
|||
local msValue = tonumber(ms:match("%d+")) |
|||
if msValue then |
|||
local fts = msValue * 3.28084 |
|||
return string.format("%.2f meter per seconds(%.2f ft/s)", msValue, fts) |
|||
end |
|||
return ms |
|||
end |
|||
local function ftsToMs(fts) |
|||
local ftsValue = tonumber(fts:match("%d+")) |
|||
if ftsValue then |
|||
local ms = ftsValue / 3.28084 |
|||
return string.format("%.2f foot per seconds(%.2f m/s)", ftsValue, ms) |
|||
end |
|||
return fts |
|||
end |
|||
local function mToFeet(m) |
|||
local mValue = tonumber(m:match("%d+")) |
|||
if mValue then |
|||
local feet = mValue * 3.28084 |
|||
return string.format("%d meter (%.2f ft)", mValue, feet) |
|||
end |
|||
-- Check for variations of "m", "meter", and "metre" |
|||
local mValueAlt = tonumber(m:match("(%d+)%s*[mM]")) |
|||
if mValueAlt then |
|||
local feetAlt = mValueAlt * 3.28084 |
|||
return string.format("%d m (%.2f ft)", mValueAlt, feetAlt) |
|||
end |
|||
local mValueAlt2 = tonumber(m:match("(%d+)%s*[mM][eE][tT][eE]?[rR]?")) |
|||
if mValueAlt2 then |
|||
local feetAlt2 = mValueAlt2 * 3.28084 |
|||
return string.format("%d metre (%.2f ft)", mValueAlt2, feetAlt2) |
|||
end |
|||
local mValueAlt3 = tonumber(m:match("(%d+)%s*[mM][eE][tT][eE][rR]?")) |
|||
if mValueAlt3 then |
|||
local feetAlt3 = mValueAlt3 * 3.28084 |
|||
return string.format("%d meter (%.2f ft)", mValueAlt3, feetAlt3) |
|||
end |
|||
return m |
|||
end |
|||
--//m/// |
|||
local function celsiusToFahrenheit(c) |
|||
local cValue = tonumber(c:match("%d+")) |
|||
if cValue then |
|||
local fahrenheit = cValue * 9/5 + 32 |
|||
return string.format("%.2f°C (%.2f°F)", cValue, fahrenheit) |
|||
end |
|||
return c |
|||
end |
|||
local function fahrenheitToCelsius(f) |
|||
local fValue = tonumber(f:match("%d+")) |
|||
if fValue then |
|||
local celsius = (fValue - 32) * 5/9 |
|||
return string.format("%.2f°F (%.2f°C)", fValue, celsius) |
|||
end |
|||
return f |
|||
end |
|||
function capitalizeFirstLetter(str) |
|||
return str:gsub("^%l", string.upper) |
|||
end |
|||
--[[ local conversionPatterns = { |
|||
{ pattern = "%s*kilohertz%s*", convertedValue = kilohertzToHertz}, |
|||
{ pattern = "%s*kg%s*", conversion = kgToPounds }, |
|||
{ pattern = "%s*mph%s*", conversion = mphToKmh }, |
|||
-- Add more conversion patterns as needed |
|||
} ]] |
|||
local abec='{|' |
|||
function p.dynami(frame) |
|||
local args = frame:getParent().args |
|||
local output = '{| class="infobox biography vcard" style="width: 22em;"\n|-\n' |
|||
local firstDescriptionFound = false |
|||
local cmProcessed = false |
|||
local abvParams = {} -- Store abv parameters separately |
|||
for key, value in pairs(args) do |
|||
if value ~= "" then |
|||
if key:sub(1, 2) == 'up' then |
|||
if value ~= "" then |
|||
output = output .. '| style="background:lightgray;font-size:20px;text-align: center; background-color: ' .. (args.headcolor or "#f2f2f2") .. '; color: ' .. (args.color or "inherit") .. '; font-weight: bold;" | ' .. value .. '\n|-\n' |
|||
firstDescriptionFound = true |
|||
end |
|||
elseif key:sub(1, 3) == 'abv' then |
|||
if value ~= "" then |
|||
abvParams[value] = true -- Store abv parameters in a table with value as key to avoid duplicates |
|||
end |
|||
end |
|||
end |
|||
end |
|||
if args.image then |
|||
local caption = args.caption or "" |
|||
local imagesize = args.imagesize or "" |
|||
output = output .. '|' ..'<div style="text-align:center;">'.. args.image.. '</div>'..'<small style="display: block; text-align: center;">' .. caption .. '</small>\n|-\n' |
|||
end |
|||
for key, value in pairs(args) do |
|||
if value ~= "" then |
|||
if key ~= 'image' and key ~= 'imagecaption' and key~=('abv'or'abv1'or'abv2'or' abv3'or'abv4'or'abv5'or'abv6'or'abv7'or'abv8'or' abv9'or'abv10'or'abv11') |
|||
and key ~= 'caption' and key ~= 'headcolor' and key ~= 'color' and key ~= 'abvstyle' and key ~= 'abvheadcolor' and key ~= 'abvcolor' and not key:match("^abv%d+$") and key~=abvParams then |
|||
if key ~= 'up' and not (key:sub(1, 2) == 'up' and firstDescriptionFound) then |
|||
local convertedValue = value |
|||
local originalValue = value |
|||
if type(value) == "string" then |
|||
if value:match("%s*cm#%s*")or value:match("%s*centimeters#%s*")or value:match("%s*centimetres#%s*") or value:match("%s*centimeter#%s*") or value:match("%s*centimetre#%s*") then |
|||
convertedValue = cmToFeetAndInches(value) |
|||
cmProcessed = true |
|||
elseif value:match("%s*kg#%s*") or value:match("%s*kgs#%s*")or value:match("%s*kilograms#%s*") or value:match("%s*kilogram#%s*") then |
|||
convertedValue = kgToPounds(value) |
|||
elseif value:match("%s*mph#%s*")or value:match("%s*mp/h#%s*")or value:match("%s*miles per hour#%s*")or value:match("%s*mile per hour#%s*") then |
|||
convertedValue = mphToKmh(value) |
|||
elseif value:match("%s*kmh#%s*")or value:match("%s*km/h#%s*")or value:match("%s*kilometers per hour#%s*")or value:match("%s*kilometer per hour#%s*")or value:match("%s*kilometres per hour#%s*")or value:match("%s*kilometre per hour#%s*") then |
|||
convertedValue = kmhToMph(value) |
|||
elseif value:match("%s*kms#%s*")or value:match("%s*km#%s*")or value:match("%s*kilometer#%s*")or value:match("%s*kilometers#%s*")or value:match("%s*kilometre#%s*")or value:match("%s*kilometres#%s*") then |
|||
convertedValue = kmToMi(value) |
|||
elseif value:match("%s*mi#%s*")or value:match("%s*mile#%s*")or value:match("%s*miles#%s*") then |
|||
convertedValue = miToKm(value) |
|||
elseif value:match("%s*hp#%s*")or value:match("%s*hps#%s*")or value:match("%s*horsepower#%s*")or value:match("%s*horsepowers#%s*") then |
|||
convertedValue = hpToKw(value) |
|||
elseif value:match("%s*kw#%s*")or value:match("%s*kws#%s*")or value:match("%s*kilowatt#%s*")or value:match("%s*kilowatts#%s*") then |
|||
convertedValue = kwToHp(value) elseif value:match("%s*ft#%s*") then elseif value:match("%s*lb#%s*")or value:match("%s*pound#%s*")or value:match("%s*pounds#%s*") then |
|||
convertedValue = lbToKg(value) |
|||
elseif value:match("%s*mmms#%s*")or value:match("%s*mm#%s*")or value:match("%s*millimeter#%s*")or value:match("%s*millimetre#%s*")or value:match("%s*millimeters#%s*")or value:match("%s*millimetres#%s*") then |
|||
convertedValue = mmToInches(value) |
|||
elseif value:match("%s*in#%s*")or value:match("%s*ins#%s*")or value:match("%s*inch#%s*")or value:match("%s*inche#%s*")or value:match("%s*inches#%s*") then |
|||
convertedValue = inchesToMm(value) |
|||
elseif value:match("%s*ms#%s*")or value:match("%s*millisecond#%s*")or value:match("%s*milliseconds#%s*") then |
|||
convertedValue = msToFts(value) |
|||
elseif value:match("%s*fts#%s*")or value:match("%s*ft/s#%s*")or value:match("%s*fps#%s*")or value:match("%s*footperseconds#%s*") then |
|||
convertedValue = ftsToMs(value) |
|||
elseif value:match("%s*°c#%s*") or value:match("%s*celsius#%s*") or value:match("%s*Celsius#%s*") then |
|||
convertedValue = celsiusToFahrenheit(value) |
|||
elseif value:match("%s*°f#%s*") or value:match("%s*fahrenheit#%s*") then |
|||
convertedValue = fahrenheitToCelsius(value) |
|||
--❗ |
|||
elseif value:match("%s*liters#%s*")or value:match("%s*litres#%s*")or value:match("%s*litre#%s*")or value:match("%s*liter#%s*") then |
|||
convertedValue = litersToGallons(value) |
|||
elseif value:match("%s*gallons#%s*")or value:match("%s*gallon#%s*") then |
|||
convertedValue = gallonsToLiters(value) |
|||
elseif value:match("%s*m³#%s*")or value:match("%s*m3#%s*")or value:match("%s*cubicmeters#%s*")or value:match("%s*cubicmetres#%s*")or value:match("%s*cubicmetre#%s*")or value:match("%s*cubicmeter#%s*") then |
|||
convertedValue = cubicMetersToCubicFeet(value) |
|||
elseif value:match("%s*ft³#%s*")or value:match("%s*ft3#%s*")or value:match("%s*cubicfeet#%s*")or value:match("%s*cubicfoot#%s*")or value:match("%s*cubicfeets#%s*")or value:match("%s*cubicfoots#%s*") then |
|||
convertedValue = cubicFeetToCubicMeters(value) elseif value:match("%s*sqm#%s*")or value:match("%s*sqms#%s*")or value:match("%s*sqmetre#%s*")or value:match("%s*sqmetres#%s*")or value:match("%s*squaremeter#%s*")or value:match("%s*squaremeters#%s*")or value:match("%s*sqmeter#%s*")or value:match("%s*sqmeters#%s*") then |
|||
convertedValue = squareMetersToSquareFeet(value) |
|||
elseif value:match("%s*sqft#%s*")or value:match("%s*sqfoot#%s*")or value:match("%s*sqfeet#%s*")or value:match("%s*squarefoots#%s*")or value:match("%s*squarefeets#%s*")or value:match("%s*squarefeet#%s*") then |
|||
convertedValue = squareFeetToSquareMeters(value) |
|||
elseif value:match("%s*hectares#%s*")or value:match("%s*hectare#%s*") then |
|||
convertedValue = hectaresToAcres(value) |
|||
elseif value:match("%s*acres#%s*")or value:match("%s*acre#%s*") then |
|||
convertedValue = acresToHectares(value) |
|||
elseif value:match("%s*hours#%s*")or value:match("%s*hour#%s*") then |
|||
convertedValue = hoursToMinutes(value) |
|||
elseif value:match("%s*minutes#%s*")or value:match("%s*minute#%s*") then |
|||
convertedValue = minutesToHours(value) |
|||
elseif value:match("%s*days#%s*")or value:match("%s*day#%s*") then |
|||
convertedValue = daysToHours(value) |
|||
elseif value:match("%s*joules#%s*")or value:match("%s*joule#%s*") then |
|||
convertedValue = joulesToCalories(value) |
|||
elseif value:match("%s*calories#%s*")or value:match("%s*calorie#%s*") then |
|||
convertedValue = caloriesToJoules(value) |
|||
elseif value:match("%s*kwh#%s*")or value:match("%s*kilowatthours#%s*")or value:match("%s*kilowatthour#%s*") then |
|||
convertedValue = kilowattHoursToJoules(value) |
|||
elseif value:match("%s*psi#%s*")or value:match("%s*pounds per square inch#%s*")or value:match("%s*pound per square inches#%s*") then |
|||
convertedValue = psiToPascals(value) |
|||
elseif value:match("%s*pascals#%s*")or value:match("%s*pascal#%s*") then |
|||
convertedValue = pascalsToPsi(value) |
|||
elseif value:match("%s*atm#%s*")or value:match("%s*atmospheres#%s*")or value:match("%s*atmosphere#%s*") then |
|||
convertedValue = atmospheresToPascals(value) |
|||
elseif value:match("%s*rpm#%s*") then |
|||
convertedValue = rpmToHertz(value) |
|||
elseif value:match("%s*kilohertz#%s*")or value:match("%s*khz#%s*") then |
|||
convertedValue = kilohertzToHertz(value) |
|||
elseif value:match("%s*hertz#%s*") then |
|||
convertedValue = hertzToRPM(value) |
|||
elseif value:match("%s*horsepower#%s*") then |
|||
convertedValue = horsepowerToWatts(value) |
|||
elseif value:match("%s*megawatts#%s*") or value:match("%s*megawatt#%s*")then |
|||
convertedValue = megawattsToHorsepower(value) |
|||
elseif value:match("%s*watts#%s*") or value:match("%s*watt#%s*")then |
|||
convertedValue = wattsToHorsepower(value) |
|||
elseif value:match("%s*hp#%s*") then |
|||
convertedValue = horsepowerToMegawatts(value) |
|||
elseif value:match("%s*m#%s*")or value:match("%s*meters#%s*")or value:match("%s*metres#%s*")or value:match("%s*meter#%s*")or value:match("%s*metre#%s*") then |
|||
convertedValue = mToFeet(value) |
|||
elseif value:match("%s*ft#%s*") or value:match("%s*feet#%s*")or value:match("%s*feets#%s*")or value:match("%s*foot#%s*")or value:match("%s*foots#%s*") then |
|||
convertedValue = ftToCm(value) |
|||
elseif value:match("%d%d%d%d/")or value:match("%d%d%d/")or value:match("%d%d/") then -- Check if the value is a date |
|||
local age =calculateAge(value) |
|||
if age then |
|||
convertedValue = value .. ' (' .. age .. ')' |
|||
end |
|||
end |
|||
elseif type(value) == "number" then |
|||
local age = calculateAge(tostring(value)) |
|||
if age then |
|||
convertedValue = tostring(value) .. ' (' .. age .. ')' |
|||
end |
|||
end |
|||
-- Capitalize the key before displaying |
|||
output = output .. '| style="font-weight:bolder;padding-right:1em;" | ' .. capitalizeFirstLetter(key) .. "'''<br />\n" |
|||
if originalValue ~= convertedValue then |
|||
output = output .. '| ' .. convertedValue .. '\n|-\n' |
|||
else |
|||
output = output .. '| ' .. value .. '\n|-\n' |
|||
end |
|||
end |
|||
end |
|||
end |
|||
end |
|||
if not args.abvstyle then |
|||
args.abvstyle = "" -- Set default value for abvstyle if not provided |
|||
end |
|||
for abvParam, _ in pairs(abvParams) do |
|||
output = output .. '| style="background:'..(args.abvheadcolor or 'transpaent')..';color:'..(args.abvcolor or'black')..';font-size:18px;text-align:center;' .. args.abvstyle .. '" | ' .. abvParam .. '\n|-\n' |
|||
end |
|||
output =output.. '|}\n|-\n' |
|||
return output |
|||
end |
|||
--/////// |
|||
return p |
Revision as of 13:08, 17 April 2024
![]() | This is the module sandbox page for Module:Module sandbox (diff). |
Usage
{{#invoke:Module sandbox|function_name}}
Example
{{#invoke:Module sandbox|main}}
yields:
Hello world!
Documentation
Package items
sandbox.hello_world(name)
(function)- Prints hello world
- Parameter:
name
Person to address (string) - Returns: hello world string
- TODO: make it say "Hello, [name]".
sandbox.main(frame)
(function)- Main entrypoint.
- Parameter:
frame
calling frame (table) - Returns: output wikitext
local p = {}
local m={}
local currentYear = tonumber(os.date("%Y"))
local function calculateAge(dateString)
local year = tonumber(dateString:match("%d+")) -- Extract the year from the date string
if year then
return currentYear - year .. " years old"
end
local success, parsedDate = pcall(function() return os.date("%Y", os.time{year=tonumber(dateString)}) end)
if success then
return currentYear - parsedDate .. " years old"
end
return dateString
end
--❗
-- Volume conversions
local function litersToGallons(liters)
local litersValue = tonumber(liters:match("%d+"))
if litersValue then
local gallons = litersValue * 0.264172
return string.format("%d liters (%.2f gallons)", litersValue, gallons)
end
return liters
end
local function gallonsToLiters(gallons)
local gallonsValue = tonumber(gallons:match("%d+"))
if gallonsValue then
local liters = gallonsValue * 3.78541
return string.format("%d gallons (%.2f liters)", gallonsValue, liters)
end
return gallons
end
local function cubicMetersToCubicFeet(cubicMeters)
local cubicMetersValue = tonumber(cubicMeters:match("%d+"))
if cubicMetersValue then
local cubicFeet = cubicMetersValue * 35.3147
return string.format("%d cubic meters (%.2f cubic feet)", cubicMetersValue, cubicFeet)
end
return cubicMeters
end
local function cubicFeetToCubicMeters(cubicFeet)
local cubicFeetValue = tonumber(cubicFeet:match("%d+"))
if cubicFeetValue then
local cubicMeters = cubicFeetValue * 0.0283168
return string.format("%d cubic feet (%.2f cubic meters)", cubicFeetValue, cubicMeters)
end
return cubicFeet
end
-- Area conversions
local function squareMetersToSquareFeet(squareMeters)
local squareMetersValue = tonumber(squareMeters:match("%d+"))
if squareMetersValue then
local squareFeet = squareMetersValue * 10.7639
return string.format("%d square meters (%.2f square feet)", squareMetersValue, squareFeet)
end
return squareMeters
end
local function squareFeetToSquareMeters(squareFeet)
local squareFeetValue = tonumber(squareFeet:match("%d+"))
if squareFeetValue then
local squareMeters = squareFeetValue * 0.092903
return string.format("%d square feet (%.2f square meters)", squareFeetValue, squareMeters)
end
return squareFeet
end
local function hectaresToAcres(hectares)
local hectaresValue = tonumber(hectares:match("%d+"))
if hectaresValue then
local acres = hectaresValue * 2.47105
return string.format("%d hectares (%.2f acres)", hectaresValue, acres)
end
return hectares
end
local function acresToHectares(acres)
local acresValue = tonumber(acres:match("%d+"))
if acresValue then
local hectares = acresValue * 0.404686
return string.format("%d acres (%.2f hectares)", acresValue, hectares)
end
return acres
end
-- Time conversions
local function hoursToMinutes(hours)
local hoursValue = tonumber(hours:match("%d+"))
if hoursValue then
local minutes = hoursValue * 60
return string.format("%d hours (%d minutes)", hoursValue, minutes)
end
return hours
end
local function minutesToHours(minutes)
local minutesValue = tonumber(minutes:match("%d+"))
if minutesValue then
local hours = minutesValue / 60
return string.format("%d minutes (%.2f hours)", minutesValue, hours)
end
return minutes
end
local function daysToHours(days)
local daysValue = tonumber(days:match("%d+"))
if daysValue then
local hours = daysValue * 24
return string.format("%d days (%d hours)", daysValue, hours)
end
return days
end
local function hoursToDays(hours)
local hoursValue = tonumber(hours:match("%d+"))
if hoursValue then
local days = hoursValue / 24
return string.format("%d hours (%.2f days)", hoursValue, days)
end
return hours
end
-- Energy conversions
local function joulesToCalories(joules)
local joulesValue = tonumber(joules:match("%d+"))
if joulesValue then
local calories = joulesValue * 0.000239006
return string.format("%d joules (%.6f calories)", joulesValue, calories)
end
return joules
end
local function caloriesToJoules(calories)
local caloriesValue = tonumber(calories:match("%d+"))
if caloriesValue then
local joules = caloriesValue / 0.000239006
return string.format("%d calories (%.2f joules)", caloriesValue, joules)
end
return calories
end
local function kilowattHoursToJoules(kWh)
local kWhValue = tonumber(kWh:match("%d+"))
if kWhValue then
local joules = kWhValue * 3600000
return string.format("%d kWh (%.2e joules)", kWhValue, joules)
end
return kWh
end
local function joulesToKilowattHours(joules)
local joulesValue = tonumber(joules:match("%d+"))
if joulesValue then
local kWh = joulesValue / 3600000
return string.format("%.2e joules (%d kWh)", joulesValue, kWh)
end
return joules
end
-- Pressure conversions
local function pascalsToPSI(pascals)
local pascalsValue = tonumber(pascals:match("%d+"))
if pascalsValue then
local psi = pascalsValue * 0.000145038
return string.format("%d pascals (%.6f PSI)", pascalsValue, psi)
end
return pascals
end
local function psiToPascals(psi)
local psiValue = tonumber(psi:match("%d+"))
if psiValue then
local pascals = psiValue / 0.000145038
return string.format("%.6f PSI (%d pascals)", psiValue, pascals)
end
return psi
end
local function atmospheresToPascals(atmospheres)
local atmospheresValue = tonumber(atmospheres:match("%d+"))
if atmospheresValue then
local pascals = atmospheresValue * 101325
return string.format("%d atmospheres (%d pascals)", atmospheresValue, pascals)
end
return atmospheres
end
local function pascalsToAtmospheres(pascals)
local pascalsValue = tonumber(pascals:match("%d+"))
if pascalsValue then
local atmospheres = pascalsValue / 101325
return string.format("%d pascals (%.6f atmospheres)", pascalsValue, atmospheres)
end
return pascals
end
-- Frequency conversions
local function hertzToRPM(hertz)
local hertzValue = tonumber(hertz:match("%d+"))
if hertzValue then
local rpm = hertzValue * 60
return string.format("%d Hertz (%d RPM)", hertzValue, rpm)
end
return hertz
end
local function rpmToHertz(rpm)
local rpmValue = tonumber(rpm:match("%d+"))
if rpmValue then
local hertz = rpmValue / 60
return string.format("%d RPM (%.2f Hertz)", rpmValue, hertz)
end
return rpm
end
local function kilohertzToHertz(kHz)
local kHzValue = tonumber(kHz:match("%d+"))
if kHzValue then
local hertz = kHzValue * 1000
return string.format("%d kHz (%d Hertz)", kHzValue, hertz)
end
return kHz
end
local function hertzToKilohertz(hertz)
local hertzValue = tonumber(hertz:match("%d+"))
if hertzValue then
local kHz = hertzValue / 1000
return string.format("%d Hertz (%.2f kHz)", hertzValue, kHz)
end
return hertz
end
-- Power conversions
local function wattsToHorsepower(watts)
local wattsValue = tonumber(watts:match("%d+"))
if wattsValue then
local horsepower = wattsValue * 0.00134102
return string.format("%d watts (%.6f horsepower)", wattsValue, horsepower)
end
return watts
end
local function horsepowerToWatts(horsepower)
local horsepowerValue = tonumber(horsepower:match("%d+"))
if horsepowerValue then
local watts = horsepowerValue / 0.00134102
return string.format("%.6f horsepower (%d watts)", horsepowerValue, watts)
end
return horsepower
end
local function megawattsToHorsepower(megawatts)
local megawattsValue = tonumber(megawatts:match("%d+"))
if megawattsValue then
local horsepower = megawattsValue * 1341.02
return string.format("%d megawatts (%.2f horsepower)", megawattsValue, horsepower)
end
return megawatts
end
local function horsepowerToMegawatts(horsepower)
local horsepowerValue = tonumber(horsepower:match("%d+"))
if horsepowerValue then
local megawatts = horsepowerValue / 1341.02
return string.format("%.2f horsepower (%d megawatts)", horsepowerValue, megawatts)
end
return horsepower
end
-- Add these functions to the p dynamic function to integrate them into the infobox
--❗
local function cmToFeetAndInches(cm)
local cmValue = tonumber(cm:match("%d+"))
if cmValue then
local totalInches = cmValue / 2.54
local feet = math.floor(totalInches / 12)
local inches = math.floor(totalInches % 12)
return string.format("%d centimeters (%d feet %d inches)", cmValue, feet, inches)
end
return cm
end
local function mphToKmh(mph)
local mphValue = tonumber(mph:match("%d+"))
if mphValue then
local kmh = mphValue * 1.60934
return string.format("%d mph (%.2f km/h)", mphValue, kmh)
end
return mph
end
local function kmhToMph(kmh)
local kmhValue = tonumber(kmh:match("%d+"))
if kmhValue then
local mph = kmhValue / 1.60934
return string.format("%d km/h (%.2f mph)", kmhValue, mph)
end
return kmh
end
local function kmToMi(km)
local kmValue = tonumber(km:match("%d+"))
if kmValue then
local miles = kmValue * 0.621371
return string.format("%d kilometers (%.2f mi)", kmValue, miles)
end
return km
end
local function miToKm(mi)
local miValue = tonumber(mi:match("%d+"))
if miValue then
local km = miValue / 0.621371
return string.format("%d miles (%.2f km)", miValue, km)
end
return mi
end
local function hpToKw(hp)
local hpValue = tonumber(hp:match("%d+"))
if hpValue then
local kw = hpValue * 0.735499
return string.format("%d horsepower (%.2f kW)", hpValue, kw)
end
return hp
end
local function kwToHp(kw)
local kwValue = tonumber(kw:match("%d+"))
if kwValue then
local hp = kwValue / 0.735499
return string.format("%d Kilowatt(%.2f hp)", kwValue, hp)
end
return kw
end
local function ftToCm(ft)
local ftValue = tonumber(ft:match("%d+"))
if ftValue then
local cm = ftValue * 30.48
return string.format("%d ft (%.2f cm)", ftValue, cm)
end
return ft
end
local function lbToKg(lb)
local lbValue = tonumber(lb:match("%d+"))
if lbValue then
local kg = lbValue * 0.453592
return string.format("%d lb (%.2f kg)", lbValue, kg)
end
return lb
end
local function kgToPounds(kg)
local kgValue = tonumber(kg:match("%d+"))
if kgValue then
local pounds = kgValue * 2.20462
return string.format("%d kg (%.2f pounds)", kgValue, pounds)
end
return kg
end
local function mmToInches(mm)
local mmValue = tonumber(mm:match("%d+"))
if mmValue then
local inches = mmValue * 0.0393701
return string.format("%d mm (%.2f inches)", mmValue, inches)
end
return mm
end
local function inchesToMm(inches)
local inchesValue = tonumber(inches:match("%d+"))
if inchesValue then
local mm = inchesValue / 0.0393701
return string.format("%d inches (%.2f mm)", inchesValue, mm)
end
return inches
end
local function msToFts(ms)
local msValue = tonumber(ms:match("%d+"))
if msValue then
local fts = msValue * 3.28084
return string.format("%.2f meter per seconds(%.2f ft/s)", msValue, fts)
end
return ms
end
local function ftsToMs(fts)
local ftsValue = tonumber(fts:match("%d+"))
if ftsValue then
local ms = ftsValue / 3.28084
return string.format("%.2f foot per seconds(%.2f m/s)", ftsValue, ms)
end
return fts
end
local function mToFeet(m)
local mValue = tonumber(m:match("%d+"))
if mValue then
local feet = mValue * 3.28084
return string.format("%d meter (%.2f ft)", mValue, feet)
end
-- Check for variations of "m", "meter", and "metre"
local mValueAlt = tonumber(m:match("(%d+)%s*[mM]"))
if mValueAlt then
local feetAlt = mValueAlt * 3.28084
return string.format("%d m (%.2f ft)", mValueAlt, feetAlt)
end
local mValueAlt2 = tonumber(m:match("(%d+)%s*[mM][eE][tT][eE]?[rR]?"))
if mValueAlt2 then
local feetAlt2 = mValueAlt2 * 3.28084
return string.format("%d metre (%.2f ft)", mValueAlt2, feetAlt2)
end
local mValueAlt3 = tonumber(m:match("(%d+)%s*[mM][eE][tT][eE][rR]?"))
if mValueAlt3 then
local feetAlt3 = mValueAlt3 * 3.28084
return string.format("%d meter (%.2f ft)", mValueAlt3, feetAlt3)
end
return m
end
--//m///
local function celsiusToFahrenheit(c)
local cValue = tonumber(c:match("%d+"))
if cValue then
local fahrenheit = cValue * 9/5 + 32
return string.format("%.2f°C (%.2f°F)", cValue, fahrenheit)
end
return c
end
local function fahrenheitToCelsius(f)
local fValue = tonumber(f:match("%d+"))
if fValue then
local celsius = (fValue - 32) * 5/9
return string.format("%.2f°F (%.2f°C)", fValue, celsius)
end
return f
end
function capitalizeFirstLetter(str)
return str:gsub("^%l", string.upper)
end
--[[ local conversionPatterns = {
{ pattern = "%s*kilohertz%s*", convertedValue = kilohertzToHertz},
{ pattern = "%s*kg%s*", conversion = kgToPounds },
{ pattern = "%s*mph%s*", conversion = mphToKmh },
-- Add more conversion patterns as needed
} ]]
local abec='{|'
function p.dynami(frame)
local args = frame:getParent().args
local output = '{| class="infobox biography vcard" style="width: 22em;"\n|-\n'
local firstDescriptionFound = false
local cmProcessed = false
local abvParams = {} -- Store abv parameters separately
for key, value in pairs(args) do
if value ~= "" then
if key:sub(1, 2) == 'up' then
if value ~= "" then
output = output .. '| style="background:lightgray;font-size:20px;text-align: center; background-color: ' .. (args.headcolor or "#f2f2f2") .. '; color: ' .. (args.color or "inherit") .. '; font-weight: bold;" | ' .. value .. '\n|-\n'
firstDescriptionFound = true
end
elseif key:sub(1, 3) == 'abv' then
if value ~= "" then
abvParams[value] = true -- Store abv parameters in a table with value as key to avoid duplicates
end
end
end
end
if args.image then
local caption = args.caption or ""
local imagesize = args.imagesize or ""
output = output .. '|' ..'<div style="text-align:center;">'.. args.image.. '</div>'..'<small style="display: block; text-align: center;">' .. caption .. '</small>\n|-\n'
end
for key, value in pairs(args) do
if value ~= "" then
if key ~= 'image' and key ~= 'imagecaption' and key~=('abv'or'abv1'or'abv2'or' abv3'or'abv4'or'abv5'or'abv6'or'abv7'or'abv8'or' abv9'or'abv10'or'abv11')
and key ~= 'caption' and key ~= 'headcolor' and key ~= 'color' and key ~= 'abvstyle' and key ~= 'abvheadcolor' and key ~= 'abvcolor' and not key:match("^abv%d+$") and key~=abvParams then
if key ~= 'up' and not (key:sub(1, 2) == 'up' and firstDescriptionFound) then
local convertedValue = value
local originalValue = value
if type(value) == "string" then
if value:match("%s*cm#%s*")or value:match("%s*centimeters#%s*")or value:match("%s*centimetres#%s*") or value:match("%s*centimeter#%s*") or value:match("%s*centimetre#%s*") then
convertedValue = cmToFeetAndInches(value)
cmProcessed = true
elseif value:match("%s*kg#%s*") or value:match("%s*kgs#%s*")or value:match("%s*kilograms#%s*") or value:match("%s*kilogram#%s*") then
convertedValue = kgToPounds(value)
elseif value:match("%s*mph#%s*")or value:match("%s*mp/h#%s*")or value:match("%s*miles per hour#%s*")or value:match("%s*mile per hour#%s*") then
convertedValue = mphToKmh(value)
elseif value:match("%s*kmh#%s*")or value:match("%s*km/h#%s*")or value:match("%s*kilometers per hour#%s*")or value:match("%s*kilometer per hour#%s*")or value:match("%s*kilometres per hour#%s*")or value:match("%s*kilometre per hour#%s*") then
convertedValue = kmhToMph(value)
elseif value:match("%s*kms#%s*")or value:match("%s*km#%s*")or value:match("%s*kilometer#%s*")or value:match("%s*kilometers#%s*")or value:match("%s*kilometre#%s*")or value:match("%s*kilometres#%s*") then
convertedValue = kmToMi(value)
elseif value:match("%s*mi#%s*")or value:match("%s*mile#%s*")or value:match("%s*miles#%s*") then
convertedValue = miToKm(value)
elseif value:match("%s*hp#%s*")or value:match("%s*hps#%s*")or value:match("%s*horsepower#%s*")or value:match("%s*horsepowers#%s*") then
convertedValue = hpToKw(value)
elseif value:match("%s*kw#%s*")or value:match("%s*kws#%s*")or value:match("%s*kilowatt#%s*")or value:match("%s*kilowatts#%s*") then
convertedValue = kwToHp(value) elseif value:match("%s*ft#%s*") then elseif value:match("%s*lb#%s*")or value:match("%s*pound#%s*")or value:match("%s*pounds#%s*") then
convertedValue = lbToKg(value)
elseif value:match("%s*mmms#%s*")or value:match("%s*mm#%s*")or value:match("%s*millimeter#%s*")or value:match("%s*millimetre#%s*")or value:match("%s*millimeters#%s*")or value:match("%s*millimetres#%s*") then
convertedValue = mmToInches(value)
elseif value:match("%s*in#%s*")or value:match("%s*ins#%s*")or value:match("%s*inch#%s*")or value:match("%s*inche#%s*")or value:match("%s*inches#%s*") then
convertedValue = inchesToMm(value)
elseif value:match("%s*ms#%s*")or value:match("%s*millisecond#%s*")or value:match("%s*milliseconds#%s*") then
convertedValue = msToFts(value)
elseif value:match("%s*fts#%s*")or value:match("%s*ft/s#%s*")or value:match("%s*fps#%s*")or value:match("%s*footperseconds#%s*") then
convertedValue = ftsToMs(value)
elseif value:match("%s*°c#%s*") or value:match("%s*celsius#%s*") or value:match("%s*Celsius#%s*") then
convertedValue = celsiusToFahrenheit(value)
elseif value:match("%s*°f#%s*") or value:match("%s*fahrenheit#%s*") then
convertedValue = fahrenheitToCelsius(value)
--❗
elseif value:match("%s*liters#%s*")or value:match("%s*litres#%s*")or value:match("%s*litre#%s*")or value:match("%s*liter#%s*") then
convertedValue = litersToGallons(value)
elseif value:match("%s*gallons#%s*")or value:match("%s*gallon#%s*") then
convertedValue = gallonsToLiters(value)
elseif value:match("%s*m³#%s*")or value:match("%s*m3#%s*")or value:match("%s*cubicmeters#%s*")or value:match("%s*cubicmetres#%s*")or value:match("%s*cubicmetre#%s*")or value:match("%s*cubicmeter#%s*") then
convertedValue = cubicMetersToCubicFeet(value)
elseif value:match("%s*ft³#%s*")or value:match("%s*ft3#%s*")or value:match("%s*cubicfeet#%s*")or value:match("%s*cubicfoot#%s*")or value:match("%s*cubicfeets#%s*")or value:match("%s*cubicfoots#%s*") then
convertedValue = cubicFeetToCubicMeters(value) elseif value:match("%s*sqm#%s*")or value:match("%s*sqms#%s*")or value:match("%s*sqmetre#%s*")or value:match("%s*sqmetres#%s*")or value:match("%s*squaremeter#%s*")or value:match("%s*squaremeters#%s*")or value:match("%s*sqmeter#%s*")or value:match("%s*sqmeters#%s*") then
convertedValue = squareMetersToSquareFeet(value)
elseif value:match("%s*sqft#%s*")or value:match("%s*sqfoot#%s*")or value:match("%s*sqfeet#%s*")or value:match("%s*squarefoots#%s*")or value:match("%s*squarefeets#%s*")or value:match("%s*squarefeet#%s*") then
convertedValue = squareFeetToSquareMeters(value)
elseif value:match("%s*hectares#%s*")or value:match("%s*hectare#%s*") then
convertedValue = hectaresToAcres(value)
elseif value:match("%s*acres#%s*")or value:match("%s*acre#%s*") then
convertedValue = acresToHectares(value)
elseif value:match("%s*hours#%s*")or value:match("%s*hour#%s*") then
convertedValue = hoursToMinutes(value)
elseif value:match("%s*minutes#%s*")or value:match("%s*minute#%s*") then
convertedValue = minutesToHours(value)
elseif value:match("%s*days#%s*")or value:match("%s*day#%s*") then
convertedValue = daysToHours(value)
elseif value:match("%s*joules#%s*")or value:match("%s*joule#%s*") then
convertedValue = joulesToCalories(value)
elseif value:match("%s*calories#%s*")or value:match("%s*calorie#%s*") then
convertedValue = caloriesToJoules(value)
elseif value:match("%s*kwh#%s*")or value:match("%s*kilowatthours#%s*")or value:match("%s*kilowatthour#%s*") then
convertedValue = kilowattHoursToJoules(value)
elseif value:match("%s*psi#%s*")or value:match("%s*pounds per square inch#%s*")or value:match("%s*pound per square inches#%s*") then
convertedValue = psiToPascals(value)
elseif value:match("%s*pascals#%s*")or value:match("%s*pascal#%s*") then
convertedValue = pascalsToPsi(value)
elseif value:match("%s*atm#%s*")or value:match("%s*atmospheres#%s*")or value:match("%s*atmosphere#%s*") then
convertedValue = atmospheresToPascals(value)
elseif value:match("%s*rpm#%s*") then
convertedValue = rpmToHertz(value)
elseif value:match("%s*kilohertz#%s*")or value:match("%s*khz#%s*") then
convertedValue = kilohertzToHertz(value)
elseif value:match("%s*hertz#%s*") then
convertedValue = hertzToRPM(value)
elseif value:match("%s*horsepower#%s*") then
convertedValue = horsepowerToWatts(value)
elseif value:match("%s*megawatts#%s*") or value:match("%s*megawatt#%s*")then
convertedValue = megawattsToHorsepower(value)
elseif value:match("%s*watts#%s*") or value:match("%s*watt#%s*")then
convertedValue = wattsToHorsepower(value)
elseif value:match("%s*hp#%s*") then
convertedValue = horsepowerToMegawatts(value)
elseif value:match("%s*m#%s*")or value:match("%s*meters#%s*")or value:match("%s*metres#%s*")or value:match("%s*meter#%s*")or value:match("%s*metre#%s*") then
convertedValue = mToFeet(value)
elseif value:match("%s*ft#%s*") or value:match("%s*feet#%s*")or value:match("%s*feets#%s*")or value:match("%s*foot#%s*")or value:match("%s*foots#%s*") then
convertedValue = ftToCm(value)
elseif value:match("%d%d%d%d/")or value:match("%d%d%d/")or value:match("%d%d/") then -- Check if the value is a date
local age =calculateAge(value)
if age then
convertedValue = value .. ' (' .. age .. ')'
end
end
elseif type(value) == "number" then
local age = calculateAge(tostring(value))
if age then
convertedValue = tostring(value) .. ' (' .. age .. ')'
end
end
-- Capitalize the key before displaying
output = output .. '| style="font-weight:bolder;padding-right:1em;" | ' .. capitalizeFirstLetter(key) .. "'''<br />\n"
if originalValue ~= convertedValue then
output = output .. '| ' .. convertedValue .. '\n|-\n'
else
output = output .. '| ' .. value .. '\n|-\n'
end
end
end
end
end
if not args.abvstyle then
args.abvstyle = "" -- Set default value for abvstyle if not provided
end
for abvParam, _ in pairs(abvParams) do
output = output .. '| style="background:'..(args.abvheadcolor or 'transpaent')..';color:'..(args.abvcolor or'black')..';font-size:18px;text-align:center;' .. args.abvstyle .. '" | ' .. abvParam .. '\n|-\n'
end
output =output.. '|}\n|-\n'
return output
end
--///////
return p