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@file 

3@brief Extracts old quote from tex files. 

4""" 

5import re 

6 

7 

8class FormatException(Exception): 

9 """ 

10 Raised when not able to interpret a line. 

11 """ 

12 pass 

13 

14 

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

16 """ 

17 Enumerates quote from a filename or a stream 

18 

19 @param filename filename or stream 

20 @param encoding applicable only if filename 

21 @param empty_name replces an empty author name 

22 @return enumerate on quote 

23 

24 A quote is defined a dictionary. 

25 """ 

26 if isinstance(filename, str): 

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

28 for q in enumerate_quotes(f): 

29 yield q 

30 else: 

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

32 re2 = re.compile( 

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

34 re3 = re.compile( 

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

36 re4 = re.compile( 

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

38 re5 = re.compile( 

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

40 re6 = re.compile( 

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

42 

43 def process_content(il, content): 

44 find = re2.search(content[0]) 

45 if find: 

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

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

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

49 else: 

50 find = re3.search(content[0]) 

51 if find: 

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

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

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

55 else: 

56 find = re4.search(content[0]) 

57 if find: 

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

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

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

61 tag="enfant") 

62 else: 

63 find = re5.search(content[0]) 

64 if find: 

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

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

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

68 date=date) 

69 else: 

70 find = re6.search(content[0]) 

71 if find: 

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

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

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

75 else: 

76 raise FormatException( # pragma: no cover 

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

78 

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

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

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

82 obs["content"] = content 

83 if not obs["author"]: 

84 obs["author"] = empty_name 

85 return obs 

86 

87 year = None 

88 content = [] 

89 for il, line in enumerate(filename): 

90 sline = line.strip() 

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

92 chap = re1.search(sline) 

93 if chap: 

94 year = chap.groups()[0] 

95 else: 

96 raise FormatException( # pragma: no cover 

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

98 else: 

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

100 content.append(sline) 

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

102 content.append(sline) 

103 yield process_content(il, content) 

104 content.clear() 

105 else: 

106 if content: 

107 content.append(sline) 

108 else: 

109 # between quotes 

110 pass