Kontent qismiga oʻtish

Modul:Find sources

Vikipediya, ochiq ensiklopediya

Bu modul uchun Modul:Find sources/doc nomli hujjat sahifasini yaratishingiz mumkin

local ROOT_PAGE = 'Modul:Find sources'
local TEMPLATE_ROOT = ROOT_PAGE .. '/templates/'  
local LINK_CONFIG = ROOT_PAGE .. '/links' 
local CONFIG_PAGE = ROOT_PAGE .. '/config'  

--  
local checkType = require('libraryUtil').checkType
local cfg = mw.loadData(CONFIG_PAGE)

local p = {}

local function maybeLoadData(page)
	local success, data = pcall(mw.loadData, page)
	return success and data
end

local function substituteParams(msg, ...)
	return mw.message.newRawMessage(msg, ...):plain()
end

local function renderSearchString(searchTerms, separator, transformFunc)
  
	local searchStrings = {}
	for i, s in ipairs(searchTerms) do
		searchStrings[i] = s
	end
	if transformFunc then
		for i, s in ipairs(searchStrings) do
			searchStrings[i] = transformFunc(s)
		end
	end
	return table.concat(searchStrings, separator)
end

function p._renderLink(code, searchTerms, display, tooltip)

	local links = maybeLoadData(LINK_CONFIG)
	local linkCfg = links[code]
	if not linkCfg then
		error(string.format(
			"yaroqsiz '%s' havola kodi; no link config found at [[%s]]",
			code,
			LINK_CONFIG
		))
	end

	--  
	local url
	do
		local separator = linkCfg.separator or "+"
		local searchString = renderSearchString(
			searchTerms,
			separator,
			mw.uri.encode
		)
		url = substituteParams(linkCfg.url, searchString)
	end
	
	if tooltip then
		return string.format('<span title="%s" style="border-bottom: 1px dotted;">[%s %s]</span>', 
			mw.text.encode(tooltip), url, display or linkCfg.display)
	else
		return string.format('[%s %s]', url, display or linkCfg.display)
	end
end

function p._main(template, args)

	checkType('_main', 1, template, 'string')
	checkType('_main', 2, args, 'table', true)
	args = args or {}
	local title = mw.title.getCurrentTitle()
 
	local templateCfgPage = TEMPLATE_ROOT .. template
	local templateCfg = maybeLoadData(templateCfgPage)
	if not templateCfg then
		error(string.format(
			"yaroqsiz '%s' andoza nomi; no template config found at [[%s]]",
			template, templateCfgPage
		))
	end

	if not templateCfg.isUsedInMainspace and title.namespace == 0 then
		local formatString = '<strong class="error">%s</strong>'
		if cfg['namespace-error-category'] then
			formatString = formatString .. '[[%s:%s]]'
		end
		return string.format(
			formatString,
			cfg['namespace-error'],
			mw.site.namespaces[14].name,
			cfg['namespace-error-category']
		)
	end

	local searchTerms = {}
	for i, s in ipairs(args) do
		searchTerms[i] = s
	end
	if not searchTerms[1] then
 
		local searchTitle = args.title or title.subpageText
		local term, dab = searchTitle:match('^(.*) (%b())$')
		if dab then
			dab = dab:sub(2, -2)  
		end
		if term and dab then
			searchTerms[1] = term
			searchTerms[2] = dab
		else
			searchTerms[1] = searchTitle
		end
	end
	searchTerms[1] = '„' .. searchTerms[1] .. '“'

	local introLink
	if templateCfg.introLink then
		local code = templateCfg.introLink.code
		local display = templateCfg.introLink.display or renderSearchString(
			searchTerms,
			'&nbsp;'
		)
		local tooltip = templateCfg.introLink.tooltip
		introLink = p._renderLink(code, searchTerms, display, tooltip)
	else
		introLink = ''
	end

	local links = {}
	local separator = templateCfg.separator or cfg['default-separator']
	local sep = ''
	for i, t in ipairs(templateCfg.links) do
		links[i] = sep .. p._renderLink(t.code, searchTerms, t.display, t.tooltip) ..
			(t.afterDisplay or '')
		sep = t.separator or separator
	end
	links = table.concat(links)

	local blurb = substituteParams(templateCfg.blurb, introLink, links)
	local span = mw.html.create('span')
	span
		:addClass('plainlinks')
		:addClass(templateCfg.class)
		:cssText(templateCfg.style)
		:wikitext(blurb)

	return tostring(span)
end

setmetatable(p, { __index = function(t, template)
  
	local tname = template
	if tname:sub(-8) == '/qumloq' then

		tname = tname:sub(1, -9)
	end
	return function(frame)
		local args = require('Modul:Arguments').getArgs(frame, {
			wrappers = mw.site.namespaces[10].name .. ':' .. tname
		})
		return t._main(template, args)
	end
end})

return p