Module:Sort title
Appearance
![]() | This Lua module is used on approximately 25,000 pages and changes may be widely noticed. Test changes in the module's /sandbox or /testcases subpages, or in your own module sandbox. Consider discussing changes on the talk page before implementing them. |
![]() | This module depends on the following other modules: |
Module:Sort title is used to create sortkeys for pages.
What it does
The module preforms the following checks:
- If the first word of the title is an article, it moves it to the end of the title. For example:
The Avengers
will have a sortkey ofAvengers, The
. - If the title is disambiguated and the first word of the disambiguation is an article, it moves it to the end of the disambiguation. For example:
Pilot (The Flash)
will have a sortkey ofPilot (Flash, The)
. - If the title is disambiguated and the first word of the title and the disambiguation is an article, both of the above changes will occur. For example:
The End (A Series of Unfortunate Events)
will have a sortkey ofEnd, The (Series of Unfortunate Events, A)
. - If the title is a number which is separated by a commas, it removes the commas. For example:
4,722 Hours
will have a sortkey of4722 Hours
.
Usage
From template:
{{#invoke:Sort title|getSortKey}}
{{#invoke:Sort title|getDefaultSort}}
From module:
local sortkeyModule = require('Module:Sort title')
local sortkey = sortkeyModule._getSortKey()
Function list
Function | Explanation |
---|---|
getSortKey
|
Returns a sortkey |
getDefaultSort
|
Returns a DEFAULTSORT string |
local match = require("Module:String")._match
local p = {}
local function cleanNumberFromCommas(title)
return string.gsub(title, "(%d+)(,+)", "%1")
end
local function isFirstWordAnArticle(word)
word = string.lower(word)
if (word == "a" or word == "an" or word == "the") then
return true
else
return false
end
end
local function getTitleWithoutDisambiguation(title, disambiguation)
local newTitle = string.gsub(title, "%(".. disambiguation .. "%)", "")
return mw.text.trim(newTitle)
end
local function getTitleWithoutFirstWord(title)
return mw.ustring.gsub(title, "^[^%s]*%s*", "")
end
local function getFirstWord(title)
return match(title, "^[^%s]*", 1, 1, false, "")
end
local function getDisambiguation(title)
local disambiguation = match(title, "%s%((.-)%)", 1, -1, false, "")
if (disambiguation == "") then
return "", ""
end
local firstWord = getFirstWord(disambiguation)
local disambiguationSort = disambiguation
if (isFirstWordAnArticle(firstWord)) then
local disambiguationWihoutFirstWord = getTitleWithoutFirstWord(disambiguation)
disambiguationSort = disambiguationWihoutFirstWord .. ", " .. firstWord
end
return disambiguation, "(" .. disambiguationSort .. ")"
end
local function _main(title)
if (not title) then
title = mw.title.getCurrentTitle().text
end
local firstWord = getFirstWord(title)
local disambiguation, disambiguationSort = getDisambiguation(title)
title = getTitleWithoutDisambiguation(title, disambiguation)
title = cleanNumberFromCommas(title)
if (isFirstWordAnArticle(firstWord)) then
title = getTitleWithoutFirstWord(title)
title = title .. ", " .. firstWord
end
local sortKey = title .. " " .. disambiguationSort
return mw.text.trim(sortKey)
end
function p._getSortKey()
return _main()
end
function p.getSortKey(frame)
return p._getSortKey()
end
function p.getDefaultSort(frame)
local sortKey = p._getSortKey()
return frame:preprocess{text = "{{DEFAULTSORT:" .. sortKey .. "}}"}
end
function p.testcases(frame)
local getArgs = require('Module:Arguments').getArgs
local args = getArgs(frame)
return _main(args[1])
end
return p