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