]> the.earth.li Git - htag.git/blob - plugins/15merge
7b48711562969776490d7ff5d5e9807afdcbad09
[htag.git] / plugins / 15merge
1 #!/usr/bin/perl -w
2
3 # Copyright (C) 2000-2001 Simon Huggins
4 # merge merges the sig and the tag but also merges the sig and the new style
5 # plugin things (i.e. all those silly files in $cfg{'tmpdir'}
6
7 # This program is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by the Free
9 # Software Foundation; either version 2 of the License, or (at your option)
10 # any later version.
11 #
12 # This program is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 # or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15 # for more details.
16 #
17 # You should have received a copy of the GNU General Public License along
18 # with this program; if not, write to the Free Software Foundation, Inc., 59
19 # Temple Place, Suite 330, Boston, MA 02111-1307  USA
20
21 use strict;
22 use Text::Wrap;
23
24 $Text::Wrap::columns=defined $cfg{'maxlinelen'} ? $cfg{'maxlinelen'} : 72;
25 $cfg{'first'}  ||= "";
26 $cfg{'leader'} ||= "";
27
28 my $anal_merge_debug=0;
29
30 sub remove_space($) {
31         my $text=shift;
32
33 # Remove whitespace at the end of lines but not newlines themselves.
34 # And don't remove the space if it comes directly after a -- which is
35 # anchored at the beginning of a line.
36
37         $text =~ s/(?<!^--)[    ]*$//mg;
38
39 # Remove any newlines from the very end of the string.
40         $text =~ s/\n*$//;
41         return $text;
42 }
43
44 sub merge($$) {
45         my ($tag,$sig) = @_;
46         my $chunk;
47         my $notag=1;
48
49         chomp($tag);
50         $tag =~ s/\t/ /g;
51         
52         my ($plugin,$len,$align,$wascr);
53         $wascr=0;
54
55         while ($sig =~ /@([A-Za-z]?)([1-9][0-9]*)([RC]?)@/) {
56                 # Ick.
57                 if (defined $3) {
58                         $plugin = $1;
59                         $len    = $2;
60                         $align  = $3;
61                 } elsif (not defined $2) {
62                         $len    = $1;
63                         $plugin = "";
64                         $align  = "L";
65                 } elsif ($2 =~ /^[RC]$/) {
66                         $plugin = "";
67                         $len    = $1;
68                         $align  = $2;
69                 } else {
70                         $plugin = $1;
71                         $len    = $2;
72                         $align  = "L";
73                 }
74                         
75 print STDERR "plugin,len,type = #$plugin#,#$len#,#$align#\n" if $anal_merge_debug;
76                 if ($plugin ne "") {
77                         $chunk = getplugin($plugin);
78                         print STDERR "Got plugin $plugin and $chunk\n"
79                                 if $anal_merge_debug;
80                         $sig =~ s/\@$plugin$len[RC]?@/$chunk/;
81                         print STDERR "Sig is now:\n$sig" if $anal_merge_debug;
82                         $chunk = "";
83                 } else {
84                         my $extra;
85                         $notag=0;
86                         $chunk =  substr $tag, 0, $len;
87 print STDERR "chunk,tag = #$chunk#,#$tag#".length($tag)." ".length($chunk)."\n"
88         if $anal_merge_debug;
89                         if ($chunk =~ s/^([^\n]+)\n+(.*)$/$1/s) {
90                                 $extra = $2;
91                                 print STDERR "\$extra = [$extra]\n"
92                                         if $anal_merge_debug;
93                         }
94                         if (length($chunk) < $len) {
95                                 print STDERR "length(chunk) < $len\n"
96                                         if $anal_merge_debug;
97                                 $chunk=&chunksizealign($chunk,$len,$align);
98                                 print STDERR "chunk = #$chunk#\n"
99                                         if $anal_merge_debug;
100                         }
101                         if (length($tag) < $len + 1) {
102                                 $tag= $extra ? $extra : "";
103 print STDERR "length(tag) < $len + 1, tag now = #$tag#(extra = #$extra#)\n"
104         if $anal_merge_debug;
105                         } elsif (substr $tag, 0,  $len + 1 eq ' ') {
106                                 $tag=substr $tag, $len + 1;
107                                 $tag=$extra . $tag if defined $extra;
108 print STDERR "substr tag, 0, $len + 1 was a space.  tag now = #$tag#\n"
109         if $anal_merge_debug;
110                         } else {
111                                 $tag=substr $tag, $len;
112                                 ### Back up a word in $chunk
113                                 $tag=$extra . $tag if defined $extra;
114 print STDERR "didn't break at space.  Backing up word.  tag now = #$tag#\n"
115         if $anal_merge_debug;
116                                 if ($chunk =~ s/(.*) (.*)$/$1/) {
117                                         $tag=$2 . $tag;
118                                         $chunk=&chunksizealign($chunk,$len,$align);
119                                 }
120 print STDERR "If space in chunk then change chunk and add word to tag.".
121 "Reformat chunk now = #$chunk# (tag = #$tag#)\n" if $anal_merge_debug;
122                         }
123                         $sig =~ s/\@$plugin$len[RC]?@/$chunk/;
124                 }
125         }
126         $sig =~ s/@([0-9]+)[RC]?@/" " x $1/eg;
127         $cfg{'notag'} = $notag;
128         if ($tag and not $notag) {
129                 return undef;
130         }
131         return &remove_space($sig);
132 }
133
134 {
135 my %plugins;
136 sub getplugin($) {
137         my $plugin = shift;
138
139         my $count = 0;
140         $count = $plugins{$plugin} if defined $plugins{$plugin};
141         open(IN, "$cfg{'tmpdir'}/$plugin") or htagdie "$0: Could not open $cfg{'tmpdir'}/$plugin: $!\n";
142         my $chunk;
143         while ($count > 0) {
144                 $chunk = <IN>;
145                 $count--;
146         }
147         $plugins{$plugin} = $count+1;
148         $chunk = <IN>;
149         chomp $chunk;
150         return $chunk;
151 }
152 }
153
154 {
155 my ($tag,$sig,$newsig);
156 open(SIG, "<$cfg{'tmpsigfile'}") or htagdie "$0: Could not open $cfg{'tmpsigfile'}: $!\n";
157 while(<SIG>) {
158         $sig .= $_;
159 }
160 close(SIG);
161 open(TAG, "<$cfg{'tmptagfile'}") or htagdie "$1: Could not open $cfg{'tmptagfile'}: $!\n";
162 while(<TAG>) {
163         $tag .= $_;
164 }
165 close(TAG);
166 if (defined $sig and $sig =~ /@[A-Za-z]?[1-9][0-9]*[RC]?@/) {
167         $sig =  merge($tag,$sig);
168 } else {
169         my $formatted_tag = Text::Wrap::wrap($cfg{'first'},$cfg{'leader'},$tag);
170         $sig .= $formatted_tag;
171         $sig =  &remove_space($sig);
172         $cfg{'notag'} = 0;
173 }
174 if (defined $sig) {
175         open(SIG, ">$cfg{'tmpsigfile'}") or htagdie "$0: Could not open $cfg{'tmpsigfile'}: $!\n";
176         print SIG "\n" while $cfg{'newline'}--;
177         print SIG $sig;
178         close(SIG);
179         return;
180 } else {
181         return(10);
182 }
183 }