File: vo_maketags.pl

package info (click to toggle)
vimoutliner 0.3.4%2Bpristine-9
  • links: PTS, VCS
  • area: main
  • in suites: squeeze, wheezy
  • size: 592 kB
  • ctags: 287
  • sloc: python: 897; perl: 678; sh: 133; makefile: 29
file content (330 lines) | stat: -rwxr-xr-x 7,150 bytes parent folder | download | duplicates (4)
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
#!/usr/bin/perl -w
# #######################################################################
# vo_maketags.pl: Vim outline tagging system, main program, version 0.1.2
#   Copyright (C) 2001-2003 by Steve Litt (slitt@troubleshooters.com)
#
#   This program is free software; you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation; either version 2 of the License, or
#   (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program; if not, write to the Free Software
#   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
# Steve Litt, slitt@troubleshooters.com, http://www.troubleshooters.com
# #######################################################################

# #######################################################################
# #######################################################################
# #######################################################################
# HISTORY
# V0.1.0 Pre-alpha
#     Starting at a "top level" indent-defined Vim outline, this
#     program finds all "tags" defined as headlines starting with
#     _tag_, and containing a subheadline containing the file
#     to which the tag should jump. This program creates a tags
#     file.
#Steve Litt, 5/28/2001
#End of version 0.1.0
#
# V0.1.1 Pre-alpha
#     Bug fixes, including ../ resolution
#
#Steve Litt, 5/28/2001
#End of version 0.1.1
#
#
# V0.1.2 Pre-alpha
#	More bug fixes, and facility to create a new outline
#	file from a tag whose corresponding file doesn't yet
#	exist.  
#Steve Litt, 5/30/2001
#End of version 0.1.2
#
# V0.1.3 Pre-alpha
#	More bug fixes. This was the first version released
#	file from a tag whose corresponding file doesn't yet
#	exist.  
#Steve Litt, 6/01/2001
#End of version 0.1.3
#
# V0.2.0 Pre-alpha
#Steve Litt, 12/03/2002
#	This file unchanged. The overall Vimoutliner version
#	0.2.0 has extensive improvements, including intuitive
#	collapse/expand.
#End of version 0.2.0
#END OF HISTORY
#
#
# V0.1.2 Pre-alpha
#	More bug fixes, and facility to create a new outline
#	file from a tag whose corresponding file doesn't yet
#	exist.  
#Steve Litt, 5/30/2001
#End of version 0.1.2
#END OF HISTORY
#
# #######################################################################


use strict;
use vars qw($TAGFILENAME);
use Cwd;

$TAGFILENAME = $ENV{"HOME"} . "/.vimoutliner/vo_tags.tag";

sub process1Outline($$); #Early prototype the recursive routine
sub makeDirectory($);    #Early prototype the recursive routine

sub makeTagFileStartingAt($)
	{
	unless(@ARGV == 1)
		{
		usage();
		die;
		}
	my($absoluteFileName) =  deriveAbsoluteFileName(Cwd::cwd(), $_[0]);
	
	my(%processedFiles) = ();
	recordFileAsProcessed($absoluteFileName,\%processedFiles);
	unlink $TAGFILENAME;
	process1Outline($absoluteFileName, \%processedFiles);
	sortAndDeleteDupsFromTagFile();
	}

sub sortAndDeleteDupsFromTagFile()
	{
	my($TEMPTAGFILENAME) = "$ENV{'HOME'}/temptagfile.tag";
	system("sort $TAGFILENAME | uniq > $TEMPTAGFILENAME"); 
	system("rm $TAGFILENAME");
	system("mv $TEMPTAGFILENAME $TAGFILENAME");
	}


sub process1Outline($$)
	{
	my($fileName) = $_[0];
	my($processedFilesHashRef) = $_[1];
	
	unless(fileExists($fileName))
		{
		makeDirectory($fileName);
		makeEmptyFile($fileName);
		}

	print "Begin processing file $fileName.\n";

	my($baseDirectory) = getBaseDirectory($fileName);
	my(%tags) = getTagsFromFile($fileName);
	my(@tagKeys) = keys(%tags);
	my($tagKey);
	foreach $tagKey (@tagKeys)
		{
		my($absoluteFileName);
		if(isAbsoluteFilePath($tags{$tagKey}))
			{
			$absoluteFileName = $tags{$tagKey};
			}
		else
			{
			$absoluteFileName =
			 deriveAbsoluteFileName($baseDirectory, $tags{$tagKey});
			}
		appendTagToTagFile($tagKey,$absoluteFileName);
 		if(notProcessedYet($absoluteFileName, $processedFilesHashRef))
 			{
			recordFileAsProcessed($absoluteFileName,$processedFilesHashRef);
 			process1Outline($absoluteFileName, $processedFilesHashRef);
 			}
		}
	}

sub appendTagToTagFile($$)
	{
	open(TAGFILE, ">>$TAGFILENAME");
	print TAGFILE "$_[0]	$_[1]	:1\n";
	close(TAGFILE);
	}


sub makeEmptyFile($)
	{
	open(OUTLINEFILE, ">" . $_[0]);
	close(OUTLINEFILE);
	}


sub makeDirectory($)
	{
	my($completeFileName) = $_[0];
	my($directoryName) = ($completeFileName =~ m/^(.*?)[^\/]*$/);
	unless($directoryName eq "")
		{
		my($temp) = ($directoryName =~ m/^(.*).$/);
		makeDirectory($temp);
		print "Creating $directoryName...";
		if(mkdir $directoryName)
			{
			print " succeeded.\n";
			}
		else
			{
			print " no action: $!.\n";
			}
		}
	}

sub fileExists($)
	{
	my($outlineFileName) = $_[0];
	my($success) = open(OUTLINEFILE, "<" . $outlineFileName);
	if($success)
		{
		close(OUTLINEFILE);
		return(1);
		}
	else
		{
		return(0);
		}
	}

sub getTagsFromFile($)
	{
	my($outlineFileName) = $_[0];
	my(%tags);
	my($tagString) = "";
	my($success) = open(OUTLINEFILE, "<" . $outlineFileName);
	unless($success)
		{
		print "Failed to open $outlineFileName\n";
		return(());
		}
	while(<OUTLINEFILE>)
		{
		my($line) = $_;
		chomp($line);
		if($line =~ m/^\s*(_tag_\S+)/)
			{
			$tagString = $1;
			}
		elsif($tagString ne "")
			{
			$line =~ m/^\s*(\S+)/;
			my($filename) = $1;
			$tags{$tagString} =
			  deriveAbsoluteFileName(getBaseDirectory($_[0]), $1);
			$tagString = "";
			}
		}	
	return(%tags);
	}

sub recordFileAsProcessed($$)
	{
	my($absoluteFileName) = $_[0];
	my($processedFilesHashRef) = $_[1];
	${$processedFilesHashRef}{$absoluteFileName} = "1";
	}

sub notProcessedYet($$)
	{
        my($absoluteFileName) = $_[0];
	my(%processedFiles) = %{$_[1]};
	if(defined($processedFiles{$absoluteFileName}))
		{
		return(0);
		}
	else
		{
		return(1);
		}
	}

sub dia($)
	{
	print "dia " . $_[0] . "\n";
	}


sub isAbsoluteFilePath($)
	{
	if($_[0] =~ m/^\//)
		{
		return 1;
		}
	else
		{
		return 0;
		}
	}

sub getFileNameOnly($)
	{ 
	my($fileString); 
	if ($_[0] =~ m/.+\/(.*)$/)
		{
		$fileString= $1
		}
	else
		{
		$fileString = $_[0];
		}

	return $fileString;
	}

sub getBaseDirectory($)
	{ 
	my($dirString) = ($_[0] =~ m/(.+\/).*$/);
	return $dirString;
	}

sub deriveAbsoluteFileName($$)
	{
	my($absoluteFileName);
	my($baseDirectory) = $_[0];
	my($passedFileName) = $_[1];
	unless($baseDirectory =~ m/\/$/)
		{
		$baseDirectory= $baseDirectory . "/"; 
		}
	if($passedFileName =~ m/^\//)
		{
		$absoluteFileName = $passedFileName;
		}
	else
		{
		$absoluteFileName = $baseDirectory . $passedFileName;
		}

	$absoluteFileName =~ s/\/\.\//\//g;  #remove all "./";
	deleteDoubleDots($absoluteFileName);

	return($absoluteFileName);
	}

sub deleteDoubleDots($)
	{
	while($_[0] =~ m/\.\./)
		{
		$_[0] =~ s/\/[^\/]*\/\.\.//;
		}
	}

sub usage()
	{
	print "\nUsage is:\n";
	print "otltags topLevelOutlineFileName\n\n";
	}
	

makeTagFileStartingAt($ARGV[0])