Jump to content

Module:Infobox functions

From WhiskerWiki

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

local cargo = mw.ext.cargo -- for cargo queries if needed
local cf = require("Module:Custom_functions")



local ibf = {}


function ibf.create_infobox_small(root, title, html_theme_class)
    -- Begin creating the html table infobox template
    root = mw.html.create('table')
        :addClass('infobox_small')
        :addClass('infobox_small' .. html_theme_class)

    -- add title
    root:tag('tr')
        :tag('th')
        :addClass('infobox_small-title')
        -- add additional styles based on taxon
        :addClass('infobox_small-title' .. html_theme_class)
        :attr('colspan', 2)
        :wikitext(title)
        :done()
        
        -- Add spacer row after
        root:tag('tr')
            :tag('td')
                :addClass('spacer-row')
                :attr('colspan', colspan or 2)
            :done()
        
    return root
end 






function ibf.create_infobox(root, title, subtitle, html_theme_class)
    -- Begin creating the html table infobox template
    root = mw.html.create('table')
        :addClass('infobox')
        :addClass('infobox' .. html_theme_class)

    -- Prepare title and subtitle content
    local content = mw.html.create('')
    content:wikitext(title)
    if subtitle and subtitle ~= '' then
        -- Add the subtitle directly after the title, within the same <th> element
        content
            :newline()
            :tag('div') -- Use a div for subtitle for more control
                :addClass('infobox-subtitle')
                :wikitext(subtitle)
                :allDone()
    end

    -- Add title (and subtitle) to the infobox
    root:tag('tr')
        :tag('th')
            :addClass('infobox-title')
            -- add additional styles based on taxon
            :addClass('infobox-title' .. html_theme_class)
            :attr('colspan', 2)
            :node(content) -- Use the prepared content that includes both title and subtitle
            :done()

    -- Add spacer row after the title/subtitle
    root:tag('tr')
        :tag('td')
            :addClass('spacer-row')
            :attr('colspan', 2)
        :done()

    return root
end





function ibf.add_subtitle(root, text, html_theme_class, colspan)
    if text then
	    root:tag('tr')
	        :tag('th')
	            :addClass('infobox-subtitle')
	            :addClass('infobox-subtitle' .. html_theme_class)
	            :wikitext(text)
	            :attr('colspan', colspan or 2)
	            :css('text-align', 'center')
	        :done()
	        
	    -- Add spacer row after
        root:tag('tr')
            :tag('td')
                :addClass('spacer-row')
                :attr('colspan', colspan or 2)
            :done()
            return root
    	else return root
	end	
end





function ibf.add_header(root, text, html_theme_class, colspan)
    if text then
        -- Add spacer row before header
        root:tag('tr')
            :tag('td')
                :addClass('spacer-row') -- Use a different class if styling differs from the after-header spacer
                :attr('colspan', colspan or 2)
            :done()

        -- Add header row
        root:tag('tr')
            :tag('th')
                :addClass('infobox-header')
                :addClass('infobox-header' .. html_theme_class)
                :wikitext(text)
                :attr('colspan', colspan or 2)
                :css('text-align', 'center')
            :done()

        -- Add spacer row after header (as before)
        root:tag('tr')
            :tag('td')
                :addClass('spacer-row')
                :attr('colspan', colspan or 2)
            :done()

        return root
    else 
        return root
    end
end





function ibf.add_image(root, image, html_theme_class)
    if image ~= nil and image ~= "" then
	    -- check if the image is a URL from an external host instead of a file
	    local is_url = string.match(image, "http://") or string.match(image, "https://") ~= nil
	    local td = root:tag('tr'):tag('td')
	    td:addClass('infobox-image')
	        :addClass('infobox-image' .. html_theme_class)
	        :attr('colspan', 2)
	    if is_url then
	        td:wikitext(image)
	    else
	        td:wikitext(string.format('[[File:%s|300px|frameless|center]]', image))
	    end
	    td:done()
	    
	    -- Add spacer row after
        root:tag('tr')
            :tag('td')
                :addClass('spacer-row')
                :attr('colspan', colspan or 2)
            :done()
	    return root
    else return root
	end
end




function ibf.add_row(root, name, value, html_theme_class, format, link_text)
    -- Return root immediately if either name or value are nil or empty
    if not (name and name ~= "" and value and value ~= "") then
        return root
    end
    
    -- parse text so that it is formatted correctly
    if format == "page" or format == "ext_link" or format == "italic page" then
        value = cf.parse_arguments(value, format, link_text)
    else
        value = cf.parse_arguments(value)
    end

    local row_count = 0
    row_count = row_count + 1
    local row_type_class = (row_count % 2 == 0) and 'infobox-row-even' or 'infobox-row-odd'
    root:tag('tr')
        :tag('td')
            :addClass(row_type_class)
            :addClass(row_type_class .. html_theme_class)
            :wikitext('<b>' .. name .. '</b>')
        :done()
        :tag('td')
            :addClass(row_type_class)
            :addClass(row_type_class .. html_theme_class)
            :wikitext(value)
        :done()

    return root
end






function ibf.add_spanning_row(root, text, html_theme_class, format, link_text)
    if text and text ~= '' then
        if format == "page" then
            text = cf.parse_arguments(text, format)
        end
        root:tag('tr')
            :tag('td')
                :attr('colspan', '2')
                :addClass('infobox-row')
                :addClass('infobox-row' .. html_theme_class)
                :css('text-align', 'center') -- This line centers the text
                :wikitext(text)
                :done()
        return root
    else 
        return root
    end
end



return ibf
Cookies help us deliver our services. By using our services, you agree to our use of cookies.