Module:Coderef
Jump to navigation
Jump to search
This module aims to replace the entirely template-based code powering the overly complex Template:Sourcecode and Template:Refsrc (and their friends, Template:Function and Template:Reffunc). The eventual goal is to make those templates merely invoke this module.
Keep in mind that as this is a work in progress, this should not be used outside of testing/example pages yet.
Usage
| Source | Output |
|---|---|
{{#invoke:Coderef|line|allmain.c|42}}
|
allmain.c, line 42 |
{{#invoke:Coderef|line|allmain.c|42|game=slashem|version=SLASH'EM 0.0.7E7F2}}
|
allmain.c in SLASH'EM 0.0.7E7F2, line 42 |
{{#invoke:Coderef|line|allmain.c|42|dir=src|game=unnethack|rev=30a07ee}}
|
allmain.c in UnNetHack revision 30a07ee, line 42 |
{{#invoke:Coderef|lineref|allmain.c|42}}
|
[1] |
{{#invoke:Coderef|lineref|allmain.c|42|game=slashem|version=SLASH'EM 0.0.7E7F2}}
|
[2] |
{{#invoke:Coderef|lineref|allmain.c|42|comment=interesting comment goes here}}
|
[3] |
{{#invoke:Coderef|lineref|allmain.c|42|name=magic_cookie}}
|
[4] |
<ref name='magic_cookie'/>
|
[4] |
Example references
- ↑ allmain.c, line 42
- ↑ allmain.c in SLASH'EM 0.0.7E7F2, line 42
- ↑ allmain.c, line 42: interesting comment goes here
-- vim: ft=lua
local p = {}
function mklinelink(where, revision, path, line, text)
if where == "nethack" or where == "slashem" then
if revision then
return "[[Source:" .. revision .. "/" .. path .. "#line" .. line .. "|" .. text .. "]]"
else
return "[[Source:" .. path .. "#line" .. line .. "|" .. text .. "]]"
end
elseif where == "unnethack" then
local urlbase = "https://github.com/UnNetHack/UnNetHack/blob/"
local urltail = revision .. "/" .. path .. "#L" .. line
local link = "[" .. urlbase .. urltail .. " " .. text .. "]"
return tostring(mw.html.create("span"):addClass("plainlinks"):wikitext(link))
else
error("mklinelink: unknown game type '" .. where .. "'")
end
end
function mklinelinktext(where, version, revision, file, line)
local versionstring = ""
if (where == "nethack" or where == "slashem") and version then
versionstring = " in " .. version
elseif where == "unnethack" then
if version then
versionstring = " in " .. version
else
versionstring = " in UnNetHack revision " .. revision
end
end
return file .. versionstring .. ", line " .. line
end
function p.line(frame)
-- allow explicit named args to override the positional ones
local file = frame.args.file or frame.args[1]
local line = frame.args.line or frame.args[2]
local where = frame.args.game or "nethack"
local version = frame.args.version
local revision = frame.args.rev or frame.args.version
local dir = frame.args.dir
local path
if version and #version == 0 then
version = nil
end
if revision and #revision == 0 then
revision = nil
end
if not dir or #dir == 0 then
path = file
else
path = dir .. "/" .. file
end
local linktext = mklinelinktext(where, version, revision, file, line)
return mklinelink(where, revision, path, line, linktext)
end
function p.lineref(frame)
local reftext = p.line(frame)
local refargs = {}
if frame.args.comment and #frame.args.comment > 0 then
reftext = reftext .. ": ''" .. frame.args.comment .. "''"
end
if frame.args.name and #frame.args.name > 0 then
refargs.name = frame.args.name
end
return frame:extensionTag("ref", reftext, refargs)
end
return p