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