Module:Title formatting

local p = {}

function p.formatTitle(frame) local args = frame.args local title if args.title then title = mw.title.new(args.title) else title = mw.title.getCurrentTitle end

-- Divide the title into three parts : -- 1. prefix = namespace and possibly parent page (if it's a sub-page) -- 2. mainPart = part to format (page or sub-page, without part in brackets) -- 3. disambig = part in brackets local prefix = '' if title.nsText ~= '' then prefix = title.nsText .. ':'   end if title.isSubpage then prefix = prefix .. title.baseText .. '/'   end

local mainPart = title.subpageText

local disambig = '' local disambigStart = mainPart:find(' %(.+%)$') if disambigStart then disambig = mainPart:sub(disambigStart) mainPart = mainPart:sub(1, disambigStart - 1) end

-- Bonding of the three parts by inserting a language tag and italics if necessary. local lang = args.lang if lang and lang ~= '' then mainPart = ' ' .. mainPart .. ' '   end local italic = args.italic if italic and italic ~= '' then mainPart =  .. mainPart ..    end return prefix .. mainPart .. disambig end

--[[ Changes the format of the main part of a page title. Possible changes are the italicization and the addition of a language indication.

Parameters (passed via frame) lang: if not empty, insert a tag indicating that the main part of the title is in this language. italic: if not empty, italicizes the main part of the title. options: second parameter passed to DISPLAYTITLE. Possible values are "noreplace" and "noerror". --]] function p.setDisplayTitle(frame) local args = frame.args local displayTitle = p.formatTitle(frame) if displayTitle == mw.title.getCurrentTitle.prefixedText then return '' end return mw.getCurrentFrame:callParserFunction('DISPLAYTITLE', displayTitle, frame.args.options) end

return p