-
Notifications
You must be signed in to change notification settings - Fork 2
/
mk_ahelp_pages.pl
executable file
·504 lines (425 loc) · 13.7 KB
/
mk_ahelp_pages.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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
#!/usr/bin/env perl -w
#
# Usage:
# mk_ahelp_pages.pl [name1 ... namen]
# --config=name
# --type=test|live|trial
# --localxslt
# --verbose
#
# The default is --type=test, which sets up for test web site.
# The live option sets things up for the live (ie cxc.harvard.edu) site.
# Don't use the trial option unless you know what it does.
#
# The --config option gives the path to the configuration file; this
# defaults to config.dat in the same directory as the script.
#
# The --localxslt option is used for testing; it overrides the
# %stylesheets setting in the config file, using the location
# of this script instead.
#
# The --verbose option is useful for testing/debugging the code.
#
# Aim:
# Convert an ahelp XML file (or more than one) to HTML format.
#
# If no files are given then process all *xml files in the doc/xml/ and
# contrib/doc/xml/ sub-directories of the ahelpfiles directory
# given in the config file.
# This *MAY* change.
#
# Files are only processed if the XML file is newer than the HTML
# file, or the index file is newer than the HTML file (ie the
# seealso info may have been updated)
#
# Creates:
# Files in the storage location given in the config file
#
# Requires:
# in styledir
# ahelp.xsl
# ahelp_common.xsl
#
# Author:
# Doug Burke ([email protected])
#
# To Do:
# -
#
# Future?:
# -
#
use strict;
$|++;
use Carp;
use Getopt::Long;
use Cwd;
use IO::File;
use File::Basename;
use FindBin;
use lib $FindBin::Bin;
use CIAODOC qw( :util :xslt :cfg :deps );
## Subroutines (see end of file)
#
sub read_ahelpindex ($);
sub find_ahelpfiles ($$);
## set up variables that are also used in CIAODOC
use vars qw( $configfile $verbose $group $site );
$configfile = "$FindBin::Bin/config.dat";
$verbose = 0;
$group = "";
$site = "";
## Variables
#
my $progname = (split( m{/}, $0 ))[-1];
my $usage = <<"EOD";
Usage:
$progname --config=name --type=test|live|trial --localxslt --verbose [file(s)]
The default is --type=test, which publishes to the test web site.
The live option publishes to the live (ie cxc.harvard.edu) site.
Don\'t use the trial option unless you know what it does.
The --config option gives the path to the configuration file; this
defaults to config.dat in the same directory as the script.
The --localxslt option is used for testing; it overrides the
\%stylesheets setting in the config file, using the location
of this script instead.
The --verbose option is useful for testing/debugging the code.
EOD
## Code
#
# this will be mangled later
my $dname = cwd();
# make sure you are in an ahelp directory
my @dirs = split /\//, $dname;
unless ($dirs[-1] =~ "ahelp") {
die "This script should be run from the 'ahelp' subdirectory\n";
}
# handle options
my $type = "test";
my $localxslt = 0;
die $usage unless
GetOptions
'config=s' => \$configfile,
'type=s' => \$type,
'localxslt!' => \$localxslt,
'verbose!' => \$verbose;
# what OS are we running?
#
my $ostype = get_ostype;
# check the options
my $config = parse_config( $configfile );
dbg "Parsed the config file";
# most of the config stuff is parsed below, but we need these two here
my $site_config;
( $site, $site_config ) = find_site $config, $dname;
$config = undef; # DBG: just make sure no one is trying to access it
dbg "Site = $site";
check_type_known $site_config, $type;
dbg "Type = $type";
dbg "OS = $ostype";
check_ahelp_site_valid $site;
# Handle the remaining config values
#
# shouldn't have so many global variables...
# - the depth returned by check_location is
# a dummy variable which we manipulate (so
# perl doesn't complain) and then don't do anything with
# [depth information is stored in the 'database' files]
# Why don't I just ignore it from the return value then?
#
$group = get_group $site_config;
my ( $version, $version_config, $dhead, $_depth ) = check_location $site_config, $dname;
$_depth = undef;
# we actually want the CIAO version number (e.g. "3.0.2" rather than
# the version id used in the config file - ie "ciao3"), so we 'override'
# the $version variable
#
$version = get_config_version $version_config, "version_string";
my $ahelpfiles = get_config_type $version_config, "ahelpfiles", $type;
my $outdir = get_config_type $version_config, "outdir", $type;
my $outurl = get_config_type $version_config, "outurl", $type;
my $stylesheets = get_config_type $version_config, "stylesheets", $type;
if ($localxslt) {
dbg "Overriding stylesheets setting: from $stylesheets to $FindBin::Bin/";
$stylesheets = "$FindBin::Bin/";
}
my $ahelpstore = get_config_type $version_config, "ahelpindexdir", $type;
my $ahelpindex_xml = "${ahelpstore}ahelpindex.xml";
my $ahelpindex_dat = "${ahelpstore}ahelpindex.dat";
foreach my $f ( $ahelpindex_xml, $ahelpindex_dat ) {
die "ERROR: Unable to find ahelp index - has mk_ahelp_setup.pl been run?\n\n missing=$f\n"
unless -e $f;
}
# check we can find the needed stylesheets
#
foreach my $name ( qw( ahelp ahelp_common ) ) {
my $x = "${stylesheets}$name.xsl";
die "Error: unable to find $x\n"
unless -e $x;
}
# now we can check on which files to process
# - not 100% sure on what 'no arguments' means
# For now go with it implying the contents of
# doc/xml/ and contrib/doc/xml/ of the
# ahelpfiles directory (from the config file).
# [this way it matches mk_ahelp_setup.sl]
#
# We want the names without the .xml suffix
# AND we exclude any names inclding 'onapplication' - even if
# the user specified them
#
# As of CIAO 4 this has got more complicated since we
# store all the XML files within one directory but we publish
# to separate sites. So we need to:
# user gives files to process => check files belong to this site
# no files given => select the correct files
#
my @allowed_names = find_ahelpfiles $site, $ahelpindex_xml;
my @names;
if ( $#ARGV == -1 ) {
# Use the database to select the files to process
#
@names = @allowed_names;
} else {
@names = @ARGV;
# We only check on the file name, not the path, for these files.
# This could lead to problems but let's not bother with that for
# now. We do need the full path to the data files, so have to
# "add that back in" *ONLY IF* there is no path component to the
# input name.
#
my %check_names = map { my $f = (split "/",$_)[-1]; ($f,$_); } @allowed_names;
@names = ();
foreach my $name (@ARGV) {
my $t = (split "/", $name)[-1];
die "Error: file not known for site=$site: $t\n"
unless exists $check_names{$t};
if (index($name, "/") == -1) {
push @names, $check_names{$t};
} else {
push @names, $name;
}
}
dbg "Converted input file names to";
dbg "@names";
}
@names = map { s/\.xml$//; $_; }
grep { !/onapplication/ } @names;
die "Error: no ahelp files have been specified or found in the directory\n"
if $#names == -1;
dbg "Found " . (1+$#names) . " ahelp files";
#########################
my $uname = `whoami`;
chomp $uname;
dbg "*** CONFIG DATA (start) ***";
dbg " type=$type";
dbg " dname=$dname";
dbg " dhead=$dhead";
##dbg " depth=$depth";
dbg " outdir=$outdir";
dbg " stylesheets=$stylesheets";
dbg " ahelpindex_xml=$ahelpindex_xml";
dbg " ahelpindex_dat=$ahelpindex_dat";
dbg " version=$version";
dbg " ---";
# start work
#
# parse the ahelp index file (dat) to get the mapping from xml file name
# to HTML head and then check we have a mapping for all the files
#
# not 100% convinced about the directory munging (necessary since v1.3
# when we changed to picking up files from the ahelpfiles directory
# rather than the local directory)
#
dbg "Reading in ahelp index from $ahelpindex_dat";
my $html_mapping = read_ahelpindex $ahelpindex_dat;
foreach my $fullname ( @names ) {
my $name = (split("/",$fullname))[-1];
die <<"EOE" unless exists $$html_mapping{$name};
Error: $fullname.xml is not found in the ahelp index
you will probably need to re-run
mk_ahelp_setup.pl
mk_ahelp_indexes.pl
EOE
}
# create the output directories if we have to
$outdir .= $dhead;
mymkdir $outdir;
my @extra = (
type => $type eq "trial" ? "test" : $type,
outdir => $outdir,
site => $site,
version => $version,
);
my $cssfile = get_config_type $version_config, "css", $type;
my $cssprintfile = get_config_type $version_config, "cssprint", $type;
my $favicon = get_config_type $version_config, "favicon", $type;
my $searchssi = get_config_type $version_config, "searchssi", $type;
my $googlessi = get_config_version( $version_config, "googlessi" );
my $urlbase = get_config_type $version_config, "outurl", $type;
# site banner is optional
my $sitebanner = "";
$sitebanner = get_config_type( $version_config, "sitebanner", $type )
if check_config_exists( $version_config, "sitebanner" );
my $storageloc = "";
$storageloc = get_config_type( $version_config, "storageloc", $type )
if check_config_exists( $version_config, "storageloc" );
die "Error: unable to find storageloc=$storageloc\n"
unless $storageloc eq "" or -e $storageloc;
my $published = "";
$published = get_storage_location($storageloc, $site)
unless $storageloc eq "";
unless ($published eq "") {
$published .= $dhead;
mymkdir $published;
}
# optional "postfix" text for page headers
my $headtitlepostfix = "";
my $texttitlepostfix = "";
$headtitlepostfix = get_config_version( $version_config, "headtitlepostfix" )
if check_config_exists( $version_config, "headtitlepostfix" );
$texttitlepostfix = get_config_version( $version_config, "texttitlepostfix" )
if check_config_exists( $version_config, "texttitlepostfix" );
# only needed for a warning in the output HTML file (to point to
# the work directory).
#
my $sourcedir = cwd() . "/";
dbg " uname=$uname";
dbg " urlbase=$urlbase";
dbg " searchssi=$searchssi";
dbg " sitebanner=$sitebanner";
dbg " cssfile=$cssfile";
dbg " cssprintfile=$cssprintfile";
dbg " favicon=$favicon";
dbg " googlessi=$googlessi";
dbg " storageloc=$storageloc";
dbg " headtitlepostfix=$headtitlepostfix";
dbg " texttitlepostfix=$texttitlepostfix";
dbg " sourcedir=$sourcedir";
dbg "*** CONFIG DATA (end) ***";
@extra =
(
@extra,
updateby => $uname,
cssfile => $cssfile,
cssprintfile => $cssprintfile,
favicon => $favicon,
searchssi => $searchssi,
sitebanner => $sitebanner,
googlessi => $googlessi,
urlbase => $urlbase,
storageloc => $storageloc,
headtitlepostfix => $headtitlepostfix,
texttitlepostfix => $texttitlepostfix,
sourcedir => $sourcedir,
);
my %paramlist = @extra;
# Loop through each file
#
foreach my $in ( @names ) {
# To match publish.pl
my @ans = fileparse $in;
my $name = $ans[0];
print "Parsing [ahelp]: $name\n";
# We need to convert depth from a number to a string
# - e.g. from 2 to '../' - here
#
my ( $depth, $outname, $seealso_name ) = @{ $$html_mapping{$name} };
# we 'hardcode' the output of the transformation
# and ensure that any old files have been deleted
#
my @names = ( $outname );
my @pages = map { "${outdir}${_}.html"; } @names;
foreach my $page ( @pages ) {
dbg " ---> deleting (if it exists) $page";
myrm( $page );
}
$paramlist{outname} = $outname;
$paramlist{seealsofile} = "${ahelpstore}$seealso_name";
$paramlist{depth} = '../' x ($depth-1);
$paramlist{dname} = $dname;
# HACK to support common::add-dependencies; this is the
# same as the outname parameter. The ahelp code should
# probably update to using pagename (or there's some
# consistency needed between out/pagename).
#
$paramlist{pagename} = $name;
clear_dependencies;
my $flag = translate_file "${stylesheets}ahelp.xsl",
"${in}.xml", \%paramlist;
# we skip further processing on error
#
die "-> problem generating HTML for $in\n"
unless defined $flag;
# success or failure?
foreach my $page ( @pages ) {
die "Error: transformation did not create $page\n"
unless -e $page;
mysetmods( $page );
dbg("Created: $page");
}
dump_dependencies;
write_dependencies $name, $published, cwd() . "/", $stylesheets;
} # foreach: $in
print "\nTry page(s) at: ${outurl}$dhead\n";
# End of script
#
exit;
## Subroutines
#
# Usage:
# my $map = read_ahelpindex $ahelpindex_dat;
#
# Aim:
# given the name of the file containing the mapping
# between XML file name and the HTML name for the output.
#
# The return value is a hash reference:
# keys = xml file name (with no .xml suffix)
# values = [
# depth value,
# head (ie no .html suffix) of HTML file name,
# seealso file name (with .xml but without path)
# ]
#
# Note:
# Now that we process the XML index proper, is this file still
# useful?
#
sub read_ahelpindex ($) {
my $infile = shift;
my $fh = IO::File->new( "< $infile" )
or die "ERROR: unable to open $infile for reading\n";
my %map;
while ( <$fh> ) {
next if /^(#|\s*$)/;
chomp;
my ( $xml, $depth, $html, $seealso ) = split;
$xml =~ s/\.xml$//;
die "Error: multiple occurrnces of $xml found in $infile\n"
if exists $map{$xml};
$map{$xml} = [ $depth, $html, $seealso ];
}
$fh->close;
return \%map;
} # sub: read_ahelpindex
# Usage:
# @filelist = find_ahelpfiles $site, $ahelpindex_xml;
#
# Returns a reference to a list of all the ahelp files to process
# for the given site. This MUST include the full path to the
# file.
#
sub find_ahelpfiles ($$) {
my $site = shift;
my $xmlfile = shift;
dbg "Reading ahelp file info from: $xmlfile";
my $dom = read_xml_file $xmlfile;
my $root = $dom->documentElement();
my @out;
dbg "Processing ahelp index to find pages in site=$site";
foreach my $node ($root->findnodes("/ahelpindex/ahelplist/ahelp[site='$site']")) {
push @out, $node->findvalue("xmlname");
}
return @out;
} # sub: find_ahelpfiles