Module:Text highlight

Module documentation

The Module:Text highlight can help in implementing the {{Citation needed}} and {{Unclear}}.

History

edit

This module was imported from w:it:Modulo:Chiarimento version 2019-03-24 h. 13:01 - see its full w:it:Special:History/Modulo:Chiarimento.

---
-- Module implementing functionalities of the template {{Citation needed}}.
-- It prevents HTML errors in MediaWiki.
-- 
-- imported from [[w:it:Modulo:Chiarimento]] version [[w:it:Special:PermaLink/103556351]]
-- see its full history in [[w:it:Special:History/Modulo:Chiarimento]]

local p = {}

function p.main(frame)
	local ret = {}
	local args = frame.args

	-- TODO eventually merge parent arguments - actually unuseful
	--local argsParent = frame:getParent().args

	local styles = 'Module:Text highlight/style.css'

	-- find text sequences followed by an empty row
	local lines = mw.text.split(mw.text.trim(args[1] or ''), '\n%s*\n')
	for k, v in ipairs(lines) do
		-- find sub-sequences newline-separated
		local sublines = mw.text.split(v, '\n')
		for i, line in ipairs(sublines) do
			local prefix = ''
			local firstline = k == 1 and i == 1
			-- search and keep '*, :, #' in the first part of the sequence
			-- look at [[phab:T14974]]
			if not firstline or mw.ustring.match(sublines[2] or '', '^[%*:%#]') then
				prefix = mw.ustring.match(line, '^[%*:%#]+') or ''
				if firstline and prefix ~= '' then
					prefix = '\n' .. prefix
				end
				line = mw.ustring.gsub(line, '^[%*:%#]+', '')
			end
			local span = mw.html.create('span')
			span
				:addClass('module-text-highlight')
				:attr('title', args[3])
				:wikitext(line)
			sublines[i] = prefix .. tostring(span)
		end
		table.insert(ret, table.concat(sublines, '\n'))
	end

	-- generate the text in the apix in square brakets
	args[2] = '&#91;<i>' .. (args[2] or '') .. '</i>&#93;'
	local sup = mw.html.create('sup')
	sup
		:addClass('noprint module-text-highlight-apix')
		:attr('title', args[3])
		:wikitext(args[2])

	return frame:extensionTag{
			name = 'templatestyles',
			args = {src = styles}
		} .. table.concat(ret, '\n\n') .. tostring(sup)
end

return p