FANDOM


local p = {}
 
local tc = '[ %%!"%$&\'%(%)%*,%-%./:;=%?@\\%^_`~%+%d%a]' --[ %!"$&'()*,\-./0-9:;=?@A-Z\\\^_`a-z~+\u0080-\uFFFF]
local nc = '[ _%d%a-]' --[ _0-9A-Za-z\x80-\xff-]
 
p.legalTitleChars = tc
p.legalNamespaceChars = nc
 
local function p1(text, b1, b2) -- "ns:page (context)"
    local pattern = '('..tc..tc..'-) ?'..b1..tc..'+'..b2
    return select(3, mw.ustring.find(
        text,
        '^'..nc..'+:'..pattern..'$'
    )) or select(3, mw.ustring.find(
        text,
        '^'..pattern..'$'
    ))
end
 
local function p2(text, c) -- "ns:page (context), context"
    local pattern = '('..tc..tc..'-) ?%('..tc..'+'..'%)'..c..tc..'+'
    return select(3, mw.ustring.find(
        text,
        '^'..nc..'+:'..pattern..'$'
    )) or select(3, mw.ustring.find(
        text,
        '^'..pattern..'$'
    ))
end
 
local function p3(text, c) -- "ns:page, context"
    local pattern = '('..tc..tc..'-)'..c..tc..'+'
    return select(3, mw.ustring.find(
        text,
        '^'..nc..'+:'..pattern..'$'
    )) or select(3, mw.ustring.find(
        text,
        '^'..pattern..'$'
    ))
end
 
local function p4(text) -- "ns:page"
    return select(3, mw.ustring.find(
        text,
        '^'..nc..'+:('..tc..tc..'-)$'
    ))
end
 
function p.pipeTrick(frame)
    local text = frame.args and frame.args[1] or tostring(frame)
    if not text or text == '' then return '' end
    text = mw.ustring.gsub(text, '^:', '')
 
    local res
    res = p1(text, '%(', '%)') -- (standard brackets)
    if res then return res, 'p1 b1' end
    res = p1(text, '(', ')') -- (double-width brackets)
    if res then return res, 'p1 b2' end
 
    res = p2(text, ', ') -- (standard comma)
    if res then return res, 'p2 c1' end
    res = p2(text, ',') -- (double-width comma)
    if res then return res, 'p2 c2' end
 
    res = p3(text, ', ') -- (standard comma)
    if res then return res, 'p3 c1' end
    res = p3(text, ',') -- (double-width comma)
    if res then return res, 'p3 c2' end
 
    res = p4(text)
    if res then return res, 'p4' end
    return text, 'none'
end
 
 
function p.tests()
    local tests = {
        {"title", "title"},
        {"title ", "title "},
        {" title", " title"},
        {" title ", " title "},
        {"title, con", "title"},
        {"title, con ", "title"},
        {" title, con", " title"},
        {" title, con ", " title"},
        {"title  , con", "title  "},
        {"title  , con ", "title  "},
        {" title  , con", " title  "},
        {" title  , con ", " title  "},
        {"title, con, con", "title"},
        {"title, con, con ", "title"},
        {" title, con, con", " title"},
        {" title, con, con ", " title"},
        {"title (par)", "title"},
        {"title (par) ", "title (par) "},
        {" title (par)", " title"},
        {" title (par) ", " title (par) "},
        {"title(par)", "title"},
        {"title(par) ", "title(par) "},
        {" title(par)", " title"},
        {" title(par) ", " title(par) "},
        {"title  (par)", "title "},
        {"title  (par) ", "title  (par) "},
        {" title  (par)", " title "},
        {" title  (par) ", " title  (par) "},
        {", con", ", con"},
        {", con ", ", con "},
        {" , con", " "},
        {" , con ", " "},
        {"title (par)(par)", "title"},
        {"title (par)(par) ", "title (par)(par) "},
        {" title (par)(par)", " title"},
        {" title (par)(par) ", " title (par)(par) "},
        {"title (par) (par)", "title"},
        {"title (par) (par) ", "title (par) (par) "},
        {" title (par) (par)", " title"},
        {" title (par) (par) ", " title (par) (par) "},
        {"title (par), con", "title"},
        {"title (par), con ", "title"},
        {" title (par), con", " title"},
        {" title (par), con ", " title"},
        {"title, con (par)", "title, con"},
        {"title, con (par) ", "title"},
        {" title, con (par)", " title, con"},
        {" title, con (par) ", " title"},
        {"ns:title (par)", "title"},
        {"ns:title (par) ", "title (par) "},
        {" ns:title (par)", "title"},
        {" ns:title (par) ", "title (par) "},
        {"ns:title", "title"},
        {"ns:title ", "title "},
        {" ns:title", "title"},
        {" ns:title ", "title "},
        {":ns:title", "title"},
        {":ns:title ", "title "},
        {" :ns:title", "ns:title"},
        {" :ns:title ", "ns:title "},
        {": ns:title", "title"},
        {"::ns:title", ":ns:title"},
        {"::ns:title ", ":ns:title "},
        {" ::ns:title", ":ns:title"},
        {" ::ns:title ", ":ns:title "},
        {":: ns:title", ": ns:title"},
        {": : ns:title", " ns:title"},
        {" : : ns:title", " : ns:title"},
        {"title (p:ar)", "title"},
        {"title (p:ar) ", "title (p:ar) "},
        {" title (p:ar)", " title"},
        {" title (p:ar) ", " title (p:ar) "},
        {"(par)", "(par)"},
        {"(par) ", "(par) "},
        {" (par)", " "},
        {" (par) ", " (par) "},
        {", con", ", con"},
        {", con ", ", con "},
        {" , con", " "},
        {" , con ", " "},
        {"ns:(par)", "ns:"},
        {"ns:(par) ", "(par) "},
        {" ns:(par)", " ns:"},
        {" ns:(par) ", "(par) "},
        {"ns: (par)", " "},
        {"ns: (par) ", " (par) "},
        {" ns: (par)", " "},
        {" ns: (par) ", " (par) "},
        {"ns: , con (par)", " , con"},
        {"ns: (par), con", " "},
        {"ns:title (par) , con", "title (par) "},
        {"ns:(par)", "ns:"},
        {"title  (", "title  ("},
        {"title, con  (", "title"},
    }
    local tab = mw.html.create('table'):addClass('article-table'):newline()
    tab:tag('tr')
       :tag('th'):wikitext('Good?'):done()
       :tag('th'):wikitext('Input'):done()
       :tag('th'):wikitext('Output'):done()
       :tag('th'):wikitext('Expected'):done()
    local total, trueCount = 0, 0
    for i, v in ipairs(tests) do
        total = total+1
        local test, expect = unpack(v)
        local res, m = p.pipeTrick(test)
 
        local tr = tab:tag('tr'):newline()
        tr:tag('td'):wikitext(tostring(res == expect))
        tr:newline()
        tr:tag('td'):wikitext('\''..test..'\'')
        tr:newline()
        tr:tag('td'):wikitext('\''..res..'\'')
        tr:newline()
        tr:tag('td'):wikitext('\''..expect..'\'')
        tr:newline()
 
        if res==expect then trueCount = trueCount+1 end
    end
    tab:tag('tr')
       :tag('th'):wikitext(mw.ustring.format('%d / %d', trueCount, total)):done()
    return tab:newline()
end
 
return p

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.