Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

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

""" 

@file 

@brief Extracts old quote from tex files. 

""" 

import re 

 

 

class FormatException(Exception): 

""" 

Raised when not able to interpret a line. 

""" 

pass 

 

 

def enumerate_quotes(filename, encoding="utf-8", empty_name="Inconnu"): 

""" 

Enumerates quote from a filename or a stream 

 

@param filename filename or stream 

@param encoding applicable only if filename 

@param empty_name replces an empty author name 

@return enumerate on quote 

 

A quote is defined a dictionary. 

""" 

if isinstance(filename, str): 

with open(filename, "r", encoding=encoding) as f: 

for q in enumerate_quotes(f): 

yield q 

else: 

re1 = re.compile("chapter[{]([0-9]+)[}]") 

re2 = re.compile( 

"[\\]begin[{]xcitt?[}][{](.*?)[}][{](.*?)[}][{](.*?)[}][{](.+?)[}]") 

re3 = re.compile( 

"[\\]begin[{]xcita[}][{](.*?)[}][{](.*?)[}][{](.+?)[}][{](.*?)[}][{](.*?)[}][{](.+?)[}]") 

re4 = re.compile( 

"[\\]begin[{]xcitenfant[}][{](.*?)[}][{](.*?)[}][{](.*?)[}][{](.+?)[}]") 

re5 = re.compile( 

"[\\]begin[{]xcitw[}][{](.*?)[}][{](.*?)[}][{](.*?)[}][{](.+?)[}][{](.+?)[}]") 

re6 = re.compile( 

"[\\]begin[{]xcita3[}][{](.*?)[}][{](.*?)[}][{](.+?)[}][{](.*?)[}][{](.+?)[}][{](.*?)[}][{](.*?)[}][{](.+?)[}]") 

 

def process_content(il, content): 

find = re2.search(content[0]) 

if find: 

author, name, book, index = find.groups() 

obs = dict(author="{0} {1}".format(name, author), 

book=book, index=index, year=year) 

else: 

find = re3.search(content[0]) 

if find: 

author1, name1, author2, name2, book, index = find.groups() 

obs = dict(author="{0} {1}, {2} {3}".format(name1, author1, name2, author2), 

book=book, index=index, year=year) 

else: 

find = re4.search(content[0]) 

if find: 

author, name, book, index = find.groups() 

obs = dict(author="{0} {1}".format(name, author), 

book=book, index=index, year=year, 

tag="enfant") 

else: 

find = re5.search(content[0]) 

if find: 

author, name, book, index, date = find.groups() 

obs = dict(author="{0} {1}".format(name, author), 

book=book, index=index, year=year, 

date=date) 

else: 

find = re6.search(content[0]) 

if find: 

author, name, a2, n2, a3, n3, book, index = find.groups() 

obs = dict(author="{} {}, {} {}, {} {}".format(name, author, n2, a2, n3, a3), 

book=book, index=index, year=year) 

else: 

raise FormatException( 

"Unable to interpret line {0}: '{1}'".format(il, content[0])) 

 

content = "\n".join(content[1:-1]) 

content = content.replace("~", " ") 

content = content.replace("\\quad", "...") 

obs["content"] = content 

if not obs["author"]: 

obs["author"] = empty_name 

return obs 

 

year = None 

content = [] 

for il, line in enumerate(filename): 

sline = line.strip() 

if sline.startswith("\\chapter{"): 

chap = re1.search(sline) 

if chap: 

year = chap.groups()[0] 

else: 

raise FormatException( 

"Unable to process line {0}: '{1}'".format(il, sline)) 

else: 

if sline.startswith("\\begin{xcit"): 

content.append(sline) 

elif sline.startswith("\\end{xcit"): 

content.append(sline) 

yield process_content(il, content) 

content.clear() 

else: 

if content: 

content.append(sline) 

else: 

# between quotes 

pass