Toggle menu
Toggle preferences menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

Module:CiteGithub: Difference between revisions

From starcup wiki
No edit summary
mNo edit summary
 
(13 intermediate revisions by the same user not shown)
Line 8: Line 8:
local argPath = args[1]
local argPath = args[1]
if not argPath:startswith(FILE_PATH_PREFIX) then
if not StringEx.startswith(argPath, FILE_PATH_PREFIX) then
error("Invalid argument: string must point to a file in the starcup" ..  
error("Invalid argument: string must point to a file in the starcup" ..  
" github repository")
" github repository")
Line 14: Line 14:
end
end
local trimmedPath = StringEx.replace(argPath, "https://github.com/teamstarcup/starcup/blob/")
local trimmedPath = StringEx.replace(argPath, "https://github.com/teamstarcup/starcup/blob/", "")
local filePath = trimmedPath:sub(trimmedPath:find("/"))
local firstSlashIndex, _evilStealthReturnValue = trimmedPath:find("/")
local filePath = trimmedPath:sub(firstSlashIndex + 1)
-- trim off line number range
-- trim off line number range
local displayedFilePath = trimmedPath:sub(1, trimmedPath:find("#"))
local firstOctothorpeIndex, _evilStealthReturnValue = filePath:find("#")
firstOctothorpeIndex = firstOctothorpeIndex or 1
local displayedFilePath = filePath:sub(1, firstOctothorpeIndex - 1)
return string.format("[%s]", trimmedPath)
return string.format("[%s %s]", argPath, displayedFilePath)
end
end


return CiteGithub
return CiteGithub

Latest revision as of 07:33, 30 May 2025

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

local CiteGithub = {}
local StringEx = require("Module:StringExtension")

local FILE_PATH_PREFIX = "https://github.com/teamstarcup/starcup/blob/"

function CiteGithub.main(frame)
	local args = frame:getParent().args
	local argPath = args[1]
	
	if not StringEx.startswith(argPath, FILE_PATH_PREFIX) then
		error("Invalid argument: string must point to a file in the starcup" .. 
			" github repository")
		return
	end
	
	local trimmedPath = StringEx.replace(argPath, "https://github.com/teamstarcup/starcup/blob/", "")
	local firstSlashIndex, _evilStealthReturnValue = trimmedPath:find("/")
	local filePath = trimmedPath:sub(firstSlashIndex + 1)
	
	-- trim off line number range
	local firstOctothorpeIndex, _evilStealthReturnValue = filePath:find("#")
	firstOctothorpeIndex = firstOctothorpeIndex or 1
	local displayedFilePath = filePath:sub(1, firstOctothorpeIndex - 1)
	
	return string.format("[%s %s]", argPath, displayedFilePath)
end

return CiteGithub