Module:Glossary

From MEpedia, a crowd-sourced encyclopedia of ME and CFS science and history

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

--[[

This module provides helper functions for Template:Glossary_entry.
Created by Pyrrhus for me-pedia.org November 2019

]]


local gloss = {}


--[[
tooltipCleanse: (adapted from Module:String.replace)

This function allows one to cleanse pre-processed, pre-HTML wikitext for
use as a tooltip text.  It removes:
   1) <ref> strip markers of the form '"`UNIQ--ref-00000001-QINU`"'
   2) all other strip markers, except for <nowiki> strip markers
   3) tracking categories of the form 'Category:tracking_category', with brackets
   4) superscripts, such as citation needed, of the form <sup> ... </sup>

It also protects double quotation marks so that the tooltip can be
specified in a <span title="mytooltip"></span> construct.

What is "pre-processed, pre-HTML wikitext", you ask?  I'm not sure.
It might be "parsed, but not HTML rendered".  Again, I'm really not sure.

Usage:
{{#invoke:Glossary|tooltipCleanse|source=my_preprocessed_preHTML_wikitext}}

]]
function gloss.tooltipCleanse( frame )
	local my_args = gloss._getParameters( frame.args, {'source'} )
	local source_str = my_args['source'] or ''
	local trackPattern = '%[%[[Cc]ategory:[^%]]*%]%]'

	local supPattern = "<sup.+</sup>"
	if source_str == '' then
		return source_str
	end

	-- remove all strip markers, including <ref>
	-- (except <nowiki> strip markers)
	source_str = mw.text.unstrip( source_str )

	-- remove superscripts such as citation needed
	source_str = mw.ustring.gsub( source_str, supPattern, '' )

	-- remove tracking categories 
	source_str = mw.ustring.gsub( source_str, trackPattern, '' )

	-- protect double quotes 
	source_str = mw.ustring.gsub( source_str, "\"", '&#34;' )

	return source_str
end


--[[
hostname: This function returns the hostname for a URL
]]
function gloss.hostname( frame )
	local new_args = gloss._getParameters( frame.args, {'url'} )
	local myUrl = new_args['url'] or ''
	local myUrlObject = mw.uri.new( myUrl )
	return myUrlObject["host"]
end


--[[
_getParameters: (copied from Module:String)
Helper function that populates the argument list given that user may need to use a mix of
named and unnamed parameters.  This is relevant because named parameters are not
identical to unnamed parameters due to string trimming, and when dealing with strings
we sometimes want to either preserve or remove that whitespace depending on the application.
]]
function gloss._getParameters( frame_args, arg_list )
	local new_args = {}
	local index = 1
	local value

	for _, arg in ipairs( arg_list ) do
		value = frame_args[arg]
		if value == nil then
			value = frame_args[index]
			index = index + 1
		end
		new_args[arg] = value
	end

	return new_args
end


return gloss