Module:Solar eclipse/sandbox: Difference between revisions

Content deleted Content added
No edit summary
Added support for previous/next in database
 
(10 intermediate revisions by the same user not shown)
Line 20:
end
 
local function ifnotempty(s, a, b)
if return (s and s ~= '') thenand a or b
return a
else
return b
end
end
 
Line 34 ⟶ 30:
end
 
local function parsedate(y, m, d)
local lang = mw.language.getContentLanguage()
d = (tonumber(d) < 10)d and= string.format('0' .."%02d", tonumber(d)) or (d)
m = (tonumber(m) < 10)m and= string.format('0' .."%02d", tonumber(m)) or (m)
local success, result = pcall(lang.formatDate,local lang,date_str 'F j, Y',= y .. '-' .. m .. '-' .. d)
local success, result = pcall(lang.formatDate, lang, 'F j, Y', date_str)
return success and result or nildate_str
end
 
local function parsecoord(frame, s)
local lat, NS, lon, EW = s:match('^%s*([%d][+%d.]?%d*)%s*([NS])%s*[(%d][+%d.]?%d*)%s*([EW])%s*$')
if( lat and NS and lon and EW ) then
local NS = s:match('^%s*[%d][%d.]*%s*([NS])%s*[%d][%d.]*[EW]%s*$')
return frame:expandTemplate{
local lon = s:match('^%s*[%d][%d.]*%s*[NS]%s*([%d][%d.]*)[EW]%s*$')
title = 'coord',
local EW = s:match('^%s*[%d][%d.]*%s*[NS]%s*[%d][%d.]*([EW])%s*$')
return frame:expandTemplate{ title = 'coord', args = {lat, NS, lon, EW, 'type:landmark'} }
if( lat and NS and lon and EW ) then
}
return frame:expandTemplate{ title = 'coord', args = {lat, NS, lon, EW, 'type:landmark'} }
end
else
return s
end
end
 
Line 87 ⟶ 83:
return c
end
end
 
local function createLink(label, suffix)
if not label or label == '' then
return a''
end
 
local prefix = "Solar eclipse of "
-- check if label starts with the prefix
if mw.ustring.find(label, "^" .. prefix) then
-- extract the date part from the label
local dateLabel = mw.ustring.sub(label, #prefix + 1)
local page = label -- this is the full page title to link to
 
if ifexist(page) then
return string.format('[[%s|%s]]', page, dateLabel)
else
-- if page doesn't exist, display the date
return dateLabel
end
else
-- if the label is a a link, keep it
if mw.ustring.match(label, "^%[%[.*%]%]$") then
return label
else
-- for labels without the prefix, display as plain text
return label
end
end
end
 
Line 129 ⟶ 154:
setarg('end_total', dargs['TiTE'])
setarg('end_partial', dargs['TiPE'])
setarg('previous', '')
setarg('next', '')
end
end
Line 134 ⟶ 161:
 
local function infobox(frame)
local abovestrtitlestr = ifnotempty(args['date'],
"Solar eclipse of " .. (args['date'] or ''),
"For instructions on use, see [[Template:Infobox Solar eclipse]]")
Line 143 ⟶ 170:
"<div style='line-height:1.2em; padding-top:0.1em;'>" ..
(args['map_caption'] or '') .. "</div>", '') .. '</div>')
local prevLink = createLink(args['previous'], 'solar eclipse')
local nextLink = createLink(args['next'], 'solar eclipse')
local belowstr = ''
if prevLink ~= '' and nextLink ~= '' then
belowstr = string.format('<div style="width:100%%"><div class="noprint" style="float:left">← %s</div><div class="noprint" style="float:right">%s →</div></div>',
prevLink, nextLink)
elseif prevLink ~= '' then
belowstr = string.format('<div style="width:100%%"><div class="noprint" style="text-align:left">← %s</div></div>', prevLink)
elseif nextLink ~= '' then
belowstr = string.format('<div style="width:100%%"><div class="noprint" style="text-align:right">%s →</div></div>', nextLink)
end
local eclipse_types = {
total = { label = "Total eclipse", color = "#690005" },
partial = { label = "Partial eclipse", color = "#c56b55" },
annular = { label = "Annular eclipse", color = "#5555c5" },
hybrid = { label = "Hybrid eclipse", color = "#55c555" }
}
local nature = args['nature'] and args['nature']:lower() or ''
local eclipse_config = eclipse_types[nature] or { label = "Eclipse", color = "#727272" }
local type_header = string.format(
'<span style="background-color: %s; color: white; display: block;">%s</span>',
eclipse_config.color,
eclipse_config.label
)
 
 
return frame:expandTemplate{ title = 'infobox', args = {
["bodyclass"] = "vevent",
["bodystyle"] = "width:25em; text-align:left; font-size:88%;",
["abovetitle"] = abovestrtitlestr,
["aboveclasssubheader"] = "summary"type_header,
["below"] = belowstr,
["abovestyle"] = "padding-bottom:0.25em; background:" .. bgcolor .. "; line-height:1.2em; text-align:center; font-size:115%;",
["belowstyle"] = "text-align:center; padding-top:0.5em",
------------------ Images and maps ------------------
["image"] = InfoboxImage(args['image']),
Line 157 ⟶ 213:
["data1"] = mapstr,
------------- Type of eclipse and saros -------------
["header2"] = "Type of eclipse" .. (args['type_ref'] or ''),
["label3"] = "Nature",
["data3"] = args['nature'] or '',
["label4"] = "[[Gamma (eclipse)|Gamma]]",
["data4"] = args['gamma'] or '',
Line 212 ⟶ 265:
loadsolardb(frame,args['1'])
end
 
return infobox(frame)
end