Module:Truncate

From Fallen London Wiki

Documentation for this module may be created at Module:Truncate/doc

local p = {}

function p.check(frame)
	patterns_rem = {						-- Patterns to remove
		[1] = "'''?",										-- Wikitext bold/&italics
		[2] = " ?[%[%(][%.…]+[%]%)] ?",						-- "Snip markers" like […]
		[3] = "%<span.-%>%[%[File.-as: Text Uses%]%]",		-- "Af" and "Ai" templates
		[4] = "%[%[File:Hint.-layers%' City Text Uses%]%]",	-- "Tlc" template
		[5] = '<span name="TLC variant">.-</span>',			-- Alt "tlc" while changing
		[6] = '</?p>'										-- HTML <p> tag
	}
	text = frame.args.text
	warning = frame.args.warning or "yes"
	count = frame.args.count or "yes"
	chars = tonumber(frame.args.chars) or 250
	preview = "no"
	if (frame.args.preview == "") then preview = "yes" end
	ttext = text
	
	for i=1,#patterns_rem do
		ttext = mw.ustring.gsub(ttext, patterns_rem[i], "")
	end
	dlbs = select(2, mw.ustring.gsub(ttext, "\n\n", "")) -- Double Line BreakS
	
	tlen = tonumber(frame:callParserFunction("#len", {ttext})) - dlbs
	-- Parser functions are used instead of string ones to replicate previous behaviour exactly
	
	if (tlen > chars) then
		text = frame:callParserFunction("#sub", {text, 0, chars})
		text = text .. "[…]"
		if (count == "yes" and preview == "yes") then 
			text = text .. "<br />''char count:" .. tlen .. "''"
		end
		if (warning == "yes") then 
			text = text .. frame:expandTemplate{title="Immediate review"} 
		end
	else 
		if (count == "yes" and preview == "yes") then
			text = text .. "<br />''char count:" .. tlen .. "''"
		end
	end
	
	return text
end

return p