~ubuntu-branches/ubuntu/utopic/bacula-doc/utopic

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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#!/usr/bin/perl -w
# Fixes various things within tex files.

use strict;

my %args;


sub get_includes {
	# Get a list of include files from the top-level tex file.
	my (@list,$file);
	
	foreach my $filename (@_) {
		$filename or next;
		# Start with the top-level latex file so it gets checked too.
		push (@list,$filename);

		# Get a list of all the html files in the directory.
		open IF,"<$filename" or die "Cannot open input file $filename";
		while (<IF>) {
			chomp;
			push @list,"$1.tex" if (/\\include\{(.*?)\}/);
		}

		close IF;
	}
	return @list;
}

sub convert_files {
	my (@files) = @_;
	my ($linecnt,$filedata,$output,$itemcnt,$indentcnt,$cnt);
	
	$cnt = 0;
	foreach my $file (@files) {
		# Open the file and load the whole thing into $filedata. A bit wasteful but
		#   easier to deal with, and we don't have a problem with speed here.
		$filedata = "";
		open IF,"<$file" or die "Cannot open input file $file";
		while (<IF>) {
			$filedata .= $_;
		}
		close IF;
		
		# We look for a line that starts with \item, and indent the two next lines (if not blank)
		#  by three spaces.
		my $linecnt = 3;
		$indentcnt = 0;
		$output = "";
		# Process a line at a time.
		foreach (split(/\n/,$filedata)) {
			$_ .= "\n"; # Put back the return.
			# If this line is less than the third line past the \item command,
			#  and the line isn't blank and doesn't start with whitespace
			#  add three spaces to the start of the line. Keep track of the number
			#  of lines changed.
			if ($linecnt < 3 and !/^\\item/) {
				if (/^[^\n\s]/) {
					$output .= "   " . $_;
					$indentcnt++;
				} else {
					$output .= $_;
				}
				$linecnt++;
			} else {
				$linecnt = 3;
				$output .= $_;
			}
			/^\\item / and $linecnt = 1;
		}

		
		# This is an item line.  We need to process it too. If inside a \begin{description} environment, convert 
		#  \item {\bf xxx} to \item [xxx] or \item [{xxx}] (if xxx contains '[' or ']'.
		$itemcnt = 0;
		$filedata = $output;
		$output = "";
		my ($before,$descrip,$this,$between);

		# Find any \begin{description} environment
		while ($filedata =~ /(\\begin[\s\n]*\{[\s\n]*description[\s\n]*\})(.*?)(\\end[\s\n]*\{[\s\n]*description[\s\n]*\})/s) {
			$output .= $` . $1;
			$filedata = $3 . $';
			$descrip = $2;

			# Search for \item {\bf xxx}
			while ($descrip =~ /\\item[\s\n]*\{[\s\n]*\\bf[\s\n]*/s) {
				$descrip = $';
				$output .= $`;
				($between,$descrip) = find_matching_brace($descrip);
				if (!$descrip) {
					$linecnt = $output =~ tr/\n/\n/;
					print STDERR "Missing matching curly brace at line $linecnt in $file\n" if (!$descrip);
				}

				# Now do the replacement.
				$between = '{' . $between . '}' if ($between =~ /\[|\]/);
				$output .= "\\item \[$between\]";	
				$itemcnt++;
			}
			$output .= $descrip;
		}
		$output .= $filedata;
	
		# If any hyphens or \item commnads were converted, save the file.
		if ($indentcnt or $itemcnt) {
			open OF,">$file" or die "Cannot open output file $file";
			print OF $output;
			close OF;
			print "$indentcnt indent", ($indentcnt == 1) ? "" : "s"," added in $file\n";
			print "$itemcnt item", ($itemcnt == 1) ? "" : "s"," Changed in $file\n";
		}

		$cnt += $indentcnt + $itemcnt;
	}
	return $cnt;
}

sub find_matching_brace {
	# Finds text up to the next matching brace.  Assumes that the input text doesn't contain
	#  the opening brace, but we want to find text up to a matching closing one.
	# Returns the text between the matching braces, followed by the rest of the text following
	#   (which does not include the matching brace).
	# 
	my $str = shift;
	my ($this,$temp);
	my $cnt = 1;

	while ($cnt) {
		# Ignore verbatim constructs involving curly braces, or if the character preceding
		#  the curly brace is a backslash.
		if ($str =~ /\\verb\*?\{.*?\{|\\verb\*?\}.*?\}|\{|\}/s) {
			$this .= $`;
			$str = $';
			$temp = $&;

			if ((substr($this,-1,1) eq '\\') or 
				$temp =~ /^\\verb/) {
					$this .= $temp;
					next;
			}
				
			$cnt +=  ($temp eq '{') ? 1 : -1;
			# If this isn't the matching curly brace ($cnt > 0), include the brace.
			$this .= $temp if ($cnt);
		} else {
			# No matching curly brace found.
			return ($this . $str,'');
		}
	}
	return ($this,$str);
}

sub check_arguments {
	# Checks command-line arguments for ones starting with --  puts them into
	#   a hash called %args and removes them from @ARGV.
	my $args = shift;
	my $i;

	for ($i = 0; $i < $#ARGV; $i++) {
		$ARGV[$i] =~ /^\-+/ or next;
		$ARGV[$i] =~ s/^\-+//;
		$args{$ARGV[$i]} = "";
		delete ($ARGV[$i]);
		
	}
}

##################################################################
#                       MAIN                                  ####
##################################################################

my @includes;
my $cnt;

check_arguments(\%args);
die "No Files given to Check\n" if ($#ARGV < 0);

# Examine the file pointed to by the first argument to get a list of 
#  includes to test.
@includes = get_includes(@ARGV);

$cnt = convert_files(@includes);
print "No lines changed\n" unless $cnt;