DokuWiki

It's better when it's simple

User Tools

Site Tools


tips:autolink

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
tips:autolink [2010-03-23 14:52] 192.54.144.229tips:autolink [2015-01-29 16:46] (current) – [Sample of link page] 84.204.72.82
Line 3: Line 3:
 author : Vincent Fleury\\  author : Vincent Fleury\\ 
 mail : fleury.vincent[at]gmail.com\\  mail : fleury.vincent[at]gmail.com\\ 
-status : **experimental script** (used into my compagny)+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 ===== ===== Script =====
  
-  #/usr/bin/perl +<code perl autolink.pl> 
-   +#!/usr/bin/perl 
-  sub substi + 
-  +sub substi 
-   local $file = $_[0]; +
-   + local $file = $_[0]; 
-   local $str  = $_[1]; + 
-   local $ns   = $_[2]; + local $str  = $_[1]; 
-   + local $ns   = $_[2]; 
-   open(FD,$file); + 
-   open(FD_TEMP,'>file.tmp') or die; + open(FD,$file); 
-   while(<FD>+ open(FD_TEMP,'>file.tmp') or die; 
-      + while(<FD>
-     if(/==+/ || /   *[^\*]) + { 
-     + 
-          print FD_TEMP; +   if(/==+/ || /   *[^*]/
-     +   
-     else +        print FD_TEMP; 
-     +   
-         $line = $_; +   else 
-         $i = 0; +   
-         $ignore = 0; +       $line = $_; 
-         $in_link = 0;        +       $i = 0; 
-         @fields = split(/(\[\[|\]\]|\}\}|\{\{)/,$line); +       $in_link = 0; 
-       foreach $f (@fields) +       $ignore = 0; 
 +       $in_link = 0; 
 +       @fields = split(/(\[\[|\]\]|\}\}|\{\{)/,$line); 
 +       foreach $f (@fields)
        {        {
            if(($f eq "[[" || $f eq "{{") && $ignore == 0)            if(($f eq "[[" || $f eq "{{") && $ignore == 0)
-    {+           {
              $ignore = 1;              $ignore = 1;
              if ($f eq "[[") {$in_link = 1;}              if ($f eq "[[") {$in_link = 1;}
            }            }
            elsif (($f eq "]]" || $f eq "}}") && $ignore == 1)            elsif (($f eq "]]" || $f eq "}}") && $ignore == 1)
-    +           
-      $ignore = 0;+             $ignore = 0; 
              if ($f eq "]]") {$in_link = 0;}              if ($f eq "]]") {$in_link = 0;}
            }            }
 +
 +
            if ($ignore == 1)            if ($ignore == 1)
-    {+           {
              if($in_link == 1)              if($in_link == 1)
-      {+             {
              $f =~ /(.+)\|(.+)/;              $f =~ /(.+)\|(.+)/;
              $l = $1;              $l = $1;
              $w = $2;              $w = $2;
              if($w =~  /$str/i)              if($w =~  /$str/i)
-      +             
-               print FD_TEMP $ns."|".$w;   +               print FD_TEMP $ns."|".$w;
              }              }
              else              else
-      {+             {
               print FD_TEMP $f;               print FD_TEMP $f;
              }              }
-      }+             }
              else              else
-      {+             {
               print FD_TEMP $f;               print FD_TEMP $f;
              }              }
-           +           }
            else            else
-    +           
-            if($f =~ /$str/i) +            if($f =~ /($str)/i) 
-       +            
-             $f =~  s/([., :\*{2}])($str)([., :\*{2}])/$1\[\[$ns\|$2\]\]$3/gi; +             $f =~  s/([\(\"\)., :\*{2}\_{2}])($str)([\)\(., :\"\*{2}\_{2}])/$1\[\[$ns\|$2\]\]$3/gi; 
-             $f =~  s/^($str)/\[\[$ns\|$1\]\]/gi; +             $f =~  s/^($str)([\)\(., :\"\*{2}\_{2}])/\[\[$ns\|$1\]\]/gi; 
-             $f =~  s/($str)$/\[\[$ns\|$1\]\]/gi; +             $f =~  s/([\(\)., :\"\*{2}\_{2}])($str)$/\[\[$ns\|$1\]\]/gi; 
-      }+ 
 +#print $f."<br/>"; 
 +}
              print FD_TEMP $f;              print FD_TEMP $f;
-             +           } 
-   + 
-    }+         }
     }     }
-   + 
-  }+ }
           close(FD_TEMP);           close(FD_TEMP);
-          close(FD); +          close(FD); 
           unlink ($file);           unlink ($file);
-          rename ('file.tmp',$file);       +          rename ('file.tmp',$file); 
-          chmod  (0770,$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);
   }   }
-   + 
-  sub apply_to+  opendir(DIR,$ns) or return; 
 +  my @files = readdir(DIR); 
 +  closedir(DIR); 
 + 
 +  foreach my $file (@files)
   {   {
-    local $ns       = $_[0];  
-    local $str      = $_[1]; 
-    local $link     = $_[2];  
-    local $fp = $ns . ".txt"; 
-     
-    if(-f $fp) 
-    { 
-      substi($fp,$str,$link); 
-      return;  
-    }   
-   
-    opendir(DIR,$ns) or return; 
-    my @files = readdir(DIR); 
-    closedir(DIR); 
-     
-    foreach my $file (@files)  
-    { 
         next if $file =~ /^\.\.?$/;         next if $file =~ /^\.\.?$/;
 +        next if $file =~ /autolink.txt/;
         $file = $ns."/".$file;         $file = $ns."/".$file;
 +
 +
         if (-f $file)         if (-f $file)
- {+        { 
 +#print $file." ".$str." ".$link."<br/> ";
           substi($file,$str,$link);           substi($file,$str,$link);
         }         }
-        elsif (-d $file)  +        elsif (-d $file) 
-        {   +        {
           &apply_to($file,$str,$link);           &apply_to($file,$str,$link);
         }         }
-    } 
   }   }
-   +
-  sub autolink+ 
 + 
 +sub autolink 
 +
 +  local $link_file = $_[0]; 
 +  local $data_dir  = $_[1]; 
 + 
 + 
 +  open(FD_L,$link_file) || die ("$link_file Not Found"); 
 +  while(<FD_L>)
   {   {
-    local $link_file = $_[0];  +    @sub split(/\|/,$_)
-    local $data_dir  = $_[1];  +    if($sub[1] ne "")
-   +
-    open(FD_LINK,$link_file) || die ("$link_file Not Found");   +
-    while(<FD_LINK>)+
     {     {
-      @sub = split(/\|/,$_); +     $sub[1] =~ s/:/\//g; 
-      if($sub[1] ne "")  + if (substr($data_dir,length($data_dir),-1) ne "/"
-      { +        {$data_dir .= "/";
-        $sub[1] =~ s/:/\//g; +      &apply_to($data_dir.$sub[1],$sub[2],$sub[3]);
-        if (substr($data_dir,length($data_dir),-1) ne "/"+
-          {$data_dir .= "/";+
-        &apply_to($data_dir.$sub[1],$sub[2],$sub[3]);   +
-      }+
     }     }
-    close(FD_LINK);    
-  } 
-     
-     
-     
-  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]); 
   }   }
 +  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]);
 +}
 +
 +
 +
 +
 +</code>
  
  
-===== Deployement =====+===== Deployment =====
  
   - copy script into your web directory   - copy script into your web directory
Line 176: Line 206:
 ===== The link page ===== ===== The link page =====
  
-  * **Only line started with '|' char are analysed**. Then you can create Title, table, or adding text into autolink page.\\ \\ +  * **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 :__ __The analysed line format is :__
   |<appliance namespace>|<word_to_link>|<link>|   |<appliance namespace>|<word_to_link>|<link>|
  
-  * this script : **remplace existings links** to new one 
-  * ignore :  title, image 
  
 ==== Sample of link page ==== ==== Sample of link page ====
Line 211: Line 244:
   * **apple** to **fruits:apple** into namesapce **fruits**   * **apple** to **fruits:apple** into namesapce **fruits**
   * **flute** to **instruments:cord:flute** into **instruments:wind**   * **flute** to **instruments:cord:flute** into **instruments:wind**
-  * ...+  * ...  
 + 
  
tips/autolink.1269352379.txt.gz · Last modified: 2010-03-23 14:52 by 192.54.144.229

Except where otherwise noted, content on this wiki is licensed under the following license: CC Attribution-Share Alike 4.0 International
CC Attribution-Share Alike 4.0 International Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki