[pmwiki-users] automatic link creation

Eemeli Aro eemeli at gmail.com
Wed Aug 26 07:19:28 CDT 2009


2009/8/26 Eemeli Aro <eemeli at gmail.com>:
> 2009/8/26 adam overton <a at plus1plus1plus.org>:
>> i would like to have pmwiki convert certain commonly appearing names
>> (or terms, or orgs) into a links to the respective person's external
>> website.
>
> I'm not aware of any recipe that'll do this, but it is something I've
> been thinking of recently of implementing. From my point of view,
> however, it's more of a matter of figuring out the best way of
> generating and maintaining the list of valid names to link up.

So I ended up finally doing this, and in case it's of use to someone,
here's slightly hack-y recipe for linking text matching a profile
page's title to the profile page itself.

First, in Site.LocalTemplates:

[[#usernamelist]]
(:template default wrap=inline:)
(:template default group=Profiles:)
(:template default name=-Profiles,-*-Contrib,-RecentChanges:)
{=$Title}	{=$FullName}
[[#usernamelistend]]

Note that that's a tab character between the Title and FullName. Note
also that you can customize the mapping to use your own page text
variables or whatnot, and since the second parameter will be passed
onto MakeLink, it can be a full http://address or intermap link as
well.

In local/Profiles.php, or otherwise configured to only trigger for
pages that are included in the above pagelist template:

$EditFunctions[] = 'UpdateUserNameList';
function UpdateUserNameList($pagename, &$page, &$new) {
  global $IsPagePosted, $WorkDir;
  if (!$IsPagePosted) return;
  $markup = FmtPageList('$MatchList', $pagename,
    array('o' => 'fmt=#usernamelist'));
  preg_match_all('/^(.+)\t(\S+)$/m', $markup, $matches, PREG_SET_ORDER);
  $map = array();
  foreach( $matches as $m ) $map[$m[1]] = $m[2];
  file_put_contents("$WorkDir/users-map", serialize($map));
  $regexp = implode('|', array_keys($map));
  file_put_contents("$WorkDir/users-regexp", "/\b(`?)($regexp)\b/e");
}

And finally, in config.php:

if (file_exists("$WorkDir/users-regexp")) Markup(
  '[[user]]', '>[[',
  file_get_contents("$WorkDir/users-regexp"),
  "Keep(UserLink(\$pagename, PSS('$2'), '$1'), 'L')");
function UserLink($pagename, $name, $ignore) {
  global $WorkDir;
  if ($ignore || !file_exists("$WorkDir/users-map")) return $name;
  $map = unserialize(file_get_contents("$WorkDir/users-map"));
  if (empty($map[$name])) return $name;
  else return MakeLink($pagename, $map[$name], $name);
}


eemeli



More information about the pmwiki-users mailing list