translate.convert.accesskey

Package translate :: Package convert :: Module accesskey |
[hide private]
[frames] | no frames]

# Source Code for Module translate.convert.accesskey

      1  #!/usr/bin/env python
      2  # -*- coding: utf-8 -*-
      3  #
      4  # Copyright 2002-2008 Zuza Software Foundation
      5  #
      6  # This file is part of The Translate Toolkit.
      7  #
      8  # This program 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  # This program 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  """functions used to manipulate access keys in strings"""
     23
     24  from translate.storage.placeables.general import XMLEntityPlaceable
     25
     26  DEFAULT_ACCESSKEY_MARKER = u"&"
     27
     28
     29 -def extract(string, accesskey_marker=DEFAULT_ACCESSKEY_MARKER):
     30      """Extract the label and accesskey from a label+accesskey string
     31
     32      The function will also try to ignore &entities; which would obviously not
     33      contain accesskeys.
     34
     35      @type string: Unicode
     36      @param string: A string that might contain a label with accesskey marker
     37      @type accesskey_marker: Char
     38      @param accesskey_marker: The character that is used to prefix an access key
     39      """
     40      assert isinstance(string, unicode)
     41      assert isinstance(accesskey_marker, unicode)
     42      assert len(accesskey_marker) == 1
     43      if string == u"":
     44          return u"", u""
     45      accesskey = u""
     46      label = string
     47      marker_pos = 0
     48      while marker_pos >= 0:
     49          marker_pos = string.find(accesskey_marker, marker_pos)
     50          if marker_pos != -1:
     51              marker_pos += 1
     52              if marker_pos == len(string):
     53                  break
     54              if accesskey_marker == '&' and XMLEntityPlaceable.regex.match(string[marker_pos-1:]):
     55                  continue
     56              label = string[:marker_pos-1] + string[marker_pos:]
     57              accesskey = string[marker_pos]
     58              break
     59      return label, accesskey
     60
     61
     62 -def combine(label, accesskey,
     63              accesskey_marker=DEFAULT_ACCESSKEY_MARKER):
     64      """Combine a label and and accesskey to form a label+accesskey string
     65
     66      We place an accesskey marker before the accesskey in the label and this creates a
     67      string with the two combined e.g. "File" + "F" = "&File"
     68
     69      @type label: unicode
     70      @param label: a label
     71      @type accesskey: unicode char
     72      @param accesskey: The accesskey
     73      @rtype: unicode or None
     74      @return: label+accesskey string or None if uncombineable
     75      """
     76      assert isinstance(label, unicode)
     77      assert isinstance(accesskey, unicode)
     78      if len(accesskey) == 0:
     79          return None
     80      searchpos = 0
     81      accesskeypos = -1
     82      in_entity = False
     83      accesskeyaltcasepos = -1
     84      while (accesskeypos < 0) and searchpos < len(label):
     85          searchchar = label[searchpos]
     86          if searchchar == '&':
     87              in_entity = True
     88          elif searchchar == ';':
     89              in_entity = False
     90          else:
     91              if not in_entity:
     92                  if searchchar == accesskey.upper():
     93                      # always prefer uppercase
     94                      accesskeypos = searchpos
     95                  if searchchar == accesskey.lower():
     96                      # take lower case otherwise...
     97                      if accesskeyaltcasepos == -1:
     98                          # only want to remember first altcasepos
     99                          accesskeyaltcasepos = searchpos
    100                          # note: we keep on looping through in hope
    101                          # of exact match
    102          searchpos += 1
    103      # if we didn't find an exact case match, use an alternate one if available
    104      if accesskeypos == -1:
    105          accesskeypos = accesskeyaltcasepos
    106      # now we want to handle whatever we found...
    107      if accesskeypos >= 0:
    108          string = label[:accesskeypos] + accesskey_marker + label[accesskeypos:]
    109          string = string.encode("UTF-8", "replace")
    110          return string
    111      else:
    112          # can't currently mix accesskey if it's not in label
    113          return None
    114