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

Dominique Faure dominique.faure at gmail.com
Thu Nov 2 05:06:34 CST 2006


Having developing a markup directive to download/play mp3 page
attachments (with the help of a free tiny flash player[1]), I was
wondering if didn't already talked about sharing common recipe php
functions:
I've found that almost all the media related recipes are using quasily
identical but specific code to handle attachment or external url
parameters, and so do I either.
I'm not by any mean trying to say that I did a better job than others
:), but I believe I've come to a nice generic way to handle such kind
of markup parameters and wanted to share it with the community.
Now the code!

The markup itself:

Markup('dewplayer', 'directives',
       '/\\(:dewplayer\\s+([^\\s]+)(\\s+.*)?\\s*:\\)/e',
       "Keep(DewPlayer(\$pagename,'$1',PSS('$2')))");

function DewPlayer($pagename, $tgt, $args) {
  if(!ResolveLink($pagename, $tgt, $url, $lpath, $txt))
    return isset($lpath) ? $url : '';
  $out = 'some html to output';
  return $out;
}

Where the ResolveLink() function is responsible to split the $tgt
attachement/url specification into it's related components
[(Attach:)myFile(.jpg) specifications are therefore allowed], and
gracefully handles exception cases (attachement not found, external
resources not allowed):
* $url is the target url specification,
* $lpath, the path to the local file attachment (if applicable)
* $txt, the visual rendering of the link.

The function code:

function ResolveLink($pagename, $tgt, &$url, &$lpath, &$txt) {
  global $UploadFileFmt, $EnableExternalResource;
  $txt = ExplodeLink($pagename, $tgt, $imap, $path);
  $ok = false;
  if($imap == 'Attach:') {
    $lpath = FmtPageName("$UploadFileFmt/$path", $pagename);
    $ok = file_exists($lpath);
    $url = LinkUpload($pagename, $imap, $path, NULL, $path,
                      $ok ? '$LinkUrl' : NULL);
  } else {
    $url = "$imap$path";
    $ok = IsEnabled($EnableExternalResource, 0);
  }
  return $ok;
}

It makes use of an internal ExplodeLink() extra function (a stripped
down version of the MakeLink() core function), which do the url
specification splitting itself.

The function code:

function ExplodeLink($pagename, $tgt, &$imap, &$path) {
  global $LinkPattern;
  $t = preg_replace('/[()]/', '', trim($tgt));
  preg_match("/^($LinkPattern)?(.+?)(\"(.*)\")?$/", $t, $m);
  $imap = $m[1]; $path = $m[2];
  if(!$imap) $imap = '<:page>';
  $txt = preg_replace('/\\([^)]*\\)/', '', $tgt);
  if($imap == '<:page>') {
    $txt = preg_replace('!/\\s*$!', '', $txt);
    $txt = preg_replace('!^.*[^<]/!', '', $txt);
  }
  return $txt;
}

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...

Awaiting you comments,
Dom

[1] http://www.alsacreations.fr/dewplayer (french speaking)



More information about the pmwiki-devel mailing list