Module:Object weight/sandbox
| This is the module sandbox page for Module:Object weight (diff). See also the companion subpage for test cases (run). |
| This module is rated as ready for general use. It has reached a mature state, is considered relatively stable and bug-free, and may be used wherever appropriate. It can be mentioned on help pages and other Wikipedia resources as an option for new users. To minimise server load and avoid disruptive output, improvements should be developed through sandbox testing rather than repeated trial-and-error editing. |
| This Lua module is used on approximately 2,800 pages and changes may be widely noticed. Test changes in the module's /sandbox or /testcases subpages, or in your own module sandbox. Consider discussing changes on the talk page before implementing them. |
This module is intended to aid in converting weights in {{Infobox mobile phone}}.
Usage
{{#invoke:Object weight|weight|123 g}}
See also
-- This module is based off of [[Module:Person weight]]
local p = {}
local tracking_category = ' '
if mw.title.getCurrentTitle():inNamespaces(0, 828, 829) then
-- Category only in namespaces: 0=article, 828=module & 829=module talk (last 2 needed for testcases)
tracking_category = '[[Category:Pages where weight is being automatically converted]]'
end
local function clean_weight(s)
s = mw.ustring.gsub(s, 'grams', 'g')
s = mw.ustring.gsub(s, 'gram', 'g')
s = mw.ustring.gsub(s, 'gs', 'g')
s = mw.ustring.gsub(s, 'g[%.,]', 'g')
s = mw.ustring.gsub(s, 'ounces', 'oz')
s = mw.ustring.gsub(s, 'ounce', 'oz')
s = mw.ustring.gsub(s, 'oz[%.,]', 'oz')
return s
end
local function isnumber(s)
if s then
s = mw.ustring.gsub(s, '%+%s*%d+%s*/%s*%d+%s*$', '')
s = mw.ustring.gsub(s, '%s*[–%-]%s*', '')
return tonumber(s)
end
return nil
end
local function get_convert_weight_args(s)
local prefer_g = (prefer or '') == 'g'
local force_g = (enforce or '') == 'g'
local prefer_oz = (prefer or '') == 'oz'
local force_oz = (enforce or '') == 'oz'
unconverted = clean_weight(s or '') -- basic unit cleaning
s = mw.ustring.gsub(unconverted, '&[Nn][Bb][Ss][Pp];', ' ')
local g = mw.ustring.find(s, 'g')
local oz = mw.ustring.find(s, 'oz')
if g == nil and oz == nil then
return '', unconverted
end
if g ~= nil and oz == nil then
local n = mw.ustring.sub(s, 1, g - 1)
if isnumber(n) then
return {n,'g','oz',0,['abbr']='on'}, mw.ustring.sub(s, g+1)
end
return '', unconverted
end
if oz ~= nil and g == nil then
local n = mw.ustring.sub(s, 1, oz - 1)
if isnumber(n) then
return {n,'oz','g',0,['abbr']='on'}, mw.ustring.sub(s, oz+2)
end
return '', unconverted
end
return '', unconverted
end
function convert_weight(frame, args)
local targs, str = get_convert_weight_args(args[1])
if type(targs) == 'table' then
return frame:expandTemplate{ title = 'convert', args = targs} .. str .. tracking_category
else
return str
end
end
function p.weight(frame)
return convert_weight(frame, frame.args[1] and frame.args or frame:getParent().args)
end
return p
Content Disclaimer
Informasi ini disarikan dari Wikipedia dan disajikan kembali untuk tujuan edukasi. Konten tersedia di bawah lisensi CC BY-SA 3.0. Kami tidak bertanggung jawab atas ketidakakuratan data yang bersumber dari kontribusi publik tersebut.
- The information displayed on this website is sourced in part or in whole from Wikipedia and has been adapted for the purpose of restating it. We strive to provide accurate and relevant information, however:
- There is no guarantee of absolute accuracy. Wikipedia is an open, collaborative project that can be edited by anyone, so information is subject to change.
- It is not intended to constitute professional advice. The content displayed is for informational and educational purposes only. For important decisions (e.g., medical, legal, or financial), please consult a professional.
- Content copyright. Wikipedia is licensed under the Creative Commons Attribution-ShareAlike License (CC BY-SA). This means that content may be reused with appropriate attribution and shared under a similar license.
- Responsible use. Any risk arising from the use of information from this website is entirely the responsibility of the user.