converted Anskavern to Anskaven and minor footnote issues
[project-aon.git] / scripts / gbtodot.pl
1 #!/usr/bin/perl -w
2 #
3 # Created from gengraph.pl by Thomas Wolmer
4 # 19 December 2002
5 #
6 # This program makes assumptions about the input XML file:
7 #
8 #  * the title of the numbered sections is a simple number
9 #  * the title appears on a line by itself
10 #  * the numbered sections are followed by sections of class "backmatter"
11 #
12 ################################################################################
13
14 use strict;
15
16 die( "gbtodot inputXMLfile outputDOTfile\n" ) if( $#ARGV + 1 < 2 );
17
18 my $infile = shift @ARGV;
19 my $outfile = shift @ARGV;
20 my $baseURL = shift @ARGV;
21
22 die( "Input file and output file the same: \"$infile\" \"$outfile\"\n" ) if( $infile eq $outfile );
23
24 open( INFILE, "<$infile" ) or die( "Cannot read $infile: $!\n" );
25 my @inlines = <INFILE>;
26 close INFILE;
27
28 my $graphName = $infile;
29 $graphName =~ s/\.xml$//;
30
31 open( OUTFILE, ">$outfile" ) or die( "Cannot write to $outfile: $!\n" );
32 print( OUTFILE "digraph theGraph {\n  nodesep=.1; \n  ranksep=.2;\n  ratio=auto;\n");
33 print( OUTFILE "  node [height=.3,width=.3,shape=ellipse,fixedsize=true,fontname=Arial,fontsize=8]\n");
34 print( OUTFILE "  edge [arrowsize=.7]\n");
35
36 my $section = -1;
37 my $combat = 0;
38 my $smallIll = 0;
39 my $ill = 0;
40 my @attributes = ();
41 my $backmatterFound;
42
43 foreach my $line (@inlines) {
44     if( $line =~ m|class="backmatter"| ) {
45         last;
46     }
47     if( $line =~ m|<title>(\d+)</title>| ) {
48         # Visualize the previous section's attributes
49         if( $combat ) {
50             push( @attributes, ( "color=firebrick1", "style=filled" ) );
51           }
52         # Assume that small and large illustrations never appear together
53         if( $smallIll ) {
54             push( @attributes, ( "fontcolor=DarkGreen", "label=\"s$smallIll\\n$section\"" ) );
55         }
56         if( $ill ) {
57             push( @attributes, ( "fontcolor=blue", "label=\"i$ill\\n$section\"" ) );
58         }
59         if( defined( $baseURL ) ) {
60             push( @attributes, ( "URL=\"${baseURL}sect${section}.htm\"", "" ) );
61         }
62
63         # Print out extra section data if needed
64         if( $#attributes > 0 && $section != -1 ) {
65 #            print( OUTFILE "  $section [", join( ',', @attributes ), "];\n");
66             printf( OUTFILE "  %03d [%s];\n", $section, join( ',', @attributes ) );
67         }
68         $combat = 0;
69         $smallIll = 0;
70         $ill = 0;
71         @attributes = ();
72         $section = $1;
73     }
74     elsif( $line =~ m|<choice\sidref="sect(\d+)">| ) {
75 #        print( OUTFILE "  $section -> $1;\n" );
76         printf( OUTFILE "  %03d -> %03d;\n", $section, $1 );
77     }
78     elsif( $line =~ m|idref="sect(\d+)"| && $section != -1 ) { # misc links (should be pruned later)
79         printf( OUTFILE "  %03d -> %03d [style=dotted,dir=none];\n", $1, $section );
80     }
81
82     if( $line =~ m|<combat>| ) {
83         $combat = 1;
84     }
85
86     if( $line =~ m|src=\"ill(\d+)| ) {
87         $ill = $1;
88     }
89
90     if( $line =~ m/src=\"s(mall|ect)(\d+)/ ) {
91         $smallIll = $2;
92     }
93 }
94
95 print( OUTFILE "}\n" );
96 close OUTFILE;