Mercurial > bins
diff tools/bins_txt2xml @ 0:a84c32f131df 1.1.29
Import vendor version
author | Peter Gervai <grin@grin.hu> |
---|---|
date | Wed, 15 Oct 2008 23:28:56 +0200 |
parents | |
children |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tools/bins_txt2xml Wed Oct 15 23:28:56 2008 +0200 @@ -0,0 +1,100 @@ +#!/usr/bin/perl -w + + +use strict; + +my $verbose=1; + +my $bins_edit="bins_edit"; + +sub readTxt{ + my $file=shift; + my %hash; + + if (! open (FILE, $file)) { + print "Warning: cannot open file '$file' for reading ($!), skiping."; + return 1; + } + + my $tag; + my $value=""; + LINE: while (<FILE>) { + chomp; + next LINE if /^#/; #discard comments + next LINE if /^\s*$/; #ignore total whitespace + s/^\s+//; + s/\s+$//; + print " Reading line '$_'\n" if ($verbose >= 3); + if ($tag) { + if (m%</$tag>%) { + print " Found end tag </$tag>, " if ($verbose >= 2); + chomp($value); + $hash{$tag} = $value; + print "value is '$hash{$tag}'\n" if ($verbose >= 2); + $tag = ""; + $value = ""; + } else { + $value .= $_."\n"; + } + } elsif (m/<\w+>/) { + $tag = $_; + $tag =~ s/<(\w+)>/$1/; + print " Found begin tag <$tag>\n" if ($verbose >= 2); + } + } + close (FILE) || bail ("can't close $file ($!)"); + return %hash; +} + + +sub processFile{ + my $file = shift; + my $album = shift; # -a if album, empty string otherwise + + my %hash = readTxt($file); + + my $commandLine; + if ($file =~ m%/album.txt$%) { + $file =~ s%/album.txt$%/.%; + $commandLine="$bins_edit --html --album "; + } else { + $file =~ s/.txt$/.xml/; + $commandLine="$bins_edit --html "; + } + $commandLine .= "-v " foreach (2..$verbose); + my $tagName; + my $tagValue; + while ( ($tagName, $tagValue) = each(%hash) ) { + $tagValue =~ s/\'/\'\\\'\'/g; + $commandLine .= "-g $tagName='$tagValue' " + } + $commandLine .= "$file"; + + print " Executing «$commandLine»" if ($verbose >= 3); + system($commandLine); +} + +sub main { + my $dir = shift; + + print "Processing directory '$dir'...\n" if ($verbose >= 1); + + if (!opendir(DIR, "$dir")) { + print ("Warning: can't open dir $dir: $!\n"); + return; + } + my @files = grep { !/^\./ } readdir(DIR); + closedir DIR; + + foreach my $file (@files) { + if (-d $dir."/".$file) { + main ($dir."/".$file) + } elsif ($file =~ m/^album.txt$/) { + processFile($dir."/".$file, "-a"); + } elsif ($file =~ m/.txt$/) { + processFile($dir."/".$file, ""); + } + } +} + +main($ARGV[0]);