2006-09-16 10:49:47 +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
|
|
|
|
# and also ignore
|
|
|
|
# <![CDATA[ and ]]> and <!-- and -->
|
|
|
|
sub
|
|
|
|
xml_decode ($)
|
|
|
|
{
|
|
|
|
my $input = shift;
|
|
|
|
|
|
|
|
$input =~ s/\&/&/g;
|
|
|
|
$input =~ s/</</g;
|
|
|
|
$input =~ s/>/>/g;
|
|
|
|
|
|
|
|
if ($input =~ /<!\[CDATA\[/) { $input = ""; }
|
|
|
|
if ($input =~ /]]>/) { $input = ""; }
|
|
|
|
if ($input =~ /<!--/) { $input = ""; }
|
|
|
|
if ($input =~ /-->/) { $input = ""; }
|
|
|
|
|
|
|
|
#print "Returning line $input";
|
|
|
|
return $input;
|
|
|
|
}
|
|
|
|
|
|
|
|
# main
|
|
|
|
my $output = shift @ARGV;
|
2010-12-03 10:23:27 +00:00
|
|
|
my $outputname;
|
2006-09-16 10:49:47 +00:00
|
|
|
|
2010-12-03 08:18:19 +00:00
|
|
|
# strip path parts
|
2010-12-03 10:23:27 +00:00
|
|
|
if ($output =~ m/.*\/(.*)$/)
|
|
|
|
{
|
|
|
|
$outputname = $1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$outputname = $output;
|
|
|
|
}
|
2010-12-03 08:18:19 +00:00
|
|
|
|
2006-09-16 10:49:47 +00:00
|
|
|
$found = 0;
|
|
|
|
%blocks = ();
|
|
|
|
|
|
|
|
foreach $file (@ARGV)
|
|
|
|
{
|
|
|
|
open FILE, $file or die "Cannot open file $file";
|
|
|
|
|
|
|
|
while ($line = <FILE>)
|
|
|
|
{
|
2010-12-03 08:18:19 +00:00
|
|
|
if ($line =~ /<!-- example-begin $outputname (.*?)-->/)
|
2006-09-16 10:49:47 +00:00
|
|
|
{
|
|
|
|
$found = 1;
|
|
|
|
$block_id = $1;
|
|
|
|
$block = "\n/*** block $block_id from $file ***/\n";
|
|
|
|
|
2010-12-03 08:18:19 +00:00
|
|
|
print "Extracting $outputname block $block_id from $file\n";
|
2006-09-16 10:49:47 +00:00
|
|
|
|
|
|
|
while ($line = <FILE>)
|
|
|
|
{
|
2010-12-03 08:18:19 +00:00
|
|
|
if ($line =~ /<!-- example-end $outputname (.*?)-->/)
|
2006-09-16 10:49:47 +00:00
|
|
|
{
|
|
|
|
last;
|
|
|
|
}
|
|
|
|
$block .= xml_decode ($line);
|
|
|
|
}
|
|
|
|
$blocks{$block_id} = $block;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (!$found)
|
|
|
|
{
|
2010-12-03 08:18:19 +00:00
|
|
|
print "Could not find $outputname example !\n";
|
2006-09-16 10:49:47 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
# now output all the blocks in the right order
|
|
|
|
open OUTPUT, ">$output";
|
|
|
|
@block_ids = keys %blocks;
|
|
|
|
foreach $block_id (sort @block_ids)
|
|
|
|
{
|
|
|
|
print "Writing $output block $block_id\n";
|
|
|
|
print OUTPUT $blocks{$block_id};
|
|
|
|
}
|
|
|
|
close OUTPUT;
|