| Guido van Rossum | f06ee5f | 1996-11-27 19:52:01 +0000 | [diff] [blame] | 1 | #! /usr/bin/env python | 
| Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 2 |  | 
 | 3 | # Renumber the Python FAQ | 
 | 4 |  | 
 | 5 | import string | 
 | 6 | import regex | 
 | 7 | import sys | 
 | 8 | import os | 
 | 9 |  | 
 | 10 | FAQ = 'FAQ' | 
 | 11 |  | 
 | 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]*$\)') | 
 | 17 |  | 
 | 18 | def main(): | 
 | 19 | 	print 'Reading lines...' | 
 | 20 | 	lines = open(FAQ, 'r').readlines() | 
 | 21 | 	print 'Renumbering in memory...' | 
 | 22 | 	oldlines = lines[:] | 
 | 23 | 	after_blank = 1 | 
 | 24 | 	chapter = 0 | 
 | 25 | 	question = 0 | 
 | 26 | 	chapters = ['\n'] | 
 | 27 | 	questions = [] | 
 | 28 | 	for i in range(len(lines)): | 
 | 29 | 		line = lines[i] | 
 | 30 | 		if after_blank: | 
 | 31 | 			n = chapterprog.match(line) | 
 | 32 | 			if n >= 0: | 
 | 33 | 				chapter = chapter + 1 | 
 | 34 | 				question = 0 | 
 | 35 | 				line = `chapter` + '. ' + line[n:] | 
 | 36 | 				lines[i] = line | 
 | 37 | 				chapters.append(' ' + line) | 
 | 38 | 				questions.append('\n') | 
 | 39 | 				questions.append(' ' + line) | 
 | 40 | 				afterblank = 0 | 
 | 41 | 				continue | 
 | 42 | 			n = questionprog.match(line) | 
 | 43 | 			if n < 0: n = newquestionprog.match(line) - 3 | 
 | 44 | 			if n >= 0: | 
 | 45 | 				question = question + 1 | 
| Guido van Rossum | 457c0ab | 1994-09-29 15:19:10 +0000 | [diff] [blame] | 46 | 				number = '%d.%d. '%(chapter, question) | 
 | 47 | 				line = number + line[n:] | 
| Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 48 | 				lines[i] = line | 
 | 49 | 				questions.append('  ' + line) | 
 | 50 | 				# Add up to 4 continuations of the question | 
| Guido van Rossum | 457c0ab | 1994-09-29 15:19:10 +0000 | [diff] [blame] | 51 | 				n = len(number) | 
| Guido van Rossum | a7925f1 | 1994-01-26 10:20:16 +0000 | [diff] [blame] | 52 | 				for j in range(i+1, i+5): | 
 | 53 | 					if blankprog.match(lines[j]) >= 0: | 
 | 54 | 						break | 
 | 55 | 					questions.append(' '*(n+2) + lines[j]) | 
 | 56 | 				afterblank = 0 | 
 | 57 | 				continue | 
 | 58 | 		afterblank = (blankprog.match(line) >= 0) | 
 | 59 | 	print 'Inserting list of chapters...' | 
 | 60 | 	chapters.append('\n') | 
 | 61 | 	for i in range(len(lines)): | 
 | 62 | 		line = lines[i] | 
 | 63 | 		if regex.match( | 
 | 64 | 			  '^This FAQ is divided in the following chapters', | 
 | 65 | 			  line) >= 0: | 
 | 66 | 			i = i+1 | 
 | 67 | 			while 1: | 
 | 68 | 				line = lines[i] | 
 | 69 | 				if indentedorblankprog.match(line) < 0: | 
 | 70 | 					break | 
 | 71 | 				del lines[i] | 
 | 72 | 			lines[i:i] = chapters | 
 | 73 | 			break | 
 | 74 | 	else: | 
 | 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)): | 
 | 81 | 		line = lines[i] | 
 | 82 | 		if regex.match('^Here.s an overview of the questions', | 
 | 83 | 			  line) >= 0: | 
 | 84 | 			i = i+1 | 
 | 85 | 			while 1: | 
 | 86 | 				line = lines[i] | 
 | 87 | 				if indentedorblankprog.match(line) < 0: | 
 | 88 | 					break | 
 | 89 | 				del lines[i] | 
 | 90 | 			lines[i:i] = questions | 
 | 91 | 			break | 
 | 92 | 	else: | 
 | 93 | 		print '*** Can\'t find header for list of questions' | 
 | 94 | 		print '*** Questions found:' | 
 | 95 | 		for line in questions: print line, | 
 | 96 | 	if lines == oldlines: | 
 | 97 | 		print 'No changes.' | 
 | 98 | 		return | 
 | 99 | 	print 'Writing new file...' | 
 | 100 | 	f = open(FAQ + '.new', 'w') | 
 | 101 | 	for line in lines: | 
 | 102 | 		f.write(line) | 
 | 103 | 	f.close() | 
 | 104 | 	print 'Making backup...' | 
 | 105 | 	os.rename(FAQ, FAQ + '~') | 
 | 106 | 	print 'Moving new file...' | 
 | 107 | 	os.rename(FAQ + '.new', FAQ) | 
 | 108 | 	print 'Done.' | 
 | 109 |  | 
 | 110 | main() |