Package translate :: Package filters :: Module prefilters |
[hide private]
[frames] | no frames]

# Source Code for Module translate.filters.prefilters

      1  #!/usr/bin/env python
      2  # -*- coding: utf-8 -*-
      3  #
      4  # Copyright 2004-2008,2010 Zuza Software Foundation
      5  #
      6  # This file is part of translate.
      7  #
      8  # translate is free software; you can redistribute it and/or modify
      9  # it under the terms of the GNU General Public License as published by
     10  # the Free Software Foundation; either version 2 of the License, or
     11  # (at your option) any later version.
     12  #
     13  # translate is distributed in the hope that it will be useful,
     14  # but WITHOUT ANY WARRANTY; without even the implied warranty of
     15  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     16  # GNU General Public License for more details.
     17  #
     18  # You should have received a copy of the GNU General Public License
     19  # along with translate; if not, write to the Free Software
     20  # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
     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
     31 -def removekdecomments(str1):
     32      """removed kde-style po comments i.e. starting with _: and ending with litteral \\n"""
     33      assert isinstance(str1, unicode)
     34      iskdecomment = False
     35      lines = str1.split("\n")
     36      removelines = []
     37      for linenum in range(len(lines)):
     38          line = lines[linenum]
     39          if line.startswith("_:"):
     40              lines[linenum] = ""
     41              iskdecomment = True
     42          if iskdecomment:
     43              removelines.append(linenum)
     44          if line.strip() and not iskdecomment:
     45              break
     46          if iskdecomment and line.strip().endswith("\\n"):
     47              iskdecomment = False
     48      lines = [lines[linenum] for linenum in range(len(lines)) if linenum not in removelines]
     49      return "\n".join(lines)
     50
     51
     52 -def filteraccelerators(accelmarker):
     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      # if the punctuation were included, we'd do the following:
     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
     89 -def filtervariables(startmarker, endmarker, varfilter):
     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  # a list of special words with punctuation
    116  # all apostrophes in the middle of the word are handled already
    117  wordswithpunctuation = ["'n", "'t", # Afrikaans
    118                         ]
    119  # map all the words to their non-punctified equivalent
    120  wordswithpunctuation = dict([(word, filter(str.isalnum, word)) for word in wordswithpunctuation])
    121
    122  word_with_apos_re = re.compile("(?u)\w+'\w+")
    123
    124 -def filterwordswithpunctuation(str1):
    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