no way to compare when less than two revisions
Differences
This shows you the differences between two versions of the page.
— | howtos:convert_camelcase [2011/09/14 10:54] (current) – add page about easily convert camelcase strings in Geany enrico | ||
---|---|---|---|
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. /// | ||
+ | * Configure Geany: Edit-> | ||
+ | * In the dialog add a new command and simply use: //python / | ||
+ | |||
+ | ===== Usage ===== | ||
+ | |||
+ | Select some string in Geany and use Edit-> | ||
+ | Alternatively, | ||
+ | For details, read the [[http:// | ||
+ | |||
+ | |||
+ | ===== The script ===== | ||
+ | |||
+ | <file python geany_camel_case.py> | ||
+ | # | ||
+ | # -*- 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, | ||
+ | if char.isupper() and from_char_position < current_char_position: | ||
+ | words.append(string[from_char_position: | ||
+ | from_char_position = current_char_position | ||
+ | words.append(string[from_char_position: | ||
+ | return ' | ||
+ | |||
+ | |||
+ | # | ||
+ | def lower_case_underscore_to_camel_case(string): | ||
+ | """ | ||
+ | splitted_string = string.split(' | ||
+ | # use string' | ||
+ | class_ = string.__class__ | ||
+ | return splitted_string[0] + class_.join('', | ||
+ | |||
+ | |||
+ | # | ||
+ | def read_data(): | ||
+ | return sys.stdin.read() | ||
+ | |||
+ | |||
+ | # | ||
+ | def detect_conversion_method(data): | ||
+ | if ' | ||
+ | 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() | ||
+ | |||
+ | </ | ||
+ | |||
+ | |||
+ | {{tag> |