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 accesskey_marker == '&' and XMLEntityPlaceable.regex.match(string[marker_pos-1:]):
     53                  continue
     54              label = string[:marker_pos-1] + string[marker_pos:]
     55              accesskey = string[marker_pos]
     56              break
     57      return label, accesskey
     58
     59
     60 -def combine(label, accesskey,
     61              accesskey_marker=DEFAULT_ACCESSKEY_MARKER):
     62      """Combine a label and and accesskey to form a label+accesskey string
     63
     64      We place an accesskey marker before the accesskey in the label and this creates a
     65      string with the two combined e.g. "File" + "F" = "&File"
     66
     67      @type label: unicode
     68      @param label: a label
     69      @type accesskey: unicode char
     70      @param accesskey: The accesskey
     71      @rtype: unicode or None
     72      @return: label+accesskey string or None if uncombineable
     73      """
     74      assert isinstance(label, unicode)
     75      assert isinstance(accesskey, unicode)
     76      if len(accesskey) == 0:
     77          return None
     78      searchpos = 0
     79      accesskeypos = -1
     80      in_entity = False
     81      accesskeyaltcasepos = -1
     82      while (accesskeypos < 0) and searchpos < len(label):
     83          searchchar = label[searchpos]
     84          if searchchar == '&':
     85              in_entity = True
     86          elif searchchar == ';':
     87              in_entity = False
     88          else:
     89              if not in_entity:
     90                  if searchchar == accesskey.upper():
     91                      # always prefer uppercase
     92                      accesskeypos = searchpos
     93                  if searchchar == accesskey.lower():
     94                      # take lower case otherwise...
     95                      if accesskeyaltcasepos == -1:
     96                          # only want to remember first altcasepos
     97                          accesskeyaltcasepos = searchpos
     98                          # note: we keep on looping through in hope
     99                          # of exact match
    100          searchpos += 1
    101      # if we didn't find an exact case match, use an alternate one if available
    102      if accesskeypos == -1:
    103          accesskeypos = accesskeyaltcasepos
    104      # now we want to handle whatever we found...
    105      if accesskeypos >= 0:
    106          string = label[:accesskeypos] + accesskey_marker + label[accesskeypos:]
    107          string = string.encode("UTF-8", "replace")
    108          return string
    109      else:
    110          # can't currently mix accesskey if it's not in label
    111          return None
    112