Module:UtilsMarkup/Link

local p = {} local h = {}

local utilsMarkup = require("Module:UtilsMarkup/Format") local utilsPage = require("Module:UtilsPage") local utilsString = require("Module:UtilsString") local utilsTable = require("Module:UtilsTable")

-- If backlink == false then this print an external link that looks like an internal one -- (useful for creating links that do not appear in Special:WhatLinksHere) function p.link(link, text, noBacklink) if not link then return "" end link = mw.text.trim(link) if link:find('http') then return h.externalLink(link, text) end if noBacklink then local url = utilsPage.fullUrl(link) local externalLink = h.externalLink(url, text or link) local formattedLink = utilsMarkup.class("plainlinks", externalLink) return formattedLink end local title = mw.title.new(link) or link if title.nsText == "File" or title.nsText == "Category" then link = ":" .. link if text == "" then text = title.text end end return h.internalLink(link, text) end

function p.sectionLink(page, section, display) local target = page if section then target = target .. "#" .. section end return p.link(target, display) end

function h.internalLink(page, display) if not page then return "" end if not display then return ("%s"):format(page) end return ('%s'):format(page, display) end

function h.externalLink(page, display) if not page then return "" end if not display then return page end return ('[%s %s]'):format(page, display) end

function p.file(name, options) options = options or {} local size = options.size local link = options.link local caption = options.caption local noBacklink = options.noBacklink if not utilsString.startsWith(name, "File:") then name = "File:" .. name end if link and noBacklink then link = tostring(mw.uri.fullUrl(link)) end local linkParts = utilsTable.compact({		name,		size,		link and "link="..link,		caption,	}) local fileLink = "") .. "" return fileLink end

function p.category(name, sortKey) if not utilsString.startsWith(name, "Category:") then name = "Category:" .. name end return h.internalLink(name, sortKey) end

function p.categories(categoryNames) local categoryLinks = utilsTable.map(categoryNames, p.category) return table.concat(categoryLinks) end

function p.containsLink(wikitext, external) return wikitext ~= nil and not not wikitext:find("%[%[.*%]%]") end

function p.killBacklinks(wikitext) local link = string.match(wikitext, "%[%^%*%]%]") if not link then return wikitext end local startPos, endPos = string.find(wikitext, link, 1, true) local before = string.sub(wikitext, 1, startPos -1) local after = string.sub(wikitext, endPos + 1) local externalLink = p.killBacklink(link) return before .. externalLink .. p.killBacklinks(after) end

function p.killBacklink(link) local linkParts = utilsString.trim(link, "][") local pipe = string.find(linkParts, "|") local page = string.sub(linkParts, 1, pipe and pipe - 1) local display = pipe and string.sub(linkParts, pipe + 1) local startsWithColon = utilsString.startsWith(page, ":") page = string.gsub(page, "^:", "") local title = mw.title.new(page) if not title or title.interwiki ~= "" or not startsWithColon and (title.nsText == "Category" or title.nsText == "File") then return link end return p.link(page, display or page, true) end

local categoryPattern = "%[%[Category:[^%]]*%]%]" function p.stripCategories(wikitext) local categories = {} for category in string.gmatch(wikitext, categoryPattern) do		local categoryWithoutSortKey = string.gsub(category, "|[^%]]*%]%]", "]]") local categoryLink = string.gsub(categoryWithoutSortKey, "Category", ":Category") if not utilsTable.keyOf(categories, categoryLink) then table.insert(categories, categoryLink) end end return string.gsub(wikitext, categoryPattern, ""), categories end

return p