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 number
= '%d.%d. '%(chapter
, question
)
47 line
= number
+ line
[n
:]
49 questions
.append(' ' + line
)
50 # Add up to 4 continuations of the question
52 for j
in range(i
+1, i
+5):
53 if blankprog
.match(lines
[j
]) >= 0:
55 questions
.append(' '*(n
+2) + lines
[j
])
58 afterblank
= (blankprog
.match(line
) >= 0)
59 print 'Inserting list of chapters...'
61 for i
in range(len(lines
)):
64 '^This FAQ is divided in the following chapters',
69 if indentedorblankprog
.match(line
) < 0:
75 print '*** Can\'t find header for list of chapters'
76 print '*** Chapters found:'
77 for line
in chapters
: print line
,
78 print 'Inserting list of questions...'
79 questions
.append('\n')
80 for i
in range(len(lines
)):
82 if regex
.match('^Here.s an overview of the questions',
87 if indentedorblankprog
.match(line
) < 0:
90 lines
[i
:i
] = questions
93 print '*** Can\'t find header for list of questions'
94 print '*** Questions found:'
95 for line
in questions
: print line
,
99 print 'Writing new file...'
100 f
= open(FAQ
+ '.new', 'w')
104 print 'Making backup...'
105 os
.rename(FAQ
, FAQ
+ '~')
106 print 'Moving new file...'
107 os
.rename(FAQ
+ '.new', FAQ
)