-
Notifications
You must be signed in to change notification settings - Fork 2
/
publish.pl
executable file
·2000 lines (1669 loc) · 57.3 KB
/
publish.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
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env perl -w
#
# Usage:
# publish.pl --type=test|live|trial <filename(s)>
# Default for type is test
#
# --config=config-file
# The name (including path) of the configuration file.
# Defaults to <path to this script>/config.dat
#
# --force
# Optional.
#
# --forceforce
# Optional.
#
# --verbose
# Turn on screen output that's only useful for testing/debugging
#
# --localxslt
# Replace the %stylesheets directive in the config file with
# the location of this script (for testing purposes)
#
# --ignore-missing
# Ignore missing links (i.e. let the page publish with a warning
# rather than error out). The contents of the link may contain
# place-holder text.
#
# by default will not create HTML files if they already exist
# and are newer than the XML file (also checks for other
# associated files and the created PDF files).
# Use the --force option to force the creation of the HTML files.
# [note: the thread index pages are currently ALWAYS created].
# Use --forceforce to force the copying on non XML files
# [note: implies --force]
#
# Aim:
# Convert the specified files into SSI/web pages.
# The pages must all reside in the current working directory.
# The .xml suffix need not be included.
# If the file does not end in .xml then we assume the file is
# to be copied over directly and not processed. Used for
# images/ps files/.. (ie non HTML files).
#
# The configuration file (probably called config.dat in the
# "top-level" directory for the particular site) defines a
# number of important things for the script (eg where to
# put the HTML files). Actually, to make things easier the
# config file can contain values for multiple sites
# (so that the same alias can be used irrespective of site)
# and the location can be user-defined (by default it is the same
# as the location of this script, not the site directory as
# mentioned above - ie this paragraph needs re-writing).
#
# The location of the output depends on the type option
# (which defaults to test if not specified). Please don't use
# the trial type unless you really know what you're doing.
#
# Creates:
# The location of the output HTML (and possibly PDF) files
# is defined by the contents of the config file you supply with the
# --config option
#
# Requires:
# The location searched for the stylesheets is defined
# in the config file, as are the actual stylesheets needed,
# unless the --localxslt flag is used to override this.
#
# Notes:
# This will refuse to publish anything, no matter how many force
# statements you use, in a directory containing the file
# DO_NOT_PUBLISH.
#
# Author:
# Doug Burke ([email protected])
#
use strict;
$|++;
use Getopt::Long;
use FindBin;
use Cwd;
use IO::File;
use IO::Pipe;
# we only need this when validating, but always load it in
use JSON;
use lib $FindBin::Bin;
use CIAODOC qw( :util :xslt :cfg :deps );
# only needed if reporting HTML validation errors
binmode(STDOUT, ":encoding(UTF-8)");
## Subroutines (see end of file)
#
sub process_xml ($$);
sub process_files ($$);
## 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
#
# We need a default prefix in order to find the default
# config file, even if we want to be able to over-ride
# everything from the command-line
#
my $prefix = "/data/da/Docs"; # should NOT end in a /
## Code
#
my $progname = (split( m{/}, $0 ))[-1];
my $usage = <<"EOD";
Usage:
$progname --config=name --type=test|live|trial <filename(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 --force option should be used to force the generation of the
HTML files (by default the program won't publish a file if the HTML,
PDF, and associated files already exist and are newer than the XML
file). Non XML files are *not* copied when --force is given if
they have not changed.
The --forceforce option is used to also force the copying of
non XML files, even if they have not changed. Setting this also
sets --force.
The --verbose option is useful for testing/debugging the code.
The --localxslt option overides the \%stylesheets directive in the
config file to use the location of $progname instead.
The --ignore-missing option is used to avoid circular dependencies
when publishing pages: i.e. if page a needs info from page b, but
page b needs info from page a. This is experimental and should be
carefully, and rarely, used. One consequence is that the output may
contain place-holder text.
If the directory contains the file DO_NOT_PUBLISH then the script
will error out, no matter the number of force options used. This is
to support data directories that contain data used to generate
files used in the publishing code, but that themselves should not be
published.
EOD
# this will be mangled later
my $dname = cwd();
# handle options
my $type = "test";
my $force = 0;
my $forceforce = 0;
my $localxslt = 0;
my $ignoremissinglink = 0;
die $usage unless
GetOptions
'config=s' => \$configfile,
'type=s' => \$type,
'force!' => \$force,
'forceforce!' => \$forceforce,
'localxslt!' => \$localxslt,
'ignore-missing!' => \$ignoremissinglink,
'verbose!' => \$verbose;
# check no "sentinel" file indicating this is a not-to-be-published
# directory; also check up the parent chain (unfortunately no easy
# way to know when to stop for now, so just assume we have stuff in
# /data/da/Docs/blah/blah-with-version/ as the base).
#
my $sentinel = "DO_NOT_PUBLISH";
my @dtoks = split "/", $dname;
for (my $i = $#dtoks; $i >= 5; $i--) {
my $filename = (join "/", @dtoks[0 .. $i]) . "/" . $sentinel;
die "Sentinel file $filename was found. Publishing is forbidden!\n"
if -e $filename;
}
$force = 1 if $forceforce;
$ignoremissinglink = $ignoremissinglink ? "yes" : "no";
# what OS are we running?
#
my $ostype = get_ostype;
# check the options
die "Error: the config option can not be blank\n"
if $configfile eq "";
my $config = parse_config( $configfile );
# most of the config stuff is parsed below, but we need these two here
my $site_config;
( $site, $site_config ) = find_site $config, $dname;
dbg "Site = $site";
# Find the validator.
#
my $validator = get_config_main( $config, "validator" );
{
my $retval = `java -jar $validator --version`;
if ( $? == 0 ) {
dbg "validator found - version $retval";
} else {
$validator = undef;
}
}
$config = undef; # DBG: just make sure no one is trying to access it
check_type_known $site_config, $type;
dbg "Type = $type";
dbg "OS = $ostype";
# check usage
#
die $usage if $#ARGV == -1;
# Handle the remaining config values
#
# shouldn't have so many global variables...
#
$group = get_group $site_config;
my ( $version, $version_config, $dhead, $depth ) = check_location $site_config, $dname;
# used to let a page know it's "name" as it is likely to be specified
# in the navbar (and hence so that it can be highlighted using CSS)
#
# see add-htmlhead in helper.xsl - we don't actually use this at the
# moment since it's not clear how to handle all situations
#
my $navbar_link = '../'x($depth-1) . $dhead;
$navbar_link = "index.html" if $navbar_link eq "";
my $stylesheets = get_config_type $version_config, "stylesheets", $type;
my $outdir = get_config_type $version_config, "outdir", $type;
my $outurl = get_config_type $version_config, "outurl", $type;
my $css = get_config_type $version_config, "css", $type;
my $cssprint = get_config_type $version_config, "cssprint", $type;
my $favicon = get_config_type $version_config, "favicon", $type;
if ($localxslt) {
dbg "Overriding stylesheets setting: from $stylesheets to $FindBin::Bin/";
$stylesheets = "$FindBin::Bin/";
}
if ($ignoremissinglink) {
dbg "Stylesheets will not error out if missing links/info are found.";
}
# get the site version
my $site_version = "";
if ( ! ($site =~ /caldb/)) {
if (check_config_exists( $version_config, "number" )){
$site_version = get_config_version( $version_config, "number" );
} else {
die "Error: version $version in the config file ($configfile) does not contain the number parameter\n";
}
}
# as is this
# - since we do send these to the processor then we can not let them
# default to "" since that will cause problems (it will get lost
# in the shell expansion and so mess up everything). So we use the
# string "dummy" which is checked for in the stylesheet
#
# - with the current set of "config" routines this isn't particularly pretty
#
my $newsfile = "dummy";
$newsfile = get_config_type( $version_config, "newsfile", $type )
if check_config_exists( $version_config, "newsfile" );
my $newsfileurl = "dummy";
$newsfileurl = get_config_type( $version_config, "newsurl", $type )
if check_config_exists( $version_config, "newsurl" );
# note: no "dummy" here (can't remember if that's important for the stylesheets)
#
my $watchouturl = "";
$watchouturl = get_config_type( $version_config, "watchouturl", $type )
if check_config_exists( $version_config, "watchouturl" );
my $searchssi = "/incl/search.html";
$searchssi = get_config_type( $version_config, "searchssi", $type )
if check_config_exists( $version_config, "searchssi" );
my $sitebanner = "";
$sitebanner = get_config_type( $version_config, "sitebanner", $type )
if check_config_exists( $version_config, "sitebanner" );
# If the banner is set, ensure we can find it
#
if ($sitebanner ne "") {
dbg "** checking for sitebanner=$sitebanner depth=$depth";
my $sitebannerfile = '../' x ($depth - 1) . $sitebanner;
die "ERROR: Unable to find sitebanner=$sitebannerfile\n" unless
-e $sitebannerfile;
}
# google analytics include
#
my $googlessi = "";
$googlessi = get_config_version( $version_config, "googlessi" )
if check_config_exists( $version_config, "googlessi" );
# MathJax
my $mathjaxpath = "";
$mathjaxpath = get_config_type( $version_config, "mathjaxpath", $type )
if check_config_exists( $version_config, "mathjaxpath" );
# storage/published is optional [sort of, depends on the site]
#
my $storageloc = "";
$storageloc = get_config_type( $version_config, "storageloc", $type )
if check_config_exists( $version_config, "storageloc" );
my $published = "";
$published = get_storage_location($storageloc, $site)
unless $storageloc eq "";
# set up the ahelp index file based on the storeage location
#
my $ahelpindexdir = "";
$ahelpindexdir = get_config_type( $version_config, "ahelpindexdir", $type )
if check_config_exists( $version_config, "ahelpindexdir" );
# logo image/text/url is also optional
# - only needed for navbar pages
#
my $logoimage = "";
my $logotext = "";
my $logourl = "";
$logoimage = get_config_version( $version_config, "logoimage" )
if check_config_exists( $version_config, "logoimage" );
$logotext = get_config_version( $version_config, "logotext" )
if check_config_exists( $version_config, "logotext" );
$logourl = get_config_version( $version_config, "logourl" )
if check_config_exists( $version_config, "logourl" );
# the following are only useful for threads: they
# define the image to add at the end of links to 'images'
# [which should probably go away]
#
my $imglinkicon = "imgs/imageicon.gif";
my $imglinkiconwidth = 30;
my $imglinkiconheight = 30;
$imglinkicon = get_config_version( $version_config, "imglinkicon" )
if check_config_exists( $version_config, "imglinkicon" );
$imglinkiconwidth = get_config_version( $version_config, "imglinkiconwidth" )
if check_config_exists( $version_config, "imglinkiconwidth" );
$imglinkiconheight = get_config_version( $version_config, "imglinkiconheight" )
if check_config_exists( $version_config, "imglinkiconheight" );
# optional
#
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" );
# add on our current working directory
$outdir .= $dhead;
$outurl .= $dhead;
$published .= $dhead unless $published eq "";
# check for the stylesheets (just in case)
foreach my $xslt ( @{ get_config_site( $site_config, "stylesheets" ) } ) {
my $x = "${xslt}.xsl";
die "Error: unable to find the stylesheet $x in $stylesheets\n"
unless -e "$stylesheets$x";
}
# the test pages need to know who you are
#
my $uname = `whoami`;
chomp $uname;
dbg "*** CONFIG DATA ***";
dbg " uname=$uname";
dbg " dname=$dname";
dbg " dhead=$dhead";
dbg " depth=$depth";
dbg " ignoremissinglink=$ignoremissinglink";
dbg " outdir=$outdir";
dbg " outurl=$outurl";
dbg " published=$published";
dbg " storageloc=$storageloc";
dbg " ahelpindexdir=$ahelpindexdir";
dbg " version=$site_version";
dbg " css=$css";
dbg " newsfile=$newsfile";
dbg " newsfileurl=$newsfileurl";
dbg " watchouturl=$watchouturl";
dbg " searchssi=$searchssi";
dbg " sitebanner=$sitebanner";
dbg " mathjaxpath=$mathjaxpath";
dbg " logoimage=$logoimage";
dbg " logotext=$logotext";
dbg " logourl=$logourl";
dbg " imglinkicon=$imglinkicon [$imglinkiconwidth x $imglinkiconheight]";
dbg " headtitlepostfix=$headtitlepostfix";
dbg " texttitlepostfix=$texttitlepostfix";
dbg "*** CONFIG DATA ***";
# Handle the ahelpindex file
#
my $ahelpindex = "${ahelpindexdir}ahelpindex.xml";
die "Error: can not find ahelpindex file - check config file for\n ahelpindexdir=$ahelpindexdir\n"
unless "" eq $ahelpindexdir or -e $ahelpindex;
die "Error: unable to find storageloc=$storageloc\n"
unless $storageloc eq "" or -e $storageloc;
# Get the list of files to work on
my @xml;
my @nonxml;
# need filenames: strip off trailing .xml if present
# - split into XML and non XML files
# we don't do anything clever but just rely on the file name
#
# - skip directories (unless there's a .xml version)
# and any file ending in ",v" or "~"
#
foreach my $in ( map { s/\.xml$//; $_; } @ARGV ) {
# for some reason I am suddenly seeing empty values of $in, so skip them
# (rather than work out where they are coming from)
next unless $in;
# if ( -d $in ) {
# print "skipping directory $in\n";
# next;
# }
if ( (-d $in) && (! -e "${in}.xml") ) {
print "skipping directory $in\n";
next;
}
if ( $in =~ /,v$/ ) {
print "skipping $in as ends in ,v so taken to be RCS file\n";
next;
}
if ( $in =~ /~$/ ) {
print "skipping $in as ends in ~ so taken to be an emacs backup file\n";
next;
}
if ( -e "${in}.xml" ) { push @xml, $in; }
elsif ( -e $in ) { push @nonxml, $in; }
else { die "Error: Unable to find in=$in\n"; }
# check that the file is in the current working directory
# [just makes some things a bit easier later on]
#
die "Error: $in must be in the current directory\n"
if $in =~ /\//;
}
# create the output directories if we have to
mymkdir $outdir;
mymkdir $published unless $published eq "";
# note we process the non-XML files first since they
# may be needed to create the PDF versions of the
# pages (ie images)
#
process_files $type, \@nonxml;
process_xml $type, \@xml;
# End of script
#
exit;
## Subroutines
#
# dup the stdout and set it to /dev/null
#
# return a filehandle for the original STDOUT channel
sub dup_stdout () {
my $fh = IO::File->new( ">&STDOUT" )
or die "Unable to dup STDOUT\n";
open STDOUT, ">/dev/null"
or die "Unable to set STDOUT to /dev/null\n";
return $fh;
}
sub undup_stdout ($) {
my $fh = shift;
my $fd = $fh->fileno;
open STDOUT, ">&$fd"
or die "Unable to restore default STDOUT\n";
}
# dup the stderr and set it to /dev/null
#
# return a filehandle for the original STDERR channel
sub dup_stderr () {
my $fh = IO::File->new( ">&STDERR" )
or die "Unable to dup STDERR\n";
open STDOUT, ">/dev/null"
or die "Unable to set STDERR to /dev/null\n";
return $fh;
}
sub undup_stderr ($) {
my $fh = shift;
my $fd = $fh->fileno;
open STDERR, ">&$fd"
or die "Unable to restore default STDERR\n";
}
# return if should_we_skip $in, @files
#
# if $in is a scalar, then it is the name of an xml file
# (needn't contain a trailing .xml)
# if it's a reference, then it's a reference to the "age" (-M $foo)
# of the file to check against
#
# returns 1 if we can skip processing this file
# 0 otherwise
#
# uses the global variable $force
#
sub should_we_skip ($@) {
my $in = shift;
my @files = @_;
# don't skip if force is selected
return 0 if $force;
# do we need to mess around with things?
my $intime;
if ( ref($in) ) {
$intime = $$in;
} else {
$in =~ s/\.xml$//;
$in .= ".xml";
if (-e $in) {
$intime = -M $in;
} else {
# HACK for when the XML is created in-memory
# For now we assume that we can skip these pages;
# perhaps in this case the intime should have been
# sent in instead.
#
return 0;
}
}
foreach my $file ( @files ) {
return 0
unless -e $file and -M $file < $intime;
} # foreach: @files
# if got this far the file's for skipping
print "\tskipping\n";
return 1;
} # should_we_skip()
# math2image $head, $outfile
#
# convert trhe equation sotred in the file $head.tex
# into an image called $outfile, sets its protections
#
# then delete $tex
#
# NOTE: this is based on text2im v1.5
#
# NOTE: this code is not needed when using MathJax.
#
sub math2image ($$) {
return if use_mathjax;
my $head = shift;
my $outfile = shift;
my $tex = $head . ".tex";
die "Error: transformation did not create $tex\n"
unless -e $tex;
# create the dvi file
#
system "latex", "-interaction=batchmode", $tex
and die "Error: unable to latex $tex\n";
# and the ps
my $rflag = system "dvips", "-o", "$head.eps", "-E", "$head.dvi";
die "Error: unable to run dvips on $head.dvi to create $head.eps\n"
if $rflag;
# and now the output file
system "convert", "+adjoin", "-density", "150x150", "$head.eps", "$head.png"
and die "Error: unable to convert to PNG\n";
die "Error: PNG for equation=$head was not created\n"
unless -e "$head.png";
system "cp", "$head.png", $outfile;
# clean up and return
foreach my $ext ( qw( log aux dvi eps tex png ) ) { myrm $head . ".$ext"; }
mysetmods $outfile;
print "\nCreated: $outfile\n";
} # sub: math2image()
# can we publish this page for this site?
#
sub site_check ($$$) {
my ( $site, $label, $ok ) = @_;
my %ok = map { ($_,1); } @{$ok};
die "Error: currently can only convert $label pages in site=" . join(",",@{$ok}) . "\n"
unless exists $ok{$site};
} # sub: site_check
# Usage:
# initialise_pages( $page1, ..., $pageN );
#
# Aim:
# Ensures the directories exist for these pages
# and then deletes any current version of the
# page
#
sub initialise_pages {
foreach my $page ( @_ ) {
my $dir = $page;
$dir =~ s/\/[^\/]+.html$//;
mymkdir $dir;
myrm $page;
}
} # sub: initialise_pages()
# Usage:
# check_for_page( @soft );
#
# Aim:
# checks that the transformation created the necessary
# pages AND sets the correct permission/group
#
sub check_for_page {
foreach my $page ( @_ ) {
die "Error: transformation did not create $page\n"
unless -e $page;
mysetmods $page;
print "Created: $page\n";
}
} # sub: check_for_page()
# Usage:
# validate_page( @soft );
#
# Aim:
#
# Pass through the pages through the W3C validator (if available). It
# relies on only being called on "full" pages (and not things like
# slug files or the redirect page).
#
# This only checks .html files
#
sub validate_page (@) {
return if not (defined $validator);
foreach my $page ( @_ ) {
next if not ($page =~ /\.html$/i);
print "Validate: $page\n";
# Check the errors and warnings from $page
my $output = `java -jar $validator --stdout --format json $page`;
if ( $? != 0 ) {
print "HTML issues (see Doug):\n";
my $json = decode_json $output;
my $msgs = $$json{"messages"};
while (my ($idx, $msg) = each @$msgs) {
my $ctr = $idx + 1;
print "$ctr : $$msg{'type'} - $$msg{'message'}\n";
# provide mode information
dbg "$$msg{'extract'}";
}
}
}
} # sub: validate_page()
#
# Usage:
# clean_up_math( $outdir, $page1, ..., $pageN );
#
# Aim:
# ensures none of the files that will be needed to support the math
# tag are present
#
# NOTE: this code is not needed when using MathJax.
#
sub clean_up_math {
return if use_mathjax;
my $outdir = shift;
foreach my $page ( @_ ) {
myrm "${page}.tex";
myrm "${page}.aux";
myrm "${page}.log";
myrm "${page}.dvi";
myrm "${page}.eps";
myrm "${outdir}${page}.png";
}
} # clean_up_math()
#
# Usage:
# process_math( $outdir, $page1, ..., $pageN );
#
# Aim:
# Creates the PNG images
#
# NOTE: this code is not needed when using MathJax.
#
sub process_math {
return if use_mathjax;
my $outdir = shift;
foreach my $page ( @_ ) { math2image $page, "${outdir}${page}.png"; }
} # process_math()
# Usage:
# $num = count_slash_in_string $str
#
# returns the number of times that '/' occurs
# in $str
#
sub count_slash_in_string ($) {
my $str = shift;
$str =~ s/[^\/]//g;
return length ($str);
} # count_slash_in_string
# Given an array of file names we expect the stylesheet to produce
# and the string output of the stylesheet, which contains the names
# it did produce, return an array with the merged set of names.
#
# Errors out if any of the expected names are not in the actual names,
# so it isn't really a merge as the expected values should be in the
# return of the stylesheet.
#
# Order is not preserved.
#
# This prototype is obviously not correct, so comment out
#sub merge_filenames (@$) {
sub merge_filenames {
my @exp = shift;
my $rval = shift;
my %created;
foreach my $line (split /\n/, $rval) {
$line =~ s/^\s+|\s+$//g;
next if $line eq "";
$created{$line} = 1;
}
foreach my $efile (@exp) {
die "ERROR: expected file $efile but not created by stylesheet\n"
unless exists $created{$efile};
}
return keys %created;
} # merge_filenames
# QUESTION:
#
# How best to extend this to support multiple output files?;
# perhaps have a "primary" output and ancillary ones, which may
# or may not be HTML.
#
# Probably do not want to set outurl here since used to
# generate the canonical link and header for hardcopy output
#
# Create the basic/default set of options for the stylesheets.
#
# TODO: should url be sent in as an optional/named argument?
sub basic_params ($) {
my $opts = shift;
my $in = $$opts{xml};
my $outurl = $$opts{outurl};
my $url = "${outurl}${in}.html";
my $out = {
type => $$opts{type},
site => $$opts{site},
lastmod => $$opts{lastmod},
lastmodiso => $$opts{lastmodiso},
install => $$opts{outdir},
canonicalbase => $outurl,
pagename => $in,
url => $url,
# TODO: should outurl be set ?
sourcedir => cwd() . "/",
updateby => $$opts{updateby},
depth => $$opts{depth},
siteversion => $site_version,
ahelpindex => $ahelpindex,
cssfile => $css,
cssprintfile => $cssprint,
favicon => $favicon,
newsfile => $newsfile,
newsfileurl => $newsfileurl,
watchouturl => $watchouturl,
searchssi => $searchssi,
sitebanner => $sitebanner,
googlessi => $googlessi,
mathjaxpath => $mathjaxpath,
headtitlepostfix => $headtitlepostfix,
texttitlepostfix => $texttitlepostfix,
storageloc => $$opts{storageloc},
ignoremissinglink => $ignoremissinglink,
};
# hack way to copy over extra keywords
while (my ($key, $value) = each %$opts) {
next unless $key =~ /^COPY-/;
$$out{substr $key, 5} = $value;
}
return $out;
} # basic_params
# xml2html_basic
# Process a 'basic' or 'generic' page style
#
# note: $xslt, $outdir, and $outurl end in a /
#
# At present pagelabel and stylesheethead are set to the
# same value, so could amalgamate; leave as is for now
#
# The option no_validate is used to control whether to
# validate the HTML output.
#
sub xml2html_basic ($$$) {
my $pagelabel = shift; # used to identify the page type to the user
my $stylesheethead = shift; # name of stylesheet, without path or trailing .xsl
my $opts = shift;
my $in = $$opts{xml};
my $dom = $$opts{xml_dom};
my $outdir = $$opts{outdir};
my $outurl = $$opts{outurl};
print "Parsing [${pagelabel}]: $in";
# We 'hardcode' the output of the transformation.
# Note: for 'ancillary' files, such as the slug files created by
# the bugs and relnotes pages, we rely on calling a perl routine
# from within the stylesheet to handle the deletion of the pages.
#
my @pages = ( "${outdir}${in}.html" );
# how about math pages?
#
my @math = find_math_pages $dom;
# do we need to recreate (include the equations created by any math blocks)
return if should_we_skip $in, @pages, map( { "${outdir}${_}.png"; } @math );
print "\n";
# remove files [already ensured the dir exists]
foreach my $page ( @pages ) { myrm $page; }
clean_up_math( $outdir, @math );
my $url = "${outurl}${in}.html";
my $params = basic_params $opts;
my $retval = translate_file "$$opts{xslt}${stylesheethead}.xsl", $dom, $params;
my @outfiles = merge_filenames(@pages, $retval);
# success or failure?
check_for_page( @outfiles );
if (exists $$opts{no_validate}) {
print "Skipping validation of @pages\n";
} else {
validate_page( @pages ); # note: not @outfiles
}
# math?
process_math( $outdir, @math );
print "\nThe page can be viewed at:\n ${url}\n\n";
} # sub: xml2html_basic
# See https://stackoverflow.com/a/31726665
#
# sub begins_with ($$) {
# my $str = shift;
# my $prefix = shift;
#
# return substr($str, 0, length($prefix)) eq $prefix;
# }
# xml2html_navbar - called by xml2html
#
# 02/13/04 - we now pass the site depth into the stylesheet
# (since the slang directory has its own navbar we can no longer
# assume that depth=1)
# 05/03/13 - is this still needed (was it added for the proglang
# code or some other reason)?
#
sub xml2html_navbar ($) {
my $opts = shift;
my $in = $$opts{xml};
my $dom = $$opts{xml_dom};
my $depth = $$opts{depth};
my $outdir = $$opts{outdir};
# convert the input XML file into HTML files
#
# output of stylesheet is a list of pages that have been created
# and the actual pages (created in $outdir).
# We need to hack the output files to remove unwanted content
# [managed to remove most of this but the first line contains
# '<!DOCTYPE...>' which we don't want]
#
# NOTE: $outdir MUST end in a '/' (this is checked for by XSLT stylesheet
#
print "Parsing [navbar]: $in"; # don't '\n' until skip check
my $params = basic_params $opts;
$$params{logoimage} = $logoimage if $logoimage ne "";
$$params{logotext} = $logotext if $logotext ne "";
$$params{logourl} = $logourl if $logourl ne "";
# get a list of the pages: we need this so that:
# - we can create the directory if necessary
# - we can delete them [if they exist] before the processor runs
# (since we write protect them after creation so the processor
# can't actually create the new files)
#
my @pages;
my %depths;
# Process the dirs/dir elements of section elements that contain an id attribute
# This is a lot simpler than the old XSLT code; it is not clear to me why
# the old code needed that complexity (ie I think it could have used the
# logic below). I think it's because I based it on the code used to create
# the actual navbar's, which needed said logic.
#
# Hmm, now I want to calculate the depth values as well I am probably
# going to re-introduce some of this complexity. However, it is still
# much simpler.
#
my $rnode = $dom->documentElement();
foreach my $node ($rnode->findnodes('descendant::section[boolean(@id)]')) {
my $id = $node->findvalue('@id');
my $tail = "navbar_${id}.incl";
if ($node->findvalue("count(dirs/dir[.=''])!=0") eq "true") {
push @pages, "${outdir}$tail";
$depths{$depth} = [] unless exists $depths{$depth};
push @{ $depths{$depth} }, $pages[-1];
}
foreach my $dnode ($node->findnodes("dirs/dir[.!='']")) {
my $content = $dnode->textContent;
$content .= "/" unless $content =~ /\/$/;