http://ephemeralfilm.info/a/Module:Detect_singular/history?feed=atom&Module:Detect singular - Revision history2024-03-29T14:28:42ZRevision history for this page on the wikiMediaWiki 1.36.2http://ephemeralfilm.info/index.php?title=Module:Detect_singular&diff=37300&oldid=prevJJR: 1 revision imported: Slavko Vorkapich2022-03-21T02:55:02Z<p>1 revision imported: Slavko Vorkapich</p>
<table style="background-color: #fff; color: #202122;" data-mw="interface">
<tr class="diff-title" lang="en">
<td colspan="1" style="background-color: #fff; color: #202122; text-align: center;">← Older revision</td>
<td colspan="1" style="background-color: #fff; color: #202122; text-align: center;">Revision as of 02:55, 21 March 2022</td>
</tr><tr><td colspan="2" class="diff-notice" lang="en"><div class="mw-diff-empty">(No difference)</div>
</td></tr></table>JJRhttp://ephemeralfilm.info/index.php?title=Module:Detect_singular&diff=35206&oldid=prevJJR: Created page with "local p = {} local getArgs = require('Module:Arguments').getArgs local yesNo = require('Module:Yesno') local getPlain = require('Module:Text').Text().getPlain -- function to..."2022-02-26T23:32:13Z<p>Created page with "local p = {} local getArgs = require('Module:Arguments').getArgs local yesNo = require('Module:Yesno') local getPlain = require('Module:Text').Text().getPlain -- function to..."</p>
<table style="background-color: #fff; color: #202122;" data-mw="interface">
<tr class="diff-title" lang="en">
<td colspan="1" style="background-color: #fff; color: #202122; text-align: center;">← Older revision</td>
<td colspan="1" style="background-color: #fff; color: #202122; text-align: center;">Revision as of 23:32, 26 February 2022</td>
</tr><tr><td colspan="2" class="diff-notice" lang="en"><div class="mw-diff-empty">(No difference)</div>
</td></tr></table>JJRhttp://ephemeralfilm.info/index.php?title=Module:Detect_singular&diff=37299&oldid=prevw>Hike395: implement named arguments: singular, plural, link2022-01-28T07:30:24Z<p>implement named arguments: singular, plural, link</p>
<p><b>New page</b></p><div>local p = {}<br />
local getArgs = require('Module:Arguments').getArgs<br />
local yesNo = require('Module:Yesno')<br />
local getPlain = require('Module:Text').Text().getPlain<br />
<br />
-- function to determine whether "sub" occurs in "s"<br />
local function plainFind(s, sub)<br />
return mw.ustring.find(s, sub, 1, true)<br />
end<br />
<br />
-- function to count the number of times "pattern" (a regex) occurs in "s"<br />
local function countMatches(s, pattern)<br />
local _, count = mw.ustring.gsub(s, pattern, '')<br />
return count<br />
end<br />
<br />
-- Determine whether a string is singular or plural (i.e., it represents one<br />
-- item or many)<br />
-- Arguments:<br />
-- origArgs[1]: string to process<br />
-- origArgs.no_comma: if false, use commas to detect plural (default false)<br />
-- origArgs.parse_links: if false, treat wikilinks as opaque singular objects (default false)<br />
function p._main(origArgs)<br />
origArgs = type(origArgs) == 'table' and origArgs or {}<br />
local args = {}<br />
-- canonicalize boolean arguments<br />
for key, default in pairs({no_comma=false,parse_links=false,any_comma=false,no_and=false}) do<br />
if origArgs[key] == nil then<br />
args[key] = default<br />
else<br />
args[key] = yesNo(origArgs[key],default)<br />
end<br />
end<br />
local checkComma = not args.no_comma<br />
local checkAnd = not args.no_and<br />
local rewriteLinks = not args.parse_links<br />
local anyComma = args.any_comma<br />
local s = origArgs[1] -- the input string<br />
if not s then<br />
return nil -- empty input returns nil<br />
end<br />
s = tostring(s)<br />
if plainFind(s,'forcedetectsingular') then -- magic data string to return true<br />
return true<br />
end<br />
if plainFind(s,'forcedetectplural') then -- magic data string to return false<br />
return false<br />
end<br />
-- count number of list items<br />
local numListItems = countMatches(s,'<%s*li')<br />
-- if exactly one, then singular, if more than one, then plural<br />
if numListItems == 1 then<br />
return true<br />
end<br />
if numListItems > 1 then<br />
return false<br />
end<br />
-- if "list of" occurs inside of wlink, then it's plural<br />
if mw.ustring.find(s:lower(), '%[%[[^%]]*list of[^%]]+%]%]') then<br />
return false<br />
end<br />
-- replace all wikilinks with fixed string<br />
if rewriteLinks then<br />
s = mw.ustring.gsub(s,'%b[]','WIKILINK') <br />
end<br />
-- Five conditions: any one of them can make the string a plural<br />
local hasBreak = mw.ustring.find(s,'<%s*br')<br />
-- For the last 4, evaluate on string stripped of wikimarkup<br />
s = getPlain(s)<br />
local commaPattern = anyComma and '[,;]' or '%D[,;]%D' -- semi-colon similar to comma<br />
local hasComma = checkComma and mw.ustring.find(s, commaPattern)<br />
local hasAnd = checkAnd and mw.ustring.find(s,'[,%s]and%s')<br />
local hasBullets = countMatches(s,'%*+') > 1<br />
local multipleQids = mw.ustring.find(s,'Q%d+[%p%s]+Q%d+') -- has multiple QIDs in a row<br />
-- return bool: is it singular?<br />
return not (hasComma or hasAnd or hasBreak or hasBullets or multipleQids)<br />
end<br />
<br />
function p._pluralize(args)<br />
args = type(args) == 'table' and args or {}<br />
local singularForm = args[3] or args.singular or ""<br />
local pluralForm = args[4] or args.plural or ""<br />
local link = args[5] or args.link<br />
if link then<br />
link = tostring(link)<br />
singularForm = '[['..link..'|'..singularForm..']]'<br />
pluralForm = '[['..link..'|'..pluralForm..']]'<br />
end<br />
if args[2] then<br />
return pluralForm<br />
end<br />
local detect = p._main(args)<br />
if detect == nil then<br />
return "" -- return blank on complete failure<br />
end<br />
return detect and singularForm or pluralForm<br />
end<br />
<br />
function p.main(frame)<br />
local args = getArgs(frame)<br />
-- For template, return 1 if singular, blank if plural or empty<br />
local result = p._main(args)<br />
if result == nil then<br />
return 1<br />
end<br />
return result and 1 or ""<br />
end<br />
<br />
function p.pluralize(frame)<br />
local args = getArgs(frame)<br />
return p._pluralize(args)<br />
end<br />
<br />
return p</div>w>Hike395