Jump to content

Module:munge text

Wiktionary වෙතින්

Applies a function fn to text, but not any HTML tags or wikilink targets within.


return function(text, fn)
	local has_html = text:find("<")
	local has_two_part_link = text:find("%[%[.*|")
	if not has_html and not has_two_part_link then
		return fn(text)
	end

	local function do_munge(text, pattern, functor)
		local index = 1
		local length = mw.ustring.len(text)
		local result = ""
		pattern = "(.-)(" .. pattern .. ")"
		while index <= length do
			local first, last, before, match = mw.ustring.find(text, pattern, index)
			if not first then
				result = result .. functor(mw.ustring.sub(text, index))
				break
			end
			result = result .. functor(before) .. match
			index = last + 1
		end
		return result
	end
	
	local function munge_text_with_html(txt)
		return do_munge(txt, "<[^>]->", fn)
	end

	if has_two_part_link then -- contains wikitext links
		return do_munge(text, "%[%[[^%[%]|]-|", has_html and munge_text_with_html or fn)
	else -- HTML tags only
		return munge_text_with_html(text)
	end
end
"https://si.wiktionary.org/w/index.php?title=Module:munge_text&oldid=29035" වෙතින් සම්ප්‍රවේශනය කෙරිණි