跳转到内容

Module:ExListing

来自维基导游
-- THIS IS AN EXPERIMENTAL MODULE DO NOT USE AS ITS FOR TESTING PURPOSES ONLY


local p = {}

--  This is a test of the listing template found in Wikivoyage
--  Converted this from ParserFunction/template coding to a LUA MODULE - A HEADACHE INDEED!
--  Template listing can stand alone or be called from other (see, do, buy, eat, drink, sleep) templates
--  The other calling templates do not seem to need to be changed as they pass parameters to the listing template
--  The field order is apparently the same for all - Parameters are named parameters with exception of 1 in curly brackets
--  Built this using functions to give a little more flexibility to be able to handle changes in design, order etc.

--  Small select icons that can be placed in front of name - can add more

local icontable = {}                           -- possible for limited icons
icontable["see"] = "[[File:SymbolMuseum.svg|See|16px|link=]]"
icontable["eat"] = "[[File:SymbolRestaurant.svg|16px|Eat|link=]]"
icontable["sleep"] = "[[File:SymbolHotel.svg|16px|Sleep|link=]]"
icontable["parking"] = "[[File:SymbolParkingSite.svg|16px|Car parking|link=]]"
icontable["train"] = "[[File:SymbolTrain.svg|16px|Train station|link=]]"
icontable["bus"] = "[[File:SymbolBus.svg|16px|Bus stop|link=]]"
icontable["dbus"] = "[[File:SymbolStrassenbahnhaltestelle.svg|16px|Bus stop|link=]]"
icontable["boat"] = "[[File:SymbolBoating.svg|16px|Boat mooring|link=]]"
icontable["boatlaunch"] = "[[File:BoatlaunchNPS0.png|16px|Boat launch|link=]]"
icontable["ferry"] = "[[File:SymbolFerry.svg|16px|Ferry crossing|link=]]"
icontable["chairlift"] = "[[File:Pictograms-nps-winter-chair lift-ski lift.svg|16px|chair lift|link=]]"
icontable["viewpoint"] = "[[File:Pictograms-nps-misc-scenic viewpoint.svg|16px|viewpoint|link=]]"
icontable["shelter"] = "[[File:NPS map symbol shelter.png|16px|shelter|link=]]"
icontable["metro"] = "[[File:Metro-M.svg|16px|Metro|link=]]"
icontable["hospital"] = "[[File:Hospital.svg|16px|Hospital|link=]]"
icontable["bicycle"] = "[[File:Bicycle.svg|16px|Bicycle|link=]]"
icontable["airplane"] = "[[File:20 airtransportation.svg|18px|Air transportation|link=]]"
icontable["camping"] = "[[File:Noun project 465.svg|16px|Camping|link=]]"

local colors = {}                               -- for future map
colors['go'] = "brown"
colors['see'] = "steelblue"
colors['do'] =  "grey"
colors['buy'] = "teal"
colors['eat'] = "chocolate"
colors['drink'] = "black"
colors['sleep'] = "navy"
colors['default'] = "fuchsia"


--  ****************************FUNCTIONS FOR PROCESSING*********************************
--  *** Here you can edit or add new functions based upon design / order ***


function proc_map(text,colortype,lat,long,zoom,layer,language,pagename,maplink)

if type ~= "" then
    if lat ~= "" then
        if long ~= "" then
            text = text .. "<span class=\"noprint listing-coordinates\"><abbr class=\"geo\" title=\"" .. lat .. ";" .. long .. "\"></abbr></span><b>"
            text = text .. "<span class=\"noprint plainlinks\">\[//tools.wmflabs.org/wikivoyage/w/poimap2.php?lat="
--            text = text .. "<span class=\"noprint plainlinks\">\[http://maps.wikivoyage-ev.org/w/poimap2.php?lat="         
            text = text .. lat .. "&lon=" .. long .. "&zoom=" .. zoom .. "&layer=" .. layer .. "&lang=" .. language .. "&name=" .. pagename
            text = text .. "<span class=\"listing-map"
--  text = text .. "&#32;listing-general\""
            text = text .. "\" style=\"background:" .. colortype
            text = text .. ";\" title=\"Link to map\">&#160;</span>\]"
            text = text .. "</span><span class=\"printonly listing-map"
--  text = text .. "&#32;listing-general"
            text = text ..  "\" style=\"background:" .. colortype
            text = text .. "; border: 1px solid " .. colortype .. ";\" title=\"Link to map\">&#160;&#32;</span></b>"
            maplink = "yes"
         end -- END LONG
    end -- END IF LAT
end -- IF TYPE

return text

end -- END FUNCTION

--  ICON - add icon(s) in front of name
function proc_icon(icon,text,icontable)
   if icon ~= "" then
     mw.ustring.gsub(icon," ","")
     local str = ""
         for str in mw.ustring.gmatch(icon,"([^,]+)") do
             if icontable[str] ~= nil then
                 text = text .. icontable[str]
             end
         end
     end
     return text
end
--  Name & URL
function proc_urlname(flag,url,name,text,maplink,icon)
    if flag ~= "" then
        text = text .. "[[Image:Flag_of_" .. flag .. ".svg|frameless|22px|link=]]"
      end
    if maplink == "yes" or icon ~= "" or flag ~= "" then
        text = text .. "&#32;"                          -- insert space before name
      end
    if url ~= "" and name ~= "" then
        text = text .. "'''[" .. url .. "<span class=\"fn org listing-name\">" .. name .. "</span>]'''"
-- name is required so it has to be present for else        
--        elseif name ~= "" then
--            text = text .. "<span class=\"fn org listing-name\">'''" .. name .. "</span>'''" 
--        elseif url ~= "" then
--            text = text .. "'''[" .. url .. "]'''"

else
   text = text .. "<span class=\"fn org listing-name\">'''" .. name .. "</span>'''" 
    end -- END IF
    return text
end

--  Alt
function proc_alt(alt,islatincheck,text)
    if alt ~= "" then
        islatincheck = isLatin(alt)
        if islatincheck == "yes" then
            text = text .. "&#32;(''<span class=\"nickname listing-alt\">" .. alt .. "</span>'')"
        else
            text = text .. "&#32;(<span class=\"nickname listing-alt\">" .. alt .. "</span>)"
        end
    end -- END IF
    return text
end
--  Address 
function proc_address(address,name,url,text)
    if address ~= ""  then
        if url ~= "" or name ~= "" then
            text = text .. ",&#160;" .. "<span class=\"label listing-address\">" .. address .. "</span>"
        else
            text = text .. "<span class=\"label listing-address\">" .. address .. "</span>"
        end
    end -- END IF
    return text
end
--   Directions
function proc_directions(directions,text)
    if directions ~= "" then
        text = text .. "&#32;(''<span class=\"listing-directions\">" .. directions .. "</span>'')"
    end -- END IF
    return text
end
--  Phone
function proc_phone(phone,name,url,address,directions,text)
    if phone ~= "" then
phone = mw.ustring.gsub(phone,"([0-9]+) ([0-9]+) ([0-9]+)","%1&nbsp;%2&nbsp;%3")
phone = mw.ustring.gsub(phone,"([0-9]+) ([0-9]+)","%1&nbsp;%2")
        if name ~= "" or url ~= "" or address ~= "" or directions ~= "" then
            text = text .. ",&#160;<abbr title=\"phone\">&#x260e;</abbr> <span class=\"tel listing-phone\">" .. phone .. "</span>"
        else
            text = text .. "<abbr title=\"phone\">&#x260e;</abbr> <span class=\"tel listing-phone\">" .. phone .. "</span>"
        end -- END IF
    end -- END IF
    return text
end
--  Tollfree
function proc_tollfree(tollfree,name,url,address,directions,phone,text)
    if tollfree ~= "" then
tollfree = mw.ustring.gsub(tollfree,"([0-9]+) ([0-9]+) ([0-9]+)","%1&nbsp;%2&nbsp;%3")
tollfree = mw.ustring.gsub(tollfree,"([0-9]+) ([0-9]+)","%1&nbsp;%2")
        if name ~= "" or url ~= "" or address ~= "" or directions ~= "" or phone ~= "" then
            text = text .. ",&#160;'''toll-free''': <span class=\"tel listing-tollfree\">" .. tollfree .. "</span>"
        else
            text = text .. "'''toll-free''': <span class=\"tel listing-tollfree\">" .. tollfree .. "</span>"
        end -- END IF
    end -- END IF
    return text
end
--  TTY
function proc_tty(tty,tollfree,name,url,address,directions,phone,text)
    if tty ~= "" then
tty = mw.ustring.gsub(tty,"([0-9]+) ([0-9]+) ([0-9]+)","%1&nbsp;%2&nbsp;%3")
tty = mw.ustring.gsub(tty,"([0-9]+) ([0-9]+)","%1&nbsp;%2")
        if name ~= "" or url ~= "" or address ~= "" or directions ~= "" or phone ~= "" or tollfree ~= "" then
            text = text .. ",&#160;'''TTY''': <span class=\"tel listing-tty\">" .. tty .. "</span>"
        else
            text = text .. "'''TTY''': <span class=\"tel listing-tty\">" .. tty .. "</span>"            -- need span class listing-tty 
        end -- END IF
    end -- END IF
    return text
end
--  Fax
function proc_fax(fax,name,url,address,directions,phone,tollfree,text)
    if fax ~= "" then
fax = mw.ustring.gsub(fax,"([0-9]+) ([0-9]+) ([0-9]+)","%1&nbsp;%2&nbsp;%3")
fax = mw.ustring.gsub(fax,"([0-9]+) ([0-9]+)","%1&nbsp;%2")
        if name ~= "" or url ~= "" or address ~= "" or directions ~= "" or phone ~= "" or tollfree ~= "" then
            text = text .. ",&#160;<span class=\"tel\"><span class=\"type\">'''fax'''</span>: <span class=\"value listing-fax\">" .. fax .. "</span></span>"
        else
            text = text .. "<span class=\"tel\"><span class=\"type\">'''fax'''</span>: <span class=\"value listing-fax\">" .. fax .. "</span></span>"
        end -- END IF
    end -- END IF
    return text
end
--  E-mail
function proc_email(email,name,url,address,directions,phone,tollfree,fax,text)
    if email ~= "" then
        if name ~= "" or url ~= "" or address ~= "" or directions ~= "" or phone ~= "" or tollfree ~= "" or fax ~= "" then
             text = text .. ",&#160;'''e-mail''': <span class=\"email listing-email\">[mailto:" .. email .. " " .. email .. "]</span>"
        else
             text = text .. "'''e-mail''': <span class=\"email listing-email\">[mailto:" .. email .. " " .. email .. "]</span>"
        end -- END IF
    end -- END IF
    return text
end
--  Lat/Long co-ordinates
function proc_latlong(lat,long,text)
    if lat ~= "" or long ~= "" then
        text = text .. "<span class=\"noprint listing-coordinates\"> <abbr class=\"geo\" title=\"" .. lat .. ";" .. long .. "\">[[File:Map mag16.png|14px|link=special:mapsources/" .. lat .. "," .. long .. "]]</abbr></span>"
    end -- END IF
    return text
end
--  Hours
function proc_hours(hours,text)
    if hours ~= "" then
        text = text .. "&#32;'''Hrs''': <span class=\"note listing-hours\">" .. hours .. "</span>."
    end -- END IF
    return text
end
--  Checkin and checkout
function proc_checkinout(checkin,checkout,text)
   if checkin ~= "" and checkout ~= "" then
        text = text .. "&#32;<span class=\"note\">'''Check-in''': <span class=\"listing-checkin\">" .. checkin .. "</span>, '''Check-out''': <span class=\"listing-checkout\">" .. checkout .. "</span></span>."
   elseif checkin ~= "" then
        text = text .. "&#32;<span class=\"note\">'''Check-in''': <span class=\"listing-checkin\">" .. checkin .. "</span></span>."
   elseif checkout ~= "" then
        text = text .. "&#32;<span class=\"note\">'''Check-out''': <span class=\"listing-checkout\">" .. checkout .. "</span></span>."
   end -- END IF
   return text
end
--  General listing content - alternative is parameter 1 in curly brackets
function proc_content(content,one,text)
    if content ~= "" then
         text = text .. "&#32;'''Descr''': <span class=\"note listing-content\">" .. content .. "</span>"
    elseif one ~= "" then
        text = text .. "&#32;'''Descr''': <span class=\"note listing-content\">" .. one .. "</span>"
    end -- END IF
    return text
end
--  Price
function proc_price(price,text)
    if price ~= "" then
        text = text .. "&#32;'''Prc''': <span class=\"note listing-price\">" .. price .. "</span>"
    end -- END IF
    return text
end
--  Function to check for non-Latin/Latin characters
function isLatin ( xyz )                          -- from  WOSlinker - Module:IsLatin - http://en.wikipedia.org/wiki/User:WOSlinker
    if xyz == '' then
        return '';
    end
    len = mw.ustring.len(xyz);
    pos = 1;
    charval = ""
    while ( pos <= len ) do
        charval = mw.ustring.codepoint(mw.ustring.sub(xyz, pos)) -- note 8364 is the € symbol
        if charval>687 and charval~=8364 then
            return "no";
        end 
        pos = pos + 1;
    end
    return "yes";
end
--  Function to ESCAPE characters if REQUIRED or NEEDED for certain string functions
function escape ( xyz )
    local esc = ""
    esc = mw.ustring.gsub(xyz, "([%(%)%.%%%+%-%*%?%[%^%$%]])", "%%%1" )
    return esc
end


function p.listing ( frame )
    local text = ""                          -- build data output
    local islatin = ""                       -- used to check for non-lating chars
    local latcheck = ""                      -- used to check latitude format after decimal
    local longcheck = ""                     -- used to check longitude format after decimal
    local ESC = ""                           -- if required to escape charaters in a string - see function escape()
    local colortype = ""
    local maplink = "no"
    local type = frame.args[1] or "listing"  -- listing will be the DEFAULT
    local flag = frame.args[2] or ""         -- flag for embassy or consulate (user puts in flag - 
    local name = frame.args[3] or ""         -- name
    local alt = frame.args[4] or ""          -- alternate name
    local url = frame.args[5] or ""          -- url - begin with http://....
    local email = frame.args[6] or ""        -- email address
    local address = frame.args[7] or ""      -- physical address
    local lat = frame.args[8] or ""          -- latitude
    local long = frame.args[9] or ""         -- longitude
    local zoom = frame.args[10] or "17"      -- layer default is 15 -- range 1 to 18 - do check for range -- future map goes with lat and long
    local layer = frame.args[11] or "O"      -- M= Mapnik, W=wv, N=Traffic, O=MapQ Open, A= MapQ Aerial, P=POI, L=Labels, H=Hiking, S=Shading, C=Cycling, D=Destinations
    local directions = frame.args[12] or ""  -- directions to entity
    local phone = frame.args[13] or ""       -- phone #
    local tollfree = frame.args[14] or ""    -- tollfree #
    local tty = frame.args[15] or ""         -- tty # (added - needs class name - future maybe!)
    local fax = frame.args[16] or ""         -- fax #
    local hours = frame.args[17] or ""       -- hours of operation
    local price = frame.args[18] or ""       -- price
    local checkin = frame.args[19] or ""     -- checkin time
    local checkout = frame.args[20] or ""    -- checkout time
    local content = frame.args[21] or ""     -- description
    local icon = frame.args[22] or ""        -- icon name or names separated by a comma to put in front of name -- (added - not used)
    local one = frame.args[23] or ""         -- alternative text if no content available?
    local language = frame.args[24]          -- LANUGUAGE -- for use with future map - lat,long,zoom,layer,language and pagenamee
    local pagename = frame.args[25]          -- PAGENAMEE -- for use with future map - lat,long,zoom,layer,language and pagenamee

--  ***  MINOR GENERAL ERROR CHECKING AND FIXES ***
--  NAME - check to see if REQUIRED name
    if name == "" then
        error("'NAME is a required field!'")    
    end -- END IF
--  URL .. if does not begin with "http" or "https" then add "http://" to url  ...  NOT KNOWN if others are to be used such as image, file etc.
    if url ~= "" then
        if mw.ustring.find(url,"^www") then
           url = "http://" .. url
        end
        if mw.ustring.find(url,"^http://") == nil and mw.ustring.find(url,"^https://") == nil then      
            url = "http://www." .. url                                 -- add default of "http://www."?
        end -- END IF
    end
--  LAT and LONG FIX attempt at some format check - (lat and long should have the same number of decimal places)
--  lat and long are not required - one can exist without the other; the following is a pad for the lat and long
--  based upon the notion that the numbers are nnn.nnnn this may be an incorrect assumption and limited - comment out if incorrect

    if lat ~= "" and long ~= "" then
        if mw.ustring.find(lat,"%.") ~= nil and mw.ustring.find(long,"%.") ~= nil then
            latcheck = mw.ustring.gsub(lat,"^(.*)%.","")
            longcheck = mw.ustring.gsub(long,"^(.*)%.","")
               if #latcheck > #longcheck then
                  long = long .. mw.ustring.rep("0", #latcheck - #longcheck)
                 elseif #longcheck > #latcheck then
                   lat = lat .. mw.ustring.rep("0", #longcheck - #latcheck)
                  end
      end
    end -- END IF
--  NOTE - THIS IS A FIX -- Multiline content - newlines only
    if one ~= "" then
        one = mw.ustring.gsub(one,"\n$","")
        one = mw.ustring.gsub(one,"(\n+)","<br/>&nbsp;&bull;&nbsp;")
    end -- END IF

--  WE FIX MULTILINE CONTENT FROM A TEMPLATE - NEWLINES ONLY
       content = mw.ustring.gsub(content,"\n$","")
       content = mw.ustring.gsub(content,"(\n+)","<br/>&nbsp;&bull;&nbsp;")


--  DOING COLORTYPE FOR MAPS BASED ON TYPE
    if colors[type] == nil then
       colortype = colors['default']
    else
       colortype = colors[type]
    end

--  END MINOR ERROR Checks

--  *** PROCESS THE GIVEN DATA INFORMATION BASED ON TYPE AT MOMENT (listing,see,do,buy,eat,drink and sleep) ***
--  *** HERE YOU CAN ADD OR CHANGE THINGS AROUND BASED UPON "TYPE" ***

   if type == "listing" or type == "see" or type == "do"  or 
         type == "buy" or type == "eat" or type == "drink" or type == "sleep" then
        text = proc_icon(icon,text,icontable)
        text = proc_map(text,colortype,lat,long,zoom,layer,language,pagename)
        text = proc_urlname(flag,url,name,text)
        text = proc_alt(alt,islatincheck,text)
        text = proc_address(address,name,url,text)
        text = proc_directions(directions,text)
        text = proc_phone(phone,name,url,address,directions,text)
        text = proc_tollfree(tollfree,name,url,address,directions,phone,text)
        text = proc_tty(tty,tollfree,name,url,address,directions,phone,text)
        text = proc_fax(fax,name,url,address,directions,phone,tollfree,text)
        text = proc_email(email,name,url,address,directions,phone,tollfree,fax,text)
        text = proc_latlong(lat,long,text)
        text = proc_hours(hours,text)
        text = proc_checkinout(checkin,checkout,text)
        text = proc_content(content,one,text)
        text = proc_price(price,text)

    elseif type == "SAMPLE" then                    -- This ELSEIF IS JUST A SIMPLE EXAMPLE OF DIFFERENT OUTPUT
        text = proc_urlname(flag,url,name,text)
        text = proc_alt(alt,islatincheck,text)
        text = proc_content(content,one,text)
    else
        error("types allowed are listing,see,do,buy,eat,drink or sleep... !")
    end

--  **************************ENDING THE LISTING FUNCTION*******************************    
   text =  "<span class=\"vcard\">" .. text .. "</span>"
   return text

end
--   *************************END OF THE LISTING FUNCTION *******************************


--  **************************END OF MODULE RETURN RESULT******************************* 
return p