Difference between revisions of "Module:SortName"

From blackwiki
Jump to navigation Jump to search
blackwiki>Bility
m (see if this works)
blackwiki>Bility
m (fixes)
Line 4: Line 4:
 
     --local currentpage = mw.title.getCurrentTitle()
 
     --local currentpage = mw.title.getCurrentTitle()
 
     --local pagetitle = currentpage.text
 
     --local pagetitle = currentpage.text
     local pagetitle = frame.args[1] or 'noinputgiven'
+
     local pagetitle = frame or 'nothing'--frame.args[1] or 'noinputgiven'
 +
    local langvar = mw.language.getContentLanguage()
 
     local text1 = ''
 
     local text1 = ''
 
     local text2 = ''
 
     local text2 = ''
Line 10: Line 11:
 
     local partmatch = false
 
     local partmatch = false
 
     if string.find( pagetitle, ' ' ) ~= nil then
 
     if string.find( pagetitle, ' ' ) ~= nil then
         pagetitle = string.gsub( pagetitle, '^List of ', '', 1 )
+
         pagetitle = string.gsub( string.gsub( string.gsub( pagetitle, '%b()', '' ), ' +', ' '), ' $', '' )
        pagetitle = mw.text.trim( string.gsub( string.gsub( pagetitle, '%b()', '' ), ' +', ' ') )
+
        if string.find( pagetitle, '^List of ' ) ~= nil then
         pagetitle = string.gsub( pagetitle, ',.*$', '' )
+
            pagetitle =langvar:ucfirst( string.gsub( pagetitle, '^List of ', '', 1 ) )
        pagetitle = string.gsub( pagetitle, ' of .*$', '' )
+
         else
        for i in ipairs( parts ) do
+
            pagetitle = string.gsub( pagetitle, ',.*$', '' )
            if string.find( pagetitle, ' ' .. parts[i] .. ' ' ) ~= nil then
+
            pagetitle = string.gsub( pagetitle, ' of .*$', '' )
                text1 = string.sub( pagetitle, string.find( pagetitle, ' ' .. parts[i] .. ' ' ) + 1, #pagetitle )
+
            for i in ipairs( parts ) do
                text2 = string.sub( pagetitle, 0, string.find( pagetitle, ' ' .. parts[i] .. ' ' ) )
+
                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
 
                 pagetitle = text1 .. ', ' .. text2
                partmatch = true
 
                break
 
 
             end
 
             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
 
     end

Revision as of 23:42, 9 April 2013

Module:SortName (edit | talk | history | links | watch | logs)

This module returns a string in a format suitable as a category sortkey, per WP:NAMESORT.

Input

The module accepts a string as the first and only unnamed parameter, but is meant to be used with no parameter, in which case the title of the calling page is used.

Usage

This module should be subst'd for use in template parameters that accept a sortkey or in the {{DEFAULTSORT}} variable.

  • No input (sorts the pages title): {{subst:#invoke:SortName|sortname}}
  • With input: {{subst:#invoke:SortName|sortname|input string}}

local p = {}
 
function p.sortname(frame)
    --local currentpage = mw.title.getCurrentTitle()
    --local pagetitle = currentpage.text
    local pagetitle = frame or 'nothing'--frame.args[1] or 'noinputgiven'
    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