Module:FormUtil

local util_args = require("Module:ArgsUtil") local util_table = require('Module:TableUtil') local util_text = require("Module:TextUtil") local util_vars = require('Module:VarsUtil') local i18n = require('Module:i18nUtil')

local p = {} local h = {} function h.escape(str) local lookup = { [' '] = '%%20',		['"'] = '%%22',		['>'] = '%%3E',		['<'] = '%%3C',		['+'] = '%%2B',	}	for k, v in pairs(lookup) do		str = str:gsub(k, v)	end	return str end

function p.fullURL(formInfo, args) local tbl = {} for k, v in pairs(args) do		if v and v ~= 'No' then tbl[#tbl+1] = ('%s%%5B%s%%5D=%s'):format(formInfo.template, k, tostring(v)) end end local form_args = h.escape(table.concat(tbl,'&')) return ('%s/Special:RunQuery/%s?%s&pfRunQueryFormName=%s'):format(		mw.site.server,		formInfo.form,		form_args,		formInfo.form	) end

function p.partialURL(formInfo, args) local tbl = {} for k, v in pairs(args) do		if v and v ~= 'No' then tbl[#tbl+1] = ('%s%%5B%s%%5D=%s'):format(formInfo.template, k, tostring(v)) end end local form_args = h.escape(table.concat(tbl,'&')) return ('%s/Special:RunQuery/%s?%s'):format(		mw.site.server,		formInfo.form,		form_args	) end

function p.addToURL(formInfo, args) local tbl = {} for k, v in pairs(args) do		if v and v ~= 'No' then tbl[#tbl+1] = ('%s%%5B%s%%5D=%s'):format(formInfo.template, k, tostring(v)) end end local newQuery = h.escape(table.concat(tbl, '&')) return ('%s&%s&pfRunQueryFormName=%s'):format(		formInfo.partial,		newQuery,		formInfo.form	) end

function p.templateArgs(formInfo, args) local formInfo = util_table.guaranteeTable(formInfo) local tbl = {} for k, v in pairs(args) do tbl[#tbl+1] = '|' .. k .. '=' .. v	end return mw.text.nowiki(():format(formInfo.template, util_table.concat(tbl, ))) end

function p.permalink(output, args, formInfo) output:wikitext(i18n.print('Permalink'), p.fullURL(formInfo, args)) output:wikitext(' ') output:wikitext(i18n.print('TemplateArgs')) output:tag('div') :css('display', 'inline-block') :wikitext(mw.text.tag{		name = 'code',		content = p.templateArgs(formInfo, args)	}) end

function p.printLog(output) local log = util_vars.getVar('log') if not log then return end output:tag('br') output:wikitext(log) end

function p.makeIntroSentence(formInfo, args, formArgs) if not util_args.castAsBool(args.showformlink) then return '' end return util_text.extLink(		p.fullURL(formInfo, formArgs),		i18n.print('introSentence')	) end

function p.makeTableCellFilterLink(formInfo, formArgs, class) local output = mw.html.create('span') :addClass('table-cell-filter-link') :addClass(class) :wikitext(util_text.extLink(p.fullURL(formInfo, formArgs), ' ')) return tostring(output) end

return p