Differences

This shows you the differences between two versions of the page.

Link to this comparison view

howtos:convert_camelcase [2011/09/14 10:54] (current)
enrico add page about easily convert camelcase strings in Geany
Line 1: Line 1:
 +====== Convert CamelCase to lower_case_underscore and vice versa ======
  
 +With the following script you can easily convert a selected string in Geany to CamelCase or lower_case_underscore format depending on the source format of the string.
 +
 +===== Installation =====
 +
 +  * Python is required (works with Python 2 and 3)
 +  * Simply copy the script somewhere on your system, e.g. ///​home/<​username>/​bin/​geany_camel_case.py//​.
 +  * Configure Geany: Edit->​Format->​Send Selection to->Set Custom Commands
 +  * In the dialog add a new command and simply use: //python /​home/<​username>/​bin/​geany_camel_case.py//​
 +
 +===== Usage =====
 +
 +Select some string in Geany and use Edit->​Format->​Send Selection to-><​your configured command> and Geany will replace the string by the converted version.
 +Alternatively,​ you can use the shortcut Ctrl-<​number>​ in case your command is one of the first three configured commands.
 +For details, read the [[http://​www.geany.org/​manual/#​sending-text-through-custom-commands|documentation about Send Selection to commands]].
 +
 +
 +===== The script =====
 +
 +<file python geany_camel_case.py>​
 +#​!/​usr/​bin/​env python
 +# -*- coding: utf-8 -*-
 +
 +import sys
 +
 +
 +#​----------------------------------------------------------------------
 +def camel_case_to_lower_case_underscore(string):​
 +    """​
 +    Split string by upper case letters.
 +
 +    F.e. useful to convert camel case strings to underscore separated ones.
 +
 +    @return words (list)
 +    """​
 +    words = []
 +    from_char_position = 0
 +    for current_char_position,​ char in enumerate(string):​
 +        if char.isupper() and from_char_position < current_char_position:​
 +            words.append(string[from_char_position:​current_char_position].lower())
 +            from_char_position = current_char_position
 +    words.append(string[from_char_position:​].lower())
 +    return '​_'​.join(words)
 +
 +
 +#​----------------------------------------------------------------------
 +def lower_case_underscore_to_camel_case(string):​
 +    """​Convert string or unicode from lower-case underscore to camel-case"""​
 +    splitted_string = string.split('​_'​)
 +    # use string'​s class to work on the string to keep its type
 +    class_ = string.__class__
 +    return splitted_string[0] + class_.join('',​ map(class_.capitalize,​ splitted_string[1:​]))
 +
 +
 +#​----------------------------------------------------------------------
 +def read_data():​
 +    return sys.stdin.read()
 +
 +
 +#​----------------------------------------------------------------------
 +def detect_conversion_method(data):​
 +    if '​_'​ in data:
 +        return lower_case_underscore_to_camel_case
 +    else:
 +        return camel_case_to_lower_case_underscore
 +
 +
 +#​----------------------------------------------------------------------
 +def main():
 +    data = read_data()
 +    conversion_method = detect_conversion_method(data)
 +    result = conversion_method(data)
 +    sys.stdout.write(result)
 +
 +
 +if __name__ == '​__main__':​
 +    main()
 +
 +</​file>​
 +
 +
 +{{tag>​howto selection camelcase}}
Print/export