Feed The Beast Wiki

Follow the Feed The Beast Wiki on Discord or Mastodon!

READ MORE

Feed The Beast Wiki
m (Sizable's changes)
(If langCode is not valid, just return itself)
 
(8 intermediate revisions by one other user not shown)
Line 1: Line 1:
local langNames = require([[Module:Language/Names]])
+
local langNames = require( [[Module:Language/Names]] )
local code = function(title)
+
local processArgs = require( [[Module:ProcessArgs]] )
  +
local subPage = (title or mw.title.getCurrentTitle()).subpageText:lower()
 
  +
local function code( title )
 
local titleObj = title or mw.title.getCurrentTitle()
  +
if not titleObj.isSubpage then
  +
return nil
 
end
  +
local subPage = titleObj.subpageText:lower()
 
if langNames[subPage] then
 
if langNames[subPage] then
 
return subPage
 
return subPage
 
end
 
end
 
end
 
end
local pageSuffix = function()
+
local function pageSuffix()
 
local langCode = code()
 
local langCode = code()
 
if langCode then
 
if langCode then
Line 12: Line 18:
 
end
 
end
 
return ''
 
return ''
 
end
  +
-- Copied from [[wp:Module:Delink]]
  +
-- Arguments should be supplied from other modules where "s" is the link without brackets
  +
  +
-- Returns the link text to be displayed from this link after applying the pipe trick
  +
local function linktext( s )
  +
if type(s) ~= "string" then
  +
return nil
 
end
  +
  +
-- We need to deal with colons, brackets, and commas, per [[wp:Help:Pipe trick]].
  +
  +
-- First, remove the text before the first colon, if any.
  +
if mw.ustring.match(s, ":") then
  +
s = mw.ustring.match(s, ".-:(.*)")
  +
end
  +
  +
-- Next up, brackets and commas.
  +
if mw.ustring.match(s, "%(.-%)$") then -- Brackets trump commas.
  +
s = mw.ustring.match(s, "(.-) ?%(.-%)$")
  +
elseif s:match(",") then -- If there are no brackets, display only the text before the first comma.
  +
s = mw.ustring.match(s, "(.-),.*$")
  +
end
  +
  +
return s
 
end
 
end
   
Line 17: Line 48:
   
 
-- See {{L}}
 
-- See {{L}}
p.link = function(f)
+
p.link = function( f )
  +
-- Set keepEmptyArgs to true to allow pipe trick
local args = f or {}
 
  +
local args = processArgs.normDefault( f, true )
if f == mw.getCurrentFrame() then
 
  +
args = f.args or f:getParent().args
 
end
 
 
 
-- Get language suffix for links
 
-- Get language suffix for links
 
if not args[1] or args[1]:lower() == 't' then
 
if not args[1] or args[1]:lower() == 't' then
 
return pageSuffix()
 
return pageSuffix()
 
end
 
end
  +
 
 
local page = args[1] .. pageSuffix()
 
local page = args[1] .. pageSuffix()
  +
 
-- Get page name with suffix
 
-- Get page name with suffix
 
if args.dest then
 
if args.dest then
 
return page
 
return page
 
end
 
end
  +
-- Get page link with suffix and specified text
 
return '[[' .. page .. '|' .. ( args.o or args[2] or args[1] ) .. ']]'
+
local usePipeTrick = args.o == '' or args[2] == ''
  +
local display
  +
-- The pipe trick should only be used with English; but we have to support /en pages
  +
if usePipeTrick then
  +
-- Run pipe trick on page title without langauage code; otherwise it breaks
  +
display = linktext(args[1])
  +
else
  +
display = args.o or args[2] or args[1]
  +
end
  +
 
-- Get page link with suffix and specified display text
  +
return '[[' .. page .. '|' .. display .. ']]'
 
end
 
end
   
 
-- See {{Language}}
 
-- See {{Language}}
p.name = function(f)
+
p.name = function( f )
local args = f or {}
+
local args = processArgs.normDefault( f )
if f == mw.getCurrentFrame() then
 
args = f.args or f:getParent().args
 
end
 
 
 
local langCode = code(args[1] and mw.title.new(args[1])) or 'en'
+
local langCode = args[1] or 'en'
 
 
 
-- Get language code for specified title or current page
 
-- Get language code for specified title or current page
Line 52: Line 90:
 
 
 
local langName = langNames[langCode]
 
local langName = langNames[langCode]
  +
  +
-- If langCode is not valid, just return itself
  +
if langName == nil then
  +
return langCode
  +
end
  +
 
-- Get language name in English if the 2nd arg or 'en' arg is set
 
-- Get language name in English if the 2nd arg or 'en' arg is set
 
if (args[2] and args[2] ~= '') or (args.en and args.en ~= '') then
 
if (args[2] and args[2] ~= '') or (args.en and args.en ~= '') then
 
return langName[1]
 
return langName[1]
 
end
 
end
  +
 
-- Get language name in that language (autonym)
 
-- Get language name in that language (autonym)
 
return langName[2]
 
return langName[2]
Line 61: Line 106:
   
 
-- See {{ML}}
 
-- See {{ML}}
p.modlink = function(f)
+
p.modlink = function( f )
local args = f or {}
+
local args = processArgs.normDefault( f )
  +
if f == mw.getCurrentFrame() then
 
 
local modname = require([[Module:Mods]]).getData(args[2]).name
args = f.args or f:getParent().args
 
end
+
 
local modname = require([[Module:Mods]]).getData(args[2]).localized
 
 
 
if not modname then
 
if not modname then
 
return 'No mod found'
 
return 'No mod found'
 
end
 
end
  +
 
 
local disambiguated = string.format("%s (%s)", args[1], modname)
 
local disambiguated = string.format("%s (%s)", args[1], modname)
  +
 
return p.link{disambiguated, args[1]}
+
return p.link{disambiguated, args.title or args[1]}
 
end
 
end
   
p.code = function(f)
+
p.code = function( f )
local args = f or {}
+
local args = processArgs.normDefault( f )
  +
if f == mw.getCurrentFrame() then
 
args = f.args or f:getParent().args
 
end
 
 
 
-- 'de' and 'Main Page/de' will be correctly processed regardless of the arg name
 
-- 'de' and 'Main Page/de' will be correctly processed regardless of the arg name
 
-- These aliases are mainly to keep a standard
 
-- These aliases are mainly to keep a standard
 
local selected = args.code or args.title
 
local selected = args.code or args.title
  +
 
local code = code(selected and mw.title.new(selected) or mw.title.getCurrentTitle())
+
local code = code( selected and mw.title.new(selected) or mw.title.getCurrentTitle() )
  +
 
 
if not code then
 
if not code then
 
return args.default or ''
 
return args.default or ''
 
end
 
end
  +
 
 
return code
 
return code
 
end
 
end

Latest revision as of 23:56, 23 July 2021

Documentation for this module may be created at Module:Language/doc

local langNames = require( [[Module:Language/Names]] )
local processArgs = require( [[Module:ProcessArgs]] )

local function code( title )
	local titleObj = title or mw.title.getCurrentTitle()
	if not titleObj.isSubpage then
		return nil
	end
	local subPage = titleObj.subpageText:lower()
	if langNames[subPage] then
		return subPage
	end
end
local function pageSuffix()
	local langCode = code()
	if langCode then
		return '/' .. langCode
	end
	return ''
end
-- Copied from [[wp:Module:Delink]]
-- Arguments should be supplied from other modules where "s" is the link without brackets

-- Returns the link text to be displayed from this link after applying the pipe trick
local function linktext( s )
	if type(s) ~= "string" then
		return nil
	end
	
    -- We need to deal with colons, brackets, and commas, per [[wp:Help:Pipe trick]].
    
    -- First, remove the text before the first colon, if any.
    if mw.ustring.match(s, ":") then
        s = mw.ustring.match(s, ".-:(.*)")
    end
    
    -- Next up, brackets and commas.
    if mw.ustring.match(s, "%(.-%)$") then -- Brackets trump commas.
        s = mw.ustring.match(s, "(.-) ?%(.-%)$")
    elseif s:match(",") then -- If there are no brackets, display only the text before the first comma.
        s = mw.ustring.match(s, "(.-),.*$")
    end

	return s
end

local p = {}

-- See {{L}}
p.link = function( f )
	-- Set keepEmptyArgs to true to allow pipe trick
	local args = processArgs.normDefault( f, true )
	
	-- Get language suffix for links
	if not args[1] or args[1]:lower() == 't' then
		return pageSuffix()
	end
	
	local page = args[1] .. pageSuffix()
	
	-- Get page name with suffix
	if args.dest then
		return page
	end
	
	local usePipeTrick = args.o == '' or args[2] == ''
	local display
	-- The pipe trick should only be used with English; but we have to support /en pages
	if usePipeTrick then
		-- Run pipe trick on page title without langauage code; otherwise it breaks
		display = linktext(args[1])
	else
		display = args.o or args[2] or args[1]
	end
	
	-- Get page link with suffix and specified display text
	return '[[' .. page .. '|' .. display .. ']]'
end

-- See {{Language}}
p.name = function( f )
	local args = processArgs.normDefault( f )
	
	local langCode = args[1] or 'en'
	
	-- Get language code for specified title or current page
	if args.code then
		return langCode
	end
	
	local langName = langNames[langCode]

    -- If langCode is not valid, just return itself
    if langName == nil then
        return langCode
    end
	
	-- Get language name in English if the 2nd arg or 'en' arg is set
	if (args[2] and args[2] ~= '') or (args.en and args.en ~= '') then
		return langName[1]
	end
	
	-- Get language name in that language (autonym)
	return langName[2]
end

-- See {{ML}}
p.modlink = function( f )
	local args = processArgs.normDefault( f )
	
	local modname = require([[Module:Mods]]).getData(args[2]).name
	
	if not modname then
		return 'No mod found'
	end
	
	local disambiguated = string.format("%s (%s)", args[1], modname)
	
	return p.link{disambiguated, args.title or args[1]}
end

p.code = function( f )
	local args = processArgs.normDefault( f )
	
	-- 'de' and 'Main Page/de' will be correctly processed regardless of the arg name
	-- These aliases are mainly to keep a standard
	local selected = args.code or args.title
	
	local code = code( selected and mw.title.new(selected) or mw.title.getCurrentTitle() )
	
	if not code then
		return args.default or ''
	end
	
	return code
end

return p