Only registered users can edit. The registration requests please send to nablawiki@gm**l.com

Модуль:Example

Материал из Chameleon Test
Перейти к:навигация, поиск

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

local p = {}
 
local data = mw.loadData( 'Module:Example/data' )
 
function p2c( page )
	return( string.gsub( page, '[:/ (),\.]',  data.subst ))
end
 
function setParam( text, value )
	return( string.gsub( text, '%{%{%{f%|?%}%}%}', value ))
end
 
 
 
function p.Main( frame )
	local args = frame:getParent().args
	local chapter = mw.text.trim( args[1] or '' )
	local exname = mw.text.trim( args[2] or '' )
	if exname == '' then
		exname = 'Example'
	end
	local curti = mw.title.getCurrentTitle()
	if curti.subpageText == chapter then
		chapter = ''
	end
	if chapter ~= '' then
		exname = chapter .. '/' .. exname
	end
	local name = {}
	local state = {}
	local show = {}
	local note = {}
	local file = {}
 
	local params = {}
	for k, v in pairs(args) do
		if mw.ustring.sub (k, 1, 2) == 'p-' then
			params [mw.ustring.sub(k,3,-1)] = v
		end
	end	
 
 
 
	name.orig = 'HTML:Ex:' .. curti.fullText .. '/' .. exname
	local class = p2c( name.orig )
	file.orig = mw.title.new( name.orig )
	if not file.orig.exists then
		return 'Example file [[' .. name.orig .. ']] not exists!' 
	end
	state.orig = mw.text.trim( args['orig'] or 'skip' )
	-- возможные значения: yes...n/a — превращается в индикатор, blank — отсутствие индикатора, skip = 'show=no'
	show.orig = mw.text.trim( args['origshow'] or 'all') -- all/title/exec/text/noexec/notext/no
	if show.orig == '' then
		show.orig = 'all'
	end
	note.orig = args['orignote'] or ''
 
 
	name.wiki = name.orig .. '/wiki'
--	file.wiki = mw.title.new( name.wiki )
	state.wiki = mw.text.trim( args['wiki'] or 'ok' )
	-- возможные значения: yes...n/a — превращается в индикатор, blank — отсутствие индикатора, skip = 'show=no'
	show.wiki = mw.text.trim( args['wikishow'] or 'all') -- all/title/exec/text/noexec/notext/no
	if show.wiki == '' then
		show.wiki = 'all'
	end
	note.wiki = args['wikinote'] or ''
 
 
	name.html = name.orig .. '/html'
--	file.html = mw.title.new( name.html )
	state.html = mw.text.trim( args['html'] or 'skip' )
	-- возможные значения: yes...n/a — превращается в индикатор, blank — отсутствие индикатора, skip = 'show=no'
	show.html = mw.text.trim( args['htmlshow'] or 'all') -- all/title/exec/text/noexec/notext/no
	if show.html == '' then
		show.html = 'all'
	end
	note.html = args['htmlnote'] or ''
 
 
	local origshown = (mw.text.trim( args['origshow'] or 'regular')) == 'no'
	local result = ''
	local collapsibleStart = true -- false что-то есть всегда
 
	function element (out)
		if state[out] ~= 'skip' and show[out] ~= 'no' then
			file[out] = mw.title.new( name[out] )
			local xfile = out
			if not file[out].exists then
				xfile = 'orig'
			end
--			result = result .. '<div style="display:inline">[[' .. name [xfile] .. ']]</div>'
			-- title of example
			result = result .. '<div type="button" class="pull-right btn btn-default btn-xs mw-customtoggle-' .. 
			  class .. '-' .. out .. '-all"><i class="glyphicon glyphicon-off"></i></div><p class="h6">'
			result = result .. data.indicator[state[out]] .. ' [[' .. name [xfile] .. ']] <small>( '
			if out == 'orig' then
				result = result .. '[' .. tostring( mw.uri.fullUrl(name[out], {['action']='edit'}) ) .. ' edit]' 
			elseif xfile == out then
				result = result .. '[' .. tostring( mw.uri.fullUrl(name[out], {['action']='edit'}) ) .. ' edit] &bull; [' .. 
					tostring( mw.uri.fullUrl(name.orig, {['action']='edit'}) ) .. ' edit original] &bull; [' ..
					tostring( mw.uri.fullUrl('Special:ComparePages',{['page1']=name.orig,['page2']=name[out]}) ) .. ' compare]'
			else
				result = result .. 
						'[' .. tostring( mw.uri.fullUrl(name[out], {['action']='edit',['preload']=name.orig}) ) .. 
						' fork] &bull; [' .. tostring( mw.uri.fullUrl(name.orig, {['action']='edit'}) ) .. ' edit original]'
			end
			result = result .. ' )</small></p>'
			if note[out] ~= '' then
				result = result .. '<p>' .. note[out] .. '</p>'
			end
			-- box
			result = result .. '<div class="mw-collapsible mw-collapsible-content" id="mw-customcollapsible-' .. 
				class..'-' .. out .. '-all">'
			-- execution
			if data.showexec [show[out]] then
				result = result .. '<div class="demo-exec mw-collapsible" id="mw-customcollapsible-' ..
					class ..'-' .. out .. '-exec"><div class=demo-' .. data.mode[out] .. '-exec>'
				if data.mode[out] == 'wiki' then
					params['f'] = out
					result = result .. '\n' .. frame:expandTemplate( {title = ':' .. name[xfile], args = params} ) .. '\n'
				else
					params['f'] = out
					params['name'] = mw.ustring.sub(name[xfile],9,-1) -- ??????????
					result = result .. frame:expandTemplate( {title = ':HTML:Exec', args = params} )
				end
				result = result .. '</div></div>' -- newlines before and after content required for proper operation with wiki markup
			end
			-- name
			result = result .. '<div class="demo-name"><span class="mw-customtoggle-' ..
				class..'-' .. out .. '-exec" title="Hide/show result">' .. data.modetitle[out] .. 
				' <i class="glyphicon glyphicon-arrow-up"></i></span>, &nbsp; <span class="mw-customtoggle-' ..
				class..'-' .. out .. '-code" title="Hide/show code">' .. data.sourcetitle[xfile] .. 
				' <i class="glyphicon glyphicon-arrow-down"></i></span></div>\n'
			-- source
			if data.showtext [show[out]] then
				result = result .. 
					frame:extensionTag{ name = 'syntaxhighlight', 
						content = '\n' .. setParam( file[xfile]:getContent(), out ) .. '\n', 
						args = { lang = 'html5', 'strict', 
							class = 'demo-' .. data.corrorig [xfile] .. '-code mw-collapsible mw-collapsed', 
							id = 'mw-customcollapsible-' .. class .. '-' .. out .. '-code' } } .. '\n'
				if xfile == 'orig' then
					origshown = true
				end
			end
			result = result .. '</div>\n'
		end
	end -- element
 
	element ('wiki')
	element ('html')
	element ('orig')
 
 
	if not origshown then
--		result = result .. '<div class="demo-text-orig" title="' .. data.modetitle.orig ..' source code">\n' ..
--			frame:preprocess( '<syntaxhighlight lang="html5" strict>\n' .. file.orig:getContent() .. 
--			'\n</syntaxhighlight>' ) .. '\n</div>'
		-- сверху
		result = result .. 
			'<br><div class="mw-collapsible-content"><div class="demo-name"><span class="mw-customtoggle-' ..
			class..'-orig-code" title="Hide/show code">' .. data.sourcetitle.orig .. 
			' <i class="glyphicon glyphicon-arrow-down"></i></span></div>\n'
		-- source
		result = result .. 
			frame:extensionTag{ name = 'syntaxhighlight', 
				content = '\n' .. setParam( file.orig:getContent(), 'orig' ) .. '\n', 
				args = { lang = 'html5', 'strict', class = 'demo-orig-code mw-collapsible mw-collapsed', 
					id = 'mw-customcollapsible-' .. class .. '-orig-code' } } .. '</div>\n'
	end
 
 
	return result
 
 
end
 
 
function p.ShowText ( frame )
    local name = mw.text.trim( frame.args[1] or '-- nihil --' )
    local lang = mw.text.trim( frame.args[2] or 'html5' )
    local page = mw.title.makeTitle( '', name )
    local content = page:getContent() or '-- nihil --'
--    return frame:preprocess( '<syntaxhighlight lang="' .. lang .. '">' .. content .. '</syntaxhighlight>' )
--    return frame:preprocess( '<syntaxhighlight lang="' .. lang .. '" line strict>\n' .. content .. '\n</syntaxhighlight>' )
--    return frame:preprocess( '<syntaxhighlight lang="' .. lang .. '" strict>\n' .. content .. '\n</syntaxhighlight>' )
    return frame:extensionTag{ name = 'syntaxhighlight', content = '\n' .. content .. '\n', args = { lang = lang, 'strict' } }
end
 
 
return p