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