summaryrefslogtreecommitdiff
path: root/fixupdiff.pl
blob: 94c5112888f4eb8ea97847edbee0ba4f67a1e1b6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
my $bufferdiff="";
my $diff="";
my $buffer="";
my $lstlisting=0;
while (<>) {
	my $line = $_;
	if (m/%DIFDELCMD\s+<\s+\\begin{lstlisting}/) {
		$lstlisting=1;
	}
	if ($lstlisting) {
		$line =~ s/%DIFDELCMD\s+<\s+//;
		#print "%FIXED BY RULE 1\n";
	}
	#In section headings, replace begin/end with begin/endFL,
	#but be careful in case some tag spills over to the next
	#line
	if (m/\\(section|subsection|subsubsection|paragraph)/ and m/DIF/) {
		my @list = split(/(\\DIF(?:add|del)(?:begin|end)(?:FL)?)/, $line, -1);
		#if there's only one tag, don't touch it:
		#matching one is on the other line
		if ($#list >= 5) {
			#if first tag is end, don't touch it - matching
			#begin is on the previous line
			if ($list[1] =~ m/begin$/) {
				$list[1] .= "FL";
			}
			#if last tag is begin, don't touch it - matching
			#end is on the next line
			if ($list[$#list - 1] =~ m/end$/) {
				$list[$#list - 1] .= "FL";
			}
		}
		for (my $i = 3; $i <= $#list - 3; $i += 2) {
			if (not $list[$i] =~ m/FL$/) {
				$list[$i] .= "FL";
			}
		}
		$line = join("", @list);
		#print "%FIXED BY RULE 2\n";
	}
	#detect where we have DIFbegin/end cross
	#enumerate/itemize environments and fix up
	if (m/\\DIF(?:add|del)(?:begin|end)/) {
		my @list = split(/(\\DIF(?:add|del)(?:begin|end)(?:FL)?)/, $line, -1);
		$diff = $list[$#list - 1];
		if ($diff =~ m/begin/) {
			$diff =~ s/begin/end/;
		} else {
			$diff = "";
		}
	}
	if ($diff ne "" and m/\\(?:begin|end){(?:enumerate|itemize)}$/ and not m/\\DIF/) {
		$buffer = $line;
		$bufferdiff = $diff;
		$line = "";
		#print "%BUFFERED BY RULE 3: $bufferdiff\n";
	}
	if ($buffer ne "" and $line ne "") {
		if (m/^(\\DIF(?:add|del)end(?:FL)?)/ and $bufferdiff ne $1) {
			$line =~ s/^(\\DIF(?:add|del)end(?:FL)?)//;
			$buffer =~ s/(\\(?:begin|end){(?:enumerate|itemize)})$/$bufferdiff$1/;
			#print "%FIXED BY RULE 3: $bufferdiff\n";
		}
		print $buffer;
		$buffer = "";
		$bufferdiff = "";
	}
	print $line;
	if (m/%DIFDELCMD\s+<\s+\\end{lstlisting}/) {
		$lstlisting=0;
	}
}