<?php if (!defined('PmWiki')) exit();
/*
Use http://www.topomap.co.nz/ to display an excerpt from a map in PmWiki
+
| Copyright 2013 Simon Davis
| This program is free software; you can redistribute it and/or modify it under the terms of the
| GNU General Public License, Version 2, as published by the Free Software Foundation.
| http://www.gnu.org/copyleft/gpl.html
| This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
| warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
+
*/
# Version date
$RecipeInfo['NZTopo']['Version'] = '2013-07-14';
$FmtPV['$NZTopoVersion'] = $RecipeInfo['NZTopo']['Version']; // return version as a custom page variable
# 2013-07-00 Initial version

# declare $NZTopo for (:if enabled NZTopo:) recipe installation check
global $NZTopo; $NZTopo = 1;

## Add a custom markup 
# (:nztopo ll=-41.293722,174.871482 :)
# (:nztopo topo50=BP33912708,BP33876687 height=300 width=400 pin=1:)
# directive arguments are
#  ll= -- decimal latitude,longitude
#  llbs = -- -- decimal latitude,longitude;latitude,longitude
  $vlat     = '[-+]?\d{1,2}[.]\d+'; # -90 .. 90
  $vlong    = '[-+]?[1]?\d{1,2}[.]\d+'; # -180 .. 180
  $vlatlong = $vlat . '[,]' . $vlong;
  $pll      = 'll=' . $vlatlong;
  $pllbs    = 'llbs=' . $vlatlong . '[;]' . $vlatlong;
# nztm
  $vnztm    = '\d{7}[.]\d{0,3}'; # single NZTM co-ordinate
  $vnztm2   = $vnztm . '[,]' . $vnztm; # pair of NZTAM co-ordinates
  $pnzne    = 'nzne=' . $vnztm2;
  $pnzbs    = 'nzbs=' . $vnztm2 . '[;]' . $vnztm2;
# topo50= --  topo50 grid coordinate, or two grid coordinates (not currently implemented in nztopomap
  $ptopo50  = 'topo50=[ABC][A-Z][0-4]\d{7}(?:[,][ABC][A-Z][0-4]\d{7})?';
# -- only one of topo50, ll, llbs, nzne, nzbs,, kml or gpx can be supplied
  $vscheme  = '(?:https?|ftp):\/\/'; # 
  $vurl     = '(?:(?:\'' . $vscheme . '[^\']+\')|(?:"' . $vscheme . '[^"]+"))'; # provide for single and double quoted strings
# kml= -- URL to kml file
  $pkml     = 'kml=' . $vurl;
# kml= -- URL to kml file
  $pgpx     = 'gpx=' . $vurl;
#  height= -- image height in pixels
  $pheight  = 'height=\d{1,5}';
#  width= -- image width in pixels
  $pwidth   = 'width=\d{1,5}';
#  pin= -- show pin
  $ppin     = 'pin=[01]';
#  label -- tool tip label for pin
  $plabel   = 'label=(?:(?:\'[^\']+\')|(?:"[^"]+")|(?:\w+))'; # provide for single and double quoted strings, and unquoted string
# zoom = scale factor for map
  $pzoom    = 'zoom=\d{1,2}';
# only one location can be supplied
  $qlocale  = '(?:' . $pll . ')|(?:' . $pllbs . ')|(?:' . $pkml . ')|(?:' . $pgpx . ')|(?:' . $pnzne . ')|(?:' . $pnzbs . ')';
Markup( 'NZTopo', 
  'directives', "/\\(:nztopo (" . $qlocale . ")\s*(?:(" . $pheight . ")\s*|(" . $pwidth . ")\s*|(" . $ppin . ")\s*|(" . $plabel . ")\s*|(" . $pzoom .  ")\s*){0,5}\s*:\\)/ei", # 
  "Keep(NZTopo_Parse(PSS('$1'), PSS('$2'), PSS('$3'), PSS('$4'), PSS('$5'), PSS('$6'), PSS('$7')))" );
# s = dot matches all chars including newline
# e = backreference substitution by expression
# i = case insensitive
# m = multiline
# uses lazy evaluation, preserves leading and trailing white space
# Keep prevents PmWiki markup being applied
#
/** Main NZTopo parser
 *   /param   arguements as documented above
 *   /return  The HTML-formatted NZTopo markup wrapped in a <div> of class "nztopo", see http://www.topomap.co.nz/.
 */
function NZTopo_Parse($p1, $p2, $p3, $p4, $p5, $p6, $p7) {

// Initialise variables
  $debugon = false; # debug
  $retval  = ''; # return value
  $opt     = ParseArgs($p1 . ' ' . $p2 . ' ' . $p3 . ' ' . $p4 . ' ' . $p5 . ' ' . $p6 . ' ' . $p7);
  $version = 2; # fixed
  $width   = $opt['width']  ? ' width="'  . $opt['width'] . '"'  : ""; # iframe parameter
  $height  = $opt['height'] ? ' height="' . $opt['height'] . '"' : ""; # iframe parameter
  
// query string parameters
  $query  = ''; 
  $query .= $opt['ll']     ? '&ll='     . $opt['ll']     : "";
  $query .= $opt['llbs']   ? '&llbs='   . $opt['llbs']   : "";
  $query .= $opt['topo50'] ? '&topo50=' . $opt['topo50'] : "";
  $query .= $opt['nzbs']   ? '&nzbs='   . $opt['nzbs']   : "";
  $query .= $opt['nzne']   ? '&nzne='   . $opt['nzne']   : "";
  $query .= $opt['kml']    ? '&kml='    . rawurlencode($opt['kml']) : "";
  $query .= $opt['gpx']    ? '&gpx='    . rawurlencode($opt['gpx']) : "";
  $query .= $opt['pin']    ? '&pin='    . $opt['pin']    : "";
  $query .= $opt['label']  ? '&lbl='    . rawurlencode(htmlentities($opt['label'])) : ""; # double url encode unicode chars for them to work with the topo map
  $query .= $opt['zoom']   ? '&z='      . $opt['zoom']   : '&z=14';
  $src    = 'http://www.topomap.co.nz/NZTopoMapEmbedded?v=' . $version . $query;

  $retval .= '<div class="nztopo">' . "\n";
  $retval .= '<iframe frameborder="0" scrolling="no" marginheight="0" marginwidth="0"' . $width . $height . ' src="' . $src . '">';
  $retval .= "\n" . '</iframe>';
  $retval .= '</div>' . "\n";
/* example HTML for nztopomap
<iframe width="300" height="300" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="http://www.topomap.co.nz/NZTopoMapEmbedded?v=2&ll=-41.293722,174.871482&z=15&pin=1"></iframe>
*/
  if( $debugon ) {
    $retval .= '<div style="font-size:smaller;">p1=' . $p1 . ' p2=' . $p2 . ' p3=' . $p3 . ' p4=' . $p4 . ' p5=' . $p5 . ' p6=' . $p6 . ' p7=' . $p7 . '</br>' . "\n";
    $retval .= 'src=' . $src . '</div>' . "\n";
  }
  return $retval;
} # NZTopo_Parse