Module:Galeria: Difference between revisions
From Neodyland Wiki
More actions
m test |
m corr |
||
| (14 intermediate revisions by 3 users not shown) | |||
| Line 2: | Line 2: | ||
local p = {} | local p = {} | ||
-- | -- parses the size value | ||
local function parseval ( value, parm2 ) | local function parseval ( value, parm2 ) | ||
local par = value or '' | local par = value or '' | ||
| Line 31: | Line 31: | ||
end | end | ||
end | end | ||
end | end -- function: parseval | ||
-- checks whether the value is text, or the next file | |||
local function iffil ( ftext, optn ) | |||
local name = mw.ustring.lower(ftext or '') ; -- trimmed | |||
local fmat = 'x'; -- assume it's text | |||
if mw.ustring.sub ( name or ' ', 1, 1 ) == "*" | |||
or mw.ustring.sub ( name or ' ', 1, 1 ) == "." then | |||
fmat = 'f' -- short code for filename | |||
elseif name ~= "" then | |||
local inx = mw.ustring.find( name .. "\n", "\n" ); | |||
name = mw.ustring.sub ( name, 1, inx - 1 ); | |||
if mw.ustring.sub (name, -5) == '.tiff' then | |||
name = mw.ustring.sub (name, 1, #name-4)..'tif'; | |||
fmat = 'f'; | |||
end | |||
if mw.ustring.sub (name, -5) == '.jpeg' then | |||
name = mw.ustring.sub (name, 1, #name-4)..'jpg'; | |||
fmat = 'f'; | |||
end | |||
if #name > 4 and | |||
mw.ustring.sub (name, -4, -4 ) == '.' then | |||
local map = {svg=1, png=1, jpg=1, gif=1, tif=1, xcf=1, pdf=1}; | |||
if map[mw.ustring.sub (name, -3)] then | |||
fmat = "f"; -- it looks like a file name | |||
end | |||
end | |||
end | |||
if optn == 'F' then fmat = mw.ustring.sub (name, 1, #name-4) end | |||
return fmat | |||
end -- function: iffil | |||
-- returns the file name without the .ext | |||
local function namprt ( filnam ) | |||
return iffil ( filnam, 'F') | |||
end -- function: namprt | |||
-- changes all occurences of "_" against " " | |||
local function pagnam ( filnam ) | |||
return mw.ustring.gsub( filnam or ' ', '_', ' ' ),_ | |||
end -- function: pagename | |||
-- prepares the display of the second param | |||
local function disp2nd ( fnam, fmat ) -- depending on parameter 'f' | |||
if iffil ( fnam ) == "f" then | |||
if fmat == '2' then | |||
return "|<div class=\"center\">{{F|" .. fnam .. "}}</div>" | |||
elseif fmat == '4' then | |||
return "|<div class=\"center\">{{F|" .. fnam .."|"..namprt(fnam).."}}</div>" | |||
elseif fmat == '5' then | |||
return "|<div class=\"center\">" .. namprt ( fnam ) .. "</div>" | |||
else | |||
return "|<div class=\"center\">" .. pagnam ( fnam ) .. "</div>" | |||
end | |||
else | |||
return "|<div class=\"center\">" .. fnam .. "</div>" | |||
end | |||
end -- function: disp2nd | |||
-- | -- main function | ||
function p.gallery ( frame ) | function p.gallery ( frame ) | ||
local | local ppar = mw.getCurrentFrame():getParent().args; | ||
local tagtab = {}; | local tagtab = {}; | ||
local instab = {}; | local instab = {}; | ||
local fmat = | local form = ppar.f or ppar.format or ""; | ||
local mode = | local fmat = ppar.f or ppar.format or "2"; | ||
local mode = ppar.m or ppar.mode or ""; | |||
local insv = ""; | local insv = ""; | ||
local strg = " | local strg = ""; | ||
tagtab [1] = "\n"; | tagtab [1] = "\n"; | ||
for _, v in ipairs( | for _, v in ipairs(ppar) do | ||
v = mw.text.trim (v); | |||
strg = strg .. "\006" .. v -- replace pipe by ACK | strg = strg .. "\006" .. v -- replace pipe by ACK | ||
end | end | ||
strg = mw.ustring.gsub( strg, "[\n]" , "\006" ); -- replace CRLF by ACK | strg = mw.ustring.gsub( strg, "[\n]" , "\006" ); -- replace CRLF by ACK | ||
local | -------------------------------- | ||
if fmat == "" then | local par2 = ppar[2] or "" | ||
if | if fmat == "" then -- when format NOT specified | ||
if | if par2 ~= "" then -- when second parm: | ||
if iffil ( par2 ) == "f" then | |||
fmat = "1" -- default format="1" | |||
else | |||
fmat = "2" -- default format="2" | |||
end | end | ||
else -- no second parm | |||
fmat = "1" | |||
end | end | ||
end | end | ||
-------------------------------- | |||
for _, v in ipairs(mw.text.split(strg , "\006") ) do | for _, v in ipairs(mw.text.split(strg , "\006") ) do | ||
if fmat == "2" then | v = mw.text.trim (v); | ||
if insv == "" then | local scnd = "" | ||
if fmat == "1" or fmat == "2" or fmat == "3" or fmat == "4" or fmat == "5" then -- param pairs | |||
if insv == "" then -- 1st value | |||
insv = v; | insv = v; | ||
else | else -- 2nd value: | ||
if v == "" then | if iffil ( v ) == "f" then -- it's a filename | ||
v = " | if pagnam ( mw.ustring.lower(insv) ) == pagnam ( mw.ustring.lower(v) ) then | ||
table.insert(instab, insv .. disp2nd ( v, fmat ) ); | |||
else -- it is the next file name | |||
table.insert(instab, insv .. disp2nd ( insv, fmat ) ); | |||
scnd = v; | |||
end | |||
else -- may be a short code | |||
if v == "" or v == "+" then | |||
table.insert(instab, insv .. disp2nd ( insv, fmat ) ); | |||
else | |||
table.insert(instab, insv .. disp2nd ( v, '3' ) ); -- just text | |||
end | |||
end | end | ||
insv = scnd; | |||
end | end | ||
else -- fmat = 0, 1 | |||
table.insert(instab, v ); -- table.insert(instab, fmat fma2 ); | |||
end -- if fmat | |||
end -- for | |||
if insv ~= "" then -- last value single? | |||
if fmat == "2" or fmat == "3" then -- make a pair | |||
table.insert(instab, insv .. disp2nd ( insv, fmat ) ); | |||
else | else | ||
table.insert(instab, | table.insert(instab, insv ); | ||
end | end | ||
end | end | ||
-- | |||
for i, v in ipairs( instab ) do | for i, v in ipairs( instab ) do | ||
if v == "" or v == "+" then | if v == "" or v == "+" then | ||
tagtab [1] = tagtab [1] .. "\n" | tagtab [1] = tagtab [1] .. "\n" -- same | ||
else | else | ||
tagtab [1] = tagtab [1] .. "\n" .. v | if v == "*" then | ||
end end | local titl = mw.title.getCurrentTitle(); | ||
v = titl.text; | |||
end | |||
if mw.ustring.sub (v, 1, 1) == "." then | |||
local titl = mw.title.getCurrentTitle(); | |||
local namp = namprt ( titl.text ); | |||
if v == "." | |||
or v == ".p" then v = namp..".png" | |||
elseif v == ".j" then v = namp..".jpg" | |||
elseif v == ".g" then v = namp..".gif" | |||
elseif v == ".s" then v = namp..".svg" | |||
elseif v == ".t" then v = namp..".tif" | |||
else v = namp..v | |||
end | |||
end | |||
tagtab [1] = tagtab [1] .. "\n" .. v -- add parm | |||
end | |||
end | |||
-- | |||
-- | -- | ||
if mode == 'n' then mode = 'nolines' | if mode == 'n' then mode = 'nolines' | ||
| Line 94: | Line 193: | ||
elseif mode == 't' then mode = 'traditional' | elseif mode == 't' then mode = 'traditional' | ||
end | end | ||
local wide = | local wide = ppar.w or ppar.width or ppar.widths or ''; | ||
local high = | local high = ppar.h or ppar.height or ppar.heights or ''; | ||
local disp = | local disp = ppar.d or ppar.dis or ppar.disp or ppar.display or ''; | ||
if wide .. high == '' and disp ~= '' then | if wide .. high == '' and disp ~= '' then | ||
wide = parseval (disp, 'w') | wide = parseval (disp, 'w') | ||
| Line 102: | Line 201: | ||
end | end | ||
tagtab [2] = mode; | tagtab [2] = mode; | ||
tagtab [3] = | tagtab [3] = ppar.c or ppar.caption or ''; | ||
tagtab [4] = | tagtab [4] = ppar.p or ppar.perrow or ''; | ||
tagtab [5] = wide; | tagtab [5] = wide; | ||
tagtab [6] = high; | tagtab [6] = high; | ||
tagtab [7] = | tagtab [7] = ppar.s or ppar.style or ''; | ||
tagtab [8] = | tagtab [8] = ppar.a or ppar.attr or ppar.class or ''; | ||
tagtab [9] = | -- tagtab [9] = ppar.t or ppar.text or ''; | ||
tagtab [10] = fmat; | tagtab [10] = fmat; | ||
if fmat == '' then | if fmat == '' then | ||
tagtab [10] = '1'; | tagtab [10] = '1'; -- default format | ||
end | |||
tagtab [2] = 'nolines'; | if form..wide..mode == '' then | ||
if ppar.m == nil and ppar.mode == nil then -- mode empty ? | |||
tagtab [2] = 'nolines'; -- only when completely missing | |||
end | end | ||
end | end | ||
return mw.getCurrentFrame():expandTemplate{ title = "G/layout", args = tagtab }; | return mw.getCurrentFrame():expandTemplate{ title = "G/layout", args = tagtab }; | ||
end | end -- function gallery | ||
-- main function gallang | |||
function p.gallang ( frame ) | |||
local gpar = frame.args; | |||
local temp = ''; | |||
if gpar.sw == "y" then temp = "y" end -- and fmat = '0' | |||
local ppar = mw.getCurrentFrame():getParent().args;-- parent parms | |||
local file = ppar.file or mw.title.getCurrentTitle().text or ""; | |||
local fmat = ppar.f or ppar.format or ""; | |||
local mode = ppar.m or ppar.mode or ""; | |||
local wide = ppar.w or ppar.width or ""; | |||
local high = ppar.h or ppar.height or ""; | |||
if ppar.sw == "y" then | |||
temp = "y" | |||
fmat = "0" | |||
end | |||
local tagt = {}; | |||
tagt [1] = "\n"; | |||
for _, v in ipairs(ppar) do | |||
v = mw.text.trim (v); | |||
if v ~= '' and file ~= '' then -- add parm | |||
if temp == "y" | |||
then tagt [1] = tagt [1] .. file .. "|lang="..v.."|''Key:'' "..v.."\n" | |||
else tagt [1] = tagt [1] .. file .. "|lang="..v.."|{{LangCapt|"..v.."}}\n" | |||
end | |||
end | |||
end | |||
-- exists param "file" and at least one lang ? | |||
if mode == 'n' then mode = 'nolines' | |||
elseif mode == 'p' then mode = 'packed' | |||
elseif mode == 'o' then mode = 'packed-overlay' | |||
elseif mode == 'h' then mode = 'packed-hover' | |||
elseif mode == 's' then mode = 'slideshow' | |||
elseif mode == 't' then mode = 'traditional' | |||
end | |||
local wide = ppar.w or ppar.width or ppar.widths or ''; | |||
local high = ppar.h or ppar.height or ppar.heights or ''; | |||
local disp = ppar.d or ppar.dis or ppar.disp or ppar.display or ''; | |||
if wide .. high == '' and disp ~= '' then | |||
wide = parseval (disp, 'w') | |||
high = parseval (disp, 'h') | |||
end | |||
tagt [2] = mode; | |||
tagt [3] = ppar.c or ppar.caption or ''; | |||
tagt [4] = ppar.p or ppar.perrow or ''; | |||
tagt [5] = wide; | |||
tagt [6] = high; | |||
tagt [7] = ppar.s or ppar.style or ''; | |||
tagt [8] = ppar.a or ppar.attr or ppar.class or ''; | |||
-- tagt [9] = ppar.t or ppar.text or ''; | |||
tagt [10]= fmat; | |||
if fmat == '' then | |||
tagt [10] = '1'; -- default format | |||
end | |||
if tagt [1] == '\n' then | |||
return -- empty | |||
else | |||
return mw.getCurrentFrame():expandTemplate{ title = "G/layout", args = tagt }; | |||
end | |||
end -- function gallang | |||
-- | -- convert size to width and height | ||
function p.converse (frame) | function p.converse (frame) | ||
local | local gpar = frame.args | ||
return parseval ( gpar[1], gpar [2] ); | |||
end | end | ||
-- remove final "px" | -- remove final "px" | ||
function p.getp (frame) | function p.getp (frame) | ||
local | local gpar = frame.args | ||
return parseval ( gpar[1], "p"); | |||
end | end | ||
function p.getw (frame) | function p.getw (frame) | ||
local | local gpar = frame.args | ||
return parseval ( gpar[1], "w"); | |||
end | end | ||
function p.geth (frame) | function p.geth (frame) | ||
local | local gpar = frame.args | ||
return parseval ( gpar[1], "h"); | |||
end | end | ||
-- change understrokes | |||
function p.pagename (frame ) | |||
local gpar = frame.args | |||
return mw.ustring.gsub( gpar[1] or ' ', '_', ' ' ),_ | |||
end | |||
-- check whether it can be a filename | |||
function p.iffile ( frame ) | |||
local gpar = frame.args | |||
return iffil ( gpar[1], gpar[2] ); | |||
end | |||
-- function filename without extension (and the "Wellcome ..." text) | |||
function p.filename ( frame ) | |||
local fnam = mw.title.getCurrentTitle().text; | |||
if mw.ustring.sub( fnam, -4 ) == '.jpg' | |||
then fnam = mw.ustring.sub( fnam, 1, #fnam-4 ); | |||
local posf = mw.ustring.find( fnam, ' Wellcome' ); | |||
if posf | |||
then fnam = mw.ustring.sub( fnam, 1, posf-1 ); | |||
end | |||
elseif mw.ustring.sub( fnam, -4, -4 ) == '.' | |||
then fnam = mw.ustring.sub( fnam, 1, #fnam-4 ); | |||
elseif mw.ustring.sub( fnam, -5, -5 ) == '.' | |||
then fnam = mw.ustring.sub( fnam, 1, #fnam-5 ); | |||
end | |||
return fnam; | |||
end | |||
return p; | return p; | ||