Module:CharInserts

local h = {}

local p = {} function p.main(frame) local title = mw.title.getCurrentTitle 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 title:find(group.pattern) then return nil end local div = h.makeDiv(group.label) h.printContent(div, group.insert) return div end

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

function h.printContent(div, insert) div:wikitext(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