Jump to content

Module:SortName: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
m remove test
m adding title support
Line 2: Line 2:
function p.sortname(frame)
function p.sortname(frame)
--local currentpage = mw.title.getCurrentTitle()
local currentpage = mw.title.getCurrentTitle()
--local pagetitle = currentpage.text
local pagetitle = frame.args[1] or currentpage.text
local pagetitle = frame.args[1] or 'noinputgiven'
local langvar = mw.language.getContentLanguage()
local langvar = mw.language.getContentLanguage()
local text1 = ''
local text1 = ''

Revision as of 00:10, 10 April 2013

local p = {}
 
function p.sortname(frame)
    local currentpage = mw.title.getCurrentTitle()
    local pagetitle = frame.args[1] or currentpage.text
    local langvar = mw.language.getContentLanguage()
    local text1 = ''
    local text2 = ''
    local parts = { 'de','De','von','Von','du','Du','del','Del','zu','Zu','di','Di','van','Van','na','Na' }
    local partmatch = false
    if string.find( pagetitle, ' ' ) ~= nil then
        pagetitle = string.gsub( string.gsub( string.gsub( pagetitle, '%b()', '' ), ' +', ' '), ' $', '' )
        if string.find( pagetitle, '^List of ' ) ~= nil then
            pagetitle =langvar:ucfirst( string.gsub( pagetitle, '^List of ', '', 1 ) )
        else
            pagetitle = string.gsub( pagetitle, ',.*$', '' )
            pagetitle = string.gsub( pagetitle, ' of .*$', '' )
            for i in ipairs( parts ) do
                if string.find( pagetitle, ' ' .. parts[i] .. ' ' ) ~= nil then
                    text1 = string.sub( pagetitle, string.find( pagetitle, ' ' .. parts[i] .. ' ' ) + 1, #pagetitle )
                    text2 = string.sub( pagetitle, 0, string.find( pagetitle, ' ' .. parts[i] .. ' ' ) )
                    pagetitle = text1 .. ', ' .. text2
                   partmatch = true
                    break
                end
            end
            if not partmatch and string.find( pagetitle, ' ' ) ~= nil then
                text1 = string.sub( pagetitle, string.find( pagetitle, ' [^ ]*$' ) + 1, #pagetitle )
                text2 = string.sub( pagetitle, 0, string.find( pagetitle, ' [^ ]*$' ) )
                pagetitle = text1 .. ', ' .. text2
            end
        end
    end
    return pagetitle
end
 
return p