Module:Fix

-- This module implements.

local mTableTools = require('Module:TableTools') local cfg = mw.loadData('Module:Fix/config')

local p = {}

local function substituteParams(msg, ...) local params = {...} --	if not params[1] then return msg --	else --		return mw.message.newRawMessage(msg):params(params):plain --	end end

local function categoryExists(cat) local success, title = pcall(mw.title.makeTitle, 14, cat) return success and title and title.exists or false end

local function makeCategoryLink(cat) return string.format('%s:%s', mw.site.namespaces[14].name, cat) end

local function makeMainCategories(args) -- Get category argument numbers so that we can iterate over them. args['cat1'] = args['cat1'] or args['cat'] or cfg['default-category'] args['cat-date1'] = args['cat-date1'] or args['cat-date'] local catNums = mTableTools.affixNums(args, 'cat') local catDateNums = mTableTools.affixNums(args, 'cat-date') -- Build the list of categories. local hasInvalidCategory = false local ret = {} for _, num in ipairs(catNums) do ret[#ret + 1] = makeCategoryLink(args['cat' .. tostring(num)]) end local date = args.date for _, num in ipairs(catDateNums) do local catName = args['cat-date' .. tostring(num)] if date then catName = substituteParams(cfg['dated-category'], catName, date) end ret[#ret + 1] = makeCategoryLink(catName) if not categoryExists(catName) then hasInvalidCategory = true end end if hasInvalidCategory then ret[#ret + 1] = makeCategoryLink(cfg['invalid-date-category']) end return table.concat(ret) end

local function renderCategories(args, title) title = title or mw.title.getCurrentTitle local ret if title.namespace == 0 or title.namespace == 10 then -- Only load category handler if we are in article space or template space. local categoryHandler = require('Module:Category handler')._main

-- Main categories local mainCategories = makeMainCategories(args)

-- Template categories local templateCategories if args.name and args.name ~= title.rootText and not args.date then templateCategories = makeCategoryLink(cfg['undated-template-category']) end

-- Pass the arguments through to category handler. ret = categoryHandler{ main = mainCategories, template = templateCategories, subpage = 'no', page = title.prefixedText }	end return ret or '' end

local function makeText(args) if not args.text then return args.special or '' end -- Create the raw wikilink. local unformattedLink do local target = args.link or cfg['default-link'] local title = args.title or target if args.date then title = substituteParams(cfg['dated-title'], title, args.date) end unformattedLink = string.format(			' %s ',			target, title, args.text		) end -- Add formatting, pre-text and post-text. local formattedLink if args['pre-text'] and args['post-text'] then formattedLink = cfg['fix-text-pre-post'] elseif args['pre-text'] then formattedLink = cfg['fix-text-pre-nopost'] elseif args['post-text'] then formattedLink = cfg['fix-text-nopre-post'] else formattedLink = cfg['fix-text-nopre-nopost'] end formattedLink = substituteParams(		formattedLink,		unformattedLink,		args['pre-text'] or ,		args['post-text'] or 	) -- Add HTML tag and classes. local sup = mw.html.create('sup') local thisclass = args.class or '' sup :addClass('noprint Inline-Template' .. thisclass ) --		:addClass(args.class) :css('white-space', 'no-wrap') :wikitext(formattedLink) return tostring(sup) end

function p._main(args, title) return renderCategories(args, title) .. makeText(args) end

function p.main(frame) local args = require('Module:Arguments').getArgs(frame) return p._main(args) end

return p