1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 """This is a set of string filters that strings can be passed through before
23 certain tests."""
24
25 import re
26
27 from translate.filters import decoration
28 from translate.misc import quote
29
30
50
51
53 """returns a function that filters accelerators marked using accelmarker in strings"""
54 if accelmarker is None:
55 accelmarkerlen = 0
56 else:
57 accelmarkerlen = len(accelmarker)
58
59 def filtermarkedaccelerators(str1, acceptlist=None):
60 """modifies the accelerators in str1 marked with a given marker, using a given filter"""
61 acclocs, badlocs = decoration.findaccelerators(str1, accelmarker, acceptlist)
62 fstr1, pos = "", 0
63 for accelstart, accelerator in acclocs:
64 fstr1 += str1[pos:accelstart]
65 fstr1 += accelerator
66 pos = accelstart + accelmarkerlen + len(accelerator)
67 fstr1 += str1[pos:]
68 return fstr1
69 return filtermarkedaccelerators
70
71
72 -def varname(variable, startmarker, endmarker):
73 """a simple variable filter that returns the variable name without the marking punctuation"""
74 return variable
75
76 if startmarker is None:
77 return variable[:variable.rfind(endmarker)]
78 elif endmarker is None:
79 return variable[variable.find(startmarker)+len(startmarker):]
80 else:
81 return variable[variable.find(startmarker)+len(startmarker):variable.rfind(endmarker)]
82
83
84 -def varnone(variable, startmarker, endmarker):
85 """a simple variable filter that returns an emoty string"""
86 return ""
87
88
90 """returns a function that filters variables marked using startmarker and
91 endmarker in strings"""
92 if startmarker is None:
93 startmarkerlen = 0
94 else:
95 startmarkerlen = len(startmarker)
96 if endmarker is None:
97 endmarkerlen = 0
98 elif type(endmarker) == int:
99 endmarkerlen = 0
100 else:
101 endmarkerlen = len(endmarker)
102
103 def filtermarkedvariables(str1):
104 """modifies the variables in str1 marked with a given marker, using a given filter"""
105 varlocs = decoration.findmarkedvariables(str1, startmarker, endmarker)
106 fstr1, pos = "", 0
107 for varstart, variable in varlocs:
108 fstr1 += str1[pos:varstart]
109 fstr1 += varfilter(variable, startmarker, endmarker)
110 pos = varstart + startmarkerlen + len(variable) + endmarkerlen
111 fstr1 += str1[pos:]
112 return fstr1
113 return filtermarkedvariables
114
115
116
117 wordswithpunctuation = ["'n", "'t",
118 ]
119
120 wordswithpunctuation = dict([(word, filter(str.isalnum, word)) for word in wordswithpunctuation])
121
122 word_with_apos_re = re.compile("(?u)\w+'\w+")
123
125 """goes through a list of known words that have punctuation and removes the
126 punctuation from them"""
127 if u"'" not in str1:
128 return str1
129 occurrences = []
130 for word, replacement in wordswithpunctuation.iteritems():
131 occurrences.extend([(pos, word, replacement) for pos in quote.find_all(str1, word)])
132 for match in word_with_apos_re.finditer(str1):
133 word = match.group()
134 replacement = filter(unicode.isalnum, word)
135 occurrences.append((match.start(), word, replacement))
136 occurrences.sort()
137 if occurrences:
138 lastpos = 0
139 newstr1 = ""
140 for pos, word, replacement in occurrences:
141 newstr1 += str1[lastpos:pos]
142 newstr1 += replacement
143 lastpos = pos + len(word)
144 newstr1 += str1[lastpos:]
145 return newstr1
146 else:
147 return str1
148