Module:Sort title/sandbox: Difference between revisions
Appearance
Content deleted Content added
No edit summary |
Synced; simplified |
||
Line 1: | Line 1: | ||
⚫ | |||
local match = require("Module:String")._match |
local match = require("Module:String")._match |
||
Line 5: | Line 4: | ||
--[[ |
--[[ |
||
Local function which is used to return a |
Local function which is used to return a number without commas. |
||
⚫ | |||
--]] |
--]] |
||
local function |
local function cleanNumberFromCommas(title) |
||
return |
return string.gsub(title, "(%d+)(,+)", "%1") |
||
end |
end |
||
Line 17: | Line 17: | ||
local function isFirstWordAnArticle(word) |
local function isFirstWordAnArticle(word) |
||
word = string.lower(word) |
word = string.lower(word) |
||
return (word == "a" or word == "an" or word == "the") |
|||
⚫ | |||
else |
|||
return false |
|||
end |
|||
end |
end |
||
--[[ |
--[[ |
||
Local function which is used to return |
Local function which is used to return the title without its disambiguation. |
||
--]] |
--]] |
||
local function |
local function getTitleWithoutDisambiguation(title, disambiguation) |
||
local |
local newTitle = string.gsub(title, "%(".. disambiguation .. "%)", "") |
||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
end |
|||
⚫ | |||
⚫ | |||
end |
|||
--TODO: DOC |
|||
local function convertRomanNumerals(romanNumerals) |
|||
local convertModule = require('Module:ConvertNumeric').roman_to_numeral |
|||
local result, error = convertModule(romanNumerals) |
|||
if (tonumber(result) ~= -1) then |
|||
return tonumber(result) |
|||
else |
|||
return nil |
|||
end |
|||
end |
|||
--TODO: DOC |
|||
local function checkForRomanNumerals(title) |
|||
if (title) then |
|||
return mw.ustring.match(title, "[IVXLCDM]+") |
|||
else |
|||
return nil |
|||
end |
|||
end |
end |
||
--[[ |
--[[ |
||
Local function which is used to return |
Local function which is used to return a title without its first word. |
||
--]] |
--]] |
||
local function |
local function getTitleWithoutFirstWord(title) |
||
return |
return mw.ustring.gsub(title, "^[^%s]*%s*", "") |
||
end |
end |
||
Line 72: | Line 43: | ||
--[[ |
--[[ |
||
Local function which is used to return a |
Local function which is used to return a sort key for a specific part. |
||
⚫ | |||
--]] |
--]] |
||
local function |
local function getSortKey(title, firstWord) |
||
⚫ | |||
return string.gsub(title, "(%d+)(,+)", "%1") |
|||
⚫ | |||
end |
|||
⚫ | |||
⚫ | |||
--TODO: DOC |
|||
local function getTitlePartSortKey(title) |
|||
title = cleanNumberFromCommas(title) |
|||
⚫ | |||
local lastWord = getLastWord(title) |
|||
local romanNumerals = checkForRomanNumerals(lastWord) |
|||
if (romanNumerals) then |
|||
local lastWordModified = convertRomanNumerals(lastWord) |
|||
if (lastWordModified) then |
|||
title = string.gsub(title, lastWord, lastWordModified) |
|||
end |
|||
end |
end |
||
⚫ | |||
⚫ | |||
⚫ | |||
end |
end |
||
Line 101: | Line 62: | ||
return "" |
return "" |
||
end |
end |
||
local firstWord = getFirstWord(disambiguation) |
|||
local disambiguationSortKey = getSortKey(disambiguation, firstWord) |
|||
end |
|||
return "(" .. disambiguationSortKey .. ")" |
|||
--[[ |
|||
Local function which is used to return the title without its disambiguation. |
|||
--]] |
|||
local function getTitleWithoutDisambiguation(title, disambiguation) |
|||
local newTitle = string.gsub(title, "%(".. disambiguation .. "%)", "") |
|||
⚫ | |||
end |
end |
||
Line 128: | Line 83: | ||
The main function. |
The main function. |
||
--]] |
--]] |
||
local function _main( |
local function _main(title) |
||
if (not title) then |
|||
if (args[1]) then |
|||
title = args[1] |
|||
else |
|||
title = mw.title.getCurrentTitle().text |
title = mw.title.getCurrentTitle().text |
||
end |
end |
||
⚫ | |||
local disambiguation = getDisambiguation(title) |
local disambiguation = getDisambiguation(title) |
||
local disambiguationSortKey = getDisambiguationSortKey(disambiguation) |
local disambiguationSortKey = getDisambiguationSortKey(disambiguation) |
||
title = getTitleWithoutDisambiguation(title, disambiguation) |
title = getTitleWithoutDisambiguation(title, disambiguation) |
||
title = |
title = cleanNumberFromCommas(title) |
||
⚫ | |||
local sortKey = title .. " " .. disambiguationSortKey |
local sortKey = title .. " " .. disambiguationSortKey |
||
return mw.text.trim(sortKey) |
return mw.text.trim(sortKey) |
||
Line 157: | Line 111: | ||
--]] |
--]] |
||
function p.getSortKey(frame) |
function p.getSortKey(frame) |
||
⚫ | |||
local args = getArgs(frame) |
|||
return p._main(args) |
|||
end |
end |
||
Line 165: | Line 118: | ||
--]] |
--]] |
||
function p.getDefaultSort(frame) |
function p.getDefaultSort(frame) |
||
local |
local sortKey = _main(nil) |
||
⚫ | |||
return frame:preprocess{text = "{{DEFAULTSORT:" .. sortKey .. "}}"} |
return frame:preprocess{text = "{{DEFAULTSORT:" .. sortKey .. "}}"} |
||
end |
end |
||
Line 174: | Line 126: | ||
--]] |
--]] |
||
function p.testcases(frame) |
function p.testcases(frame) |
||
⚫ | |||
local args = getArgs(frame) |
local args = getArgs(frame) |
||
return _main(args) |
return _main(args[1]) |
||
end |
end |
||
Latest revision as of 02:28, 11 April 2023
![]() | This is the module sandbox page for Module:Sort title (diff). See also the companion subpage for test cases (run). |
![]() | 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
[edit]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
[edit]From template:
{{#invoke:Sort title|getSortKey}}
{{#invoke:Sort title|getDefaultSort}}
From module:
local sortkeyModule = require('Module:Sort title')
local sortkey = sortkeyModule._getSortKey()
Function list
[edit]Function | Explanation |
---|---|
getSortKey
|
Returns a sortkey |
getDefaultSort
|
Returns a DEFAULTSORT string |
local match = require("Module:String")._match
local p = {}
--[[
Local function which is used to return a number without commas.
For example: 4,722 will become 4722.
--]]
local function cleanNumberFromCommas(title)
return string.gsub(title, "(%d+)(,+)", "%1")
end
--[[
Local function which is used to check if the word is an article.
Returns true if it is, false if it isn't.
--]]
local function isFirstWordAnArticle(word)
word = string.lower(word)
return (word == "a" or word == "an" or word == "the")
end
--[[
Local function which is used to return the title without its disambiguation.
--]]
local function getTitleWithoutDisambiguation(title, disambiguation)
local newTitle = string.gsub(title, "%(".. disambiguation .. "%)", "")
return mw.text.trim(newTitle)
end
--[[
Local function which is used to return a title without its first word.
--]]
local function getTitleWithoutFirstWord(title)
return mw.ustring.gsub(title, "^[^%s]*%s*", "")
end
--[[
Local function which is used to return the first word from a title.
--]]
local function getFirstWord(title)
return match(title, "^[^%s]*", 1, 1, false, "")
end
--[[
Local function which is used to return a sort key for a specific part.
--]]
local function getSortKey(title, firstWord)
local sortKey = title
if (isFirstWordAnArticle(firstWord) and firstWord ~= title) then
title = getTitleWithoutFirstWord(title)
sortKey = title .. ", " .. firstWord
end
return sortKey
end
--[[
Local function which is used to return the disambiguation sort key.
--]]
local function getDisambiguationSortKey(disambiguation)
if (disambiguation == "") then
return ""
end
local firstWord = getFirstWord(disambiguation)
local disambiguationSortKey = getSortKey(disambiguation, firstWord)
return "(" .. disambiguationSortKey .. ")"
end
--[[
Local function which is used to return the disambiguation from a title.
--]]
local function getDisambiguation(title)
local disambiguation = match(title, "%s%((.-)%)", 1, -1, false, "")
if (disambiguation == "") then
return ""
else
return disambiguation
end
end
--[[
The main function.
--]]
local function _main(title)
if (not title) then
title = mw.title.getCurrentTitle().text
end
local firstWord = getFirstWord(title)
local disambiguation = getDisambiguation(title)
local disambiguationSortKey = getDisambiguationSortKey(disambiguation)
title = getTitleWithoutDisambiguation(title, disambiguation)
title = cleanNumberFromCommas(title)
title = getSortKey(title, firstWord)
local sortKey = title .. " " .. disambiguationSortKey
return mw.text.trim(sortKey)
end
--[[
Public function which allows modules to retrieve a sort key.
--]]
function p._getSortKey()
return _main(nil)
end
--[[
Public function which allows templates to retrieve a sort key.
--]]
function p.getSortKey(frame)
return _main(nil)
end
--[[
Public function which allows templates to retrieve the sort key inside a DEFAULTSORT.
--]]
function p.getDefaultSort(frame)
local sortKey = _main(nil)
return frame:preprocess{text = "{{DEFAULTSORT:" .. sortKey .. "}}"}
end
--[[
Public function which is used for testing various names and not the current page name.
--]]
function p.testcases(frame)
local getArgs = require('Module:Arguments').getArgs
local args = getArgs(frame)
return _main(args[1])
end
return p