[pmwiki-devel] Sharing common functions between recipes / a core modification request

Dominique Faure dominique.faure at gmail.com
Sat Nov 4 10:22:39 CST 2006


On 11/4/06, Patrick R. Michaud <pmichaud at pobox.com> wrote:
> On Sat, Nov 04, 2006 at 02:22:16PM +0100, Dominique Faure wrote:
> > On 11/4/06, Hans <design5 at softflow.co.uk> wrote:
> > >Thursday, November 2, 2006, 11:06:34 AM, Dominique wrote:
> > >
> > >> Thus, my request core modification is to provide an
> > >> ExplodeLink()-equivalent function in (and BTW having ResolveLink()
> > >> using it).
> > >
> > >> The ResolveLink() could be efficiently shared between all the recipes
> > >> that would make use of attachement/url specifications and therefore
> > >> being integrated also...
> > >
> > >Dom, I am all for reusing functions and code.
> > >Especially since I have difficulty in understanding them anyway!
> > >I can't comment on the implications of your request for changing the
> > >ExplodeLink function in pmwiki (if I understood you correctly).
> > >But I hope Pm was listening.
> > >
> >
> > In fact, the core only has the "all-in-one" MakeLink() function available.
> > My purpose here was to have it splitted in two, with something
> > equivalent to my ExplodeLink() made available in the core. (the
> > current implementation is a quasi pure copy-paste).
>
> I looked at the code, but I couldn't quite figure out what
> features ExplodeLink() has that MakeLink() doesn't already
> provide...?
>

Short answer: absolutely nothing

Long answer: ExplodeLink() is in fact reduced to the MakeLink() code
part which splits a wiki url spec into imap+path. I just found really
convenient to have it available for recipes which would deal with
attachement stuff.

The MakeLink() makes the same job but goes too far when providing the
resulting target url, ie. from the recipe point of view, with
MakeLink() I can't easily determine if the target url is resulting
from an attachment or somewhere else.

Since the ExplodeLink() is made from MakeLink(), I was just wondering
if the latter could be written in terms of the first. This way, we
both prevent code duplication and offer the feature to all attachment
related recipes.

An image being worth a thousand words, here's some code (untested):

function CoreExplodeLink($pagename, $tgt, &$imap, &$path, &$title,
$txt=NULL, $suffix=NULL) {
  global $LinkPattern, $LinkFunctions, $UrlExcludeChars,
$ImgExtPattern, $ImgTagFmt;
  $t = preg_replace('/[()]/', '', trim($tgt));
  $t = preg_replace('/<[^>]*>/', '', $t);
  preg_match("/^($LinkPattern)?(.+?)(\"(.*)\")?$/", $t, $m);
  $imap = $m[1];
  $path = $m[2];
  $title = @$m[4];
  if(!$imap) $imap='<:page>';
  if(preg_match("/(($LinkPattern)([^$UrlExcludeChars]+$ImgExtPattern))(\"(.*)\")?$/",
$txt, $tm))
    $txt = $LinkFunctions[$tm[2]]($pagename, $tm[2], $tm[3], @$tm[5],
$tm[1], $ImgTagFmt);
  else {
    if(is_null($txt)) {
      $txt = preg_replace('/\\([^)]*\\)/', '', $tgt);
      if($imap == '<:page>') {
        $txt = preg_replace('!/\\s*$!', '', $txt);
        $txt = preg_replace('!^.*[^<]/!', '', $txt);
      }
    }
    $txt .= $suffix;
  }
  return $txt;
}

function MakeLink($pagename, $tgt, $txt=NULL, $suffix=NULL, $fmt=NULL) {
  $txt = CoreExplodeLink($pagename, $tgt, $imap, $path, $title $txt, $suffix);
  $out = $LinkFunctions[$imap]($pagename, $imap, $path, $title, $txt, $fmt);
  return $out;
}

Dom



More information about the pmwiki-devel mailing list