Module:RQ:pi:Sai Kam Mong

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

Invoking this module as {{#invoke:RQ:pi:Sai Kam Mong|quote|a|b|c|passages=xxx}} is equivalent to invoking a template as {{{{yyy}} quote|a|b|c}}, for some yyy, so please read that template for usage. If the argument passages is omitted, the effect is equivalent to invoking the template as {{RQ:pi:Sai Kam Mong quote|a|b|c}}.

The templates and modules for the use of this come in sets of three - a citation selection template, a data module, and a display template.

The citation selection template simply invokes this module's function quote; module invocations are not allowed in the main space pages, but must be wrapped in a template. This function accesses and manipulates the data in the data module, while the display template can function independently of it and the the data.

Wiktionary requests that each template and module have a documentation page. For the first two, boilerplate templates {{RQ:pi:Sai Kam Mong/quote boilerplate|human-language-documented|data-module-name|sample-page-no|sample-passage-id|sample-word-number|passages=data-module-name}} and {{RQ:pi:Sai Kam Mong/passage boilerplate|human-language-documented|data-module-name|sample-page-no|sample-passage-id|sample-word-number}} provide most of this documentation. The boilerplate templates require that name of the citation slection template be the name of the data module augmented by the word 'quote'. The display template's documentation is less stereotyped, and adaptation of one set's to a new set's must be done manually. Adapting the example looks daunting, but is not as difficult as it looks. The trick is to start from the bottom and work up.

Existing sets include:

Citation selection template Data module Display template
{{RQ:pi:N3207 quote}} {{Module:RQ:pi:N3207}} {{RQ:pi:N3207}}
{{RQ:pi:Phaya Luang Maha Sena quote}} {{Module:RQ:pi:Phaya Luang Maha Sena}} {{RQ:pi:Phaya Luang Maha Sena}}
{{RQ:pi:Sai Kam Mong quote}} {{Module:RQ:pi:Sai Kam Mong/passages}} {{RQ:pi:Sai Kam Mong}}
{{RQ:pi:Thai chant book quote}} {{Module:RQ:pi:Thai chant book}} {{RQ:pi:Thai chant book}}
{{RQ:pi:U17106 quote}} {{Module:RQ:pi:U17106}} {{RQ:pi:U17106}}

If creating a new set, the 'Sai Kam Mong' set would be a bad set to start from, as for historical reasons, it has different and confusing naming conventions. Some of these issues are worked round by the defaults in the documentation templates.


local export = {};
local gsub = string.gsub  -- Can pretend it's all Latin-1!
local trim = mw.text.trim -- Except for trimmming strings.

function export.quote(frame)
	-- Convert from string if decimal, and trim if not.
	local fa1 = frame.args[1]
	local pageno = tonumber(fa1) or fa1 and trim(fa1)
	if not pageno or pageno == "" then
		return "Invalid or missing page number"
	end
	local quotid = frame.args[2]
	if quotid then
		quotid = trim(quotid)
		if string.len(quotid) == 0 then quotid = nil end
	end
	if not quotid then
		return "Passage ID is missing"
	end
	local wordid = tonumber(frame.args[3]) or 0
	local passages = frame.args["passages"] or 'RQ:pi:Sai Kam Mong/passages'
	passages = mw.loadData('Module:'..passages)
	local passage = passages[pageno]
	if passage then
		passage=passage[quotid]
		if passage then
            local highlight = function() end
			local function consider_word(m1)
				if string.find(m1, "^{"..wordid.."%-") then
					return "'''"..m1.."'''"
				else
					return "{"..highlight(string.sub(m1, 2))
				end
			end
			highlight = function(m2)
				return gsub(m2, "%b{}", consider_word)
			end
			local function process(line)
				if line then
					line = highlight(line)
					line = gsub(line, "{%d%d*-", "")
					line = gsub(line, "}", "")
					line = gsub(line, "''''''", "")
					line = gsub(line, '⧁', "<span style='background-color:#FFff00;'>")
					line = gsub(line, '⧀', "</span>")
				end
				return line
			end
			local text, tran, xlit, lit, norm, ts
			text = process(passage[1])
			tran = process(passage[2])
			xlit = process(passage[3])
			lit  = process(passage.lit)
			norm = process(passage.norm)
			ts   = process(passage.ts)
			local reference = frame.args.reference or passages.reference
			return frame:expandTemplate{title = trim(reference),
				args = { pageno, text, tran, xlit,
					lang = passage.lang or passages[pageno].lang or passages.lang,
					p1 = passage.p1 or passages[pageno].p1 or passages.p1,
					p2 = passage.p2 or passages[pageno].p2 or passages.p2,
					brackets = frame.args.brackets,
					norm = norm,
					lit  = lit,
					ts = ts
				} }
		else
			return "No passage dubbed \"" .. quotid .. "\" on page " .. pageno
		end
	else
		return "There are no passages from page " .. pageno
	end
end
return export