Modulo:FunzioniGeneriche: differenze tra le versioni

Da Wikitrek.
Vai alla navigazione Vai alla ricerca
Nessun oggetto della modifica
(TItles in example output)
(34 versioni intermedie di uno stesso utente non sono mostrate)
Riga 1: Riga 1:
-- Questo codice proviene dal {{FULLPAGENAME}}
-- Keyword: wikitrek
 
local p = {} --p stands for package
local p = {} --p stands for package


function p.EsempioTemplate(frame)
function p.EsempioTemplate(frame)
     local SubPageName
     local SubPageName
    local SubPageTitle
    local Title
    local Content
    local pre = mw.html.create('pre')
    local DoubleLF = string.char(10) .. string.char(10)
     if not frame.args[1] then
     if not frame.args[1] then
         SubPageName='Esempio'
         SubPageName='Esempio'
Riga 10: Riga 15:
         SubPageName=frame.args[1]
         SubPageName=frame.args[1]
     end
     end
     return mw.title.getCurrentTitle().prefixedText .. '/' .. SubPageName .. mw.title.getCurrentTitle().prefixedText:getContent()
 
     Title =  mw.title.getCurrentTitle()
    SubPageTitle = mw.title.makeTitle(Title.namespace, Title.text .. '/' .. SubPageName)
 
    local Intro = 'Questo esempio è automaticamente generato tramite script LUA a partire dal codice di esempio presente in <code>[[' .. SubPageTitle.prefixedText .. ']]</code>'
    local CodeString = '=== Il codice ==='
    local ReturnString = '=== restituisce ==='
 
Content = SubPageTitle:getContent()
if not Content then
return "La pagina <code>[[" .. SubPageTitle.prefixedText .. "]]</code> non esiste: non è possibile estrarre il testo e generare l'esempio"
else
pre
:css( 'width', '65%' )
:wikitext(mw.text.nowiki(Content))
    return  Intro .. DoubleLF .. CodeString .. DoubleLF .. tostring(pre) .. DoubleLF  .. ReturnString .. DoubleLF .. frame:expandTemplate{ title = SubPageTitle }
    end
end
function p.TableFromArray(AllRows)
local Table = mw.html.create('table')
local First
local Tr
local Cell
for _, Row in pairs(AllRows) do
Tr = mw.html.create('tr')
First = true
for _, Field in pairs(Row) do
if First then
First = false
Cell = mw.html.create('th')
if (type(Field) == "table") then
Cell
:attr('id', Field[1])
:attr('title', Field[1])
:wikitext(Field[2])
else
Cell
:wikitext(Field)
end
else
Cell = mw.html.create('td')
if #Field > 1 then
List = mw.html.create('ul')
for _, Item in pairs(Field) do
LI = mw.html.create('li')
LI:wikitext(Item)
List:node(LI)
end
Cell:node(List)
else
Cell
:wikitext(Field[1])
end
end
Tr:node(Cell)
end
Table:node(Tr)
end
--[==[if mw.wikibase.getEntity() then
Text = "Modifica i dati nella [[:datatrek:Item:" .. mw.wikibase.getEntityIdForCurrentPage() .. "|pagina della entità]] su ''DataTrek''"
else
Text = "Impossibile trovare l'entità collegata"
end
Table
  :css('font-size', 'smaller')
      :css('text-align', 'right')
      :css('margin', '1px')
      ]==]
    --return tostring(Table)
    return Table
end
function p.NoWiki(frame)
    return mw.text.nowiki(frame.args[1])
end
--- Returns the color to use in CSS for the text depending on the luminance
-- of the background
-- @param BackColor The hex code of the background color
-- @return name of the color
function p.TextColor(frame)
local hex = string.sub(frame.args[1], 2)
local R, G, B
local L
    if hex:len() == 3 then
      --return (tonumber("0x"..hex:sub(1,1))*17)/255, (tonumber("0x"..hex:sub(2,2))*17)/255, (tonumber("0x"..hex:sub(3,3))*17)/255
      R = tonumber("0x"..hex:sub(1,1))*17/255
      G = tonumber("0x"..hex:sub(2,2))*17/255
      B = tonumber("0x"..hex:sub(3,3))*17/255
    else
    R = tonumber("0x"..hex:sub(1,2))/255
    G = tonumber("0x"..hex:sub(3,4))/255
    B = tonumber("0x"..hex:sub(5,6))/255
    end
   
    L = (R * 0.299 + G * 0.587 + B * 0.114) --/ 256
    if L < 0.5 then
    return "white"
    else
    return "black"
    end
end
end
return p
return p

Versione delle 22:08, 7 apr 2021

Il modulo FunzioniGeneriche contiene la definizione di funzioni utili per tutta la wiki in generale

Funzioni

EsempioTemplate

serve per inserire nella pagina di un template sia il codice che il rendering di un esempio catturato da una sottopagina.

SVGValidate

Inserisce il pulsante di validazioni di una grafica in SVG, vedere GitPull Task 1108.

Information icon4.svg
Avviso di trasclusione da /man
Il testo riportato qui sopra è la documentazione del modulo e non ne fa parte, ma è trascluso da Il modulo vero e proprio inizia qui sotto.

-- Keyword: wikitrek
local p = {} --p stands for package

function p.EsempioTemplate(frame)
    local SubPageName
    local SubPageTitle
    local Title
    local Content
    local pre = mw.html.create('pre')
    local DoubleLF = string.char(10) .. string.char(10)

    if not frame.args[1] then
        SubPageName='Esempio'
    else
        SubPageName=frame.args[1]
    end

    Title =  mw.title.getCurrentTitle()
    SubPageTitle = mw.title.makeTitle(Title.namespace, Title.text .. '/' .. SubPageName)

    local Intro = 'Questo esempio è automaticamente generato tramite script LUA a partire dal codice di esempio presente in <code>[[' .. SubPageTitle.prefixedText .. ']]</code>'
    local CodeString = '=== Il codice ==='
    local ReturnString = '=== restituisce ==='

	Content = SubPageTitle:getContent()
	
	if not Content then
		return "La pagina <code>[[" .. SubPageTitle.prefixedText .. "]]</code> non esiste: non è possibile estrarre il testo e generare l'esempio"
	else
		pre
			:css( 'width', '65%' )
			:wikitext(mw.text.nowiki(Content))
    	return  Intro .. DoubleLF .. CodeString .. DoubleLF .. tostring(pre) .. DoubleLF  .. ReturnString .. DoubleLF .. frame:expandTemplate{ title = SubPageTitle }
    end
end
function p.TableFromArray(AllRows)
	local Table = mw.html.create('table')
	local First
	local Tr
	local Cell
	
	for _, Row in pairs(AllRows) do
		Tr = mw.html.create('tr')
		First = true
		for _, Field in pairs(Row) do
			if First then
				First = false
				Cell = mw.html.create('th')
				if (type(Field) == "table") then
					Cell
						:attr('id', Field[1])
						:attr('title', Field[1])
						:wikitext(Field[2])
				else
					Cell
						:wikitext(Field)
				end
			else
				Cell = mw.html.create('td')
				if #Field > 1 then
					List = mw.html.create('ul')
					for _, Item in pairs(Field) do
						LI = mw.html.create('li')
						LI:wikitext(Item)
						List:node(LI)
					end
					Cell:node(List)
				else
					Cell
						:wikitext(Field[1])
				end
			end
			
			Tr:node(Cell)
		end
		Table:node(Tr)
	end
	
	--[==[if mw.wikibase.getEntity() then
		Text = "Modifica i dati nella [[:datatrek:Item:" .. mw.wikibase.getEntityIdForCurrentPage() .. "|pagina della entità]] su ''DataTrek''"
	else
		Text = "Impossibile trovare l'entità collegata"
	end
	
	Table
	   :css('font-size', 'smaller')
       :css('text-align', 'right')
       :css('margin', '1px')
       ]==]
    --return tostring(Table)
    return Table
end
	
function p.NoWiki(frame)
    return mw.text.nowiki(frame.args[1])
end
--- Returns the color to use in CSS for the text depending on the luminance
-- of the background
-- @param BackColor The hex code of the background color
-- @return name of the color
function p.TextColor(frame)
	local hex = string.sub(frame.args[1], 2)
	local R, G, B
	local L
	
    if hex:len() == 3 then
      --return (tonumber("0x"..hex:sub(1,1))*17)/255, (tonumber("0x"..hex:sub(2,2))*17)/255, (tonumber("0x"..hex:sub(3,3))*17)/255
      R = tonumber("0x"..hex:sub(1,1))*17/255
      G = tonumber("0x"..hex:sub(2,2))*17/255
      B = tonumber("0x"..hex:sub(3,3))*17/255
    else
    	R = tonumber("0x"..hex:sub(1,2))/255
    	G = tonumber("0x"..hex:sub(3,4))/255
    	B = tonumber("0x"..hex:sub(5,6))/255
    end
    
    L = (R * 0.299 + G * 0.587 + B * 0.114) --/ 256
    if L < 0.5 then
    	return "white"
    else
    	return "black"
    end
		
end
return p