-
Notifications
You must be signed in to change notification settings - Fork 2
/
livedtd.pl
1794 lines (1612 loc) · 59.7 KB
/
livedtd.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/perl
#
# livedtd.pl - generate an html version of a DTD
#
# $Id: livedtd.pl,v 1.4 2005/04/06 08:33:53 bobs Exp $
#
# Copyright (c) 2000-2005 Robert Stayton
#
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation
# files (the ``Software''), to deal in the Software without
# restriction, including without limitation the rights to use,
# copy, modify, merge, publish, distribute, sublicense, and/or
# sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following
# conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# Except as contained in this notice, the names of individuals
# credited with contribution to this software shall not be used in
# advertising or otherwise to promote the sale, use or other
# dealings in this Software without prior written authorization
# from the individuals in question.
#
# Any program derived from this Software that is publically
# distributed will be identified with a different name and the
# version strings in any derived Software will be changed so that
# no possibility of confusion between the derived package and this
# Software will exist.
#
# Warranty
# --------
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL ROBERT STAYTON OR ANY OTHER
# CONTRIBUTOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.
#
# Contacting the Author
# ---------------------
# This program is maintained by Robert Stayton <bobs@sagehill.net>.
# It is available through http://www.sagehill.net/livedtd
#
#
# POD documentation:
=head1 NAME
livedtd.pl - generate an html version of a DTD
=head1 SYNOPSIS
C<livedtd.pl> [I<options>] F<dtdfile>
Where options are:
B<--catalog> I<catalogs>
if an SGML (not XML) catalog used by the DTD
B<--outdir> I<outputdirectory>
default is ./livedtd
B<--label> I<xxx>
add xxx prefix on output filenames
B<--sgml>
case insensitive element names
B<--title> "I<displayed title>"
default is main DTD filename
B<--nousage>
do not generate usage tables
B<--verbose>
lists details to STDOUT
=head1 DESCRIPTION
This program scans through an XML or SGML Document Type
Definition (DTD) to locate element and parameter entity
definitions. Then it constructs an HTML version of the DTD
with hot links from element references to element
declarations, and from entity references to entity
declarations. These links let you navigate through
the DTD with ease. Just point your favorite frames-capable
browser at I<index.html> in the output directory
to see the results.
This program was written for use by maintainers of highly
parameterized DTDs such as Docbook or TEI. It allows you
to quickly see how a parameter entity or element is defined,
following it through many levels of indirection to
its source. It also helps you find the active definition
when more than one is supplied, as when a customization
layer is used.
The program takes a single command line argument
(after any options) that is the pathname to the
main DTD file. [Windows users: use forward slashes
in all filenames.] Any additional arguments are ignored.
DTD files referenced by the main DTD as SYSTEM entities
are processed when they are encountered, unless they
are in a marked section whose status is IGNORE.
SGML catalogs are supported to locate system files,
but http URLs are not supported. XML catalogs are
not supported either.
The program handles marked sections properly. That is,
marked sections whose status is IGNORE are output
as plain text and do not have any live links.
Those whose status is INCLUDE are made live by adding
links. Parameter entities are also handled properly.
That is, if there is more than one declaration
for the same name, only the first one is active.
Livedtd parses a DTD for display purposes, but it does
not validate it. In performing its function, it will flag
many common validation errors. But lack of such errors
does not necessarily mean the DTD is valid.
This program generates an HTML file for each DTD file
used. The content is displayed within a <PRE> element
to preserve the white space and line breaks of the
original DTD file. The only difference from the original
file is the HTML markup to make it live. In fact,
removing the HTML markup leaves you with an identical
copy of the original.
It outputs them into a single output directory, even if
the originals are scattered all over the place.
It also constructs lists of elements, entities, and
filenames, and constructs an HTML framework file
to display it all.
The program generates name usage tables as well. These are accessed
by clicking on the "+" marker next to an element or
entity name in the left-frame tables of contents. A usage listing shows
where the name itself was seen in the DTD. It is not
a complete list of element usage, however. To get that,
you have to follow the various parameter entities
that contain the element name.
The program is not for authoring or editing a DTD. However,
if you have a good HTML editor that preserves lines endings
and whitespace, you can edit the HTML version with it.
When you are done, you can scrub out the HTML markup
using the program B<scrubdtd.pl> included with the
distribution. That program converts a LiveDTD file
back to a "dead" DTD file without HTML markup.
It is used on each file that makes up the DTD:
scrubdtd.pl file.dtd.html > file.dtd
If your HTML editor doesn't mess with the text,
you should have a working DTD file. Test it by
doing a round trip: use livedtd.pl to generate the
HTML version, make some editing changes with your
HTML editor, save it, and then apply scrubdtd.pl to
each generated HTML file. The only differences with the
original DTD files should be the changes you made.
=head1 OPTIONS
The B<--catalog> option lets you specify an SGML catalog
path (such as that used in SGML_CATALOG_PATH) to be used
to resolve PUBLIC identifiers in your DTD.
XML catalogs are not supported at this time.
The option can include more than one path, separated by colons.
Note that the OASIS/Catalog.pm module located below the
location of the livedtd.pl program is needed
to process a catalog file. Your environment
variable SGML_CATALOG_PATH is I<not> automatically
used, because you may be processing in a mixed XML
and SGML environment. If it is set, you can use:
--catalog "$SGML_CATALOG_PATH"
The B<--label> option lets you add a prefix to
each of the generated filenames. That allows you to
output several different liveDTDs to the same directory
without filename conflict as long as the labels are
different.
The B<--outdir> option lets you specify an output
directory for the generated HTML files. If not specified,
the program uses I<./livedtd>. That is, it creates
a subdirectory I<livedtd> below the current
working directory.
The B<--sgml> option alters how names are parsed.
Without this option, the program defaults to XML name parsing, which
is always case sensitive.
With this option, B<livedtd> treats element names as not
being case sensitive. That is a feature of the reference concrete syntax
for SGML DTDs.
Entity names remain case sensitive, but entity names may include
only characters from the class [-A-Za-z0-9.] and not
underscore or colon as are permitted in XML.
Note that the program does not automatically detect
that a DTD is SGML.
Also, if your SGML declaration deviates from the reference concrete syntax.
you can adjust the variables $SGMLEntname and $SGMLElemName
defined at the top of the program.
The B<--title> option lets you specify a printed title to appear
in the HTML display, such as "DocBook XML 4.1".
The B<--nousage> option turns off the generation of tables showing
where each element and entity name are used in declarations.
The usage tables are useful for tracking down potential effects of
any changes you want to make to a DTD, so it is on by default.
The B<--verbose> option provides many details during the
processing that can be useful for diagnosis of problems.
It also provides tables of element and parameter entity
definitions. It writes to standard output.
=head1 ACKNOWLEDGEMENTS
Thanks for Norm Walsh for permission to use code fragments
from his B<flatten> program for catalog and DTD parsing.
=cut
#######################################################
# Modules to use
#
use strict;
use IO::File;
use File::Basename;
use Getopt::Long;
use vars qw($homedir);
# Note: OASIS::Catalog loaded if --catalog option used
# Note: if you get a 'missing @INC' error, add the
# following line (with comment removed),
# and changing /perl/lib to the path where the IO::File module
# is installed:
#
# BEGIN { push @INC, '/perl/lib' };
#######################################################
# Global variables
#
my $VERSION = "1.0";
my $outdir = "./livedtd"; # default HTML output directory
my @Files = (); # Files used by the DTD.
my @ELEMENT = (); # List of declared element names.
my %ELEMENT = (); # Data records for each element.
my @ElemUsage = (); # List of used element names.
my %ElemUsage = (); # Data records for element usage.
my @PE = (); # List of declared parameter entity names.
my %PE = (); # Data records for each PE
my @PEUsage = (); # List of used parameter entity names.
my %PEUsage = (); # Data records for PE usage.
my @ATTLIST = (); # List of declared ATTLIST names.
my %ATTLIST = (); # Data records for each ATTLIST.
my $pass; # First or second pass through the DTD
my @dirstack = ('.'); # Keeps track of relative paths.
my $catalog; # Catalog object.
my $label = ''; # HTML filename prefix
my $title = ''; # HTML title string
my $verbose; # Verbosity flag.
my $MainDTD; # Main DTD filename.
my $ElementDefCount = 0; # Counts valid elements
my $AttlistDefCount = 0; # Counts valid ATTLISTs
my $MarkedSectionCount = 0; # Counts marked section starts
my $XMLEntName = '[A-Za-z][-A-Za-z0-9._:]*';
# Defines entity name char pattern
my $XMLElemName = '[A-Za-z][-A-Za-z0-9._:]*';
# Defines element name char pattern
my $SGMLEntName = '[A-Za-z][-A-Za-z0-9.]*';
# Defines entity name char pattern
my $SGMLElemName = '[A-Za-z][-A-Za-z0-9.]*';
# Defines element name char pattern
my $ML = 'XML'; # XML or SGML markup language
my $EntName = $XMLEntName; # Sets default to XML
my $ElemName = $XMLElemName; # Sets default to XML
my %ListTitle = ( # Printed HTML titles
'FileList', 'DTD Files',
'ElemList', 'Elements',
'EntityList', '% Entities',
'ElemUsage', 'Element Usage Table',
'EntityUsage', 'Parameter Entity Usage Table'
) ;
my $usagetables = 1 ; # Option flag, on by default.
my $UsageSymbol = "+"; # Link text to usage table
my $DOCTYPE = "<!DOCTYPE HTML PUBLIC"
. " \"-//W3C//DTD HTML 4.0 Transitional//EN\">\n";
#######################################################
# Usage statement
#
my $USAGE = "\nlivedtd.pl version $VERSION
Usage:
livedtd.pl [options] dtdfile
Options:
--catalog catalogfile [if DTD uses a catalog file]
--label xxx [output filename prefix]
--outdir outputdirectory [default is ./livedtd]
--title \"displayed title\" [title in HTML files]
--sgml [DTD is SGML, not default XML]
--nousage [do not generate tables of name usage]
--verbose [lots of info to STDOUT]
";
#######################################################
# Process the command line
#
my %opt = ();
&GetOptions(\%opt,
'catalog=s@',
'label:s',
'outdir:s',
'title:s',
'sgml',
'nousage',
'verbose') || die "$USAGE";
# Catalog option processed below.
$label = $opt{'label'} if $opt{'label'};
$outdir = $opt{'outdir'} if $opt{'outdir'};
$title = $opt{'title'} if $opt{'title'};
$usagetables = 0 if $opt{'nousage'};
$verbose = $opt{'verbose'} if $opt{'verbose'};
if ( $opt{'sgml'} ) {
$ML = 'SGML';
$EntName = $SGMLEntName;
$ElemName = $SGMLElemName;
}
$MainDTD = shift @ARGV || die "ERROR: Missing DTD argument.\n$USAGE";
# Set the default title
$title = basename($MainDTD) unless $title;
#######################################################
# Make sure the output dir is writable
#
if ( -d $outdir ) {
# It exists, but is it writable?
-w $outdir
or die "Cannot write to output directory $outdir.\n";
}
else {
# Create it
system ("mkdir -p $outdir") == 0
or die "Cannot create output directory $outdir.\n";
}
#######################################################
# Process the catalog file(s)
#
if ( $opt{'catalog'} ) {
print "Got here\n";
# Add current directory to @INC
($homedir = $0 ) =~ s/\\/\//g;
$homedir =~ s/^(.*)\/[^\/]+$/$1/;
unshift (@INC, $homedir);
require 'OASIS/Catalog.pm'
or die "Must have OASIS/Catalog.pm to process catalogs\n";
$catalog = new OASIS::Catalog('DEBUG'=>1);
my @catalogs = @{$opt{'catalog'}};
foreach my $catfile (@catalogs) {
print "catalog = $catfile \n";
$catalog->parse($catfile) or die "Cannot load catalog file $catfile\n";
}
}
#######################################################
# Main program
#######################################################
#
# Set outputs to autoflush
select(STDERR); $| = 1;
select(STDOUT); $| = 1;
# Set this for the first pass through the DTD to get names.
$pass = 1;
# And parse the DTD to load the name arrays.
print STDOUT "Parsing DTD files ...\n";
&parsefile($MainDTD);
# Print out verbose lists if option used.
&PrintLists if $verbose;
# Clear the Files array
@Files = ();
# Parse again and generate DTD html files
$pass = 2;
print STDOUT "Generating HTML files ...\n";
&parsefile($MainDTD);
# Generate framework and list HTML files
print STDOUT "Generating list files and HTML frameset...\n";
&MakeFramework;
# Generate usage tables
if ($usagetables) {
print STDOUT "Generating usage tables...\n" ;
&GenerateUsageTables() ;
}
print STDOUT "Done.\n";
#######################################################
# PrintLists -- Print verbose lists of elements and entities
#
sub PrintLists {
my ($i, $columns, @header, $ruleline) ;
# Print out the entity list
$columns = "%-28s %-7s %-15s %s\n";
@header = ('Name', 'Type', 'File', 'Value');
$ruleline = "-" x 75 . "\n";
print STDOUT "\n", $ruleline;
print STDOUT "ENTITIES (in order of declaration)\n";
print STDOUT $ruleline;
printf STDOUT ($columns, @header);
print STDOUT $ruleline;
for ($i = 0; $i <= $#PE; ++$i) {
my $rec = $PE{$PE[$i]};
my $value = substr($rec->{value}, 0, 20);
$value =~ s/\s+/ /gs;
$value =~ s/^ //;
$value .= " ..." if ( length($rec->{value}) > 20);
printf STDOUT ($columns,
$rec->{name},
$rec->{type},
basename($rec->{file}),
$value,
);
}
# Print out the element list
$columns = "%-30s %-15s %s\n";
@header = ('Name', 'File', 'Anchor name');
$ruleline = "-" x 60 . "\n";
print STDOUT "\n", $ruleline;
print STDOUT "ELEMENTS (in order of declaration)\n";
print STDOUT $ruleline;
printf STDOUT ($columns, @header);
print STDOUT $ruleline;
for ($i = 0; $i <= $#ELEMENT; ++$i) {
my $rec = $ELEMENT{$ELEMENT[$i]};
printf STDOUT ($columns,
$rec->{name},
basename($rec->{file}),
$rec->{anchor});
}
print STDOUT $ruleline;
}
########################################################
# resolve -- Resolve an entity
sub resolve {
my $string = shift;
if ( $string =~ /%($EntName);?/) {
# then it is a parameter entity that needs looking up
if ( $PE{$1} ) {
$string = $PE{$1}->{value};
}
}
return($string);
}
########################################################
# resolveall -- Resolve all entities in a string
sub resolveall {
my $string = shift;
while ($string =~ /(%($EntName);?)/s) {
# save this for comparison
my $prevstring = $string;
my $entityref = $1;
if ($PE{$2}) {
my $new = $PE{$2}->{value};
$string =~ s/$entityref/$new/s;
}
# Punt if it didn't change
if ($string eq $prevstring) {
die "Unresolved parameter entity $entityref in $string\n";
}
}
return($string);
}
#######################################################
# parsefile -- Parse a DTD file
# Argument should be a SYSTEM name. If the catalog option
# is used, it should already have been looked up when
# the SYSTEM entity was declared. If no catalog option
# then the SYSTEM name has not been checked for
# readability or relative pathname.
#
sub parsefile {
my $Filename = shift;
my $Basename = basename($Filename);
my $size ;
my $Output;
local ($_);
# Resolve pathnames relative to previous file
# if not absolute
if ( ! -f $Filename ) {
$Filename = $dirstack[$#dirstack] . "/" . $Filename;
}
if ($Filename =~ /^(.*)[\/\\]([^\/\\]+)$/) {
push (@dirstack, $1);
} else {
push (@dirstack, ".");
}
# Is the file already open?
if ( grep /^$Filename$/, @Files ) {
return;
}
# Add to the file array
push @Files, $Filename;
# Open and read the whole file into $_ variable for parsing
my $Filehandle = IO::File->new($Filename)
or die "Can't open file $Filename $!\n";
read ($Filehandle, $_, -s $Filename);
$Filehandle->close;
# Start output file if pass 2
if ( $pass > 1 ) {
my $outfile = $outdir . "/" . $label . $Basename . ".html";
$Output = IO::File->new("> $outfile")
or die "Cannot write to output file $outfile.\n";
# Set to autoflush
select($Output); $| = 1;
# Write the header to the file
my $onload = "parent.file.document.open();"
. "parent.file.document.write("
. "'<BODY TOPMARGIN=0><B>"
. $Basename
. "</B></BODY>');"
. "parent.file.document.close()";
print $Output $DOCTYPE;
print $Output "<HTML>\n<HEAD>\n<TITLE>$Basename</TITLE>\n",
"<LINK REL=\"stylesheet\" TYPE=\"text/css\" HREF=\"livedtd.css\">\n",
"<STYLE TYPE=\"text/css\">\n",
" STRONG { font-weight: bold; color: red;}\n",
" .COMMENT { font-style:italic; color: #A8A8A8;}\n",
"</STYLE>\n",
"</HEAD>\n",
"<BODY onLoad=\"$onload\"><?LIVEDTD ENDHEAD?>\n<PRE>";
}
print STDOUT "Parsing file $Filename pass $pass ...\n" if $verbose;
# Locate all declarations.
while (/^(.*?)<!(.*?)>/s) {
my $nondecl = $1;
my $decl = $2;
my $rest = $';
# Parse the non declaration first.
# Leading non-declarations may be PEs calling SYSTEM files.
if ($nondecl) {
&handlenondecl($nondecl, $Filename, $Output);
}
# Comments require reparse to include any > characters
if ($decl =~ /^--/) {
/^.*?<!(--.*?--)>/s;
$decl = $1;
$rest = $';
}
# Marked sections also are a special case.
if ($decl =~ /^(\[\s*(\S+)\s*\[)/s ) {
my $MSstart = $1;
my $condition = $2;
my $depth = 1;
my $content = "";
# Reset $_ to what follows the [
$_ = $' . ">" . $rest;
# This loop assembles a complete marked section
# content (between [ and ]]>, exclusive).
# Any nested marked sections are included.
# No processing of conditions yet.
while (($depth > 0) && /^(.*?)((<!\[)|(\]\]>))/s) {
$content .= $1;
if ($4) {
$depth--;
if ($depth > 0) {
$content .= $4;
}
} else {
$depth++;
$content .= $3;
}
$_ = $';
}
# Reset $rest to after this marked section
$rest = $_;
if ( &resolveall($condition) =~ /^INCLUDE$/i ) {
if ( $pass == 1 ) {
# Use it.
$_ = $content . $rest;
print STDOUT "Including marked section "
. "$condition in $Filename\n" if $verbose;
next;
}
# If pass 2
else {
print $Output "<!" ;
# Track this in usage table
# if the condition is an entity reference
if ( $condition =~ /%($EntName)(;?)/ && $usagetables ) {
my $status = $1;
# Is it a live PE?
if ( grep /^$status$/, @PE ) {
# Insert anchor
my $declname = "MS" . ++$MarkedSectionCount;
print $Output "<A NAME=\""
. $declname
. "\"></A>";
# And add to usage table
if ($usagetables) {
&UsageTables($status, 'PE', $declname, 'MS');
}
# And add the data to the record
my $address = $label . $Basename . '.html#'
. $declname ;
push @{$PEUsage{$status}->{msusage}}, $address;
}
}
print $Output &MakeLive($MSstart);
# Restore the closing marker
$_ = $content . "]]>" . $rest;
next;
}
}
elsif ( &resolveall($condition) =~ /^IGNORE$/i ) {
if ( $pass == 1 ) {
# Skip this content.
$_ = $rest;
print STDOUT "Ignoring marked section $condition"
. " in $Filename\n" if $verbose;
next;
}
# if pass 2
else {
# Just print it plain except for condition
print $Output "<!" ;
# Track this for usage table reference
# if the condition is an entity reference
if ( $condition =~ /%($EntName)(;?)/ && $usagetables ) {
my $status = $1;
# Is it a live PE?
if ( grep /^$status$/, @PE ) {
# Insert anchor
my $declname = "MS" . ++$MarkedSectionCount;
print $Output "<A NAME=\""
. $declname
. "\"></A>";
# And add to usage table
if ($usagetables) {
&UsageTables($status, 'PE', $declname, 'MS');
}
# And add the data to the record
my $address = $label . $Basename . '.html#'
. $declname ;
push @{$PEUsage{$status}->{msusage}}, $address;
}
}
print $Output &MakeLive($MSstart);
print $Output &html($content);
print $Output "]]>";
$_ = $rest;
next;
}
}
else {
die "Invalid marked section condition"
. " $condition in $Filename\n";
}
} # end of marked section processing
$_ = $rest;
&handledecl($decl, $Filename, $Output);
}
# Handle any trailing stuff at end of file
&handlenondecl($_, $Filename, $Output);
# Close off the $Output in pass 2
if ( $Output ) {
# Mark the bottom so can remove HTML
print $Output "<?LIVEDTD EOF?>\n";
# Pad bottom so #scrolls always show at top of frame
print $Output "\n" x 40 ;
print $Output "</PRE></BODY></HTML>";
# Close the file
$Output->close()
}
pop (@dirstack);
}
#######################################################
# handlenondecl -- Parse and print text outside of declarations
#
sub handlenondecl {
my $nondecl = shift;
my $Filename = shift;
my $Output = shift;
# Loop through to process all SYSTEM entities
while ( $nondecl =~ /(%($EntName)(;?))/s ) {
my $entityref = $1;
my $entname = $2;
my $rest = $';
# Output any leading stuff if pass 2
print $Output &html($`) if $Output;
if ( my $ent = $PE{$entname} ) {
if ( $ent->{type} eq 'SYSTEM' ) {
&parsefile($ent->{value}) if $ent->{value};
}
else {
# If not an empty string, then error
if ( $ent->{value} ) {
die "Parameter entity reference $entityref"
. " must be a SYSTEM file if used outside"
. " of a declaration\n";
}
}
# And output this as a hot spot
print $Output "<A HREF=\""
. $PE{$entname}->{address}
. "\">"
. $entityref
. "</A>"
if $Output;
}
else {
# Else not an active system file
print $Output $entityref if $Output;
}
$nondecl = $rest;
}
print $Output &html($nondecl) if $Output;
}
#######################################################
# handledecl -- Parse declarations
#
sub handledecl {
my $decl = shift;
my $Filename = shift;
my $Output = shift;
if ($decl =~ /^ENTITY/s ) {
if ( $Output ) {
&entityprint($decl, $Filename, $Output);
}
else {
&entitydef($decl, $Filename);
}
}
elsif ($decl =~ /^ELEMENT/s ) {
if ( $Output ) {
&elementdef($decl, $Filename, $Output);
}
else {
&elementdef($decl, $Filename);
}
}
elsif ($decl =~ /^NOTATION/s ) {
print $Output "<!" . &MakeLive($decl) . ">" if $Output;
}
elsif ($decl =~ /^ATTLIST/s ) {
&attlistprint($decl, $Filename, $Output);
}
elsif ($decl =~ /^--.*?--/s ) {
&commentprint($decl, $Output);
}
else {
print $Output "<!" . &html($decl) . ">" if $Output;
}
}
#######################################################
# commentprint -- Print Comment
#
sub commentprint {
my $decl = shift;
my $Output = shift;
print $Output "<SPAN CLASS=\"COMMENT\">" if $Output;
print $Output "<!" . &html($decl) . ">" if $Output;
print $Output "</SPAN>" if $Output;
}
#######################################################
# entitydef -- Register a valid parameter entity definition
#
sub entitydef {
my $decl = shift;
my $Filename = shift;
my $Basename = basename($Filename);
my $rec = {};
my ($name, $type, $value, $address);
# There are five cases of entity defs with keywords:
# 1 PUBLIC "publicid" SYSTEM "systemid"
# 2 PUBLIC "publicid" "systemid"
# 3 PUBLIC "publicid"
# 4 SYSTEM "systemid"
# 5 "anyvalue"
# Case 1 seems to be reserved for XML.
# Cases 2 and 3 for SGML.
# The order of resolution according to the SGML Open
# Technical Resolution 9401:1997 is:
# catalog SYSTEM identifier from the systemid
# catalog PUBLIC identifier from the publicid
# entity SYSTEM identifier from the systemid
# NOTE: ALLOW OVERRIDE catalog feature not yet supported.
# Resolved identifiers become $type = SYSTEM.
# Case 1: PUBLIC and SYSTEM
if ( $decl =~ /^ENTITY\s+%\s+($EntName)\s+PUBLIC\s+(["'])([^\2]*)\2\s+SYSTEM\s+(["'])([^\4]*)\4/si ) {
$name = $1;
my $pubid = $3;
my $sysid = $5;
$type = 'PUBLIC';
# Resolve identifier if a new name
unless ( grep /^$name$/, @PE ) {
$value = &ResolveExternalID($pubid, $sysid);
}
$type = 'SYSTEM' if $value;
}
# Case 2: PUBLIC with systemid too
elsif ( $decl =~ /^ENTITY\s+%\s+($EntName)\s+PUBLIC\s+(["'])([^\2]*)\2\s+(["'])([^\4]*)\4/si ) {
$name = $1;
my $pubid = $3;
my $sysid = $5;
$type = 'PUBLIC';
# Resolve identifier if a new name
unless ( grep /^$name$/, @PE ) {
$value = &ResolveExternalID($pubid, $sysid);
}
$type = 'SYSTEM' if $value;
}
# Case 3: only PUBLIC
elsif ( $decl =~ /^ENTITY\s+%\s+($EntName)\s+PUBLIC\s+(["'])([^\2]*)\2/si ) {
$name = $1;
my $pubid = $3;
$type = 'PUBLIC';
# Resolve identifier if a new name
unless ( grep /^$name$/, @PE ) {
$value = &ResolveExternalID($pubid, undef);
}
$type = 'SYSTEM' if $value;
}
# Case 4: only SYSTEM
elsif ( $decl =~ /^ENTITY\s+%\s+($EntName)\s+SYSTEM\s+(["'])([^\2]*)\2/si ) {
$name = $1;
my $sysid = $3;
$type = 'SYSTEM';
# Resolve identifier if a new name
unless ( grep /^$name$/, @PE ) {
$value = &ResolveExternalID(undef, $sysid );
}
}
# Case 5: not an external entity
elsif ( $decl =~ /^ENTITY\s+%\s+($EntName)\s+.*(["'])([^\2]*)\2/s ) {
$name = $1;
$type = 'pe';
$value = $3;
}
else {
# must be either a text entity or bad declaration
return;
}
# Add to list if not already there.
# Is the name already defined?
unless ( grep /^$name$/, @PE ) {
$rec->{name} = $name;
$rec->{type} = $type;
$rec->{value} = $value;
$rec->{file} = $Filename;
$rec->{anchor} = 'EntityDef.' . $name;
$rec->{address} = $label . $Basename . '.html#' . $rec->{anchor} ;
# save the whole record
$PE{$rec->{name}} = $rec;
push ( @PE, $name);
}
}
#######################################################
# elementdef -- Register or print a valid element definition
#
sub elementdef {
my $decl = shift;
my $Filename = shift;
my $Output = shift;
my @names;
my $namefield;
my $rest;
my $lead;
my $Basename = basename($Filename);
# Element def may contain list or parameter entity.
# Case 1: list (name1|name2) in parens
if ( $decl =~ /^(ELEMENT\s+)(\(\s*(.*?)\s*\))(\s+)/s ) {
# Save the pieces
$lead = $1;
# Save the raw string for output
$namefield = $2;
$rest = $4 . $' ;
# Resolve all entities and remove whitespace
my $resolved = &resolveall($3);
$resolved =~ s/\s+//g;
@names = split (/\|/, $resolved ) ;
}
elsif ( $decl =~ /(^ELEMENT\s+)(\S+)(\s+)/s ) {
$lead = $1;
$namefield = $2;
$rest = $3 . $' ;
my $resolved = &resolveall($2);
# Is the resolved string a list?
if ( $resolved =~ /\(\s*(.*?)\s*\)/s ) {
$resolved = $1;
$resolved =~ s/\s+//g;
@names = split (/\|/, $resolved ) ;
}
else {
# Else single name array
@names = ($resolved);
}
}
else {
print STDERR "Badly formed element declaration: $decl\n";
return;
}
# Process the declaration
if ( $pass == 1 ) {
# Only increment ElementDefCount once per declaration.
my $oldcount = $ElementDefCount;
for my $name (@names) {
# strip spaces
$name =~ s/\s*//gs;