Bước tới nội dung

Mô đun:preparser

Từ điển mở Wiktionary
local export = {}

local function process_args(args, div)
	local sorted_args = {}
	for k, v in pairs(args) do
		table.insert(sorted_args, k .. div .. v)
	end
	table.sort(sorted_args)
	return table.concat(sorted_args, div)
end

function export.memo_key(frame, func_name)
	local parent_frame = frame:getParent()
	return parent_frame:getTitle() .. "\250" .. process_args(parent_frame.args, "\251") .. "\252" .. frame:getTitle() .. "\253" .. func_name .. "\254" .. process_args(frame.args, "\255")
end

function export.preparse(frame, func_name)
	if not frame.preparse then
		local unstripNoWiki = mw.text.unstripNoWiki
		local i = tonumber(frame:extensionTag("nowiki", ""):match"([%dA-F]+)", 16)
		while i > 0 do
			i = i - 1
			local check = unstripNoWiki(("\127'\"`UNIQ--nowiki-%08X-QINU`\"'\127"):format(i))
			if i == 0 or check == "__PREPARSE END" then
				local parent_frame = frame:getParent()
				local preloaded = mw.loadData("Module:preparser/core")[export.memo_key(frame, func_name)]
				if preloaded then
					return preloaded
				end
				if check == "__PREPARSE END" then
					frame:extensionTag("nowiki", "__PREPARSE END")
				end
			elseif check == "__PREPARSE START" then
				frame:extensionTag("nowiki", "__PREPARSE START")
				break
			end
		end
	end
end

return export