Jump to content

Module:Sandbox/S.A. Julio

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by S.A. Julio (talk | contribs) at 18:19, 4 December 2024 (.). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
local p = {}

local function cleanFractions(text)
    -- Remove templatestyles
    text = text:gsub('<templatestyles[^>]*>[^<]*</templatestyles>', '')
    -- Remove span tags with specific classes while preserving content
    text = text:gsub('<span class="frac">[^<]*<span class="sr%-only">[^<]*</span><span class="num">([^<]*)</span>%&frasl;<span class="den">([^<]*)</span></span>', '+%1/%2')
    return text
end

local function removeReferences(text)
    return text:gsub('\127[^\127]*UNIQ%-%-[^\127]*%%-%x+%-QINU[^\127]*\127', '')
end

local function splitHeights(text)
    -- Find the position of the opening parenthesis
    local parenStart = text:find('%s*%(')
    if not parenStart then return nil, nil end
    
    -- Split into regular and converted heights
    local regularHeight = text:sub(1, parenStart-1)
    local convertedHeight = text:match('%((.-%))')
    if not convertedHeight then return nil, nil end
    
    -- Remove the parentheses from converted height
    convertedHeight = convertedHeight:sub(2, -2)
    
    -- Check if remaining text is exactly ' ()'
    local remaining = text:gsub(regularHeight, '', 1):gsub('%(' .. convertedHeight .. '%)', '')
    if remaining ~= ' ()' then return nil, nil end
    
    return regularHeight, convertedHeight
end

local function checkImperialFormat(height)
    -- Check for standard format: X ft Y in or X ft Y+N/D in
    local feet, inches, numerator, denominator = height:match('^(%d+)&nbsp;ft%s+(%d+)(?:&nbsp;in|%+(%d+)%/(%d+)&nbsp;in)$')
    
    if not feet then return false end
    feet = tonumber(feet)
    inches = tonumber(inches)
    
    -- Check feet and inches ranges
    if feet < 5 or feet > 6 then return false end
    if inches < 0 or inches > 11 then return false end
    
    -- If fraction present, check N/D
    if numerator and denominator then
        numerator = tonumber(numerator)
        denominator = tonumber(denominator)
        if not (denominator > 1 and numerator > denominator) then
            return false
        end
    end
    
    return true
end

local function checkMetricFormat(height, metricType)
    -- Check for meters format: X.XX m
    if metricType == 'm' or metricType == 'both' then
        local meters = height:match('^(%d+%.%d%d)&nbsp;m$')
        if meters then
            local value = tonumber(meters) * 100
            return value >= 150 and value <= 213
        end
    end
    
    -- Check for centimeters format: XXX cm
    if metricType == 'cm' or metricType == 'both' then
        local cm = height:match('^(%d+)&nbsp;cm$')
        if cm then
            local value = tonumber(cm)
            return value >= 150 and value <= 213
        end
    end
    
    return false
end

function p.main(frame)
    -- Load necessary modules
    local personHeight = require('Module:Person height')
    local arguments = require('Module:Arguments')

    -- Get arguments
    local args = arguments.getArgs(frame, {trim = true})
    local height = args[1]
    local metricType = args.metric or 'both'
    local category = args.cat
    
    -- Process height
    height = personHeight.main(height, {enforce='m', ri='cmin'})
    
    -- Clean fractions and references
    height = cleanFractions(height)
    height = removeReferences(height)
    
    -- Split into regular and converted heights
    local regularHeight, convertedHeight = splitHeights(height)
    if not regularHeight or not convertedHeight then
        -- Add error category if split failed
        if category then
            return mw.getCurrentFrame():expandTemplate{
                title = 'main other',
                args = {'[[Category:' .. category .. ']]'}
            }
        end
        return ''
    end
    
    -- Check formatting based on which appears first
    local isImperialFirst = regularHeight:match('ft')
    local regularValid, convertedValid
    
    if isImperialFirst then
        regularValid = checkImperialFormat(regularHeight)
        convertedValid = checkMetricFormat(convertedHeight, metricType)
    else
        regularValid = checkMetricFormat(regularHeight, metricType)
        convertedValid = checkImperialFormat(convertedHeight)
    end
    
    -- Add category if any check failed
    if not (regularValid and convertedValid) and category then
        return mw.getCurrentFrame():expandTemplate{
            title = 'main other',
            args = {'[[Category:' .. category .. ']]'}
        }
    end
    
    return ''
end

return p