2002-09-14 13:17:10 +00:00
|
|
|
#!/usr/bin/perl
|
|
|
|
|
|
|
|
# extract code fragments from xml program listings
|
|
|
|
# first argument: source code file to find
|
|
|
|
# second argument: xml files to extract code from
|
|
|
|
|
|
|
|
# main
|
|
|
|
|
|
|
|
# decodes xml by translating & < > back to what they should be
|
2004-12-15 11:29:14 +00:00
|
|
|
# and also ignore
|
2004-12-15 11:54:26 +00:00
|
|
|
# <![CDATA[ and ]]> and <!-- and -->
|
2002-09-14 13:17:10 +00:00
|
|
|
sub
|
|
|
|
xml_decode ($)
|
|
|
|
{
|
|
|
|
my $input = shift;
|
|
|
|
|
|
|
|
$input =~ s/\&/&/g;
|
|
|
|
$input =~ s/</</g;
|
|
|
|
$input =~ s/>/>/g;
|
|
|
|
|
2004-12-15 11:29:14 +00:00
|
|
|
if ($input =~ /<!\[CDATA\[/) { $input = ""; }
|
|
|
|
if ($input =~ /]]>/) { $input = ""; }
|
2004-12-15 11:54:26 +00:00
|
|
|
if ($input =~ /<!--/) { $input = ""; }
|
|
|
|
if ($input =~ /-->/) { $input = ""; }
|
2004-12-15 11:29:14 +00:00
|
|
|
|
|
|
|
#print "Returning line $input";
|
2002-09-14 13:17:10 +00:00
|
|
|
return $input;
|
|
|
|
}
|
|
|
|
|
|
|
|
# main
|
|
|
|
my $output = shift @ARGV;
|
|
|
|
|
2004-09-08 23:18:55 +00:00
|
|
|
$found = 0;
|
2004-12-15 11:29:14 +00:00
|
|
|
%blocks = ();
|
2004-09-08 23:18:55 +00:00
|
|
|
|
2002-09-14 13:17:10 +00:00
|
|
|
foreach $file (@ARGV)
|
|
|
|
{
|
|
|
|
open FILE, $file or die "Cannot open file $file";
|
|
|
|
|
|
|
|
while ($line = <FILE>)
|
|
|
|
{
|
2004-12-15 17:32:49 +00:00
|
|
|
if ($line =~ /<!-- example-begin $output (.*?)-->/)
|
2002-09-14 13:17:10 +00:00
|
|
|
{
|
2004-09-08 23:18:55 +00:00
|
|
|
$found = 1;
|
2004-12-15 11:29:14 +00:00
|
|
|
$block_id = $1;
|
|
|
|
$block = "\n/*** block $block_id from $file ***/\n";
|
|
|
|
|
|
|
|
print "Extracting $output block $block_id from $file\n";
|
|
|
|
|
|
|
|
while ($line = <FILE>)
|
2002-09-14 13:17:10 +00:00
|
|
|
{
|
2004-12-15 17:32:49 +00:00
|
|
|
if ($line =~ /<!-- example-end $output (.*?)-->/)
|
2002-09-14 13:17:10 +00:00
|
|
|
{
|
2004-12-15 11:29:14 +00:00
|
|
|
last;
|
2002-09-14 13:17:10 +00:00
|
|
|
}
|
2004-12-15 11:29:14 +00:00
|
|
|
$block .= xml_decode ($line);
|
2002-09-14 13:17:10 +00:00
|
|
|
}
|
2004-12-15 11:29:14 +00:00
|
|
|
$blocks{$block_id} = $block;
|
2002-09-14 13:17:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2004-12-15 11:29:14 +00:00
|
|
|
|
|
|
|
|
2004-09-08 23:18:55 +00:00
|
|
|
if (!$found)
|
|
|
|
{
|
|
|
|
print "Could not find $output example !\n";
|
|
|
|
exit(1);
|
|
|
|
}
|
2004-12-15 11:29:14 +00:00
|
|
|
|
|
|
|
# now output all the blocks in the right order
|
|
|
|
open OUTPUT, ">$output";
|
|
|
|
@block_ids = keys %blocks;
|
|
|
|
foreach $block_id (sort @block_ids)
|
|
|
|
{
|
2004-12-15 11:54:26 +00:00
|
|
|
print "Writing $output block $block_id\n";
|
2004-12-15 11:29:14 +00:00
|
|
|
print OUTPUT $blocks{$block_id};
|
|
|
|
}
|
|
|
|
close OUTPUT;
|