DokuWiki

It's better when it's simple

User Tools

Site Tools


Sidebar

Translations of this page?:

Learn about DokuWiki

Advanced Use

Corporate Use

Our Community


Follow us on Facebook, Twitter and other social networks.

tips:autolink

Autolink

author : Vincent Fleury
mail : fleury.vincent[at]gmail.com
status : experimental script (used at my company)

Usage

this script can be used to:

  • automatically add links to a word
  • change one link to another

Script

autolink.pl
#!/usr/bin/perl
 
sub substi
{
 local $file = $_[0];
 
 local $str  = $_[1];
 local $ns   = $_[2];
 
 open(FD,$file);
 open(FD_TEMP,'>file.tmp') or die;
 while(<FD>)
 {
 
   if(/==+/ || /   *[^*]/)
   {
        print FD_TEMP;
   }
   else
   {
       $line = $_;
       $i = 0;
       $in_link = 0;
       $ignore = 0;
       $in_link = 0;
       @fields = split(/(\[\[|\]\]|\}\}|\{\{)/,$line);
       foreach $f (@fields)
       {
           if(($f eq "[[" || $f eq "{{") && $ignore == 0)
           {
             $ignore = 1;
             if ($f eq "[[") {$in_link = 1;}
           }
           elsif (($f eq "]]" || $f eq "}}") && $ignore == 1)
           {
             $ignore = 0;
 
             if ($f eq "]]") {$in_link = 0;}
           }
 
 
           if ($ignore == 1)
           {
             if($in_link == 1)
             {
             $f =~ /(.+)\|(.+)/;
             $l = $1;
             $w = $2;
             if($w =~  /$str/i)
             {
               print FD_TEMP $ns."|".$w;
             }
             else
             {
              print FD_TEMP $f;
             }
             }
             else
             {
              print FD_TEMP $f;
             }
           }
           else
           {
            if($f =~ /($str)/i)
            {
             $f =~  s/([\(\"\)., :\*{2}\_{2}])($str)([\)\(., :\"\*{2}\_{2}])/$1\[\[$ns\|$2\]\]$3/gi;
             $f =~  s/^($str)([\)\(., :\"\*{2}\_{2}])/\[\[$ns\|$1\]\]/gi;
             $f =~  s/([\(\)., :\"\*{2}\_{2}])($str)$/\[\[$ns\|$1\]\]/gi;
 
#print $f."<br/>";
}
             print FD_TEMP $f;
           }
 
         }
    }
 
 }
          close(FD_TEMP);
          close(FD);
 
          unlink ($file);
          rename ('file.tmp',$file);
          chmod  (0770,$file);
 
}
 
sub apply_to
{
  local $ns       = $_[0];
  local $str      = $_[1];
  local $link     = $_[2];
 
  local $fp = $ns . ".txt";
  if(-f $fp)
  {
    substi($fp,$str,$link);
  }
 
  opendir(DIR,$ns) or return;
  my @files = readdir(DIR);
  closedir(DIR);
 
  foreach my $file (@files)
  {
        next if $file =~ /^\.\.?$/;
        next if $file =~ /autolink.txt/;
        $file = $ns."/".$file;
 
 
        if (-f $file)
        {
#print $file." ".$str." ".$link."<br/> ";
          substi($file,$str,$link);
        }
        elsif (-d $file)
        {
          &apply_to($file,$str,$link);
        }
  }
}
 
 
sub autolink
{
  local $link_file = $_[0];
  local $data_dir  = $_[1];
 
 
  open(FD_L,$link_file) || die ("$link_file Not Found");
  while(<FD_L>)
  {
    @sub = split(/\|/,$_);
    if($sub[1] ne "")
    {
     $sub[1] =~ s/:/\//g;
 if (substr($data_dir,length($data_dir),-1) ne "/")
        {$data_dir .= "/";}
      &apply_to($data_dir.$sub[1],$sub[2],$sub[3]);
    }
  }
  close(FD_L);
 
 
 
}
 
 
 
if($#ARGV != 1)
{
  print "usage: ./autolink.pl link_file data_directory
 
  where link_file
  is a txt formated like this
    |<aplication namespace>|<name>|<dokuwiki link>|
    ...
  sample
    |eng|toto|eng:start:toto|
 
  where data_directory
  id the main dokuwiki page directory to apply rules
 
";
}
else
{
  autolink($ARGV[0],$ARGV[1]);
}

Deployment

  1. copy script into your web directory
  2. create a link page into your wiki (in sample this page is named autolink and is on the root page directory)
  3. cron script each night

autolink.pl <link_file> <data_directory>

cron sample :

 40 0 * * * cd /var/www/html/scripts/ ; perl autolink.pl /var/www/html/wiki/data/pages/autolink.txt /var/www/html/wiki/data/pages
  • Only line started with '|' char are analysed. Then you can create Title, table, or adding text into autolink page.
  • no space into table line, except if you want to match spaces
  • it's case insensitive matching, “Toto” or “toto” in the wiki ,no differences
  • autolink.txt pages are ignored
  • for matchin whole wiki directory ':' in table (see sample)

The analysed line format is :

|<appliance namespace>|<word_to_link>|<link>|
====== Autolink ======

== Fruits ==

^ appliance namespace  ^ word  ^ link  ^
|fruits|apple|fruits:apple|

== Instruments ==

^ appliance namespace  ^ word  ^ link  ^
|instruments:wind|flute|instruments:cord:flute|
|instruments:cord|violin|instruments:cord:violin|

== All == 

^ appliance namespace  ^ word  ^ link  ^
|:|instrument|instruments|
|:|fruit|fruits|

this page will link

  • apple to fruits:apple into namesapce fruits
  • flute to instruments:cord:flute into instruments:wind
tips/autolink.txt · Last modified: 2015-01-29 16:46 by 84.204.72.82