3 # split an outline into several files.
5 # Copyright 2005 Noel Henson All rights reserved
8 # $Date: 2006/02/19 14:39:06 $
10 # $Source: /home/noel/active/otlsplit/RCS/otlsplit.py,v $
13 ###########################################################################
16 # This program accepts text outline files and splits them into
17 # several smaller files. The output file names are produced from the
18 # heading names of the parents.
20 # This program is free software; you can redistribute it and/or modify
21 # it under the terms of the GNU General Public License as published by
22 # the Free Software Foundation; either version 2 of the License.
24 # This program is distributed in the hope that it will be useful,
25 # but WITHOUT ANY WARRANTY; without even the implied warranty of
26 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 # GNU General Public License for more details.
29 # You should have received a copy of the GNU General Public License
30 # along with this program; if not, write to the Free Software
31 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
33 ###########################################################################
34 # include whatever mdules we need
40 ###########################################################################
49 ###########################################################################
50 # function definitions# usage
52 # print debug statements
54 # output: string printed to standard out
58 if debug
!= 0: print vals
61 # print the simplest form of help
63 # output: simple command usage is printed on the console
68 print "otlsplit.py [options] inputfile"
70 print " -l level The number of levels to split down to. The default is 1"
71 print " -D dir Specifiy a target directory for the output files"
72 print " -t Include a title line (the parerent heading) in split files"
73 print " -v Print version (RCS) information."
74 print " -h Show help."
75 print "output is on STDOUT"
79 # print the RCS version information
81 # output: RSC version information is printed on the console
86 print " $Revision: 1.9 $"
87 print " $Date: 2006/02/19 14:39:06 $"
88 print " $Author: noel $"
92 # Check for input arguments and set the necessary switches
94 # output: possible console output for help, switch variables may be set
97 global debug
, level
, inputfile
, title
, subdir
98 if (len(sys
.argv
) == 1):
102 for i
in range(len(sys
.argv
)):
104 if (sys
.argv
[i
] == "-d"): debug
= 1 # test for debug flag
105 elif (sys
.argv
[i
] == "-?"): # test for help flag
106 showUsage() # show the help
108 elif (sys
.argv
[i
] == "-l"): # test for the level flag
109 level
= int(sys
.argv
[i
+1]) # get the level
110 i
= i
+ 1 # increment the pointer
111 elif (sys
.argv
[i
] == "-D"): # test for the subdir flag
112 subdir
= sys
.argv
[i
+1] # get the subdir
113 i
= i
+ 1 # increment the pointer
114 elif (sys
.argv
[i
] == "-t"): title
= 1 # test for title flag
115 elif (sys
.argv
[i
] == "--help"):
118 elif (sys
.argv
[i
] == "-h"):
121 elif (sys
.argv
[i
] == "-v"):
124 elif (sys
.argv
[i
][0] == "-"):
125 print "Error! Unknown option. Aborting"
127 else: # get the input file name
128 inputfile
= sys
.argv
[i
]
131 # get the level of the current line (count the number of tabs)
132 # input: linein - a single line that may or may not have tabs at the beginning
133 # output: returns a number 1 is the lowest
135 def getLineLevel(linein
):
136 strstart
= lstrip(linein
) # find the start of text in line
137 x
= find(linein
,strstart
) # find the text index in the line
138 n
= count(linein
,"\t",0,x
) # count the tabs
139 return(n
) # return the count + 1 (for level)
141 # convertSensitiveChars
142 # get the level of the current line (count the number of tabs)
143 # input: line - a single line that may or may not have tabs at the beginning
144 # output: returns a string
146 def convertSensitiveChars(line
):
147 line
= lstrip(rstrip(line
))
148 line
= sub('\W','_',line
)
152 # make a file name from the string array provided
153 # input: line - a single line that may or may not have tabs at the beginning
154 # output: returns a string
156 def makeFileName(nameParts
):
158 global debug
, level
, subdir
161 for i
in range(level
):
162 filename
= filename
+ lstrip(rstrip(convertSensitiveChars(nameParts
[i
]))) + "-"
163 filename
= filename
[:-1]+ ".otl"
164 if subdir
!= "": filename
= subdir
+ "/" + filename
165 return(lower(filename
))
168 # split an outline file
169 # input: file - the filehandle of the file we are splitting
170 # output: output files
172 def processFile(file):
174 global debug
, level
, title
182 line
= file.readline() # read the outline title
184 line
= file.readline() # read the first parent heading
187 linelevel
= getLineLevel(line
)
188 if (linelevel
< level
):
192 nameparts
[linelevel
] = line
193 dprint(level
,linelevel
,line
)
196 ofile
= open(makeFileName(nameparts
),"w")
199 dprint("title:",title
)
200 ofile
.write(nameparts
[level
-1])
201 ofile
.write(line
[level
:])
202 line
= file.readline()
206 # input: args and input file
207 # output: output files
210 global inputfile
, debug
212 file = open(inputfile
,"r")