From WikiChip
Module:supercomputer
Revision as of 19:07, 3 February 2019 by David (talk | contribs) (Created page with "local p = {} local origArgs function p.add_file_prefix(str) if string.match(string.lower(str), '^file:') then return str else return 'File:' .. str end end fun...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Documentation for this module may be created at Module:supercomputer/doc

local p = {}
local origArgs

function p.add_file_prefix(str)
  if string.match(string.lower(str), '^file:') then
    return str
  else
    return 'File:' .. str
  end
end

function p.add_separator(box, title)
  box:tag('tr')
           :tag('td')
           :attr('class', 'header')
           :attr('colspan', '2')
           :wikitext(title)
end

function p.add_entry(value, right)
  local e = mw.html.create('tr')
  e:tag('td')
    :attr('class', 'label')
    :wikitext(value)
  e:node(right)
  return e   
end

function has_arg(name)
	-- The argument can exist and be empty or not exist at all
	return string.len(origArgs[name] or '') > 0
end

function arg(name)
	if has_arg(name) then
		return origArgs[name]
	end
	return ''
end

function p.set_val(prop, val)
	p.frame:preprocess('{{#set: ' .. prop .. '=' .. val .. '}}')	
end

function p.supercomputer(frame)
	
	p.frame = frame
    if frame == mw.getCurrentFrame() then
        origArgs = frame:getParent().args
    else
        origArgs = frame.args
    end
    
    
    local infobox = mw.html.create('table')	
	p.set_val('instance of', 'supercomputer')
	
	-- same for category
    infobox:wikitext('[[Category:all supercomputers]]')
	

    infobox
        :attr('class', 'infobox')
        
    infobox
    	:tag('tr')
			:tag('td'):attr('colspan', '2')
			:tag('small'):attr('style', 'float: left; font-weight: bold;')
            :wikitext('[[Special:FormEdit/supercomputer/' .. frame:preprocess('{{FULLPAGENAME}}') .. '|<i class="fa fa-edit"></i>Edit Values]]')

    -- header
    if has_arg('name') then
		infobox
			:tag('tr')
			:tag('td')
			:attr('class', 'header-main')
			:attr('colspan', '2')
			:wikitext(arg('name'))
		p.set_val('name', arg('name'))
    end

    -- image
	if has_arg('image') then
		local image = p.add_file_prefix(arg('image'))
		local image_size = has_arg('image size') and arg('image size') or "200px" 
		infobox
		:tag('tr')
		  :tag('td')
		    :attr('style', 'text-align: center;')
		    :attr('colspan', '2')
		    -- make sure it's the ogimage
		    :wikitext('[[' .. image .. '|link=|class=wikichip_ogimage|' .. image_size .. ']]')
		
		p.set_val('main image', image)

      -- add a caption, if provided
      if has_arg('caption') then
		infobox
			:tag('tr')
			:tag('td')
			:attr('style', 'text-align: center;')
			:attr('colspan', '2')
			:wikitext(arg('caption'))
		p.set_val('main image caption', arg('caption'))
      end
	elseif not has_arg('no image') then
      infobox
        :tag('tr')
          :tag('td')
            :attr('style', 'text-align: center;')
            :attr('colspan', '2')
            :wikitext('[[File:no photo (ic).svg|link=|200px]]')
    end

    -- ####################################################################
    -- some general info first
    p.add_separator(infobox, 'General Info')
 
	return infobox
end

return p