Module:Infoboxes

From GamingAlliance - GTA RP Wiki

local p = {}



--Vars-- local _fieldTags = {"<title", "<media", "<image", "<audio", "<video", "<data", "<caption"}


--SUPORTING FUNCTIONS--

--Infobox Functions--

--generateInfoboxExample--

--Makes a good guess at whether a page is an infobox template local function isInfobox(pageContent) return string.match(pageContent, "<infobox") end

--Makes a good guess at whether a string of text contains field data for an infobox local function isFieldData(line) for i, tag in ipairs(_fieldTags) do if string.match(string.lower(line), tag) then return true end end return false end

--WIP (not functional) local function parseLine(line) matches = {} end

--Makes a good guess at whether a field is an image local function isImage(line) return string.match(string.lower(line), "<image") end

--Makes a good guess at whether a field is the title local function isTitle(line) return string.match(string.lower(line), "<title") end

--Makes a good guess at whether a line opens a <group> tag local function isGroup(line) return string.match(string.lower(line), "<group") end

--Makes a good guess at whether a field is a status field local function isStatus(line) return string.match(string.lower(line), "status") end

--Checks if infobox preview should be generated local function useInfobox(frame) return frame.args[2] ~= "noib" and frame.args[3] ~= "noib" end

--Checks if example usage should be generated local function useExample(frame) return frame.args[2] ~= "noex" and frame.args[3] ~= "noex" end




--[[For use in Infobox template /doc pages: Automatically generates a usage example in an Infobox /doc page based on the tags in the corresponding Infobox template. ]]-- function p.generateInfoboxExample(frame)

--[[Check that both no-use options aren't specified, because if they both are, it makes running all this code pointless because it's not outputting anything. ]]-- if useInfobox(frame) or useExample(frame) then --Get page name passed in as first arg and strip "/doc" from it if it's there local pageName = string.gsub(frame.args[1], "/doc", "")

--[[Create a title object with that page name and store the raw contents of that page in pageContent if the page exists ]]-- local pageContent = require("Module:Common").getContent(pageName, "Template")

--Return if there's no page content because why bother? if not pageContent then --Error: Page doesn't exist return "ERROR: PAGE TITLE " .. pageName .. " DOESN'T EXIST OR PAGE IS EMPTY" end

--[[Initialize variables to store each stage of content parsing.

exampleConcatTable & infoBoxConcatTable: Used as stacks to store pieces of the docOutput before they get concatenated into docOutput because adding directly to docOutput each loop is much slower due to Lua garbage collecting immutable variables

previousGroup: Flag that gets set to true when the most recent parsed tag is a "group" tag, then set to false again when parsing a non-group tag. This is a workaround to not output multiple linebreaks when there are 2 or more "group" tags parsed consecutively.

infoBox: Will store the text specific to building the sample infobox at the last step of the process for code readability.

usageExample: Will store the text specific to displaying the example

infobox in

 tags for code readability.
				
			docOutput: For storing the final output for the doc page to be
				serve as a return value for code readability.
		]]--
		
		local exampleConcatTable = {}
		local infoBoxConcatTable = {}
		local previousGroup = false
		
		local infoBox = ""
		local usageExample = useExample(frame) and "Example Usage:<br><pre>{{" .. pageName .. "\n" or ""
		local docOutput = ""
		
		--Make sure (trust that) the page contains an infobox based on an opening tag
		if isInfobox(pageContent) then
			
			--[[Split the raw pageContent by \r and \n and push strings to the stack
				based on the tag on that line.
					(This isn't the most robust solution because tags not being
					properly separated by a new line can easily break this, but this
					can be made more robust in the future)
			]]--
			for s in string.gmatch(pageContent, "[^\r\n]+") do
				if isFieldData(s) then
					local firstQuote = string.find( s, '"' )
					if firstQuote then
					
						firstQuote = firstQuote + 1
						
						local source = string.sub(s, firstQuote, string.find(s, '"', firstQuote+1)-1)
						local example = ""
						
						if isImage(s) then
							example = "Placeholder.jpg"
							
						elseif isTitle(s) then
							example = pageName
						elseif isStatus(s) then
							example = "Active"
						else
							example = "Example"
						end
						
						if useExample(frame) then
							table.insert(exampleConcatTable, "|" .. source .. "=" .. example .. "\n")
						end
						
						if useInfobox(frame) and not string.match(string.lower(s), "role") then
							infoBoxConcatTable[source] = isImage(s) and "[[File:" .. example .. "]]" or example
						end
						
						previousGroup = false
					end
					
				elseif useExample(frame) and isGroup(s) then
					if not previousGroup then
						table.insert(exampleConcatTable, "\n")
						previousGroup = true
					end
				
				elseif string.match(s, "</infobox>") then
					break
				end
			end
			
			--Finally concatenate all the strings in the stack(s) into docOutput
			
			if useExample(frame) then
				usageExample = usageExample .. table.concat(exampleConcatTable) .. "}}

"

end

if useInfobox(frame) then infoBox = frame:expandTemplate{ title=pageName, args=infoBoxConcatTable} .. "\n" end

docOutput = infoBox .. usageExample else --Error: There wasn't an infobox based on the search criteria return "ERROR: NO INFOBOX FOUND" end

--Print that pretty doc to the page :) return docOutput end end

return p