5 # Normalize the .xbel file for cvs(1)/diff(1) etc.
6 # Removes any variable data from the file.
7 # Reencodes the file to utf-8.
9 # Copyright (C) 2005 Jan Kratochvil <project-xbelnormalize@jankratochvil.net>
11 # This program is free software; you can redistribute it and/or modify
12 # it under the terms of the GNU General Public License as published by
13 # the Free Software Foundation; exactly version 2 of June 1991 is required
15 # This program is distributed in the hope that it will be useful,
16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 # GNU General Public License for more details.
20 # You should have received a copy of the GNU General Public License
21 # along with this program; if not, write to the Free Software
22 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 use File::Temp qw(tempfile);
33 # No &Getopt::Long::GetOptions as we need to distinguish '-i' and '-iext'.
35 last if $ARGV[0]!~/^-/;
37 if ($_ eq "-h" || $_ eq "--help") {
41 Syntax: $0 [-i[<extension>]] [<file.xbel>]
43 -i[<extension>] edit the file in place (makes backup if extension supplied)
54 die "Invalid argument: $_\n";
56 die "Too many files\n" if 1<@ARGV;
58 my $twig=XML::Twig->new(
59 "pretty_print"=>"indented",
60 "output_encoding"=>"utf-8",
68 "metadata"=>sub { $_->delete() if !$_->has_children(); },
69 "info" =>sub { $_->delete() if !$_->has_children(); },
70 "folder"=>sub { $_->del_att("folded"); },
71 "xbel" =>sub { $_->del_att("folded"); },
75 if (!($filename=$ARGV[0])) {
76 $twig->parse(\*STDIN);
80 $twig->parsefile($filename);
83 if (!defined $opt_replace) {
87 (undef(),$tmpname)=tempfile("DIR"=>".");
88 } elsif ($opt_replace=~/[*]/) {
89 ($tmpname=$opt_replace)=~s/[*]/$filename/g;
91 $tmpname=$filename.$opt_replace;
93 rename($filename,$tmpname) or die "Error renaming \"$filename\" to \"$tmpname\": $!";
94 open $dstfh,">",$filename or die "Error opening target file \"$filename\": $!";
97 if (defined $opt_replace) {
98 close $dstfh or die "Error closing target file \"$filename\": $!";
100 unlink $tmpname or die "Error deleting temporary file \"$tmpname\": $!";