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 from translate.filters import decoration
26 from translate.misc import quote
27 import re
28
48
50 """returns a function that filters accelerators marked using accelmarker in strings"""
51 if accelmarker is None:
52 accelmarkerlen = 0
53 else:
54 accelmarkerlen = len(accelmarker)
55 def filtermarkedaccelerators(str1, acceptlist=None):
56 """modifies the accelerators in str1 marked with a given marker, using a given filter"""
57 acclocs, badlocs = decoration.findaccelerators(str1, accelmarker, acceptlist)
58 fstr1, pos = "", 0
59 for accelstart, accelerator in acclocs:
60 fstr1 += str1[pos:accelstart]
61 fstr1 += accelerator
62 pos = accelstart + accelmarkerlen + len(accelerator)
63 fstr1 += str1[pos:]
64 return fstr1
65 return filtermarkedaccelerators
66
67 -def varname(variable, startmarker, endmarker):
68 """a simple variable filter that returns the variable name without the marking punctuation"""
69 return variable
70
71 if startmarker is None:
72 return variable[:variable.rfind(endmarker)]
73 elif endmarker is None:
74 return variable[variable.find(startmarker)+len(startmarker):]
75 else:
76 return variable[variable.find(startmarker)+len(startmarker):variable.rfind(endmarker)]
77
78 -def varnone(variable, startmarker, endmarker):
79 """a simple variable filter that returns an emoty string"""
80 return ""
81
83 """returns a function that filters variables marked using startmarker and
84 endmarker in strings"""
85 if startmarker is None:
86 startmarkerlen = 0
87 else:
88 startmarkerlen = len(startmarker)
89 if endmarker is None:
90 endmarkerlen = 0
91 elif type(endmarker) == int:
92 endmarkerlen = 0
93 else:
94 endmarkerlen = len(endmarker)
95
96 def filtermarkedvariables(str1):
97 """modifies the variables in str1 marked with a given marker, using a given filter"""
98 varlocs = decoration.findmarkedvariables(str1, startmarker, endmarker)
99 fstr1, pos = "", 0
100 for varstart, variable in varlocs:
101 fstr1 += str1[pos:varstart]
102 fstr1 += varfilter(variable, startmarker, endmarker)
103 pos = varstart + startmarkerlen + len(variable) + endmarkerlen
104 fstr1 += str1[pos:]
105 return fstr1
106 return filtermarkedvariables
107
108
109
110 wordswithpunctuation = ["'n","'t"
111 ]
112
113 wordswithpunctuation = dict([(word, filter(str.isalnum, word)) for word in wordswithpunctuation])
114
116 """goes through a list of known words that have punctuation and removes the
117 punctuation from them"""
118 assert isinstance(str1, unicode)
119 occurrences = []
120 for word, replacement in wordswithpunctuation.iteritems():
121 occurrences.extend([(pos, word, replacement) for pos in quote.find_all(str1, word)])
122 for match in re.finditer("(?u)\w+'\w+", str1):
123 word = match.group()
124 replacement = filter(unicode.isalnum, word)
125 occurrences.append((match.start(), word, replacement))
126 occurrences.sort()
127 if occurrences:
128 lastpos = 0
129 newstr1 = ""
130 for pos, word, replacement in occurrences:
131 newstr1 += str1[lastpos:pos]
132 newstr1 += replacement
133 lastpos = pos + len(word)
134 newstr1 += str1[lastpos:]
135 return newstr1
136 else:
137 return str1
138