]> the.earth.li Git - htag.git/blobdiff - plugins/22reformat
Import Upstream version 0.0.23
[htag.git] / plugins / 22reformat
index 910c76850bbf21edc04736c1393bdefce0f774be..02c9518008f32df427768d7e08c2bf54fb9adf38 100644 (file)
@@ -1,6 +1,6 @@
 #!/usr/bin/perl -w
 
-# Copyright (C) 2000-2001 Simon Huggins
+# Copyright (C) 2000-2003 Simon Huggins
 # reformats longer lengths after the substitutions have taken place
 
 # This program is free software; you can redistribute it and/or modify it
 # with this program; if not, write to the Free Software Foundation, Inc., 59
 # Temple Place, Suite 330, Boston, MA 02111-1307  USA
 
-return;
-
 use strict;
-use Text::Wrap;
-
-$Text::Wrap::columns=defined $cfg{'maxlinelen'} ? $cfg{'maxlinelen'} : 72;
-$cfg{'first'}  ||= "";
-$cfg{'leader'} ||= "";
+use vars qw($nomod);
 
-my $anal_merge_debug=0;
+BEGIN {
+       unless (eval "use Text::Balanced qw(gen_extract_tagged);1;") {
+               print STDERR "Text::Balanced not available, reformat plugin will not work\n";
+               $nomod=1;
+       }
+}
+return if $nomod;
 
 sub remove_space($) {
         my $text=shift;
@@ -42,14 +42,35 @@ sub remove_space($) {
         return $text;
 }
 
-sub reformat($) {
-       my $sig = shift;
-# LRC
-#$chunk=&chunksizealign($chunk,$len,$align);
-       while ($sig =~ /\@(CENTER|RIGHT|LEFT)(\d+)?\@/) {
-       }
+sub reformat ($) {
+       my $text = shift;
+       my ($ext,$end,$beg,$otag,$ctag,$stuff);
+       my $process = gen_extract_tagged('<(?:CENTER|RIGHT|LEFT)\d+>',
+                       undef,
+                       '(?s).*(?=<(?:CENTER|RIGHT|LEFT)\d+>)',
+                       {reject => ['<(?:CENTER|RIGHT|LEFT)\d+>']} );
+       while (1) {
+               ($stuff, $end, $beg, $otag, $ext, $ctag) = &$process($text);
+               if (not defined $stuff) {
+                       if ($text =~ /<(?:CENTER|RIGHT|LEFT)\d+>/) {
+                               nicedie($@->{'error'});
+                       }
+               }
+               last if !$stuff;
 
-       return $sig;
+               my $size = $otag;
+               $size =~ s/<(?:CENTER|RIGHT|LEFT)//;
+               $size =~ s/>//;
+
+               my $align = $otag;
+               $align =~ s/^.(.).*$/$1/;
+
+               my $c = chunksizealign($ext, $size, $align);
+
+               $stuff=quotemeta($stuff);
+               $text =~ s/$stuff/$c/;
+       }
+       return $text;
 }
 
 my ($tag,$sig,$newsig);
@@ -60,7 +81,8 @@ while(<SIG>) {
 }
 close(SIG);
 if (defined $sig) {
-       $sig =  reformat($sig);
+       $sig = reformat($sig);
+       $sig = remove_space($sig);
        if (defined $sig) {
                open(SIG, ">$cfg{'tmpsigfile'}")
                        or htagdie