-
Notifications
You must be signed in to change notification settings - Fork 0
/
sh2doc.pl
executable file
·283 lines (242 loc) · 8.47 KB
/
sh2doc.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
#!/usr/bin/perl
#
##BRIEF
# Script for automatic documentation
# of shell scripts
#
##AUTHOR
# Ronni Grapenthin
#
##DATE
# 2007-11-20
#
##DETAILS
#
# This script runs though all directories specified in the field @DIRS and
# looks at all files whether they contain special documentation makers like:
# <ul>
# <li>##BRIEF</li>
# <li>##AUTHOR</li>
# <li>##DETAILS</li>
# <li>##DATE</li>
# <li>##CHANGELOG</li>
# </ul>
# If any of these are found, the file is added to an overview contained in an
# index.html file which lists file name (and a link to a detailed description).
# Furthermore another HTML file is created which contains detailed information
# of the file given in the documentation. Additional meta data of the file such as
# last changed could be added.<br/>
# A line that (starts with spaces only and) contains a comment starting with '#+#'
# will be added to the details. <p>Empty lines or "##" denote that the subsection
# (e.g. DETAILS) is finished. So use minimal HTML for paragraph formatting and line
# breaks. </p>
#
##CHANGELOG
#
# 2008-01-28 (rn): search for executables, includes non-tagged files / binaries in index.<br>
# 2008-01-29 (rn): sorts filenames, gives separate tables for each letter<br>
# 2009-10-12 (rn): added command line options<br>
#
use File::Copy;
use File::Find;
use File::Basename;
use Getopt::Long;
#CONFIGURATION
########################################################
#Array holding directories whose files are to be parsed (special delimiters in their comment section):
@DIRS;
$INDEX_FILE = "index.html";
$DETAIL_DIR = "details";
@FILES;
$first_letter = "z";
$ALPHABET = "| ";
$LINKS = 0;
$DOCUMENTED = 0;
#CHECK USAGE
if( $#ARGV + 1 < 2)
{
usage();
exit();
}
#read in commandline params:
GetOptions ( 'in-dir=s' => \$IN_DIR, #directory (list, colon separated) containing files to be parsed
'out-dir=s' => \$OUT_DIR, #path to output directory
'help' => \$help); #path to output directory
#print help
if ($help) { usage(); exit();}
#check whether output directory exists ...
if (! -d $OUT_DIR) { die("ERROR: Output directory does not exist: '$out_dir'.\n"); }
#add index and detail names to out dir
$INDEX_FILE = "$OUT_DIR/$INDEX_FILE";
$DETAIL_DIR = "$OUT_DIR/$DETAIL_DIR";
#create detail dir, if non-existant
if (! -d $DETAIL_DIR) { mkdir $DETAIL_DIR; }
#compile DIRS that are to be parsed
@DIRS = split /:/, $IN_DIR;
#create indexfile
copy("sh2doc_overview.head", $INDEX_FILE);
#copy("style.css", $OUT_DIR);
#+# fill @FILES with files found in directories given by @DIRS
print "Selecting files from directories: @DIRS\n";
#+# grep all files that are executable and not a directory
find(\&wanted, @DIRS);
#+# sort filenames (depending on first letter of the file, not the full path!)
@FILES = sort filename_sort @FILES;
#+# iterate over all files and check whether they contain tagged comments
foreach $file (@FILES)
{
print "working on file: $file ... \n";
create_doc_for($file);
}
#+# read index file and include alphabet selection
open (INDEX, "< $INDEX_FILE") or die "could not open file $INDEX_FILE: $!\n";
while(<INDEX>){
s/#ALPHABET#/$ALPHABET/;
$index .= $_
}
close(INDEX);
#+# final writing of index file ... this is inefficient, optimize, if you can!
open (INDEX, "> $INDEX_FILE") or die "could not open file $INDEX_FILE: $!\n";
print INDEX $index;
open (TAIL, "sh2doc_overview.tail") or die "could not open file sh2doc_overview.tail: $!\n";
@all_tail = <TAIL>;
print INDEX @all_tail;
close(TAIL);
close(INDEX);
$files = @FILES;
$dirs = @DIRS;
print "\n------------------------------------------\n";
print "Documented $DOCUMENTED of $files files in $dirs directories.\n";
print "created symbolic links in $DETAIL_DIR.\n";
print "------------------------------------------\n";
#----------- SUBROUTINES
#compare basenames of files, hence we have to get rid of the path ... uses File::Basename
sub filename_sort {
($fa, $dir, $s) = fileparse($a);
($fb, $dir, $s) = fileparse($b);
$fa cmp $fb;
}
# find all executables of a directory which are not directories themselves. append these to the global FILES array.
# however, exclude subdirectories. File::Find::prune = 1 does not work, hence check whether actual directory
# is in the global search array DIRS.
sub wanted {
if ( grep { $_ eq $File::Find::dir} @DIRS )
{
push(@FILES, $File::Find::name) if (-x || -X) && ! -d;
}
}
# creates doc for a given file - main routine, but mostly readings
sub create_doc_for ($) {
local($ff) = @_;
my $doc_found = 0;
my $author = "";
my $details = "";
my $changelog = "";
my $brief = "";
my $date = "";
my $f = $ff;
my $curpos = 0;
# we expect the fullpath to be given
($f, $dir, $s) = fileparse($ff, '\..*');
open (TODOC, $dir."/".$f.$s) or die "could not open file $ff: $!\n";
open (INDEX, ">>$INDEX_FILE") or die "could not open file $INDEX_FILE: $!\n";
while (<TODOC>)
{
$curpos = tell TODOC;
if(/^##BRIEF/){ $brief .= get_details($ff, $curpos); $doc_found = 1; }
elsif(/^##AUTHOR/){ $author .= get_details($ff, $curpos); $doc_found = 1; }
elsif(/^##CHANGELOG/){ $changelog .= get_details($ff, $curpos); $doc_found = 1; }
elsif(/^##DETAILS/){ $details .= get_details($ff, $curpos); $doc_found = 1; }
elsif(/^##DATE/){ $date .= get_details($ff, $curpos); $doc_found = 1; }
elsif(/^(\s*)(#\+#)(?!#)/)
{ s/#\+#//;
$details .= "$_"; $doc_found = 1; }
}
#if there is special documentation ... add it to the files
if($doc_found)
{
++$DOCUMENTED;
# create symbolic link to source
symlink("$ff", "$DETAIL_DIR/$f$s") or print "could not create link to $ff: $!\n";
#check whether new table is to be opened
my @chars = split(//, $f);
if( @chars[0] ne $first_letter)
{
$first_letter = @chars[0];
# add to the index file
print INDEX "</table>\n<a name=\"$first_letter\"></a><h2>$first_letter</h2>\n \
<table border=\"1\"> \n\
<tr><td>script</td><td>directory</td><td>short description</td>\n";
$ALPHABET .= " <a href=\"#$first_letter\">$first_letter</a> |";
}
# add to the index file
print INDEX " <tr><td> <a href=\"$DETAIL_DIR/$f$s\">$f$s</a> </td>\
<td> $dir </td>\
<td> $brief <a href=\"$DETAIL_DIR/$f$s.html\">(more)</a> </td>\n";
# open temp file, create details file
open (TEMP, "sh2doc_detail.template");
open (DETAILS, ">$DETAIL_DIR/$f$s.html") or die "could not open file $f$s.html: $!\n";
while(<TEMP>)
{
s/#AUTHOR#/$author/;
s/#CHANGELOG#/$changelog/;
s/#DETAILS#/$details/;
s/#BRIEF#/$brief/;
s/#FILENAME#/$f/;
s/#DATE#/$date/;
s/#SOURCE#/$f/;
print DETAILS $_;
}
close(DETAILS);
close(TEMP);
}
# else
# {
# $details = (-B $ff ? "Is binary." : "No tagged comments.");
#
# # add to the index file ... labeled as not documented
# print INDEX " <tr><td> <a href=\"$DETAIL_DIR/$f$s\">$f$s</a> </td>\
# <td> $dir </td>\
# <td> $details </td>\n";
# }
close(INDEX);
close(TODOC);
}
sub get_details($$)
{
local ($f, $pos) = @_;
open (FF, $f) or die "could not open file $f: $!\n";
seek FF,$pos,0;
$ret ="";
while(<FF>)
{
last if (!/^#/);
last if (/^##/);
s/#//g;
if (/[A-Za-z0-9]/)
{
$ret .= $_ . "\n";
}
}
close(FF);
return $ret;
}
sub usage()
{
#do the printing
print "$0 Ronni Grapenthin 12 October 2009 \n";
print "http://www.gps.alaska.edu/ronni/tools/sh2doc.html \n";
print "Copyright (C) 2007 through 2009, Ronni Grapenthin \n";
print "$0 comes with ABSOLUTELY NO WARRANTY \n";
print "This is free software licensed under the Gnu General Public License version 3 \n\n";
print "Usage: $0 [options] \n\n";
print "Options: \n";
print " --in-dir Directory to be parsed. A list can be given.\n";
print " Separate multiple directories using ':' .\n";
print " --out-dir Output directory. The created documentation will be saved there.\n";
print " --help This help.\n\n";
print "Example: \n";
print " sh2doc.pl --in-dir=.:/etc:/usr/local/bin --out-dir=. \n\n";
print " Parses current directory, /etc, and /usr/local/bin and writes results to\n";
print " current directory (creates index.html and 'details' directory in '.' \n\n";
}