[Home]WikiPatches/NamedAnchors

UseModWiki | WikiPatches | RecentChanges | Preferences

This feature is included in 1.0. --CliffordAdams

// \ See MeatBall:NamedAnchorsInWiki. -- SunirShah

Usage:

[#test]

* SandBox
* SandBox#test
* SampleUndefinedPage#test

* [SandBox bracket_wiki]
* [SandBox#test bracket_anchored]
* [SampleUndefinedPage#test bracket_anchored_edit]

see below for a shorter patch...'

Add $NamedAnchors, $AnchoredLinkPattern to use vars qw();

 # Minor options:
 ...
 $NamedAnchors = 1;      # 0 = no anchors, 1 = enable anchors, 2 = enable but suppress display

 sub InitLinkPatterns {
   ...
   $QDelim = '(?:"")?';     # Optional quote delimiter (not in output)
   $AnchoredLinkPattern = $LinkPattern . '#(\\w+)' . $QDelim if $NamedAnchors;
   $LinkPattern .= $QDelim;
   ...
 }

 # sub GetPageOrEditLink { 
 sub GetPageOrEditAnchoredLink {
   my ($id, $anchor, $name) = @_;
   ...
   if ($exists) {
     $id = "$id#$anchor" if $anchor;
     $name = "$name#$anchor" if $anchor && $NamedAnchors != 2;
     return &GetPageLinkText($id, $name);
   }
   ...
 }

 sub GetPageOrEditLink {
     my ($id, $name) = @_;
     return &GetPageOrEditAnchoredLink( $id, "", $name );
 }

 # Add after sub StoreBracketLink
 sub StoreBracketAnchoredLink {
   my ($name, $anchor, $text) = @_;

   return &StoreRaw(&GetPageLinkText("$name#$anchor", "[$text]"));
 }

 sub CommonMarkup {
    ...
    s/\<code\>((.|\n)*?)\<\/code\>/&StorePre($1, "code")/ige;
    s/\[\#(\w+)\]/&StoreHref(" name=\"$1\"")/ge if $NamedAnchors;
    ...

     if ($BracketText) {  # Links like [URL text of link]
       s/\[$UrlPattern\s+([^\]]+?)\]/&StoreBracketUrl($1, $2)/geos;
       s/\[$InterLinkPattern\s+([^\]]+?)\]/&StoreBracketInterPage($1, $2)/geos;
       if ($WikiLinks && $BracketWiki) {  # Local bracket-links
         s/\[$LinkPattern\s+([^\]]+?)\]/&StoreBracketLink($1, $2)/geos;
         s/\[$AnchoredLinkPattern\s+([^\]]+?)\]/&StoreBracketAnchoredLink($1, $2, $3)/geos if $NamedAnchors; 
       }
     }
     ...
     if ($WikiLinks) {
       s/$AnchoredLinkPattern/&StoreRaw(&GetPageOrEditAnchoredLink($1, $2, ""))/geo if $NamedAnchors;
       s/$LinkPattern/&GetPageOrEditLink($1, "")/geo;
     }
 }


Here we are a slightly modified syntax, which allowes referencing an anchor from the same page.

Creating an anchor:

Referencing an anchor:

Find this line in the patch for the subroutine CommonMarkup:

    s/\[\#(\w+)\]/&StoreHref(" name=\"$1\"")/ge if $NamedAnchors;
and replace it with the following lines:
    if ($NamedAnchors) {
      s/\[\:(\w+)\|([^\]]+)\]/&StoreHref(" name=\"$1\"", $2)/ge;
      s/\[\:(\w+)\]/&StoreHref(" name=\"$1\"")/ge;
      s/\[(\#\w+)\|([^\]]+)\]/&StoreHref(" href=\"$1\"", $2)/ge;
      s/\[\#(\w+)\]/&StoreHref(" href=\"#$1\"", $1)/ge;
    }

-- FerdinandPrantl


Here is an alternative patch, one with less lines/subs impacted. You also get the W3C endorsed pattern if you want it (you might not, if you want to encourage non-valid anchors in your wiki) -- EricScheid

See it in action at http://www.IAwiki.net/dev.pl?ScratchPad

 #----------------------------------------

 sub GetPageOrEditLink {
   my ($id, $name) = @_;
   my (@temp, $exists, $anchor);

   $id =~ s/(.*)(\#.*)/$1/;
   $anchor = $2;
 ...
     return &GetPageLinkText($id . $anchor, $name . $anchor);
 ...
   return $name . &GetEditLink($id,"?") . $anchor;
 }

 #----------------------------------------

 sub InitLinkPatterns {
 ...
   if ($UseSubpage) {
     # Loose pattern: If subpage is used, subpage may be simple name
     $LinkPattern = "(?:(?:$LpA)?\\/$LpB)|$LpA";
     # Strict pattern: both sides must be the main LinkPattern
     # $LinkPattern = "(?:(?:$LpA)?\\/)?$LpA";
   } else {
     $LinkPattern = "$LpA";
   }
   #$AnchorPattern = "\#[\-A-Za-z0-9_:\.]+";     # browser compatible, w3c incompatible
   $AnchorPattern = "\#[A-Za-z][\-A-Za-z0-9_:\.]*";     # http://www.w3.org/TR/1998/REC-html40-19980424/types.html#type-id
   $LinkPattern = "($LinkPattern(?:$AnchorPattern)?)";  # anchorname included as an optional trailing item

   $QDelim = '(?:"")?';     # Optional quote delimiter (not in output)
   $LinkPattern .= $QDelim;
 ...
 }

 #----------------------------------------

This does not works for me. Anything has changed? Would you look at this code again to see what could be missing? Thanks!

UseModWiki | WikiPatches | RecentChanges | Preferences
Edit text of this page | View other revisions | Search MetaWiki
Last edited September 29, 2007 9:56 pm by MarkusLude (diff)
Search: