Module

Infobox

From Dogcraft Wiki

Revision as of 22:04, 3 October 2022 by William (talk | contribs)

Edit documentation


-- Module for generating mini diagrams of transport networks
local p = {}
local data = ""
local title = "Transport Diagram"

-- Keys for parsing lines
local lineKeys = {"diagram-title", "track-type", "tracks", "station", "track", "track2", "station2"}
-- row_type: single or double line
-- l_text: left station text
-- r_text: right station text
-- l_link: left station link
-- r_link: right station link
-- image: image to use for the line
-- secondary_image: image to use for the line if it is a double line

-- Generate the HTML for a diagram row
local function generateRow(trackType, tracks, station, station2, track, track2)
    -- Create the row HTML
    lineHtml = "<div class='diagram-row'>"

    -- Add the left station text
    lineHtml = lineHtml .. "<div class='station station-left'>" .. station .. "</div>"
    
    -- Add the track images
    lineHtml = lineHtml .. "<div class='track-container'>"
    -- Add the first image
    lineHtml = lineHtml .. "<div class='track'>[[File:TIcon " .. trackType .. " " .. track .. ".png|30px]]</div>"
    -- Add the second image if present
    if tracks == "2" then
        lineHtml = lineHtml .. "<div class='track'>[[File:TIcon " .. tracks .. " " .. track2 .. ".png|30px]]</div>"
    end
    lineHtml = lineHtml .. "</div>"

    -- Add the right station text
    lineHtml = lineHtml .. "<div class='station station-left'>" .. station2 .. "</div>"

    lineHtml = lineHtml .. "</div></div>"
    return lineHtml
end

-- Parses a line and generates the HTML for it
local function parseLine(line)
    -- Prepare values for row generation
    local trackType = "SRN"
    local tracks = "1"
    local station = ""
    local station2 = ""
    local track = ""
    local track2 = ""

    -- If the line starts with |, remove it
    if string.sub(line, 1, 1) == "|" then
        line = string.sub(line, 2)
    end

    -- Trim the line of whitespace
    line = mw.text.trim(line)

    -- Split the line into equals-sign separated key-pairs by commas
    local pairs = mw.text.split(line, ",", true)
    for i = 1, #pairs do
        -- Get the value
        local value = pairs[i]

        -- Split the key-pair into a key and a value
        local keyValue = mw.text.split(value, ":", true)
        local key = keyValue[1]
        local value = keyValue[2]
        
        -- Iterate through each possible key in lineKeys and set the appropriate values
        for _, lineKey in ipairs(lineKeys) do
            if key == lineKey then
                if (key == "diagram-title") then
                    title = value
                elseif (key == "track-type") then
                    trackType = value
                elseif (key == "tracks") then
                    tracks = value
                elseif (key == "station") then
                    station = value
                elseif (key == "station2") then
                    station2 = value
                elseif (key == "track") then
                    track = value
                elseif (key == "track2") then
                    track2 = value
                end
            end
        end
    end

    -- If both track and track-2 are blank, return ""
    if track == "" and track2 == "" then
        return ""
    end
    
    return generateRow(trackType, tracks, station, station2, track, track2)
end

-- Generates the html for provided diagram markup
local function generate()
    -- Trim data of whitespace
    data = mw.text.trim(data)

	-- Use mw.text.split to split the data into lines
    local lines = mw.text.split(data, "|")

    -- Iterate through each line, calling parseLine on it and appending the string result of that to an output data
    local output = "<div class='transport-diagram'>"
    for _, line in ipairs(lines) do
        output = output .. parseLine(line)
    end
    return "<h3 class='transport-diagram-title'>" .. title .. "</h3>" .. output .. "</div>"
end

-- The main function called on #invoke via the template
function p.transportDiagram(frame)
	local args = {}
	if frame == mw.getCurrentFrame() then
		args = frame:getParent().args
	else
		args = frame
	end
    
    -- Iterate through each key pair in args
    for key, value in pairs(args) do
        -- Add to data
        data = data .. value
    end

    return generate()
end

return p
This page was last modified on 3 October 2022, at 22:04. (4 months ago)