Difference between revisions of "Module:Sort title/sandbox"

From blackwiki
Jump to navigation Jump to search
blackwiki>Gonnym
(create sandbox)
 
m (10 revisions imported)
 
(9 intermediate revisions by one other user not shown)
Line 1: Line 1:
 +
local getArgs = require('Module:Arguments').getArgs
 
local match = require("Module:String")._match
 
local match = require("Module:String")._match
  
Line 4: Line 5:
  
 
--[[
 
--[[
Local function which is used to return a number without commas.
+
Local function which is used to return a title without its first word.
For example: 4,722 will become 4722.
 
 
--]]
 
--]]
local function cleanNumberFromCommas(title)
+
local function getTitleWithoutFirstWord(title)
return string.gsub(title, "(%d+)(,+)", "%1")
+
return mw.ustring.gsub(title, "^[^%s]*%s*", "")
 
end
 
end
  
Line 25: Line 25:
  
 
--[[
 
--[[
Local function which is used to return the title without its disambiguation.
+
Local function which is used to return a sort key for a specific part.
 
--]]
 
--]]
local function getTitleWithoutDisambiguation(title, disambiguation)
+
local function getSortKey(title, firstWord)
local newTitle = string.gsub(title, "%(".. disambiguation .. "%)", "")
+
local sortKey = title
return mw.text.trim(newTitle)
+
if (isFirstWordAnArticle(firstWord) and firstWord ~= title) then
 +
title = getTitleWithoutFirstWord(title)
 +
sortKey = title .. ", " .. firstWord
 +
end
 +
 +
return sortKey
 +
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 a title without its first word.
+
Local function which is used to return the last word from a title.
 
--]]
 
--]]
local function getTitleWithoutFirstWord(title)
+
local function getLastWord(title)
return mw.ustring.gsub(title, "^[^%s]*%s*", "")
+
return string.match(title, "%s(%S+)$")
 
end
 
end
  
Line 47: Line 72:
  
 
--[[
 
--[[
Local function which is used to return a sort key for a specific part.
+
Local function which is used to return a number without commas.
 +
For example: 4,722 will become 4722.
 
--]]
 
--]]
local function getSortKey(title, firstWord)
+
local function cleanNumberFromCommas(title)
local sortKey = title
+
return string.gsub(title, "(%d+)(,+)", "%1")
if (isFirstWordAnArticle(firstWord)) then
+
end
title = getTitleWithoutFirstWord(title)
+
 
sortKey = title .. ", " .. firstWord
+
--TODO: DOC
 +
local function getTitlePartSortKey(title)
 +
title = cleanNumberFromCommas(title)
 +
local firstWord = getFirstWord(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
+
return getSortKey(title, firstWord)
return sortKey
 
 
end
 
end
  
Line 66: Line 101:
 
return ""
 
return ""
 
end
 
end
+
 
local firstWord = getFirstWord(disambiguation)
+
return "(" .. getTitlePartSortKey(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 .. "%)", "")
 +
return mw.text.trim(newTitle)
 
end
 
end
  
Line 87: Line 128:
 
The main function.
 
The main function.
 
--]]
 
--]]
local function _main(title)
+
local function _main(args)
if (not title) then
+
local title
 +
if (args[1]) then
 +
title = args[1]
 +
else
 
title = mw.title.getCurrentTitle().text
 
title = mw.title.getCurrentTitle().text
 
end
 
end
  
local firstWord = getFirstWord(title)
 
 
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 = cleanNumberFromCommas(title)
+
title = getTitlePartSortKey(title)
title = getSortKey(title, firstWord)
+
 
 
 
local sortKey = title .. " " .. disambiguationSortKey
 
local sortKey = title .. " " .. disambiguationSortKey
 
return mw.text.trim(sortKey)
 
return mw.text.trim(sortKey)
Line 115: Line 157:
 
--]]
 
--]]
 
function p.getSortKey(frame)
 
function p.getSortKey(frame)
return p._main(nil)  
+
local args = getArgs(frame)
 +
return p._main(args)  
 
end
 
end
  
Line 122: Line 165:
 
--]]
 
--]]
 
function p.getDefaultSort(frame)
 
function p.getDefaultSort(frame)
local sortKey = p._main(nil)
+
local args = getArgs(frame)
 +
local sortKey = p._main(args)
 
return frame:preprocess{text = "{{DEFAULTSORT:" .. sortKey .. "}}"}  
 
return frame:preprocess{text = "{{DEFAULTSORT:" .. sortKey .. "}}"}  
 
end
 
end
Line 130: Line 174:
 
--]]
 
--]]
 
function p.testcases(frame)
 
function p.testcases(frame)
local getArgs = require('Module:Arguments').getArgs
 
 
local args = getArgs(frame)
 
local args = getArgs(frame)
return _main(args[1])
+
return _main(args)
 
end
 
end
  
 
return p
 
return p

Latest revision as of 07:28, 28 September 2020

Documentation for this module may be created at Module:Sort title/sandbox/doc

local getArgs = require('Module:Arguments').getArgs
local match = require("Module:String")._match

local p = {}

--[[
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 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)
	if (word == "a" or word == "an" or word == "the") then
		return true
	else
		return false
	end
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

--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

--[[
Local function which is used to return the last word from a title.
--]]
local function getLastWord(title)
	return string.match(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 number without commas.
For example: 4,722 will become 4722.
--]]
local function cleanNumberFromCommas(title)
	return string.gsub(title, "(%d+)(,+)", "%1")
end

--TODO: DOC
local function getTitlePartSortKey(title)
	title = cleanNumberFromCommas(title)
	local firstWord = getFirstWord(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
	return getSortKey(title, firstWord)
end

--[[
Local function which is used to return the disambiguation sort key.
--]]
local function getDisambiguationSortKey(disambiguation)
	if (disambiguation == "") then
		return ""
	end

	return "(" .. getTitlePartSortKey(disambiguation) .. ")"
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 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(args)
	local title
	if (args[1]) then
		title = args[1]
	else
		title = mw.title.getCurrentTitle().text
	end

	local disambiguation = getDisambiguation(title)
	local disambiguationSortKey = getDisambiguationSortKey(disambiguation)
	
	title = getTitleWithoutDisambiguation(title, disambiguation)
	title = getTitlePartSortKey(title)

	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)
	local args = getArgs(frame)
	return p._main(args) 
end

--[[
Public function which allows templates to retrieve the sort key inside a DEFAULTSORT.
--]]
function p.getDefaultSort(frame)
	local args = getArgs(frame)
	local sortKey = p._main(args)
	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 args = getArgs(frame)
	return _main(args)
end

return p