Module:SourceUtil

local util_args = require('Module:ArgsUtil') local util_map = require('Module:MapUtil') local util_refs = require("Module:RefsUtil") local util_table = require("Module:TableUtil") local util_text = require("Module:TextUtil") local util_title = require("Module:TitleUtil") local util_toggle = require('Module:ToggleUtil') local util_vars = require("Module:VarsUtil")

local ARGS = { 'Link', 'Title', 'Desc', 'Language', 'Name', 'GCDBefore', 'GCDAfter' }

local h = {} local p = {}

function p.makeAndConcatRefs(str) return util_table.concat({p.makeRef(str)}) end

function p.makeRef(str) local tbl = h.strToTable(str) return unpack(util_map.inPlace(tbl, h.makeOneRef)) end

function p.makePopupRef(str) local reftable = h.strToTable(str) if not next(reftable) then return nil end local output = mw.html.create h.printPopupRef(output, reftable) return tostring(output) end

function p.printPopupRef(tbl, str) local reftable = h.strToTable(str) if not next(reftable) then return nil end h.printPopupRef(tbl, reftable) end

function p.isGCD(str) local reftable = h.strToTable(str) if not next(reftable) then return false end return reftable[1].GCDBefore or reftable[1].GCDAfter end

function h.strToTable(str) return util_args.splitArgsArray(str, ARGS) end

function h.makeOneRef(tbl) -- util_refs.ref returns nil but we want an empty string if there's nothing if not next(tbl) then return '' end return util_refs.ref(h.makeDisplay(tbl), tbl.Name) end

function h.makeDisplay(tbl) if tbl.GCDBefore or tbl.GCDAfter then return h.makeGcdDisplay(tbl) end if not tbl.Desc then return util_text.extLink(tbl.Link, mw.text.nowiki(h.titleDisplay(tbl))) end return ("%s %s"):format(		util_text.extLink(tbl.Link, mw.text.nowiki(h.titleDisplay(tbl))) or ,		tbl.Desc or 	) end

function h.titleDisplay(tbl) if tbl.Language and tbl.Title then return ('%s (%s)'):format(tbl.Title, tbl.Language) end return tbl.Title or 'Link' end

function h.makeGcdDisplay(ref) return ('GCD Archive Pages: before change - %s; after change - %s'):format(		util_text.intLinkOrText(ref.GCDBefore, h.gcdLongLink(ref.GCDBefore)),		util_text.intLinkOrText(ref.GCDAfter, h.gcdLongLink(ref.GCDAfter))	) end

function h.gcdLongLink(str) return ('%s - %s'):format(		util_title.titleparts(str, 1, 2),		util_title.titleparts(str, 1, 3)	) end

- -- popup -

function h.printPopupRef(tbl, reftable) local popup = util_toggle.popupButton(tbl) popup.button:addClass('popup-ref-button') popup.wrapper:addClass('popup-ref-wrapper') popup.inner:addClass('popup-ref-inner') h.printPopupRefs(popup.inner, reftable) end

function h.printPopupRefs(div, reftable) local ul = div:tag('ul') for _, ref in ipairs(reftable) do		h.printOnePopupRef(ul, ref) end end

function h.printOnePopupRef(ul, ref) ul:tag('li') :wikitext(h.makeRefText(ref)) end

function h.makeRefText(ref) if ref.GCDAfter or ref.GCDBefore then return h.makePopupGcdRefText(ref) end return util_text.extLink(ref.Link, mw.text.nowiki(h.titleDisplay(ref))) end

function h.makePopupGcdRefText(ref) local tbl = { util_text.intLinkOrText(ref.GCDBefore, util_title.titleparts(ref.GCDBefore, 1, 3)), util_text.intLinkOrText(ref.GCDAfter, util_title.titleparts(ref.GCDAfter, 1, 3)), }	return 'GCD Archive: ' .. util_table.concat(tbl, '; ') end

return p