Module:Solar eclipse: Difference between revisions

Content deleted Content added
No edit summary
Undid revision 1283027027 by Grapesoda22 (talk)
 
(27 intermediate revisions by 14 users not shown)
Line 1:
local eclipsep = {}
local args = {}
 
local data_module_prefix = "Module:Solar eclipse/db/"
local infobox_image = nil -- lazy load
 
local function ifnotemptyInfoboxImage(s,a,bimage)
if (simage and simage ~= '') then
if infobox_image == nil then
return a
infobox_image = require('Module:InfoboxImage').InfoboxImage
else
end
return b
return infobox_image{args = {
image = image,
size = '320px',
sizedefault = 'frameless',
upright = 1
}}
end
return nil
end
 
local function ifnotempty(s, a, b)
return (s and s ~= '') and a or b
end
 
local function ifexist(page)
if not page then return false end
if mw.title.new(page).exists then return true end
return false
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 nil
return success and result or date_str
end
 
local function parsecoord(frame, s)
local lat, NS, lon, EW = s:match('^%s*(%d+%.?%d*)%s*([NS])%s*(%d+%.?%d*)%s*([EW])%s*$')
if lat and NS and lon and EW then
return frame:expandTemplate{
title = 'coord',
args = {lat, NS, lon, EW, 'type:landmark'}
}
end
return s
end
 
local function parsekm(frame, s)
if(s and s:match('^%s*[%d][%d.]*%s*$')) then
return frame:expandTemplate{ title = 'convert', args = {s, 'km', 'mi', abbr = 'on'} }
else
if(s and s ~= '') then
return s .. ' km'
else
return nil
end
end
end
 
local function parsetime(s)
if(s and s ~= '') then
local min = s:match('^%s*(%d+)m%s*%d+s%s*$')
local sec = s:match('^%s*%d+m%s*(%d+)s%s*$')
if( min and sec ) then
return tostring(tonumber(min)*60 + tonumber(sec)) .. '&nbsp;s' ..
' (' .. min .. '&nbsp;min ' .. sec .. '&nbsp;s)'
end
end
return s
end
 
local function cataloglink(c, y, m, d)
if(cy, andm, d = tonumber(cy), tonumber(m), thentonumber(d)
local if y1 = math.floor( (tonumber(yc) -and 1)y / 10 )and *m 10and +d 1then
d = (d < 10) and ('0' .. d) or d
local y2 = y1 + 99
m = (m < 10) and ('0' .. m) or m
return '[http://eclipse.gsfc.nasa.gov/SEcat5/SE' .. tostring(y1) .. '-' .. tostring(y2) .. '.html ' .. c .. ']'
return '[https://eclipse.gsfc.nasa.gov/SEsearch/SEdata.php?Ecl=+' .. y .. m .. d .. ' ' .. c .. ']'
else
return c
Line 36 ⟶ 85:
end
 
local function loadsolardbcreateLink(slabel, suffix)
if not label or label == '' then
local yearstr = s:match('^%s*([%d][%d][%d][%d])[A-Z][a-z][a-z][%d][%d]%s*$') or ''
return ''
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
 
local function loadsolardb(frame, s)
local yearstr = s:match('^%s*(%d%d%d%d)[A-Z][a-z][a-z]%d%d%s*$') or ''
local function setarg(k, v)
if(v) thenand args[k]v ~= v'') endthen
v = mw.ustring.gsub(v, '^%s*%-(%d)', '−%1')
args[k] = v
end
end
if( yearstr ~= '' ) then
Line 46 ⟶ 127:
if (ifexist(dbpage)) then
local data = mw.loadData(dbpage)
local dargs = data[s] or error('Error: unknown date ' .. s .. ' in Infobox solar eclipse' , 0)
setarg('date', parsedate(dargs['y'], dargs['m'] or dargs['m3'] or dargs['m2'], dargs['d'] or dargs['d2']))
setarg('image', (dargs['Ph'] and dargs['Ph'] ~= '') and '[[File:' .. dargs['Ph'] .. '|320px]]' or nil)
Line 53 ⟶ 134:
setarg('map_caption', 'Map')
setarg('type_ref', '')
setarg('cat', cataloglink(dargs['Cat'], dargs['y'], dargs['m'] or dargs['m3'] or dargs['m2'], dargs['d'] or dargs['d2']) )
setarg('nature', dargs['Ty'])
setarg('gamma', dargs['Gam'])
Line 61 ⟶ 142:
setarg('saros_total', dargs['Max'])
setarg('max_eclipse_ref', '')
setarg('duration', parsetime(dargs['Dur']))
setarg('___location', '')
setarg('coords', parsecoord(frame,dargs['Loc']))
setarg('max_width', parsekm(frame,dargs['Wid'] and dargs['Wid'] .. ' km'))
setarg('times_ref', '')
setarg('start_partial', dargs['TiPB'])
Line 73 ⟶ 154:
setarg('end_total', dargs['TiTE'])
setarg('end_partial', dargs['TiPE'])
setarg('previous', '')
setarg('next', '')
end
end
Line 78 ⟶ 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]]")
local bgcolor = args['background'] or args['bgcolour'] or ''
local mapstr = ifnotempty(args['map'],
"<div style='padding-bottom:0.5em;'>" ..
(InfoboxImage(args['map']) or '') .. ifnotempty(args['map_caption'],
"<div style='line-height:1.2em; padding-top:0.1em;'><small>" ..
(args['map_caption'] or '') .. "</small></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 = "#854739" },
annular = { label = "'''Annular eclipse'''", color = "#4C4CB1" },
hybrid = { label = "'''Hybrid eclipse'''", color = "#006600" }
}
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:9088%;",
["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'] or ''),
["imagestyle"] = "padding-bottom:0.5em;",
["caption"] = args['caption'] or '',
["captionstyle"] = "padding-top:0.1em; line-height:1.2em; font-size:90%;",
["headerstyle"] = "background:#eee; font-size:105%;",
["labelstyle"] = "padding:0 0.5em 0 0; line-height:1.1em;",
["datastyle"] = "padding:0; line-height:1.2em; vertical-align:middle;",
["data1"] = mapstr,
------------- Type of eclipse and saros -------------
["header2label4"] = "Type of[[Gamma (eclipse)|Gamma]]" .. (args['type_ref'] or ''),
["label3"] = "Nature",
["data3"] = args['nature'] or '',
["label4"] = "[[Gamma (solar eclipse)|Gamma]]",
["data4"] = args['gamma'] or '',
["label5"] = "[[Magnitude of eclipse|Magnitude]]",
Line 141 ⟶ 247:
["header20"] = "References",
["label21"] = "[[Saros (astronomy)|Saros]]",
["data21"] = (args['saros'] or '')
.. " (" .. (args['saros_sequence'] or '') .. " of " .. (args['saros_total'] or '') .. ")",
["label22"] = "Catalog # (SE5000)",
["data22"] = args['cat'] or '',
} }
 
end
 
function eclipsep.box(frame)
args = require('Module:Arguments').getArgs(frame, {
wrappers = 'Template:SolarInfobox eclipsesolar dbeclipse'
})
 
if( args['2'] and args['2'] ~= '') then
loadsolardb(frame,args['2'])
elseif( args['1'] and args['1'] ~= '') then
loadsolardb(frame,args['1'])
end
Line 163 ⟶ 269:
end
 
return eclipsep