| 1 |
#!/usr/bin/perl |
| 2 |
|
| 3 |
############################################################################ |
| 4 |
# This file is part of FreeFEM. # |
| 5 |
# # |
| 6 |
# FreeFEM is free software: you can redistribute it and/or modify # |
| 7 |
# it under the terms of the GNU Lesser General Public License as # |
| 8 |
# published by the Free Software Foundation, either version 3 of # |
| 9 |
# the License, or (at your option) any later version. # |
| 10 |
# # |
| 11 |
# FreeFEM is distributed in the hope that it will be useful, # |
| 12 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of # |
| 13 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # |
| 14 |
# GNU Lesser General Public License for more details. # |
| 15 |
# # |
| 16 |
# You should have received a copy of the GNU Lesser General Public License # |
| 17 |
# along with FreeFEM. If not, see <http://www.gnu.org/licenses/>. # |
| 18 |
############################################################################ |
| 19 |
# SUMMARY : ... |
| 20 |
# LICENSE : LGPLv3 |
| 21 |
# ORG : LJLL Universite Pierre et Marie Curie, Paris, FRANCE |
| 22 |
# AUTHORS : Build an index of all Emacs org-mode hyperlinks |
| 23 |
# E-MAIL : Antoine Le Hyaric |
| 24 |
|
| 25 |
use strict; |
| 26 |
use warnings; |
| 27 |
use threads; |
| 28 |
use threads::shared; |
| 29 |
use Path::Class; # relative() |
| 30 |
use File::Basename; # dirname() |
| 31 |
use Cwd; # getcwd() |
| 32 |
|
| 33 |
# Scan all files in the FF directory tree |
| 34 |
|
| 35 |
my $pwd=getcwd(); |
| 36 |
my %anchors; |
| 37 |
share %anchors; |
| 38 |
my @q; |
| 39 |
share @q; |
| 40 |
my $dir=''; |
| 41 |
my @ignore=`cat .hgignore`; |
| 42 |
foreach my $f(`find . -type f`){push @q,$f} |
| 43 |
|
| 44 |
# core count inspired from [[file:../../../alh/perl/System.ph::nbcores]] |
| 45 |
sub nbcores{ |
| 46 |
my $n=1; |
| 47 |
if(-e "/proc/cpuinfo"){$n=`grep 'processor.*:' /proc/cpuinfo|wc -l`} # linux |
| 48 |
elsif(-x "/usr/sbin/sysctl"){$n=`sysctl -n hw.ncpu`} # macos |
| 49 |
elsif(defined $ENV{NUMBER_OF_PROCESSORS}){$n=$ENV{NUMBER_OF_PROCESSORS}} # windows |
| 50 |
return $n; |
| 51 |
} |
| 52 |
|
| 53 |
# start as many threads as possible |
| 54 |
my @threads; |
| 55 |
for(my $i=0;$i<nbcores();$i++){push @threads,threads->create(\&scanfiles)} |
| 56 |
foreach(@threads){$_->join()} |
| 57 |
|
| 58 |
sub scanfiles{ |
| 59 |
while(defined(my $f=pop @q)){ |
| 60 |
|
| 61 |
# relative path name is required for the index to be operational on any user machine |
| 62 |
$f=~s/^\.\///; |
| 63 |
chomp $f; |
| 64 |
my $frel=file($f)->relative($pwd); |
| 65 |
|
| 66 |
# files to skip because of .hgignore |
| 67 |
my $found=0; |
| 68 |
foreach my $r(@ignore){ |
| 69 |
chomp $r; |
| 70 |
if($f=~/$r/){ |
| 71 |
$found=1; |
| 72 |
last; |
| 73 |
} |
| 74 |
} |
| 75 |
next if $found; |
| 76 |
|
| 77 |
# files to skip for other reasons |
| 78 |
next if $f=~/\.(bmp|png|jpg|eps|pdf|tar|gz|zip|tgz)$/; # not text |
| 79 |
next if $f=~/\.hg\//; # not text |
| 80 |
next if $f=~/examples\+\+-load\/include\//; # duplicated text |
| 81 |
|
| 82 |
# make sure that we don't leave absolute paths in the hyperlinks because these would not work on other machines |
| 83 |
|
| 84 |
my $cref=`cat $f`; |
| 85 |
my $c=$cref; |
| 86 |
while($cref=~m/\[\[file:([^:\]]+)(.*)\]\]/g){ |
| 87 |
|
| 88 |
# hyperlink potentially containing an absolute path |
| 89 |
my $labs=$1; |
| 90 |
if($labs=~/^~\//){ |
| 91 |
|
| 92 |
# set HOME value |
| 93 |
my $labshome=$labs; |
| 94 |
$labshome=~s/^~/$ENV{HOME}/; |
| 95 |
|
| 96 |
# change absolute path to relative in file contents |
| 97 |
my $lrel=file($labshome)->relative(dirname($f)); |
| 98 |
my $labsqm=quotemeta($labs); |
| 99 |
$c=~s/\[\[file:$labsqm/\[\[file:$lrel/g; |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
# update file if some paths were changed |
| 104 |
|
| 105 |
if($c ne $cref){ |
| 106 |
print "Changed hyperlink paths to relative in $f\n"; |
| 107 |
open FILE,">$f" or die; |
| 108 |
print FILE $c; |
| 109 |
close FILE; |
| 110 |
} |
| 111 |
|
| 112 |
# find name anchors in file contents |
| 113 |
|
| 114 |
while($c=~m/<<([^<> ,{}]+)>>/g){$anchors{$1}.=" [[file:${frel}::$1][$frel]]"} |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
# print out all existing hyperlink anchors |
| 119 |
|
| 120 |
print "\n"; |
| 121 |
open OUT,">index.org" or die; |
| 122 |
print OUT "# -*- mode:org;coding:utf-8 -*-\n"; |
| 123 |
print OUT "# Hyperlinks into the FreeFEM source, built with [[file:build/orgindex]]\n"; |
| 124 |
print OUT "# (do not modify this file manually: update the source files and run [[file:build/orgindex]] again)\n"; |
| 125 |
print OUT "\n"; |
| 126 |
|
| 127 |
foreach(sort {uc($a) cmp uc($b)} keys %anchors){ |
| 128 |
print OUT "$_"; |
| 129 |
my $padding=25; |
| 130 |
if(length($_)<$padding){print OUT " "x($padding-length($_))} |
| 131 |
print OUT "$anchors{$_}\n"; |
| 132 |
} |
| 133 |
|
| 134 |
close OUT; |
| 135 |
|
| 136 |
# Local Variables: |
| 137 |
# mode:cperl |
| 138 |
# ispell-local-dictionary:"british" |
| 139 |
# coding:utf-8 |
| 140 |
# End: |