3 # extract code fragments from xml program listings
4 # first argument: source code file to find
5 # second argument: xml files to extract code from
9 # decodes xml by translating & < > back to what they should be
11 # <![CDATA[ and ]]> and <!-- and -->
17 $input =~ s/\&/&/g;
21 if ($input =~ /<!\[CDATA\[/) { $input = ""; }
22 if ($input =~ /]]>/) { $input = ""; }
23 if ($input =~ /<!--/) { $input = ""; }
24 if ($input =~ /-->/) { $input = ""; }
26 #print "Returning line $input";
31 my $output = shift @ARGV;
35 if ($output =~ m/.*\/(.*)$/)
41 $outputname = $output;
49 open FILE, $file or die "Cannot open file $file";
51 while ($line = <FILE>)
53 if ($line =~ /<!-- example-begin $outputname (.*?)-->/)
57 $block = "\n/*** block $block_id from $file ***/\n";
59 print "Extracting $outputname block $block_id from $file\n";
61 while ($line = <FILE>)
63 if ($line =~ /<!-- example-end $outputname (.*?)-->/)
67 $block .= xml_decode ($line);
69 $blocks{$block_id} = $block;
77 print "Could not find $outputname example !\n";
81 # now output all the blocks in the right order
82 open OUTPUT, ">$output";
83 @block_ids = keys %blocks;
84 foreach $block_id (sort @block_ids)
86 print "Writing $output block $block_id\n";
87 print OUTPUT $blocks{$block_id};