Module:ApplyLinkAnnotations
Usage
See documentation at the {{AnnotatedListOfLinks}} template which calls this module.
local p = {};
local regex = "(:*%**)%*%s*%[%[([^%]|:][^%]:]*[^%]|:])%]%]%s*"
local repl = "%1* {{Annotated link |%2}}"
function p.replaceLinksInUnorderedList(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 case(s?):
-- =p.replaceLinksInUnorderedList("\n*[[TEst|teST]] \n:* [[name]]\n::*[[link]] - with existing annotation\n::* [[for|a friend]] \n*[[t]]\n")
local anls, cnt1, cnt2
-- pattern and replacement updated locally to require newlines
-- on both sides
local regex_line = '\n' .. regex .. '\n'
local repl_line = '\n' .. repl .. '\n'
anls, cnt1 = mw.ustring.gsub(markup, regex_line, repl_line)
anls, cnt2 = mw.ustring.gsub(anls, regex_line, repl_line)
mw.log("Replaced " .. cnt1+cnt2 .. " links without existing annotations")
return anls
end
function p.bySubstitutionInUnorderedList(frame)
return p.replaceLinksInUnorderedList(frame.args[1])
end
function p._testline(line)
-- testing function used to evaluate a single line against the pattern(regex)
-- signature designed to accommodate Module:UnitTests:equals
-- so frame args are NOT extracted, a raw string is expected
local res, cnt = mw.ustring.gsub(line, '^'..regex..'$', repl)
return res
end
return p
Content Disclaimer
Informasi ini disarikan dari Wikipedia dan disajikan kembali untuk tujuan edukasi. Konten tersedia di bawah lisensi CC BY-SA 3.0. Kami tidak bertanggung jawab atas ketidakakuratan data yang bersumber dari kontribusi publik tersebut.
- The information displayed on this website is sourced in part or in whole from Wikipedia and has been adapted for the purpose of restating it. We strive to provide accurate and relevant information, however:
- There is no guarantee of absolute accuracy. Wikipedia is an open, collaborative project that can be edited by anyone, so information is subject to change.
- It is not intended to constitute professional advice. The content displayed is for informational and educational purposes only. For important decisions (e.g., medical, legal, or financial), please consult a professional.
- Content copyright. Wikipedia is licensed under the Creative Commons Attribution-ShareAlike License (CC BY-SA). This means that content may be reused with appropriate attribution and shared under a similar license.
- Responsible use. Any risk arising from the use of information from this website is entirely the responsibility of the user.