ru>QBA-bot
м Защитил страницу Модуль:Demo: критический шаблон или модуль (каскадная защита) ([Редактирование=только автоподтверждённые] (бессрочно) [Переименование=только автоподтверждённые] (бессрочно))
 
м 1 версия импортирована
 
(не показаны 2 промежуточные версии 2 участников)
Строка 1: Строка 1:
local p = {}
local p = {}
local getArgs = require('Модуль:Arguments').getArgs
local function hasValue(param)
if param and param:find('%S') then
return true
end
end


--creates a frame object that cannot access any of the parent's args
--creates a frame object that cannot access any of the parent's args
Строка 23: Строка 33:
return orphan, parent
return orphan, parent
end
end


function p.get(frame, arg, passArgs)
function p.get(frame, arg, passArgs)
Строка 28: Строка 39:
local code, noWiki, preserve = frame.args[arg or 1] or ''
local code, noWiki, preserve = frame.args[arg or 1] or ''
local kill_categories = not frame.args.save_categories
local kill_categories = not frame.args.save_categories
local tag, sep
local tag, sep, sh
local shl = frame.args.shl
local lang = frame.args.lang or "wikitext"
if code:match'nowiki' then
if code:match'nowiki' then
local placeholder, preserve = ('6'):char(), {}
local placeholder, preserve = ('6'):char(), {}
Строка 50: Строка 63:
end
end
noWiki = mw.text.nowiki(noWiki):format(unpack(preserve)):gsub(placeholder, '%%')
noWiki = mw.text.nowiki(noWiki):format(unpack(preserve)):gsub(placeholder, '%%')
end
if shl then
if shl == "inline" then tag = 'span' else tag = 'div' end
sh = orphan:preprocess('<syntaxhighlight lang='..lang..' '..shl..'>'..code..'</syntaxhighlight>')
end
end
return {
return {
source = noWiki or code,
source = sh or noWiki or code,
output = orphan:preprocess(code)
output = orphan:preprocess(code)
:gsub(kill_categories and '%[%[Категория:.-%]%]' or '', '')
:gsub(kill_categories and '%[%[Категория:.-%]%]' or '', '')
Строка 58: Строка 75:
:gsub(kill_categories and '%[%[Category:.-%]%]' or '', ''),
:gsub(kill_categories and '%[%[Category:.-%]%]' or '', ''),
frame = frame,
frame = frame,
args = getArgs(frame),
tag = tag,
tag = tag,
sep = sep
sep = sep
}
}
end
end


function p.main(frame, demoTable)
function p.main(frame, demoTable)
local show = demoTable or p.get(frame)
local show = demoTable or p.get(frame)
local args = show.frame.args
local args = show.args
local yesno = require('Module:Yesno')
local yesno = require('Module:Yesno')
args.reverse = yesno(args.reverse, false)
args.reverse = yesno(args.reverse, false)
    args.tag = args.tag or show.tag or "code"
args.tag = hasValue(args.tag) and args.tag or hasValue(show.tag) and show.tag or "code"
    args.sep = args.sep or args.br or show.sep
args.sep = args.sep or args.br or show.sep
    if(args.br == 'no') then args.sep = ''
else
args.sep = tonumber(args.sep) and ('<br>'):rep(args.sep or 0) or args.sep or (args.tag == 'pre' and '' or ' → ')
args.sep = tonumber(args.sep) and ('<br>'):rep(args.sep or 0) or args.sep or (args.tag == 'pre' and '' or ' → ')
end
if show[args.result_arg] then
if show[args.result_arg] then
return show[args.result_arg]
return show[args.result_arg]
end
end
return args.reverse
return args.reverse
and string.format('%s%s<%s%s>%s</%s>', show.output, args.sep, args.tag, args.style and string.format(" style='%s'", args.style) or '', show.source, args.tag)
and string.format(
or string.format('<%s%s>%s</%s>%s%s', args.tag, args.style and string.format(" style='%s'", args.style) or '', show.source, args.tag, args.sep, show.output)
'%s%s<%s%s%s>%s</%s>',
show.output,
args.sep,
args.tag,
hasValue(args.class) and string.format(' class="%s"', args.class) or '',
hasValue(args.style) and string.format(' style="%s"', args.style) or '',
show.source,
args.tag
)
or string.format(
'<%s%s%s>%s</%s>%s%s',
args.tag,
hasValue(args.class) and string.format(' class="%s"', args.class) or '',
hasValue(args.style) and string.format(' style="%s"', args.style) or '',
show.source,
args.tag,
args.sep,
show.output
)
end
end


--passing of args into other module without preprocessing
--passing of args into other module without preprocessing
Строка 127: Строка 169:
end
end
end
end


return p
return p

Текущая версия от 14:35, 22 июня 2026

Для документации этого модуля может быть создана страница Модуль:Demo/doc

local p = {}

local getArgs = require('Модуль:Arguments').getArgs


local function hasValue(param)
	if param and param:find('%S') then
		return true
	end
end


--creates a frame object that cannot access any of the parent's args
--unless a table containing a list keys of not to inherit is provided
function disinherit(frame, onlyTheseKeys)
	local parent = frame:getParent() or frame
	local orphan = parent:newChild{}
	orphan.getParent = parent.getParent --returns nil
	orphan.args = {}
	if onlyTheseKeys then
		local family = {parent, frame}
		for f = 1, 2 do
			for k, v in pairs(family[f] and family[f].args or {}) do
				orphan.args[k] = orphan.args[k] or v
			end
		end
		parent.args = mw.clone(orphan.args)
		setmetatable(orphan.args, nil)
		for _, k in ipairs(onlyTheseKeys) do
			rawset(orphan.args, k, nil)
		end
	end
	return orphan, parent
end


function p.get(frame, arg, passArgs)
	local orphan, frame = disinherit(frame, passArgs and {arg or 1})
	local code, noWiki, preserve = frame.args[arg or 1] or ''
	local kill_categories = not frame.args.save_categories
	local tag, sep, sh
	local shl = frame.args.shl
	local lang = frame.args.lang or "wikitext"
	if code:match'nowiki' then
		local placeholder, preserve = ('6'):char(), {}
		-- We replace "&#125;%-" and "%-&#123;" because of some server bug probably connected to
		-- [[mw:Parsoid/MediaWiki DOM spec/Language conversion blocks]] and leading to
		-- =mw.text.unstripNoWiki(mw.getCurrentFrame():preprocess('<nowiki>}-</nowiki>'))
		-- outputting '&#125;-' instead of "}-", while it's ok with "<nowiki>} -</nowiki>"
		code = mw.text.unstripNoWiki(code)
			:gsub('&lt;', '<')
			:gsub('&gt;', '>')
			:gsub('&#125;%-', '}-')
			:gsub('%-&#123;', '-{')
		if (mw.text.trim(code):match'\n') then
			tag = 'pre'
			sep = ''
		end
		noWiki = code:gsub('%%', placeholder)
		for k in noWiki:gmatch('&.-;') do
			table.insert(preserve, (k:gsub('&', '&amp;')))
			noWiki = noWiki:gsub('(&.-;)', '%%%s')
		end
		noWiki = mw.text.nowiki(noWiki):format(unpack(preserve)):gsub(placeholder, '%%')
	end
	if shl then
		if shl == "inline" then tag = 'span' else tag = 'div' end
		sh = orphan:preprocess('<syntaxhighlight lang='..lang..' '..shl..'>'..code..'</syntaxhighlight>')
	end
	return {
		source = sh or noWiki or code,
		output = orphan:preprocess(code)
			:gsub(kill_categories and '%[%[Категория:.-%]%]' or '', '')
			:gsub(kill_categories and '%[%[К:.-%]%]' or '', '')
			:gsub(kill_categories and '%[%[Category:.-%]%]' or '', ''),
		frame = frame,
		args = getArgs(frame),
		tag = tag,
		sep = sep
	}
end


function p.main(frame, demoTable)
	local show = demoTable or p.get(frame)
	local args = show.args
	local yesno = require('Module:Yesno')
	args.reverse = yesno(args.reverse, false)
	args.tag = hasValue(args.tag) and args.tag or hasValue(show.tag) and show.tag or "code"
	args.sep = args.sep or args.br or show.sep
    if(args.br == 'no') then args.sep = ''
	else 
	args.sep = tonumber(args.sep) and ('<br>'):rep(args.sep or 0) or args.sep or (args.tag == 'pre' and '' or ' → ')
	end
	if show[args.result_arg] then
		return show[args.result_arg]
	end

	return args.reverse
		and string.format(
			'%s%s<%s%s%s>%s</%s>',
			show.output,
			args.sep,
			args.tag,
			hasValue(args.class) and string.format(' class="%s"', args.class) or '',
			hasValue(args.style) and string.format(' style="%s"', args.style) or '',
			show.source,
			args.tag
			)
		or string.format(
			'<%s%s%s>%s</%s>%s%s',
			args.tag,
			hasValue(args.class) and string.format(' class="%s"', args.class) or '',
			hasValue(args.style) and string.format(' style="%s"', args.style) or '',
			show.source,
			args.tag,
			args.sep,
			show.output
			)
end


--passing of args into other module without preprocessing
function p.module(frame)
	local orphan, frame = disinherit(frame, {
		'demo_template',
		'demo_module',
		'demo_module_func',
		'demo_main',
		'demo_br',
		'demo_result_arg',
		'demo_save_categories'
	})
	local template = frame.args.demo_template and 'Template:'..frame.args.demo_template
	local demoFunc = frame.args.demo_module_func or 'main\n'
	local demoModule = require('Module:' .. frame.args.demo_module)[demoFunc:match('^%s*(.-)%s*$')]
	frame.args.br, frame.args.result_arg = frame.args.demo_br or frame.args.demo_sep, frame.args.demo_result_arg
	local kill_categories = not save_categories
	if demoModule then
		local named = {insert = function(self, ...) table.insert(self, ...) return self end}
		local source = {insert = named.insert, '{{', frame.args.demo_template or frame.args.demo_module, '\n'}
		if not template then
			source:insert(2, '#invoke:'):insert(4, '|'):insert(5, demoFunc)
		end
		local insertNamed = #source + 1
		for k, v in pairs(orphan.args) do
			local nan, insert = type(k) ~= 'number', {v}
			local target = nan and named or source
			target:insert'|'
			if nan then
				target:insert(k):insert'=':insert'\n'
				table.insert(insert, 1, #target)
			end
			target:insert(unpack(insert))
			local nowiki = v:match('nowiki')
			if nowiki or v:match('{{.-}}') then
				orphan.args[k] = frame:preprocess(nowiki and mw.text.unstripNoWiki(v) or v)
			end
		end
		source:insert'}}'
		table.insert(source, insertNamed, table.concat(named))
		return p.main(orphan, {
			source = mw.text.encode(table.concat(source), "<>'|=~"),
			output = tostring(demoModule(orphan)):gsub(kill_categories and '%[%[Категория:.-%]%]' or '', ''):gsub(kill_categories and '%[%[К:.-%]%]' or '', ''):gsub(kill_categories and '%[%[Category:.-%]%]' or '', ''),
			frame = frame
		})
	else
		return "ERROR: Invalid module function: "..demoFunc
	end
end


return p