Module:CTD: Difference between revisions

From Teopedia
(Created page with "local ctd = {} -- функции для Большого теософского словаря; copied from https://ru.teopedia.org/lib/Module:CTD local function isempty(s)...")
(No difference)

Revision as of 05:46, 10 June 2023


local ctd = {} -- функции для Большого теософского словаря; copied from https://ru.teopedia.org/lib/Module:CTD

local function isempty(s)
  return s == nil or s == ''
end

-------------------------------------------------
-- Convert local links [[link|text]] and URL [link text] to plain text.
-- See original function in module PM.
local function remove_links( wiki_str )
	local str = wiki_str

	-- remove local links
	i = mw.ustring.find(str,'[[',1,true)
	j = mw.ustring.find(str,']]',i,true)
	while i ~= nil do
		local link_text = mw.ustring.sub(str, i+2, j-1)
		local k = mw.ustring.find(link_text, '|', 1, true) 
		if k ~= nil then
			link_text = mw.ustring.sub(link_text, k+1)
		end
		str = mw.ustring.sub(str, 1, i-1) .. link_text .. mw.ustring.sub(str, j+2, -1)
		i = mw.ustring.find(str,'[[',1,true)
		j = mw.ustring.find(str,']]',i,true)
	end	
	
	-- remove URL
	i = mw.ustring.find(str,'[http',1,true)
	j = mw.ustring.find(str,']',i,true)
	while i ~= nil do
		local link_text = mw.ustring.sub(str, i+1, j-1)
		local k = mw.ustring.find(link_text, ' ', 1, true) 
		if k ~= nil then
			link_text = mw.ustring.sub(link_text, k+1)
		end
		str = mw.ustring.sub(str, 1, i-1) .. link_text .. mw.ustring.sub(str, j+1, -1)
		i = mw.ustring.find(str,'[http',1,true)
		j = mw.ustring.find(str,']',i,true)
	end	
	
	return str
end

-------------------------------------------------
-- Create short description without hyper links
-- Used in : Шаблон:Понятие БТС
-- Call example: 
-- {{#invoke: CTD | shortly 
--  | short_text = Short description, if exist 
--  | long_text  = Long description
-- }}
function ctd.shortly( frame )
	local result = ''

	-- check if sort description specified
	if not isempty(frame.args['short_text']) then
		result = frame.args['short_text']
	else 
		result = frame.args['long_text']
		-- keep only text before Category
		local i = mw.ustring.find(result,'[[Категория',1,true)
		if i ~= nil then
			result = mw.ustring.sub(result, 1, i-1)
		end
		-- convert all links to plain text
		result = remove_links(result)
		-- take only first 100 characters; string.sub() uses bytes instead of chars
		if mw.ustring.len(result) > 100 then
			result = mw.ustring.sub( result, 1, 100) .. '...'
		end
	end
	
	-- Use function point_at_end() when finished
	-- return point_at_end(result)
	return result
end