Module:CharInserts

local util_table = require('Module:TableUtil') local util_vars = require("Module:VarsUtil") local i18n = require('Module:i18nUtil')

local h = {}

local p = {} function p.main(frame) local title = mw.title.getCurrentTitle i18n.init('CharInserts') return h.makeOutput(h.getData(title.nsText), title.text) end

function h.getData(nsText) local moduleTitle = mw.title.new(h.getModuleName(nsText), 'Module') if not moduleTitle.exists then return nil end return mw.loadData(moduleTitle.prefixedText) end

function h.getModuleName(nsText) if nsText == '' then nsText = 'Main' end return ('CharInserts/%s'):format(nsText) end

function h.makeOutput(data, title) if not data then return end local ret = {} for _, group in ipairs(data) do		ret[#ret+1] = h.getGroupOutput(group, title) end return unpack(ret) end

function h.getGroupOutput(group, title) if not h.containsOneOf(title, group.pattern) then return nil end if h.containsOneOf(title, group.notpattern or '@@@@@@@@@@') then return nil end local div = h.makeDiv(group.label) h.printContent(div, group) return div end

function h.containsOneOf(title, patterns) patterns = util_table.guaranteeTable(patterns) for _, v in ipairs(patterns) do		if title:find(v) then return true end end end

function h.makeDiv(label) return mw.html.create('div'):attr('data-ci-label', label) end

function h.printContent(div, group) div:wikitext(h.loadingText(group.label), h.makeContent(group.insert)) end

function h.loadingText(label) local output = mw.html.create('span') :addClass('ci-loading-text') :wikitext(i18n.print('loading', label or '')) return tostring(output) end

function h.makeContent(insert) return mw.getCurrentFrame:extensionTag{ name = 'charinsert', content = h.escape(insert), args = {} } end

function h.escape(insert) local replace = { ['\n'] = '&#10;', ['( +)'] = ' %1 ',		['\\%['] = '[',		['\\%]'] = ']',	}	for k, v in pairs(replace) do		insert = insert:gsub(k, v)	end return insert end

return p