Module:GetInfoboxData

-- -- Module:GetInfoboxData -- local Self = {}                            -- table of functions local Helper = require("Module:Helper")    -- basic helper functions local ustr = mw.ustring                    -- quick/short access to ustring table

-- -- GetData -- Self.GetData = function(frame) return Self.GetDataMain(Helper.GetArgs(frame), frame) end

function Self.GetDataMain(args, frame) local page = args[1] or "Copper Bar" args[1] = nil local content = mw.title.new(page):getContent assert(content, "Failed to find content on page: "..page) return Self.ParseContent(args, content, frame) end

function Self.ParseContent(args, content, frame) repeat local redirect = ustr.match(content, "^#[Rr][Ee][Dd][Ii][Rr][Ee][Cc][Tt]%s*%[%[(.-)%]%]") if redirect then content = mw.title.new(redirect):getContent assert(content, "Failed to find content on page: "..redirect) end until not redirect local infobox for template in ustr.gmatch(content, "%b{}") do       infobox = ustr.match(template, "^%{.-%W[Ii][Nn][Ff][Oo][Bb][Oo][Xx][%s_]+%w+[^|]-(%|.*)%}%}$") if infobox then break end end assert(infobox, "Failed to find an infobox in the following content. "..content) local DataFields = {} local field = "" local str local first = Self.NextChar(infobox) while first and first == "|" do       str = ustr.match(infobox, "^%|[^%{%[%<%|]*") infobox = ustr.sub(infobox, ustr.len(str) + 1) field = field..str first = Self.NextChar(infobox) while first and first ~= "|" do           str = nil if first == "{" then str = ustr.match(infobox, "^%b{}[^%{%[%<%|]*") elseif first == "[" then str = ustr.match(infobox, "^%b[][^%{%[%<%|]*") elseif first == "<" then str = ustr.match(infobox, "^%b<>[^%{%[%<%|]*") end if not str then str = ustr.match(infobox, "^.[^%{%[%<%|]*") end infobox = ustr.sub(infobox, ustr.len(str) + 1) field = field..str first = Self.NextChar(infobox) end local k,v = ustr.match(field, "^%|%s*(.-)%s*=%s*(.-)%s*$") if k then DataFields[k] = v       end field = "" end local getdata = args[2] or "title" args[2] = nil if DataFields[getdata] then if frame then frame.args = args return frame:preprocess(DataFields[getdata]) else return DataFields[getdata] end else error(getdata) return "Unable to find data" end end function Self.NextChar(str) return ustr.match(str, "^.") end

-- -- Required for Modules to function -- return Self