Commit initial
[perso/Immae/Projets/Scripts/Public.git] / gen_html_documents
1 #!/usr/bin/env perl
2 # The MIT License (MIT)
3 #
4 # Copyright (c) 2011-2015 Ismaƫl Bouya http://www.normalesup.org/~bouya/
5 #
6 # Permission is hereby granted, free of charge, to any person obtaining a copy
7 # of this software and associated documentation files (the "Software"), to deal
8 # in the Software without restriction, including without limitation the rights
9 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 # copies of the Software, and to permit persons to whom the Software is
11 # furnished to do so, subject to the following conditions:
12 #
13 # The above copyright notice and this permission notice shall be included in
14 # all copies or substantial portions of the Software.
15 #
16 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 # THE SOFTWARE.
23
24 use strict;
25 use File::Basename;
26
27 sub parse_config_file {
28 my ($config_line, $Name, $Value, $Config);
29 (my $File, $Config) = @_;
30 if (!open (CONFIG, "$File")) {
31 print "ERROR: Config file not found : $File";
32 exit(0);
33 }
34 my $multiline = 0;
35
36 while (<CONFIG>) {
37 $config_line=$_;
38 chop ($config_line);
39 $config_line =~ s/^\s*//;
40 $config_line =~ s/\s*$//;
41 if ( ($config_line !~ /^#/) && ($config_line ne "") ){
42 if ($multiline) {
43 $$Config{$Name} =~ s/\\$//;
44 $$Config{$Name} .= $config_line;
45 } else {
46 ($Name, $Value) = split (/\s*=\s*/, $config_line);
47 $Value =~ s/^~/$ENV{"HOME"}/;
48 $$Config{$Name} = $Value;
49 }
50 $multiline = ($$Config{$Name} =~ /\\$/);
51 }
52 }
53 close(CONFIG);
54 }
55
56 my %Config;
57 &parse_config_file ($ENV{"HOME"}."/.gen_html_documents.rc", \%Config);
58
59 my $entete = $Config{"entete"};
60 my $avant = $Config{"avant"};
61 my $milieu = $Config{"milieu"};
62 my $apres = $Config{"apres"};
63 my $html = $Config{"html"};
64
65 my @ignore = split (/\s*,\s*/, $Config{"ignore"});
66 my @ext = split (/\s*,\s*/, $Config{"ext"});
67
68 my $documents = $Config{"documents"};
69 my $droot = $Config{"dossier_web"};
70
71 $Config{"dossiers"} =~ s/^"//;
72 $Config{"dossiers"} =~ s/"$//;
73 my %dossiers = split (/"?\s*,\s*"?/, $Config{"dossiers"});
74
75 my ($dossier,$description);
76
77 open F, ">".$milieu;
78
79 while(($dossier,$description) = each(%dossiers)) {
80 opendir(DIR, $documents.$dossier) || warn $documents.$dossier." coulnd't be opened\n";
81 my @content = grep {$_ !~ /^\.\.?$/} readdir(DIR);
82 my @sorted = sort { lc($a) cmp lc($b) } @content;
83 closedir(DIR);
84
85 print F "<h3>".$description."</h3>\n";
86 print F "<ul>\n";
87 foreach my $subpath (grep { -d $documents.$dossier.'/'.$_} @sorted) {
88 subparse($dossier,$subpath,4);
89 }
90 foreach my $file (grep { -f $documents.$dossier.'/'.$_} @sorted) {
91 my($filename, $directories, $suffix) = fileparse($file,qr/\.[^\.]*/);
92 next if(!in_array(\@ext,lc($suffix)));
93 $filename =~ tr/_/ /;
94 print F "<li><a href='".$droot.$dossier.'/'.$file."'>".$filename."</a></li>\n";
95 }
96 print F "</ul>\n";
97 }
98
99 sub subparse {
100 my $path = shift;
101 my $dossier = shift;
102 my $rang = shift;
103
104 opendir(DIR, $documents.$path."/".$dossier);
105 my @content = grep {$_ !~ /^\.\.?$/} readdir(DIR);
106 my @sorted = sort { lc($a) cmp lc($b) } @content;
107 closedir(DIR);
108
109 return if(in_array(\@ignore,$dossier));
110 my $dossierspace = $dossier;
111 $dossierspace =~ tr/_/ /;
112 print F "<li>".$dossierspace."\n";
113 print F "<ul>\n";
114 foreach my $subpath (grep { -d $documents.$path."/".$dossier.'/'.$_} @sorted) {
115 subparse($path."/".$dossier,$subpath,$rang+1);
116 }
117 foreach my $file (grep { -f $documents.$path."/".$dossier.'/'.$_} @sorted) {
118 my($filename, $directories, $suffix) = fileparse($file,qr/\.[^\.]*/);
119 next if(!in_array(\@ext,lc($suffix)));
120 $filename =~ tr/_/ /;
121 print F "<li><a href='".$droot.$path.'/'.$dossier.'/'.$file."'>".$filename."</a></li>\n";
122 }
123 print F "</ul></li>\n";
124 }
125
126 sub in_array {
127 my ($arr,$search_for) = @_;
128 my %items = map {$_ => 1} @$arr; # create a hash out of the array values
129 return (exists($items{$search_for}))?1:0;
130 }
131
132 close F;
133
134 exec "cat $entete $avant $milieu $apres 1> $html" or die "$!\n";
135 exec "chmod -R go=rX,u=rwX $documents"