Jump to content

Module:ApplyLinkAnnotations

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by N8wilson (talk | contribs) at 21:38, 11 June 2021 (a little doc'n and restructured pattern based on newlines (b/c wikitext uses them so we can too I think)). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

local p = {};
local s = require("Module:String")

function p.replaceLinks(markup)
  -- provided for convenience to make console testing easier for development
  --
  -- matches any wikilinks that are
  --    1. at the beginning of a list item 
  --    2. with no existing annotation (or any text) following them
  --
  -- must run twice because match utilizes the newlines on *both* sides
  -- of wikilinks and thus, "consumes" those chars during the first match
  -- such that every-other line is ineligible for matching until the second 
  -- run. (which does the same thing but for all the other lines)

  -- Test cases:
  -- =p.replaceLinks("\n*[[TEst|teST]] \n* [[name]]\n*[[link]] - with existing annotation\n* [[for|a friend]]  \n*[[t]]\n")

  local anls, cnt1, cnt2

  anls, cnt1 = string.gsub(markup, "\n%*%s?%[%[([^%]]*)%]%]%s*\n", "\n* {{Annotated link |%1}}\n")
  anls, cnt2 = string.gsub(anls,   "\n%*%s?%[%[([^%]]*)%]%]%s*\n", "\n* {{Annotated link |%1}}\n")

  mw.log("Replaced " .. cnt1+cnt2 .. " links without existing annotations")

  return anls
end

function p.toSeeAlsoList(frame)
  return p.replaceLinks(frame.args[1])
end

return p