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
124 """goes through a list of known words that have punctuation and removes the
125 punctuation from them"""
126 assert isinstance(str1, unicode)
127 occurrences = []
128 for word, replacement in wordswithpunctuation.iteritems():
129 occurrences.extend([(pos, word, replacement) for pos in quote.find_all(str1, word)])
130 for match in re.finditer("(?u)\w+'\w+", str1):
131 word = match.group()
132 replacement = filter(unicode.isalnum, word)
133 occurrences.append((match.start(), word, replacement))
134 occurrences.sort()
135 if occurrences:
136 lastpos = 0
137 newstr1 = ""
138 for pos, word, replacement in occurrences:
139 newstr1 += str1[lastpos:pos]
140 newstr1 += replacement
141 lastpos = pos + len(word)
142 newstr1 += str1[lastpos:]
143 return newstr1
144 else:
145 return str1
146