Next / Previous / Contents / Shipman's homepage

28. deCamel(): Convert Python names to XSL-FO names

See Section 27, “dash(): An attribute dictionary generator” for a discussion of the need for this function.

fohelpers.py
# - - -   d e C a m e l

def deCamel(name):
    '''Change camelCase names to hyphenated-names.

      [ name is a str ->
          return name, transformed by substituting "x-y"
          for any sequence "xY" where x is lowercase and Y is
          uppercase ]
    '''
    #-- 1
    last = 'A'
    result = []

    #-- 2
    # [ result  +:=  all but the first character of (last+name)
    #       with sequences "xY" replaced replaced by "x-y"
    #   last  :=  the last character of name ]
    for c in name:
        #-- 2 body
        result.append (("-" + c.lower())
                       if last.islower() and c.isupper()
                       else c)
        last = c

    #-- 3
    return ''.join(result)