模組:股票代號模

出自維基百科,自由嘅百科全書
模組解[]
-- vim: set sw=4 ts=4 ai sm :
---- 呢個模係2023年喺粵維原創,用來整股票代號模,因為整 tsx 模嗰陣發現一間公司如果同一間交易所多過一個代號,
---- 出來效果會好怪。用呢個模組整出嘅模,出來嘅效果會類似英維嘅 tsx 模,即係交易所名唔使重覆,但係英維嘅 tsx
---- 模因為唔係模組,有三個代號嘅限制,用呢個模組整出來嘅模原則上冇咁嘅限制。
----
---- 用法:
---- <includeonly>{{#invoke:股票代號模|main|article=交易所文章名|label=交易所名|url=網址}}[[Category:◯◯上市公司]]</includeonly><noinclude>{{Documentation}}[[Category:股票代號模]][[Category:◯◯上市公司|*]]</noinclude>
----
---- 股票代號模入面嘅網址應該包含 「%s」 代碼,模組執行期間會換做股票代碼,而◯◯係交易所嘅名(例如:「多倫多證券交易所」)

require ('strict');
local p = {};

local function ticker( symbol, url )
	local it;
	local bold = "'''";
	if url and symbol then
		it = '[' .. url:gsub('%%s', symbol) .. ' ' .. bold .. symbol .. bold .. ']';
	elseif symbol then
		it = bold .. symbol .. bold;
	end
	return it;
end

-- Entry point
p.main = function (frame)
	local parent = frame:getParent();
	local s = '';
	local article, label, url;
	local symbols = {};
	for _, a in pairs({frame, parent}) do
		if a then
			for k, v in pairs(a.args) do
				v = v:gsub('^%s+', ''):gsub('%s+$', '');
				if type(k) == 'number' then
					table.insert(symbols, v);
				elseif k == 'art' or k == 'article' or k == '文' or k == '文章' then
					article = v;
				elseif k == 'label' or k == '標題' then
					label = v;
				elseif k == 'url' or k == '網址' then
					url = v;
				else
					error('不明參數 「' .. k .. '」');
				end
			end
		end
	end
	if not article and not label then
		error('模組用法錯誤:冇指定 「文章」(article),又冇指定 「標題」(label)');
	elseif #symbols == 0 then
		error('模組用法錯誤:冇指定任何股票代碼');
	end
	if article and not label then
		label = article;
	end

	-- 標題
	if article and label then
		s = s .. '[[' .. article .. '|' .. label .. ']]:';
	elseif article then
		s = s .. '[[' .. article .. ']]:';
	else
		s = s .. label .. ':';
	end

	-- 股票代號
	for i, v in pairs(symbols) do
		if i > 1 then
			s = s .. '、';
		end
		s = s .. ticker(v, url);
	end

	-- 完
	return s;
end


p.test = function ()
	assert(ticker('ABC', nil) == "'''ABC'''");
	assert(ticker('ABC', 'http://%s') == "[http://ABC '''ABC''']");

	assert(p.main({
		['getParent'] = function ()
			return {
				['args'] = { 'RCI.A', 'RCI.B' };
			};
		end;
		['args'] = {
			['article'] = '多倫多證劵交易所';
			['label'] = 'TSX';
			['url'] = 'https://money.tmx.com/en/quote/%s';
		};
	}) == "[[多倫多證劵交易所|TSX]]:[https://money.tmx.com/en/quote/RCI.A '''RCI.A''']、[https://money.tmx.com/en/quote/RCI.B '''RCI.B''']");

	assert(p.main({
		['getParent'] = function ()
			return {
				['args'] = { 'RCI' };
			};
		end;
		['args'] = {
			['article'] = '紐約證劵交易所';
			['label'] = '紐交所';
			['url'] = 'https://www.nyse.com/quote/XNYS:%s';
		};
	}) == "[[紐約證劵交易所|紐交所]]:[https://www.nyse.com/quote/XNYS:RCI '''RCI''']");

end

return p;