[Home]WikiPatches/LinkOptions

UseModWiki | WikiPatches | RecentChanges | Preferences

A patch by ChuckAdams to usemod 1.0. Apply in the directory usemodwiki unpacks into (i.e. usemod10)

This patch makes it possible to pass parameters of a sort to FreeLinks, which can be used during wikitext rendering as well as when opening and creating. Currently, it implements only the simplest sort of option, namely it checks for the presence of a string in the options. It was a quick hack to implement a quick fix to a feature request, but it should provide a basis for more a comprehensive implementation if anyone cares to take advantage of it. The syntax for a link with options is:

 [[PageName|option1;option2;...;option_n|page description]]

Note the way this is implemented means you must use local descriptions for any freelinks with such options, i.e. the parser will render [[RecentChanges|sidebar]] as an unpatched wiki would, namely as sidebar. You MUST do something like [[RecentChanges|sidebar|Open Recent Changes in Sidebar]]

Options are case-sensitive, and not generally tolerant of extra spaces around them. Like I said, quick hack.

The options that come implemented with this are:

   new -- open a page in a new window.

   sidebar -- open a link in the side panel of IE, Mozilla, or Opera (not compatible with most other browsers)
              sidebar-displayed pages are always displayed as if $EmbedWiki were true


'Discussion goes here (any discussion below the patch may be deleted when new versions are posted)


And now the patch itself (use the edit link to view the unmolested source)

<nowiki>




--- wiki.pl.orig	2003-09-11 05:21:02.000000000 -0700
+++ wiki.pl	2005-03-12 12:01:37.501403880 -0800
@@ -64,6 +64,8 @@
   $AnchoredLinkPattern @HeadingNumbers $TableOfContents $QuotedFullUrl
   $ConfigError $UploadPattern );
 
+use vars qw(%LinkOptions);
+
 # == Configuration =====================================================
 $DataDir     = "/tmp/mywikidb"; # Main wiki directory
 $UseConfig   = 1;       # 1 = use config file,    0 = do not look for config
@@ -195,6 +197,12 @@
 @HtmlSingle = qw(br p hr li dt dd tr td th);
 @HtmlPairs = (@HtmlPairs, @HtmlSingle);  # All singles can also be pairs
 
+# FreeLink options: Keys describe option names, values describe their args.
+# Values are currently ignored, as option args have not been implemented
+%LinkOptions = (sidebar => undef,
+                new => undef,
+);
+
 # == You should not have to change anything below this line. =============
 $IndentLimit = 20;                  # Maximum depth of nested lists
 $PageDir     = "$DataDir/page";     # Stores page data
@@ -305,6 +313,7 @@
   if ($UseSubpage) {
     $FreeLinkPattern = "((?:(?:$AnyLetter+)?\\/)?$AnyLetter+)";
   }
+
   $FreeLinkPattern .= $QDelim;
   # Url-style links are delimited by one of:
   #   1.  Whitespace                           (kept in output)
@@ -1097,21 +1106,31 @@
 }
 
 sub ScriptLinkClass {
-  my ($action, $text, $class) = @_;
+  my ($action, $text, $class, $options) = @_;
+
+  my $extra = "";
+  $$options{new} and $extra .= qq[ target="_new" ];
 
+  if ($$options{sidebar}) {
+    $extra .= qq[ target="_search" ];
+    unless ($action =~ /=/) {
+       $action = "action=browse&id=$action";

+    }
+    $action .= '&embed=1';
+  }
   return "<a href=\"$ScriptName" . &ScriptLinkChar() . "$action\""
-         . ' class=' . $class . ">$text</a>";
+         . ' class=' . $class . " $extra>$text</a>";
 }
 
 sub GetPageLinkText {
-  my ($id, $name) = @_;
+  my ($id, $name, $options) = @_;
 
   $id =~ s|^/|$MainPage/|;
   if ($FreeLinks) {
     $id = &FreeToNormal($id);
     $name =~ s/_/ /g;
   }
-  return &ScriptLinkClass($id, $name, 'wikipagelink');
+  return &ScriptLinkClass($id, $name, 'wikipagelink', $options);
 }
 
 sub GetPageLink {
@@ -1121,13 +1140,13 @@
 }
 
 sub GetEditLink {
-  my ($id, $name) = @_;
+  my ($id, $name, $options) = @_;
 
   if ($FreeLinks) {
     $id = &FreeToNormal($id);
     $name =~ s/_/ /g;
   }
-  return &ScriptLinkClass("action=edit&id=$id", $name, 'wikipageedit');
+  return &ScriptLinkClass("action=edit&id=$id", $name, 'wikipageedit', $options);
 }
 
 sub GetDeleteLink {
@@ -1154,9 +1173,22 @@
   return &ScriptLink(&GetOldPageParameters($kind, $id, $revision), $name);
 }
 
+sub ParseOptions {	# XXX TODO write a full option parser
+   my ($opts_str) = @_;
+   my $opts = {};
+
+   my @optlist = split /\s*;\s*/, $opts_str;
+   foreach my $option (@optlist) {
+     $$opts{$option} = $option if exists $LinkOptions{$option};  # XXX cheezy
+  }
+
+   return $opts;
+}
+
 sub GetPageOrEditAnchoredLink {
   my ($id, $anchor, $name) = @_;
   my (@temp, $exists);
+  my ($opts_str, $options);
 
   if ($name eq "") {
     $name = $id;
@@ -1168,19 +1200,25 @@
   if ($FreeLinks) {
     $id = &FreeToNormal($id);
   }
+  if (%LinkOptions and $name =~ /(.*)\|/) {
+    $opts_str = $1;
+    $name = $';
+    $options = &ParseOptions($opts_str);
+  }
+
   $exists = 0;
   if ($UseIndex) {
     if (!$IndexInit) {
       @temp = &AllPagesList();          # Also initializes hash
     }
     $exists = 1  if ($IndexHash{$id});
-  } elsif (-f &GetPageFile($id)) {      # Page file exists
+  } elsif (-f &GetPageFile($id, $options)) {      # Page file exists
     $exists = 1;
   }
   if ($exists) {
     $id = "$id#$anchor"  if $anchor;
     $name = "$name#$anchor"  if $anchor && $NamedAnchors != 2;
-    return &GetPageLinkText($id, $name);
+    return &GetPageLinkText($id, $name, $options);
   }
   if ($FreeLinks && !$EditNameLink) {
     if ($name =~ m| |) {  # Not a single word
@@ -1188,9 +1226,9 @@
     }
   }
   if ($EditNameLink) {
-    return &GetEditLink($id, $name);
+    return &GetEditLink($id, $name, $options);
   } else {
-    return $name . &GetEditLink($id, '?');
+    return $name . &GetEditLink($id, '?', $options);
   }
 }
 
@@ -2365,7 +2403,7 @@
 }
 
 sub GetPageFile {
-  my ($id) = @_;
+  my ($id, $options) = @_;
 
   return $PageDir . "/" . &GetPageDirectory($id) . "/$id.db";
 }


# 
<nowiki> this line is not part of the patch, so delete it!

UseModWiki | WikiPatches | RecentChanges | Preferences
Edit text of this page | View other revisions | Search MetaWiki
Last edited October 14, 2007 10:24 pm by MarkusLude (diff)
Search: