Jump to content

Module:Sandbox/Wnt/WikiLink

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Wnt (talk | contribs) at 21:13, 1 April 2013 (Adding dewiki function). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

 ---- This module is meant to be processed by putting "{{#invoke:WikiLinkEverything|main}}" into an existing page of flat text to deliver all Wikilinked text (nowiki) for you to copy and paste.  For starting a new page from a Special: index that you can't directly access for example, or a list of place names most of which should exist etc.

local p = {}

function p.main(frame)
   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 input=page.getContent(page) else return "error didn't get the page contents :(" end
   input=mw.ustring.gsub(input,"{{#invoke:WikiLinkEverything|main}}","") --we don't want loops, etc.
   input=mw.ustring.gsub(input,"(%S+)","[[%1]]")
   return frame.preprocess(frame, "<nowiki>" .. input .. "</nowiki>")
end

function p.dewiki(frame)
   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
   text=mw.ustring.gsub(text,"%[%[([^[]-)%]%]","%1")
   if nowiki then text="<nowiki>" .. text .. "</nowiki>" end
   return frame.preprocess(frame,text)
end

return p

-- I wonder if there's a way to put this directly back into an open edit window...