Module:Sandbox/Matroc/Listing

local p = {}

-- This is a LEARNING 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
-- The field order is apparently the same for all - Parameters are named parameters with exception of {{{1|}}
-- Built this using functions to give a little more flexibility to be able to handle changes in design, order etc.

function p.listing ( frame )
   local text = ""
   local type = frame.args[1] or "listing"  -- listing will be the DEFAULT
   local name = frame.args[2] or ""
   local alt = frame.args[3] or ""
   local url = frame.args[4] or ""
   local email = frame.args[5] or ""
   local address = frame.args[6] or ""
   local lat = frame.args[7] or ""
   local long = frame.args[8] or ""
   local directions = frame.args[9] or ""
   local phone = frame.args[10] or ""
   local tollfree = frame.args[11] or ""
   local fax = frame.args[12] or ""
   local hours = frame.args[13] or ""
   local price = frame.args[14] or ""
   local checkin = frame.args[15] or ""
   local checkout = frame.args[16] or ""
   local content = frame.args[17] or ""
   local one = frame.args[18] or ""
   local islatin = ""
   local latcheck = ""
   local longcheck = ""

--                                                ***  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,"^http://") == nil and mw.ustring.find(url,"^https://") == nil then
            url = "http://" .. url                                 -- add default of "http://
        end
   end -- END IF
--                   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 -- Odd issue with way the last numbered parameter coded in certain templates
   if one ~= "" then
        one = mw.ustring.gsub(one,"\n","")
   end -- END IF
--            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_urlname(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_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(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 *******************************


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


--                                        Name & URL
 function proc_urlname(url,name,text)
   if url ~= "" and name ~= "" then
           text = text .. "'''[" .. url .. "<span class=\"fn org listing-name\">" .. name .. "</span>]'''"
        elseif name ~= "" then
           text = text .. "'''<span class=\"fn org listing-name\">" .. name .. "</span>'''" 
        elseif url ~= "" then
           text = text .. "'''[" .. url .. "]'''"
   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
      if name ~= "" or url ~= "" or address ~= "" or directions ~= "" then
           text = text .. ",&#160;<abbr title=\"phone\">☎</abbr> <span class=\"tel listing-phone\">" .. phone .. "</span>"
        else
           text = text .. "<abbr title=\"phone\">☎</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
      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
--                                             Fax
 function proc_fax(fax,name,url,address,directions,phone,tollfree,text)
   if fax ~= "" then
      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\"></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;<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 {{{1|}}}
 function proc_content(content,one,text)
  if content ~= "" then
          text = text .. "<span class=\"note listing-content\">" .. content .. "</span>"
       elseif one ~= "" then
          text = text .. "&#32;<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;<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

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

Content Disclaimer

Informasi ini disarikan dari Wikipedia dan disajikan kembali untuk tujuan edukasi. Konten tersedia di bawah lisensi CC BY-SA 3.0. Kami tidak bertanggung jawab atas ketidakakuratan data yang bersumber dari kontribusi publik tersebut.

  1. The information displayed on this website is sourced in part or in whole from Wikipedia and has been adapted for the purpose of restating it. We strive to provide accurate and relevant information, however:
  2. There is no guarantee of absolute accuracy. Wikipedia is an open, collaborative project that can be edited by anyone, so information is subject to change.
  3. It is not intended to constitute professional advice. The content displayed is for informational and educational purposes only. For important decisions (e.g., medical, legal, or financial), please consult a professional.
  4. Content copyright. Wikipedia is licensed under the Creative Commons Attribution-ShareAlike License (CC BY-SA). This means that content may be reused with appropriate attribution and shared under a similar license.
  5. Responsible use. Any risk arising from the use of information from this website is entirely the responsibility of the user.