Module:Cite RSI

local p = {} local args

--- Helper function checking if a substring is in a string -- -- @param needle string - Value to search for -- @param haystack string - String to search in -- -- @return bool - True if found local function stringContains( needle, haystack ) return string.find( mw.ustring.lower( haystack ), needle, 1, true ) end

local function sanitizeURL( url ) local santizedURL

if stringContains('robertsspaceindustries.com', url) then -- Remove 'www.' from the link santizedURL = mw.ustring.gsub(url, 'www%.', '') mw.log(santizedURL) else -- Add "https://robertsspaceindustries.com/" to url if it is not present santizedURL = 'https://robertsspaceindustries.com/' .. url end

return santizedURL end

--- Determine which type of RSI website it is -- -- @param url string - RSI website URL -- -- @return string - Site type or nil if unknown local function getType( url ) local type

if stringContains( '/comm-link/', url ) then type = 'Comm-Link' elseif stringContains( '/galactapedia/', url ) then type = 'Galactapedia' elseif stringContains( '/spectrum/', url ) then type = 'Spectrum' elseif stringContains( '/pledge/', url ) then type = 'Pledge Store' elseif stringContains( '/starmap', url ) then type = 'Starmap' elseif stringContains( '/issue-council', url ) then type = 'Issue Council' elseif stringContains( 'support.robertsspaceindustries', url ) then type = 'Knowledge Base' else type = nil end

return type end

local function getTitle( url, type ) local titleCase = require('Module:String2').title local titleText local throwError = function return error( 'Title can not be generated, please fill in the text parameter.') end

-- Auto title generation, might not be accurate since special characters -- are escaped in the URL --	-- e.g. /comm-link/transmission/14710-Starfarer-Q-A-Part-1 if type == 'Comm-Link' then titleText = string.match(url, '/comm%-link/%a+/%d+-(.+)') -- e.g. /article/0j46Lzl8xm-torral-aggregate elseif type == 'Galactapedia' then titleText = string.match(url, '/article/%w+-(.+)') -- NOTE: Only support threads at the moment -- e.g. /thread/star-citizen-alpha-3-13-0-live-7319707-patch-notes elseif type == 'Spectrum' then titleText = string.match(url, '/thread/%w+-(.+)') -- e.g. /hc/en-us/categories/360000295274-Updates-and-Info elseif type == 'Knowledge Base' then titleText = string.match( url, '/hc/.+/.+/%d+-(.+)') -- Will still try its best to extact a title else local urlParts = mw.text.split(url, '[/]') titleText = urlParts[#urlParts] end

if titleText and titleText ~= '' then -- Replace hyphens with space titleText = string.gsub(titleText, '-', ' ') -- Format into title case titleText = titleCase{args={titleText}} else throwError end

return titleText end

-- NOTE: Don't have internal link support yet function p.main( frame ) if args == nil then args = require( 'Module:Arguments' ).getArgs( frame ) end

local url = sanitizeURL(args.url) local type = getType(url) local date = args.accessdate local title local output if args.text then title = args.text else title = getTitle(url, type) end -- Maybe there is a smarter way? output = '' .. ' ' ..		'[' .. url .. ' ' .. title .. ']'	if type then output = output .. '. ''' .. type ..  	end if date then output = output .. '. Retrieved ' .. date end

return output end return p