Module:he-links

From Wiktionary, the free dictionary
Jump to navigation Jump to search

This module needs documentation.
Please document this module by describing its purpose and usage on the documentation page.

local m_links_templates = require("Module:links/templates")
local com = require("Module:he-common")

local export = {}

function export.term(frame)
	local args = {}

	for key, val in pairs(frame:getParent().args) do
		args[key] = val
	end

	if args["wv"] == "" then args["wv"] = nil end
	if args["dwv"] == "" then args["dwv"] = nil end

	if args[2] == nil or args[2] == "" then
		local form, formwv, formdwv = com.process_wv_triad(args[1] or "", args["wv"], args["dwv"])
		args[1] = form or ""
		if formdwv then
			args[2] = (formwv or form) .. " / " .. formdwv
		else
			args[2] = (formwv or form)
		end
	end

	args["wv"] = nil
	args["dwv"] = nil

	-- table.insert(args, 1, "he")
	-- shift manually to avoid strange bugs:
	args[4] = args[3]
	args[3] = args[2]
	args[2] = args[1]
	args[1] = "he"
	
	if args[3] and args[2]:find("[[", 1, true) then
		-- args[2] (term) contains wikilinks, so args[3] (alt) will be ignored
		-- by [[Module:links]] and will cause a tracking template to be
		-- transcluded.
		args[3] = nil
		-- [[Special:WhatLinksHere/Wiktionary:Tracking/he-links/alt removed]]
		require("Module:debug").track("he-links/alt removed")
	end

	-- Passthrough arguments after modification
	local frame2 = frame:newChild{title = frame:getParent():getTitle(), args = args}:newChild{title = frame:getTitle(), args = frame.args}

	return m_links_templates.l_term_t(frame2)
end

function export.strongs_list(frame)
	local text = frame.args[1]
	local translit_module = frame.args.module
	local translit_function = frame.args.func
	local transliterate
	if translit_module and translit_function then
		transliterate = require("Module:" .. translit_module)[translit_function]
	elseif (translit_module ~= nil) ~= (translit_function ~= nil) then
		error("Supply both |module= and |func= or neither")
	end
	local entry_name = require "Module:languages/data/2".he.entry_name
	local character_set_to_remove = "[" .. entry_name.Hebr.remove_diacritics .. "]"
	local ugsub = mw.ustring.gsub
	
	local function hebrew_link(word)
		return '<span class="Hebr" lang="he">[['
				.. ugsub(word, character_set_to_remove, "")
				.. "#Hebrew|" .. word .. "]]</span>"
	end
	
	local function hebrew_tag(word)
		return '<span class="Hebr" lang="he">' .. word .. "</span>"
	end
	
	local function get_first_letter(word)
		return word:match("^[%z\1-\127\194-\244][\128-\191]*")
	end
	
	return text:gsub(
		"%f[^\n%z]([^\t\n]+)\t([^\t\n]+)\t([^\t\n]+)",
		function(number, word, pos)
			local header = ""
			local letter = get_first_letter(word)
			if letter ~= prev_letter then
				if number ~= "1" then
					header = "</ul>\n\n"
				end
				header = header
					-- unicode-bidi: isolate; forces the Hebrew letter to the left in headers,
					-- but only &lrm; does that in the table of contents.
					.. ('===%s&lrm; &ndash; %04d===\n<ul class="plainlinks" style="column-width: 15em;">\n')
					:format(hebrew_tag(letter), tonumber(number))
				prev_letter = letter
			end
			
			local tr = transliterate and " (''" .. transliterate(word) .. "'')"
				or ""
			
			return header
				.. '<li> [https://www.blueletterbible.org/lexicon/h' .. number .. "/wlc H" .. number
				.. "]: " .. hebrew_link(word) .. tr .. " ''" .. pos:gsub(" ", ", ") .. "''"
				.. (word:find(" ", 1, true) and ("<br>(" .. word:gsub("[^ ]+", hebrew_link) .. ")") or "")
		end) .. "</ul>"
end

return export