Module:EnWiktEnAdv

From Wikipedia

Documentation for this module may be created at Module:EnWiktEnAdv/doc

local z = {}


function z.main(frame)
    -- This function is the entry-point: it's what the template invokes.
    
    local config = frame.args
    local namespace = config.namespace
    local pageName = config.pageName
    local subPageName = config.subPageName
    
    local args = frame:getParent().args
    local head = args.head or args.pos or subPageName
    local sort = args.sort ; if sort == '' then sort = nil end
    
    local ret = ''
    
    ret = ret .. '<span class="infl-inline">'
    ret = ret .. '<b class="Latn " lang="en">' .. head .. '</b>'
    
    if args[1] == '?' then
        -- nothing to do here
    elseif args[1] == '-' then
        ret = ret .. " (''not [[Appendix:Glossary‎#comparable|comparable]]'')"
    else
        ret = ret .. " (''[[Appendix:Glossary‎#comparative|comparative]]'' "
        ret = ret .. z.getComparatives(pageName, args[1], args[2], args[3])
        ret = ret .. ", ''[[Appendix:Glossary‎#superlative|superlative]]'' "
        ret = ret .. z.getSuperlatives(pageName, args[1], args[2], args[3])
        ret = ret .. ')'
    end
    
    ret = ret .. '</span>'
    
    if namespace == '' or namespace == 'Appendix' then
        ret = ret .. '[[Category:English adverbs|' .. (sort or subPageName) .. ']]'
    elseif namespace == 'Template' and subPageName ~= 'doc' then
        ret = ret .. '[[Category:English adverbs|' .. (sort or pageName) .. ']]'
    end
    
    return ret
end


function z.getComparatives(pageName, firstArg, secondArg, thirdArg)
    if not z.isValidPageName(firstArg or 'valid') then
        -- already contains a link or something; just assume it's completely
        -- ready:
        return firstArg
    end
    
    local synthetic = z.getSyntheticComparative(pageName, firstArg, secondArg)
    
    local analytic
    if synthetic == nil or secondArg == 'more' or thirdArg == 'more' then
        analytic = 'more ' .. pageName
    end
    
    if synthetic and analytic then
        return "'''" .. synthetic .. "''' ''or'' '''" .. analytic .. "'''"
    else
        return "'''" .. (synthetic or analytic) .. "'''"
    end
end


function z.getSuperlatives(pageName, firstArg, secondArg, thirdArg)
    if not z.isValidPageName(secondArg or 'valid') then
        -- already contains a link or something; just assume it's completely
        -- ready:
        return secondArg
    end
    
    local synthetic = z.getSyntheticSuperlative(pageName, firstArg, secondArg)
    
    local analytic
    if synthetic == nil or secondArg == 'more' or thirdArg == 'more' then
        analytic = 'most ' .. pageName
    end
    
    if synthetic and analytic then
        return "'''" .. synthetic .. "''' ''or'' '''" .. analytic .. "'''"
    else
        return "'''" .. (synthetic or analytic) .. "'''"
    end
end


function z.getSyntheticComparative(pageName, firstArg, secondArg)
    if firstArg == nil or firstArg == 'more' then
        return nil
    elseif firstArg == 'er' then
        return '[[' .. pageName .. 'er]]'
    elseif secondArg == 'er' or secondArg == 'ier' or secondArg == 'r' then
        return '[[' .. firstArg .. secondArg .. ']]'
    else
        return '[[' .. firstArg .. ']]'
    end
end


function z.getSyntheticSuperlative(pageName, firstArg, secondArg)
    if firstArg == nil or firstArg == 'more' then
        return nil
    elseif firstArg == 'er' then
        return '[[' .. pageName .. 'est]]'
    elseif secondArg == 'er' or secondArg == 'ier' or secondArg == 'r' then
        return '[[' .. firstArg .. secondArg:gsub('r', 'st') .. ']]'
    else
        return '[[' .. secondArg .. ']]'
    end
end


function z.isValidPageName(possiblePageName)
    -- Note: the {{isValidPageName}} template, because it can't just examine
    -- characters directly, implements a much more convoluted test than this --
    -- one that's difficult to reverse-engineer. This version should be fine for
    -- this purpose, however. Because it checks for %[ and %], it will return
    -- false if possiblePageName includes any wikilinks, and because it checks
    -- for %c (control characters, including \127), it will return false if
    -- possiblePageName includes any <nowiki> segments.
    
    return not possiblePageName:find('[%c%#%<%>%[%]%|%{%}]')
end


return z