Module:Sandbox/PHansen/Kirkeurl

local p = {} -- Truncate urls too long for an infobox - should not be more than 28-30 characters -- With User:PHansen/Kirkeurl -- Test url : "www.tryggevaeldeprovsti.dk/index.php?id=1722" - for the line -- | website = www.tryggevaeldeprovsti.dk/index.php?id=1722 - there is no http:// in the template -- Should return something like http://www.tryggevaeldeprovsti.dk or with ellipsis ? (three dots) -- Module:Sandbox/PHansen/Kirkeurl https://en.wikipedia.org/wiki/Module:Sandbox/PHansen/Kirkeurl -- Test via https://en.wikipedia.org/wiki/User:PHansen/Kirkeurl

function p.main ( frame ) -- tidl kirke_url

local len_orig = 0 local orig = frame.args[1] -- parameter from the template: 'website' or 'Websted' -- orig = mw.text.trim( orig )

local len_orig = string.len( orig ) or 0

local return_kirkeurl = ""

local done = false

local limit = 28

-- Most will be below the limit (28 characters) -- if string.len( orig ) < limit then if len_orig < limit then -- the parameter is arriving without indication of the protocol, so returning with http:// return_kirkeurl = "[http://" .. orig .. " " .. orig .. "]" -- return with http:// done = true return return_kirkeurl

end -- below limit

-- else : if parameter (orig = frame.args[1]) is longer than limit -- some domains to look through local domains = .ac.ad.ae.aero.af.ag.ai.al.am.an.ao.aq.ar.arpa.as.asia.at.au  .aw.ax.az.ba.bb.bd.be.bf.bg.bh.bi.biz.bj.bm.bn.bo.br.bs.bt.bv.bw.by.bz.ca   .cat.cc.cd.cf.cg.ch.ci.ck.cl.cm.cn.co.com.coop.cr.cs.cu.cv.cx.cy.cz.dd.de   .dj.dk.dm.do.dz.ec.edu.ee.eg.eh.er.es.et.eu.fi.firm.fj.fk.fm.fo.fr.fx.ga   .gb.gd.ge.gf.gh.gi.gl.gm.gn.gov.gp.gq.gr.gs.gt.gu.gw.gy.hk.hm.hn.hr.ht.hu   .id.ie.il.im.in.info.int.io.iq.ir.is.it.je.jm.jo.jobs.jp.ke.kg.kh.ki.km.kn   .kp.kr.kw.ky.kz.la.lb.lc.li.lk.lr.ls.lt.lu.lv.ly.ma.mc.md.me.mg.mh.mil.mk   .ml.mm.mn.mo.mobi.mp.mq.mr.ms.mt.mu.museum.mv.mw.mx.my.mz.na.name.nato.nc   .ne.net.nf.ng.ni.nl.no.nom.np.nr.nt.nu.nz.om.org.pa.pe.pf.pg.ph.pk.pl.pm   .pn.post.pr.pro.ps.pt.pw.py.qa.re.ro.ru.rw.sa.sb.sc.sd.se.sg.sh.si.sj.sk   .sl.sm.sn.so.sr.ss.st.store.su.sv.sy.sz.tc.td.tel.tf.tg.th.tj.tk.tl.tm.tn   .to.tp.tr.travel.tt.tv.tw.tz.ua.ug.uk.um.us.uy.va.vc.ve.vg.vi.vn.vu.web.wf   .ws.xxx.ye.yt.yu.za.zm.zr.zw

-- if string.len( orig ) >= limit then if len_orig >= limit then -- Crucial lua pattern is by 'hjpotter92' -- http://stackoverflow.com/users/1190388/hjpotter92 edited May 12 '14 at 1:35 (i.e. 2014-05-12 at 1:35) -- http://stackoverflow.com/questions/23590304/finding-a-url-in-a-string-lua-pattern : "finding a url in a string lua pattern" local tlds = {} -- topleveldomains for tld in domains:gmatch'%w+' do  tlds[tld] = true end

local protocols = {[''] = 0, ['http://'] = 0, ['https://'] = 0, ['ftp://'] = 0} -- of no use because the parameter arrives without

-- for pos, url, prot, subd, tld, colon, port, slash, path in string_with_URLs:gmatch for pos, url, prot, subd, tld, colon, port, slash, path in orig:gmatch '(([%w_.~!*:@&+$/?%%#-]-)(%w[-.%w]*%.)(%w+)(:?)(%d*)(/?)([%w_.~!*:@&+$/?%%#=-]*))'

do if protocols[prot:lower] == (1 - #slash) * #path and (colon ==  or port ~=  and port + 0 < 65536) and (tlds[tld:lower] or tld:find'^%d+$' and subd:find'^%d+%.%d+%.%d+%.$'     and math.max(tld, subd:match'^(%d+)%.(%d+)%.(%d+)%.$') < 256) and not subd:find'%W%W' then return_kirkeurl = "[" .. "http://" .. orig .. " " .. subd .. tld .. "]" 	-- for test in terminal: print( "[" .. url .. " " .. subd .. tld .. "]" ) -- gave www.tryggevaeldeprovsti.dk end -- if end -- for pos... do

done = true return return_kirkeurl

end -- above limit

--Default - if still here if done == false then return_kirkeurl = "[" .. "http://" .. orig .. " " .. "Church website" .. "]"

return return_kirkeurl

end -- if done == false

end -- function p.main ( frame )

return p -- export table