Module:FacetCategories

From Bodhicitta

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

-- Facet categories for library items, so the site search can filter on them.
--
-- CirrusSearch cannot read SMW properties, but it can filter by category
-- (incategory:). Class, people and publishers already arrive as categories;
-- this adds the two facets that did not:
--
--   Topic: <TermTopics value>     one per topic, e.g. "Topic: Compassion"
--   Decade: <1990s>               from the first four-digit year in Pubdate
--
-- The categories are hidden from the page footer in CSS (tws.main.scss,
-- .catlinks), so they need no category pages.
--
--   {{#invoke:FacetCategories|main|topics={{{ TermTopics | }}}|date={{GetItemData|prop=Pubdate}}}}

local p = {}

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

	for topic in mw.text.gsplit(args.topics or '', '%s*;%s*') do
		topic = mw.text.trim(topic)
		if topic ~= '' and not seen[topic] then
			seen[topic] = true
			-- A category name cannot carry these characters.
			local name = topic:gsub('[%[%]{}|#<>]', '')
			if name ~= '' then
				table.insert(out, '[[Category:Topic: ' .. name .. ']]')
			end
		end
	end

	local year = tonumber((args.date or ''):match('(%d%d%d%d)'))
	if year and year >= 1000 and year <= 2100 then
		table.insert(out, '[[Category:Decade: ' .. (year - year % 10) .. 's]]')
	end

	return table.concat(out)
end

return p