1 #! /usr/local/bin/python
3 # Renumber the Python FAQ
12 chapterprog
= regex
.compile('^\([1-9][0-9]*\)\. ')
13 questionprog
= regex
.compile('^\([1-9][0-9]*\)\.\([1-9][0-9]*\)\. ')
14 newquestionprog
= regex
.compile('^Q\. ')
15 blankprog
= regex
.compile('^[ \t]*$')
16 indentedorblankprog
= regex
.compile('^\([ \t]+\|[ \t]*$\)')
19 print 'Reading lines...'
20 lines
= open(FAQ
, 'r').readlines()
21 print 'Renumbering in memory...'
28 for i
in range(len(lines
)):
31 n
= chapterprog
.match(line
)
35 line
= `chapter`
+ '. ' + line
[n
:]
37 chapters
.append(' ' + line
)
38 questions
.append('\n')
39 questions
.append(' ' + line
)
42 n
= questionprog
.match(line
)
43 if n
< 0: n
= newquestionprog
.match(line
) - 3
45 question
= question
+ 1
46 line
= '%d.%d. '%(chapter
, question
) + line
[n
:]
48 questions
.append(' ' + line
)
49 # Add up to 4 continuations of the question
50 for j
in range(i
+1, i
+5):
51 if blankprog
.match(lines
[j
]) >= 0:
53 questions
.append(' '*(n
+2) + lines
[j
])
56 afterblank
= (blankprog
.match(line
) >= 0)
57 print 'Inserting list of chapters...'
59 for i
in range(len(lines
)):
62 '^This FAQ is divided in the following chapters',
67 if indentedorblankprog
.match(line
) < 0:
73 print '*** Can\'t find header for list of chapters'
74 print '*** Chapters found:'
75 for line
in chapters
: print line
,
76 print 'Inserting list of questions...'
77 questions
.append('\n')
78 for i
in range(len(lines
)):
80 if regex
.match('^Here.s an overview of the questions',
85 if indentedorblankprog
.match(line
) < 0:
88 lines
[i
:i
] = questions
91 print '*** Can\'t find header for list of questions'
92 print '*** Questions found:'
93 for line
in questions
: print line
,
97 print 'Writing new file...'
98 f
= open(FAQ
+ '.new', 'w')
102 print 'Making backup...'
103 os
.rename(FAQ
, FAQ
+ '~')
104 print 'Moving new file...'
105 os
.rename(FAQ
+ '.new', FAQ
)