Module:Sandbox/Wnt/WikiLink
Appearance
< Module:Sandbox | Wnt
This module is invoked by {{#invoke:WikiLink|function|parameters}}.
Functions
The functions that can be invoked are:
- dewiki: converts [[Link]] to Link, [[Link|Text]] to Text.
- keepfile: Use before dewiki (i.e. {{#invoke:WikiLink|main|function=keepfile dewiki|text}}) to preserve images from being turned into text. Otherwise images become the alt= text or, if that is lacking, whatever is in the last field of the [[File:|]] or [[Image:|]] block.
- delink: converts [http://Link Text] to Text.
- wikiall: converts "Sequence of words" to [[Sequence]] [[of]] [[words]]
- wikiline: also converts a sequence of words to wikilinks, but all words on one line of text end up as a single wikilink. Note that a single line break will be taken out in the display (all the words will appear on one line) but still counts for purposes of grouping which words become one link.
- main: does nothing by itself. For this and all functions, component words of the parameter function will be applied afterward.
Parameters
- function=: allows the specification of multiple functions, which are done in order after the function specified (not that the order should usually matter...). Thus {{#invoke:WikiLink|dewiki|function=delink|text}} will first dewiki, then delink the text.
- nowiki=: anything nonblank leads to the <nowiki> tag enclosing the output.
- text=: the text to process. It may be convenient to #invoke Module:Page to generate this in some cases. The text= parameter must be used if your text might have an = in it - otherwise it will be interpreted as the name and value of some long and unused parameter.
Bugs
- Note that a stray "[[" will prevent interpretation of the module call if it is written in the #invoke directly; however this does not happen if it is transcluded.
local p = {}
function proc(text,fcn) -- processes text sequentially according to a list of functions separated by spaces fcn
text=text or ""
fcn=fcn or ""
repeat
fcnext=mw.ustring.match(fcn,"(%S+)")
if fcnext then
fcn=mw.ustring.gsub(fcn,fcnext,"") -- function names SHALL not contain pattern chars
if fcnext=="dewiki" then
-- protect all single [ and ] by temporarily converting to a password. This allows links to remain inside file text while doing only a dewiki
text=mw.ustring.gsub(text,"([^%[])%[([^%[])","%1<Module:WikiLink internal lsbracket token>%2")
text=mw.ustring.gsub(text,"^%[([^%[])","<Module:WikiLink internal lsbracket token>%1")
text=mw.ustring.gsub(text,"([^%[])%[$","%1<Module:WikiLink internal lsbracket token>")
text=mw.ustring.gsub(text,"([^%]])%]([^%]])","%1<Module:WikiLink internal rsbracket token>%2")
text=mw.ustring.gsub(text,"^%]([^%]])","<Module:WikiLink internal rsbracket token>%1")
text=mw.ustring.gsub(text,"([^%]])%]$","%1<Module:WikiLink internal rsbracket token>")
-- process File: and Image: links, using alt text when available
text=mw.ustring.gsub(text,"%[%[File:[^%[%]]-|%s*alt=([^%[%]|]-)|[^%[%]]-%]%]","%1") -- case for File: where alt= text is present but not at end
text=mw.ustring.gsub(text,"%[%[Image:[^%[%]]-|%s*alt=([^%[%]|]-)|[^%[%]]-%]%]","%1") -- case for Image: where alt= text is present but not at end
text=mw.ustring.gsub(text,"%[%[File:[^%[%]]-|%s*alt=([^%[%]|]-)%]%]","%1") -- case for File: where alt= text is present at end
text=mw.ustring.gsub(text,"%[%[Image:[^%[%]]-|%s*alt=([^%[%]|]-)%]%]","%1") -- case for Image: where alt= text is present at end
text=mw.ustring.gsub(text,"%[%[[^%[%]]-|([^%[%]|]-)%]%]","%1") -- link, text separated by "|". Handles case of File: when no alt= is specified, -assuming- last field is the legend
text=mw.ustring.gsub(text,"%[%[([^%[%]|]-)%]%]","%1") -- link with no funny |s at all
debuglog= mw.ustring.gsub(text,"(.)","%1 ")
-- deprotect all tokens
text=mw.ustring.gsub(text,"<Module:WikiLink internal lsbracket token>","[")
text=mw.ustring.gsub(text,"<Module:WikiLink internal rsbracket token>","]")
elseif fcnext=="delink" then
text=mw.ustring.gsub(text,"%[%s*http://%S*%s+([^%]%[]+)%]","%1")
elseif fcnext=="wikiall" then
text=mw.ustring.gsub(text,"(%S+)","[[%1]]")
elseif fcnext=="wikiline" then
text=mw.ustring.gsub(text,"([^\n]+)","[[%1]]")
end -- if fcnext==etc.
end -- if fcnext
until not fcnext
return text..debuglog --- when all functions are used up (or no valid function given) return the input text straight
end
function p.main(frame,fcn) -- gets the parameters and sets up to call proc
local args=frame.args
local parent=frame.getParent(frame)
if parent then pargs=parent.args else pargs={} end
local text=args.text or args[1] or pargs.text or pargs[1] or ""
local nowiki=args.nowiki or pargs.nowiki
fcn=(fcn or "") .. (args["function"] or pargs["function"] or "")
--local page=mw.title.getCurrentTitle() --- in the long run this cheesy tactic won't fly - I should rewrite as a proper module and recommend Module:Page invocation in the #invoke instead.
--if page then text=page.getContent(page) else return "error didn't get the page contents :(" end
text=proc(text,fcn)
if nowiki then text="<nowiki>" .. text .. "</nowiki>" end
return frame.preprocess(frame,text)
end
--- the function "parameter" is added to the beginning of the list of functions to be performed as specified by "function =".
function p.dewiki(frame)
return p.main(frame,"dewiki ")
end
function p.delink(frame)
return p.main(frame,"delink ")
end
function p.wikiall(frame)
return p.main(frame,"wikiall ")
end
function p.wikiline(frame)
return p.main(frame,"wikiline ")
end
return p