From WikiChip
Difference between revisions of "Module:package"

(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...")
(No difference)

Revision as of 12:20, 11 August 2018

To add this template, simple add
{{package}}
to the page and save. The chip infobox will have a small [Edit Values] button at the top-right corner which can be used to add values using a form.
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 add_header(box, str)
      box
        :tag('tr')
          :tag('td')
            :attr('class', 'header')
            :attr('colspan', '2')
            :wikitext(str)
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)
	return origArgs[name]
end

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

function p.core(frame)
	
	p.frame = frame
    if frame == mw.getCurrentFrame() then
        origArgs = frame:getParent().args
    else
        origArgs = frame.args
    end
	
	
    local infobox = mw.html.create('table')

    infobox
        :attr('class', 'infobox')
        
    infobox
    	:tag('tr')
			:tag('td'):attr('colspan', '2')
			:tag('small'):attr('style', 'float: left; font-weight: bold;')
            :wikitext('[[Special:FormEdit/package/' .. 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('[[name::' .. arg('name') .. ']]')
    end


    -- ####################################################################
    -- some general info first
    p.add_separator(infobox, 'General Info')
    
    -- designer
    if has_arg('developer') then
      	local designers = mw.html.create('td'):attr('style', 'width: 99%;')
      	designers:wikitext('[[designer::' .. arg('developer') .. '| ]][[' .. arg('developer') .. ']]')
		for i = 2, 10 do
			if not has_arg('developer ' .. i) then break end
			designers:wikitext(', [[designer::' .. arg('developer ' .. i) .. '| ]][[' .. arg('developer ' .. i) .. ']]')
		end
   	  infobox:node(p.add_entry('Designer', designers))
	end
	
	-- release date
	if has_arg('first launched') or has_arg('first announced') then
	  local release = mw.html.create('td')
	  if has_arg('first announced') then
	    release:wikitext('[[first announced::' .. arg('first announced') .. ']] (announced)')
	  end
	  if has_arg('first launched') then
	  	if has_arg('first announced') then
	  	  release:wikitext('<br>')	
  		end
	    release:wikitext('[[first launched::' .. arg('first launched') .. ']] (launched)')
	  end
	  infobox:node(p.add_entry('Introduction', release))
	end
	
	-- market segment
    if has_arg('market') then
		p.set_val('market segment', arg('market'))
      	local markets = mw.html.create('td'):wikitext(arg('market'))
      	for i = 2, 5 do
  			if not has_arg('market ' .. i) then break end
			p.set_val('market segment', arg('market ' .. i))
      		markets:wikitext(', ' .. arg('market ' .. i))
      	end
   	  	infobox:node(p.add_entry('Market', markets))
	end
	
	-- microarch
    if has_arg('microarch') then
      local microarchs = mw.html.create('td')
      p.set_val('microarchitecture', arg('microarch'))
      microarchs:wikitext('[[' .. mw.ustring.lower(arg('designer')) .. '/microarchitectures/' ..
      		mw.ustring.lower(arg('microarch')) .. '|' .. arg('microarch') .. ']]')
      for i = 2, 10 do
      	if not has_arg('microarch ' .. i) then break end
      	p.set_val('microarchitecture', arg('microarch ' .. i))
        microarchs:wikitext(', [[' .. mw.ustring.lower(arg('designer')) .. '/microarchitectures/' ..
      		mw.ustring.lower(arg('microarch ' .. i)) .. '|' .. arg('microarch ' .. i) .. ']]')
      end
   	  infobox:node(p.add_entry('Microarchitecture', microarchs))
	end
	
	-- chipset
    if has_arg('chipset') then
		p.set_val('chipset', arg('chipset'))
		local chipset = mw.html.create('td'):wikitext(arg('chipset'))
		for i = 2, 10 do
			if not has_arg('chipset ' .. i) then break end
			p.set_val('chipset', arg('chipset ' .. i))
			chipset:wikitext(', ' .. arg('chipset ' .. i))
      	end
		infobox:node(p.add_entry('Chipset', chipset))
	end
	
	
	-- tdp
	if has_arg('tdp') then
		p.set_val('tdp', arg('tdp'))
		local tdp =  mw.html.create('td'):wikitext(arg('tdp'))
		for i = 2, 10 do
	  		if not has_arg('tdp ' .. i) then break end
			p.set_val('tdp', arg('tdp ' .. i))
	  		tdp:wikitext(', ' .. arg('tdp ' .. i))
		end
	  	infobox:node(p.add_entry('<abbr title="Thermal Design Power">TDP</abbr>', tdp))
	end
	
    -- ####################################################################
    -- package info
    
    if has_arg('package name') or has_arg('package type') then
	    p.add_separator(infobox, 'Package')
    end
    
	-- package
	if has_arg('package name') then
		local package_name =  mw.html.create('td'):wikitext(arg('package name'))
		for i = 2, 10 do
	  		if not has_arg('package name ' .. i) then break end
	  		package_name:wikitext(',<br> ' .. arg('package name ' .. i))
		end
	  	infobox:node(p.add_entry('Name', package_name))
	end
	
	-- type
	if has_arg('package type') then
	  infobox:node(p.add_entry('Type', mw.html.create('td'):wikitext( arg('package type'))))
	end	
	
	-- contacts
	if has_arg('package contacts') then
	  infobox:node(p.add_entry('Contacts', mw.html.create('td'):wikitext( arg('package contacts'))))
	end
	
	-- dimension
    if has_arg('dimension') then
      local dimension = mw.html.create('td'):wikitext(arg('dimension'))
      for i = 2, 10 do
      	if not has_arg('dimension ' .. i) then break end
      	dimension:wikitext(' × ' .. arg('dimension ' .. i))
      end
   	  infobox:node(p.add_entry('Dimension', dimension))
	end	
	
	-- pitch
	if has_arg('package pitch') then
	  infobox:node(p.add_entry('Pitch', mw.html.create('td'):wikitext( arg('package pitch'))))
	end
	
    -- ####################################################################
    -- socket info
    
    if has_arg('socket name') or has_arg('socket type') then
	    p.add_separator(infobox, 'Socket')
    end
	
	-- socket name
	if has_arg('socket name') then
		local socket_name =  mw.html.create('td'):wikitext(arg('socket name'))
		for i = 2, 10 do
	  		if not has_arg('socket name ' .. i) then break end
	  		socket_name:wikitext(',<br> ' .. arg('socket name ' .. i))
		end
	  	infobox:node(p.add_entry('Name', package_name))
	end
	
	-- type
	if has_arg('socket type') then
	  infobox:node(p.add_entry('Type', mw.html.create('td'):wikitext( arg('socket type'))))
	end	

    -- ####################################################################
    -- ####################################################################
    -- Successon
	if has_arg('successor') or has_arg('predecessor') then
		add_header(infobox, 'Succession')
		
		local s = infobox:tag('tr')
		local td = s:tag('td'):attr('colspan', '2')
		local d1 = td:tag('div'):attr('style', 'display: inline-flex;')
		local d2 = td:tag('div'):attr('style', 'display: inline-flex; float: right;')
		if has_arg('predecessor') then
			
		    local list
		    if has_arg('predecessor link') then
				list = '[[' .. arg('predecessor link') .. '|' .. arg('predecessor') .. ']]'
			else
				list = '[[' .. arg('predecessor') .. ']]'
			end
		    for i = 2, 10 do
		    	if not has_arg('predecessor ' .. i) then break end
		    	if has_arg('predecessor ' .. i .. ' link') then
		    		list = list .. '<br>[[' .. arg('predecessor ' .. i .. ' link') .. '|' .. arg('predecessor ' .. i) .. ']]'
	    		else
		    		list = list .. '<br>[[' .. arg('predecessor ' .. i) .. ']]'
    			end
			end
			
			d1:tag('div')
				:attr('style', 'float: left; padding-right: 10px; margin: auto 5px;')
				:wikitext('<i class="fa fa-chevron-left"></i>')
			d1:tag('div')
				:attr('style', 'float: left;')
				:wikitext(list)
		end
		if has_arg('successor') then
			local list
		    if has_arg('successor link') then
				list = '[[' .. arg('successor link') .. '|' .. arg('successor') .. ']]'
			else
				list = '[[' .. arg('successor') .. ']]'
			end
		    for i = 2, 10 do
		    	if not has_arg('successor ' .. i) then break end
		    	if has_arg('successor ' .. i .. ' link') then
		    		list = list .. '<br>[[' .. arg('successor ' .. i .. ' link') .. '|' ..
		    			arg('successor ' .. i) .. ']]'
	    		else
		    		list = list .. '<br>[[' .. arg('successor ' .. i) .. ']]'
    			end
			end
			
			d2:tag('div')
				:attr('style', 'float: left;')
				:wikitext(list)
			d2:tag('div')
				:attr('style', 'float: right; padding-left: 10px; margin: auto 5px;')
				:wikitext('<i class="fa fa-chevron-right"></i>')
		end
	end
    -- Contemporary
	if has_arg('contemporary') then
		add_header(infobox, 'Contemporary')
		
		local s = infobox:tag('tr')
		local td = s:tag('td'):attr('colspan', '2')
		
		td:attr('style', 'text-align: center;')

	    local list
	    if has_arg('contemporary link') then
			list = '[[' .. arg('contemporary link') .. '|' .. arg('contemporary') .. ']]'
		else
			list = '[[' .. arg('contemporary') .. ']]'
		end
	    for i = 2, 10 do
	    	if not has_arg('contemporary ' .. i) then break end
	    	if has_arg('contemporary ' .. i .. ' link') then
	    		list = list .. '<br>[[' .. arg('contemporary ' .. i .. ' link') .. '|' .. arg('contemporary ' .. i) .. ']]'
    		else
	    		list = list .. '<br>[[' .. arg('contemporary ' .. i) .. ']]'
			end
		end
		
		td:wikitext(list)
	end


	infobox:wikitext('[[instance of::package| ]]')
	infobox:wikitext('[[Category:all ic packages]]')
	
    return infobox
end

return p