Module:Sandbox/fngplg/ibox

local p = {}

function p.t (frame) local s, s1 = '', '\n\nPreview button for article comments.' local content = '' if not frame then frame = {['args'] = {}} frame.args['Author'] = 'user:fngplg' frame.args['Examples'] = 'ru.borderlands' frame.args['Description'] = 'Adds preview button to article comments.' frame.args['Updated'] = '2017.09.08' frame.args['Code'] = 'code.js' frame.args['Skins'] = 'Oasis, Monobook' frame.args['Languages'] = 'ru' frame.args['Status'] = 'beta' frame.args['Image caption'] = '' frame.args['Image file'] = '' frame.args['Image size'] = '' frame.args['Scope'] = '' frame.args['Title'] = '' frame.args['Using code by'] = '' frame.args['Other attribution'] = '' frame.args['Type'] = '' local ct = {} --current title local bt = {} --base title bt.getContent = function return s1 end ct.isSubpage = true ct.basePageTitle = bt       frame.getCurrentTitle = function  return ct end end --arguments local args = frame:getParent.args local a_author = args['Author'] local a_examples = args['Examples'] local a_description = args['Description'] local a_updated = args['Updated'] local a_code = args['Code'] local a_skins = args['Skins'] local a_languages = args['Languages'] local a_sstatus = args['Status'] local a_image_caption = args['Image caption'] local a_image_file = args['Image file'] local a_image_size = args['Image size'] local a_scope = args['Scope'] local a_title = args['Title'] local a_using_code_by = args['Using code by'] local a_other_attribution = args['Other attribution'] local a_ttype = args['Type'] local a_uselang = args['uselang'] --options --force static local a_force = args['force'] --force per param local a_fauthor = args['fAuthor'] local a_fexamples = args['fExamples'] local a_fdescription = true --args['fDescription'] local a_fupdated = args['fUpdated'] local a_fcode = args['fCode'] local a_fskins = args['fSkins'] local a_flanguages = args['fLanguages'] local a_fsstatus = args['fStatus'] local a_fimage_caption = true --args['fImage caption'] local a_fimage_file = args['fImage file'] local a_fimage_size = args['fImage size'] local a_fscope = args['fScope'] local a_ftitle = true --args['fTitle'] local a_fusing_code_by = args['fUsing code by'] local a_fother_attribution = args['fOther attribution'] local a_fttype = args['fType'] --arguments local ret = {} ret.uselang = a_uselang --or mytitle.subpageText ret.author = a_author ret.code = a_code ret.examples = a_examples ret.description = a_description ret.updated = a_updated ret.skins = a_skins ret.languages = a_languages ret.sstatus = a_sstatus ret.image_caption = a_image_caption ret.image_file = a_image_file ret.image_size = a_image_size ret.scope = a_scope ret.title = a_title ret.using_code_by = a_using_code_by ret.other_attribution = a_other_attribution ret.ttype = a_ttype local mytitle = mw.title.getCurrentTitle --p.getParent(frame) local ret1 --get data from basepage if mytitle.isSubpage and (not a_force) then content = mytitle.basePageTitle:getContent ret1 = p.process(content) if ret1 then ret = ret1 end ret.uselang = a_uselang or mytitle.subpageText ret.author = p.iif(a_fauthor, a_author, ret.author) ret.code = p.iif(a_fcode, a_code, ret.code) ret.examples = p.iif(a_fexamples, a_examples, ret.examples) ret.description = p.iif(a_fdescription, a_description, ret.description) --ret1 is free for use now ret1 = p.parseDate(ret.updated) if type(ret1) == 'table' then ret1 = ret1.normalized end ret.updated = p.iif(a_fupdated, a_updated, ret1 or ret.updated) ret.skins = p.iif(a_fskins, a_skins, ret.skins) ret.languages = p.iif(a_flanguages, a_languages, ret.languages) ret.sstatus = p.iif(a_fsstatus, a_sstatus, ret.sstatus) ret.image_caption = p.iif(a_fimage_caption, a_image_caption, ret.image_caption) ret.image_file = p.iif(a_fimage_file, a_image_file, ret.image_file) ret.image_size = p.iif(a_fimage_size, a_image_size, ret.image_size) ret.scope = p.iif(a_fscope, a_scope, ret.scope) ret.title = p.iif(a_ftitle, a_title, ret.title) ret.using_code_by = p.iif(a_fusing_code_by, a_using_code_by, ret.using_code_by) ret.other_attribution = p.iif(a_fother_attribution, a_other_attribution, ret.other_attribution) ret.ttype = p.iif(a_fttype, a_ttype, ret.ttype) end --if 1 then return '..........' .. mytitle.text .. (content or 'content') .. '..........' end --   s = s .. (ret.author or ) .. (ret.code or ) .. (ret.examples or ) ..        (ret.description or ) .. (ret.updated or ) .. (ret.skins or ) ..        (ret.languages or ) .. (ret.sstatus or ) .. (ret.image_caption or ) ..        (ret.image_file or ) .. (ret.image_size or ) .. (ret.scope or ) ..        (ret.title or ) .. (ret.using_code_by or ) .. (ret.other_attribution or '')    -- s = p.construct(ret) if frame.preprocess then s = frame:preprocess(s) end return s end

function p.construct (ret) --infobox constructor local s = '' return s end --construct

function p.process (content) --process content. return {} --link/template defender local strips = {} --patterns local pibox = '{{[iI]nfobox[ _]JavaScript/fng%s-|' --'{{([iI]nfobox[ _]JavaScript%s-|.*)}}' local pauthor = '.-|%s-Author%s-=%s-(.-)%s-[|%z]' local pcode = '.-|%s-Code%s-=%s-(.-)%s-[|%z]' local pexamples = '.-|%s-Examples%s-=%s-(.-)%s-[|%z]' local pdescription = '.-|%s-Description%s-=%s-(.-)%s-[|%z]' local pupdated = '.-|%s-Updated%s-=%s-(.-)%s-[|%z]' local pskins = '.-|%s-Skins%s-=%s-(.-)%s-[|%z]' local planguages = '.-|%s-Languages%s-=%s-(.-)%s-[|%z]' local pstatus = '.-|%s-Status%s-=%s-(.-)%s-[|%z]' local pimage_caption = '.-|%s-Image caption%s-=%s-(.-)%s-[|%z]' local pimage_file = '.-|%s-Image file%s-=%s-(.-)%s-[|%z]' local pimage_size = '.-|%s-Image size%s-=%s-(.-)%s-[|%z]' local pscope = '.-|%s-Scope%s-=%s-(.-)%s-[|%z]' local ptitle = '.-|%s-Title%s-=%s-(.-)%s-[|%z]' local pusing_code_by = '.-|%s-Using code by%s-=%s-(.-)%s-[|%z]' local pother_attribution = '.-|%s-Other attribution%s-=%s-(.-)%s-[|%z]' local pttype = '.-|%s-Type%s-=%s-(.-)%s-[|%z]' --patterns local pos = mw.ustring.find(content, pibox, 1, false) if pos == nil then --no infobox found return end local ibox = mw.ustring.match(content, '.-(%b{})', pos) --add \0 to indicate end of string ibox = mw.ustring.sub(ibox, 3, #ibox - 2) .. '\0'   local libox = ibox local lrnd for m in mw.ustring.gmatch(libox, '%b{}') do       ibox = p.strip(ibox, m, strips) end libox = ibox for m in mw.ustring.gmatch(libox, '%b[]') do       ibox = p.strip(ibox, m, strips) end local author = mw.ustring.match(ibox, pauthor)-- or '' local code = mw.ustring.match(ibox, pcode)-- or '' local examples = mw.ustring.match(ibox, pexamples)-- or '' local description = mw.ustring.match(ibox, pdescription)-- or '' local updated = mw.ustring.match(ibox, pupdated)-- or '' local skins = mw.ustring.match(ibox, pskins)-- or '' local languages = mw.ustring.match(ibox, planguages)-- or '' local sstatus = mw.ustring.match(ibox, pstatus)-- or '' local image_caption = mw.ustring.match(ibox, pimage_caption)-- or '' local image_file = mw.ustring.match(ibox, pimage_file)-- or '' local image_size = mw.ustring.match(ibox, pimage_size)-- or '' local scope = mw.ustring.match(ibox, pscope)-- or '' local title = mw.ustring.match(ibox, ptitle)-- or '' local using_code_by = mw.ustring.match(ibox, pusing_code_by)-- or '' local other_attribution = mw.ustring.match(ibox, pother_attribution)-- or '' local ttype = mw.ustring.match(ibox, pttype)-- or '' local ret = {} ret.author = p.iif(author, p.unstrip(author, strips), nil)   ret.code = p.iif(code, p.unstrip(code, strips), nil)    ret.examples = p.iif(examples, p.unstrip(examples, strips), nil)    ret.description = p.iif(description, p.unstrip(description, strips), nil)    ret.updated = p.iif(updated, p.unstrip(updated, strips), nil)    ret.skins = p.iif(skins, p.unstrip(skins, strips), nil)    ret.languages = p.iif(languages, p.unstrip(languages, strips), nil)    ret.sstatus = p.iif(sstatus, p.unstrip(sstatus, strips), nil)    ret.image_caption = p.iif(image_caption, p.unstrip(image_caption, strips), nil)    ret.image_file = p.iif(image_file, p.unstrip(image_file, strips), nil)    ret.image_size = p.iif(image_size, p.unstrip(image_size, strips), nil)    ret.scope = p.iif(scope, p.unstrip(scope, strips), nil)    ret.title = p.iif(title, p.unstrip(title, strips), nil)    ret.using_code_by = p.iif(using_code_by, p.unstrip(using_code_by, strips), nil)    ret.other_attribution = p.iif(other_attribution, p.unstrip(other_attribution, strips), nil)    ret.ttype = p.iif(ttype, p.unstrip(ttype, strips), nil)    -- --   if author then ret.author = author end    if code then ret.code = code end    if examples then ret.examples = examples end    if description then ret.description = description end    if updated then ret.updated = updated end    if skins then ret.skins = skins end    if languages then ret.languages = languages end    if sstatus then ret.sstatus = sstatus end    if image_caption then ret.image_caption = image_caption end    if image_file then ret.image_file = image_file end    if image_size then ret.image_size = image_size end    if scope then ret.scope = scope end    if title then ret.title = title end    if using_code_by then ret.using_code_by = using_code_by end    if other_attribution then ret.other_attribution = other_attribution end    -- --   table.insert(ret, author)    table.insert(ret, code)    table.insert(ret, examples)    table.insert(ret, description)    table.insert(ret, updated)    table.insert(ret, skins)    table.insert(ret, languages)    table.insert(ret, sstatus)    table.insert(ret, image_caption)    table.insert(ret, image_file)    table.insert(ret, image_size)    table.insert(ret, scope)    table.insert(ret, title)    table.insert(ret, using_code_by)    table.insert(ret, other_attribution)    -- --   ret[ --   s = author .. ':' .. code .. ':' .. examples .. ':' .. description .. ':' ..         updated .. ':' .. skins .. ':' .. languages .. ':' ..         sstatus .. ':' .. image_caption .. ':' .. image_file .. ':' ..         image_size .. ':' .. scope .. ':' .. title .. ':' ..        using_code_by .. ':' .. other_attribution    -- --s = s .. table.concat(ret, ', ') --return p.unstrip(s, strips) return ret end --process

function p.unstrip (src, strips) if (src == nil ) or (src == '') then return nil end for i = #strips, 1, -1 do       src = mw.ustring.gsub(src, strips[i].k, strips[i].v, 1) end return src end

function p.strip (src, str, strips) local lrnd lrnd = tostring(math.floor(math.random*1000000000)) table.insert(strips, {k = lrnd, v = str}) src = mw.ustring.gsub(src, p.escapeLua(str), lrnd, 1) return src end

function p.getParent (frame) local p, f = frame, frame:getParent if f then p = p.getParent(f) else return p end end

function p.iif (condition, istrue, isfalse) if condition then return istrue else return isfalse end end

function p.escapeLua (s) --https://stackoverflow.com/questions/9790688/escaping-strings-for-gsub return mw.ustring.gsub(s, '([^%w])', '%%%1') end

function p.parseDate (d) if not d then return end local mon = {jan='1', feb='2', mar='3', apr='4', may='5', jun='6', jul='7', august='8', sep='9', oct='10', nov='11', dec='12'} local delim = {' ', '.', '/'} local dt, dy, dm, dd = {}, , , '' d = mw.ustring.lower(d) for i = 1, #delim do       dy, dm, dd = mw.ustring.match(d, '(.-)%' .. delim[i] .. '(.-)%' .. delim[i] .. '(.*)') if dy and dm and dd then --try to parse month if tonumber(dm) then dm = tonumber(dm) else dm = mw.ustring.sub(dm, 1, 3) dm = mon[dm] end dy = tonumber(dy) dd = tonumber(dd) if dm and dy and dd then --works only for yyyy format if dd > 32 then dd, dy = dy, dd end dt.year = dy               dt.month = dm                dt.day = dd                dt.normalized = dy .. '.' .. dm .. '.' .. dd               return dt            end end--if date end return d end--parseDate

return p