[pmwiki-devel] Regex and recipe philosphy

rogutes at googlemail.com rogutes at googlemail.com
Sat Jul 13 02:37:42 CDT 2013


Ok, I didn't read till the end. I see you have seen the ParseArgs
function, but have you really tried it? Did you look at its source in
pmwiki.php (it also uses regexes)? ParseArgs is widely tested and it does
its job, there is no point in duplicating code. Just do any validation
after calling ParseArgs.

For example, ParseArgs('ll=-41.293722,174.871482') returns this:
  Array
  (
      [#] => Array
          (
              [0] => ll
              [1] => -41.293722,174.871482
          )

      [ll] => -41.293722,174.871482
  )

And ParseArgs('topo50=BP33912708,BP33876687 height=300 width=400 pin=1')
returns this:
  Array
  (
      [#] => Array
          (
              [0] => topo50
              [1] => BP33912708,BP33876687
              [2] => height
              [3] => 300
              [4] => width
              [5] => 400
              [6] => pin
              [7] => 1
          )

      [topo50] => BP33912708,BP33876687
      [height] => 300
      [width] => 400
      [pin] => 1
  )

Now if you want you can additionaly validate user supplied parameters:
  if (isset($args['ll']) && isset($args['topo50'])) {
    # show error
  }
  # topo50 mode
  elseif (isset($args['topo50'])) {
    if (!ctype_digit($args['width'])) {
      # show error
    }
  }
  # ll mode
  else {
  }


Rogutės Sparnuotos (2013-07-13 10:07):
> Hi,
> 
> Did you try this built-in function?
> http://www.pmwiki.org/wiki/Cookbook/ParseArgs
> 
> -- 
> --  Rogutės Sparnuotos
> 
> Simon (2013-07-13 15:39):
> > This is a question about a regex, and a corollary about the philosophy of
> > recipe parameters.
> > 
> > I'm trying to write a directive recipe (see excerpt
> > below)[1<http://khandallah.wellington.net.nz/pmwiki/pmwiki.php/Main/WikiSandbox>
> > ]
> > It seems to me that it makes sense to be relatively strict about the syntax
> > for the directive.
> > 
> > It more or less get the parsing of the parameters correct, except for the
> > backreference of the height|width parameters, where I did not want order to
> > be important
> > (in fact other than the first ll | topo50 parameter I'd rather the order
> > was not important).
> > 
> > Q: For height|width, when both are supplied I only see one (the last)
> > returned as a backreference, is this because where there are multiple
> > matches for a back reference only the last is returned?
> > (I haven't been able to locate any documentation about this).
> > 
> > 
> > In looking for an exemplar recipe I observe that
> > Markup('exemplar', 'directive',
> >   '/\\(:exemplar\s*(.*?)\s*:\\)/ei',
> >   "FmtExemplar(PSS('$1')");
> > most directives accept anything within their scope, then use
> > $arg = ParseArgs($args);
> > 
> > Then the arguments defined are processed, and any unexpected arguments are
> > disregarded.
> > 
> > So my corollary question is:
> > Is this good practice, or just expedient, given the difficulty (as I
> > perceive it) of defining a parameter list in regex?
> > 
> > tia
> > 
> > Simon
> > 
> > 
> > 
> > 
> > 
> > 
> > 
> > 
> > ## Add a custom markup
> > # (:nztopo ll=-41.293722,174.871482 :)
> > # (:nztopo topo50=BP33912708,BP33876687 height=300 width=400 pin=1:)
> > # arguments are
> > #  ll= or topo50= -- latitude,longitude or topo50 map grid coordinates
> > #  height= -- image height in pixels
> > #  width= -- image width in pixels
> > #  pin= -- show pin
> > Markup( 'NZTopo',
> >   'directives', "/\\(:nztopo
> > ((?:ll=[-+]?\d{1,2}[.]\d+[,][-+]?[1]?\d{1,2}[.]\d+)|(?:topo50=[ABC][A-Z][0-4]\d{7}[,][ABC][A-Z][0-4]\d{7}))\s*((?:height|width)=\d+\s*){0,2}(pin=[01])?\s*:\\)/ei",
> >   "Keep(NZTopo_Parse(PSS('$1'), PSS('$2'), PSS('$3'), PSS('$4'),
> > PSS('$5')))" );
> > # 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($coords, $height, $width, $pin, $p5) {
> > 
> > 
> > 
> > 
> > Useful references:
> > http://www.pmwiki.org/wiki/PmWiki/CustomMarkup
> > http://www.pmwiki.org/wiki/PmWiki/CustomMarkupAlt
> 



More information about the pmwiki-devel mailing list