Module:Penis size cm

From Porn Base Central
Jump to navigation Jump to search

Documentation for this module may be created at Module:Penis size cm/doc

local p = {}

local function toInches(cm)
  return cm / 2.54
end

local function toCentimeters(inches)
  return inches * 2.54
end

local function formatNumber(value)
  value = math.floor(value * 10 + 0.5) / 10
  return value % 1 == 0 and string.format("%.0f", value) or string.format("%.1f", value)
end

local function formatLink(category, value, unit)
  return string.format('[[:Category:Penis size %s %s|%s %s]]', value, unit, value, unit)
end

local function applyOption(value, option, tag)
  return (option == "yes" or option == "y") and string.format(tag, value) or value
end

function p.main(frame)
  local input = frame.args[1] or ""
  local categoryOption = frame.args["category"] or "yes"
  local breakOption = frame.args["break"] or "no"
  local linkspanOption = frame.args["linkspan"] or "no"

  input = string.lower(input:gsub("%s+", "")):gsub(",", ".")

  local cm, inches

  if string.match(input, "in$") or string.match(input, "inches$") then
    inches = tonumber(string.match(input, "%d+%.?%d*"))
    cm = toCentimeters(inches)
  else
    cm = tonumber(string.match(input, "%d+%.?%d*"))
    if cm == nil then return "" end
    inches = toInches(cm)
  end

  local formattedCm = formatNumber(cm)
  local formattedInches = formatNumber(inches)

  local linkCm = formatLink("cm", formattedCm, "cm")
  local linkInches = formatLink("inches", formattedInches, "in")

  linkCm = applyOption(linkCm, linkspanOption, '<span class="white-link">%s</span>')
  linkInches = applyOption(linkInches, linkspanOption, '<span class="white-link">%s</span>')

  local breakTag = applyOption("", breakOption, "<br>")

  if categoryOption == "no" then
    return linkCm .. breakTag .. " (" .. linkInches .. ")"
  elseif mw.title.getCurrentTitle().namespace == 0 then
    return "[[Category:Penis size " .. formattedCm .. " cm]]" .. linkCm .. breakTag .. " (" .. linkInches .. ")" .. "[[Category:Penis size " .. formattedInches .. " inches]]"
  else
    return linkCm .. breakTag .. " (" .. linkInches .. ")"
  end
end

return p