Module:Esperanto Translation/Klarigoj

Jump to navigation Jump to search
Documentation[create] [refresh]
This module has no documentation. If you know how to use this module, please create it.
local p = {}
local defaultDocPage = 'klarigoj'

local getType = function( namespace, page )
	local pageType = 'ŝablono'
	if namespace == 'Module' then
		pageType = 'modulo'
	elseif namespace == 'Widget' then
		pageType = 'fenestraĵo'
	elseif page.fullText:gsub( '/' .. defaultDocPage .. '$', '' ):find( '%.css$' ) then
		pageType = 'stilfolio'
	elseif page.fullText:gsub( '/' .. defaultDocPage .. '$', '' ):find( '%.js$' ) then
		pageType = 'skripto'
	elseif namespace == 'MediaWiki' then
		pageType = 'mesaĝo'
	end
	
	return pageType
end

-- Creating a documentation page or transclusion through {{subst:docc}}
-- p.create
function p.krei( f )
	local args = require( 'Module:ProcessArgs' ).norm()
	local page = mw.title.getCurrentTitle()
	local docPage = args.page or page.nsText .. ':' .. page.baseText .. '/' .. defaultDocPage
	
	local out
	if not args.enhavo and tostring( page ) == docPage then
		out = f:preprocess( '{{subst:MCW:Projects/Esperanto translation/Ŝablono:Klarigoj}}' )
	else
		local templateArgs = {}
		for _, key in ipairs{ 'tipo', 'paĝo', 'enhavo', 'mankas', 'malbona' } do
			local val = args[key]
			if val then
				if key == 'enhavo' then val = '\n' .. val .. '\n' end
				table.insert( templateArgs, key .. '=' .. val )
			end
		end
		
		out = '{{ {{lp|Ŝablono:Klarigoj}} |' .. table.concat( templateArgs, '|' ) .. '}}'
		out = out:gsub( '|}}', '}}' )
		
		if not args.klarigoj then
			out = out .. '\n<!-- Enmetu kategoriojn kaj intervikiojn en la klarigpaĝo -->'
		end
	end
	
	if not mw.isSubsting() then
		out = f:preprocess( out )
		if not args.nocat then
			out = out .. f:preprocess('{{lcl|Paĝoj kun ŝanĝendaj ŝablonoj}}')
		end
	end
	
	return out
end

-- Header on the documentation page
-- p.docPage
function p.klarigpagxo( f )
	local args = require( 'Module:ProcessArgs' ).merge( true )
	local badDoc = args.malbona
	if f:callParserFunction( '#dplvar', '$doc noheader' ) == '1' then
		if badDoc then
			f:callParserFunction( '#dplvar:set', '$doc bad', '1' )
		end
		return
	end
	
	local page = mw.title.getCurrentTitle()
	local subpage = page.subpageText
	if subpage == 'Provejo' or subpage == 'Testoj' then
		page = page.basePageTitle
	end
	local namespace = page.nsText
	local pageType = mw.ustring.lower( args.tipo or getType( namespace, page ) )
	
	local body = mw.html.create( 'div' ):addClass( 'documentation-header' )
	body
		:addClass( badDoc and 'documentation-badDoc' or '' )
		:tag( 'div' )
			:attr( 'id', 'documentation-header-tools' )
			:wikitext( '[[', page:fullUrl( 'action=purge' ), ' ' .. f:preprocess('{{lc:{{int:smw purge}}}}') .. ']]' )
		:done()
		:wikitext(
			'Ĉi tio estas klarigpaĝo. Ĝi ',
			pageType == 'modulo' and 'prezentiĝos' or 'prezentiĝu',
			' en la ĉefpaĝo de la ', pageType, '. ',
			'Vidu ' .. f:preprocess('{{ll|Ŝablono:Klarigoj}}') .. ' por ekscii pli'
		)
	if badDoc then
		body:wikitext( "<br>'''La dokumentadon pri ĉi tiu ", pageType, " necesas plibonigi aŭ provizi per pli da informoj.'''" )
	end
	if not ( args.nocat or namespace == 'User' ) then
		body:wikitext( f:preprocess('{{lcl|Klarigpaĝoj}}') )
	end
	
	return body
end

-- Wrapper around the documentation on the main page
-- p.page
function p.pagxo( f )
	-- mw.text.trim uses mw.ustring.gsub, which silently fails on large strings
	local function trim( s )
		return (s:gsub( '^[\t\r\n\f ]+', '' ):gsub( '[\t\r\n\f ]+$', '' ))
		--return string.gsub( s, '^[\t\r\n\f ]*(.-)[\t\r\n\f ]*$', '%1' )
	end
	local args = require( 'Module:ProcessArgs' ).merge( true )
	local page = mw.title.getCurrentTitle()
	local subpage = page.subpageText
	if subpage == 'Provejo' or subpage == 'Testoj' then
		page = page.basePageTitle
	end
	local namespace = page.nsText
	local docText = trim( args.enhavo or '' )
	if docText == '' then docText = nil end
	
	local docPage
	local noDoc
	if docText then
		docPage = page
	else
		docPage = mw.title.new( args["paĝo"] or namespace .. ':' .. page.text .. '/' .. defaultDocPage )
		noDoc = args.mankas or not docPage.exists
	end
	local badDoc = args.malbona
	local pageType = mw.ustring.lower( args.tipo or getType( namespace, page ) )
	
	if not docText and not noDoc then
		f:callParserFunction( '#dplvar:set', '$doc noheader', '1' )
		docText = trim( f:expandTemplate{ title = ':' .. docPage.fullText }  )
		if f:callParserFunction( '#dplvar', '$doc bad' ) == '1' then
			badDoc = 1
		end
		
		if docText == '' then
			docText = nil
			noDoc = 1
		end
	end
	if docText then
		docText = '\n' .. docText .. '\n'
	end
	
	local docClass = ''
	local message
	local category
	if noDoc then
		docClass = 'documentation-noDoc'
		message = "'''Ĉi tiu " .. pageType .. " ne havas klarigojn. " ..
			"Se vi scias uzi tiun " .. pageType .. "n, bonvolu krei dokumentadon.'''"
		if not ( args.nocat or namespace == 'User' ) then
			category = pageType:gsub("^%l", string.upper) .. 'j sen klarigoj'
			if not mw.title.new( 'Category:Esperanto Translation/' .. category ).exists then
				category = 'Paĝoj sen klarigoj'
			end
		end
	elseif badDoc then
		docClass = 'documentation-badDoc'
		message = "'''La dokumentadon pri ĉi tiu ", pageType, " necesas plibonigi aŭ provizi per pli da informoj.'''\n"
		if not ( args.nocat or namespace == 'User' ) then
			category = pageType:gsub("^%l", string.upper) .. 'j kun malbonaj klarigoj'
			if not mw.title.new( 'Category:Esperanto Translation/' .. category ).exists then
				category = 'Paĝoj kun malbonaj klarigoj'
			end
		end
	end
	
	-- Generates the [antaŭrigardi][redakti][historio][renivigi] or [krei][renovigi] links
	local links = mw.html.create( 'span' )
		:attr( 'id', 'documentation-header-tools' )

	local purgeLink = '[[Special:Purge/' .. docPage.fullText .. '|' .. f:preprocess('renovigi') .. ']]'
	if not noDoc and page ~= docPage then
		local viewLink = '[[' .. docPage.fullText .. '|' .. f:preprocess('antaŭrigardi') .. ']]'
		local editLink = '[[Special:EditPage/' .. docPage.fullText .. '|' .. f:preprocess('redakti') .. ']]'
		local historyLink = '[[Special:PageHistory/' .. docPage.fullText .. '|' .. f:preprocess('historio') .. ']]'
		links:wikitext("&#91;" .. viewLink .. "&#93; &#91;" .. editLink .. "&#93; &#91;" .. historyLink .. "&#93; &#91;" .. purgeLink .. "&#93;")
	else
		local preloadUrl = 'Minecraft Wiki:Projects/Esperanto translation/Ŝablono:Klarigoj/Antaŭŝargo'
		local encodedPreloadUrl = 'Minecraft%20Wiki%3AProjects%2FEsperanto%20translation%2F%C5%9Cablono%3AKlarigoj%2FAnta%C5%AD%C5%9Dargo'
		local createLink = '[' .. docPage:canonicalUrl{action = 'edit', preload = preloadUrl } .. ' ' .. f:preprocess('krei') .. ']'
		links:wikitext("&#91;" .. createLink .. "&#93; &#91;" .. purgeLink .. "&#93;")
	end
	
	local body = mw.html.create( 'div' ):addClass( 'documentation' )
	body
		:addClass( docClass )
	
	local header = mw.html.create( 'div' )
		:addClass( 'documentation-header-top' )
	
	header
		:node( links )
		:tag( 'span' )
			:addClass('documentation-header-title' )
			:wikitext( 'Klarigoj' )
	
	local codePages = {
		modulo = true,
		stilfolio = true,
		skripto = true,
	}
	if not noDoc and codePages[pageType] then
		header
			:tag( 'span' )
				:attr( 'id', 'documentation-jump-to-code' )
				:wikitext( '[[#the-code|Rigardi la kodon ↴]]' )
	end
	
	body
		:node( header ):done()
		:wikitext( message )
		:wikitext( docText )
	
	if not noDoc and page ~= docPage then
		body
			:tag( 'div' )
				:addClass( 'documentation-header-bottom' )
				:node( links )
				:wikitext( 'La ĉi-supraj klarigoj venas el [[', docPage.fullText, ']].' )
	end
	
	if category then
		body:wikitext( f:expandTemplate{ title = 'Translation category', args = { 'Esperanto translation/' .. category, project = '0' } } )
	end
	
	local anchor = ''
	if not noDoc and pageType ~= 'ŝablono' and pageType ~= 'mesaĝo' then
		anchor = mw.html.create( 'div' ):attr( 'id', 'the-code' )
	end
	
	return require( 'Module:TSLoader' ).call( 'Module:Documentation/styles.css' ) .. tostring( body ) .. tostring( anchor )
end

return p