tips:autolink
This is an old revision of the document!
Table of Contents
Autolink
author : Vincent Fleury
mail : fleury.vincent[at]gmail.com
status : experimental script (used into my compagny)
Usage
this script can used to :
- automaticly addings links to a word
- changing a link to another
Script
#/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; $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}])($str)([., :\*{2}])/$1\[\[$ns\|$2\]\]$3/gi; $f =~ s/^($str)/\[\[$ns\|$1\]\]/gi; $f =~ s/($str)$/\[\[$ns\|$1\]\]/gi; } 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); return; } opendir(DIR,$ns) or return; my @files = readdir(DIR); closedir(DIR); foreach my $file (@files) { next if $file =~ /^\.\.?$/; $file = $ns."/".$file; if (-f $file) { substi($file,$str,$link); } elsif (-d $file) { &apply_to($file,$str,$link); } } } sub autolink { local $link_file = $_[0]; local $data_dir = $_[1]; open(FD_LINK,$link_file) || die ("$link_file Not Found"); while(<FD_LINK>) { @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_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]); }
Deployement
- copy script into your web directory
- create a link page into your wiki (in sample this page is named autolink and is on the root page directory)
- 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
The link page
- Only line started with '|' char are analysed. Then you can create Title, table, or adding text into autolink page.
The analysed line format is :
|<appliance namespace>|<word_to_link>|<link>|
Sample of link page
====== 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.1269352580.txt.gz · Last modified: 2010-03-23 14:56 by 192.54.144.229