def act(controller, bundle, options): context = cp.get_context(controller) line_ending = cp.get_line_ending(context) hash_type = cp.get_option(options, 'type', 'md5').lower() text, target_range = cp.lines_and_range(context) if hash_type == 'adler-32': text = str(zlib.adler32(text)) elif hash_type == 'crc-32': text = str(zlib.crc32(text)) elif hash_type == 'md5': text = hashlib.md5(text).hexdigest() elif hash_type == 'rmd160': text = hashlib.new('rmd160', string=text).hexdigest() elif hash_type == 'sha-1': text = hashlib.sha1(text).hexdigest() elif hash_type == 'sha-224': text = hashlib.sha224(text).hexdigest() elif hash_type == 'sha-256': text = hashlib.sha256(text).hexdigest() elif hash_type == 'sha-384': text = hashlib.sha384(text).hexdigest() elif hash_type == 'sha-512': text = hashlib.sha512(text).hexdigest() else: return cp.insert_text(context, text, target_range)
def act(controller, bundle, options): ''' Required action method Setting decode=True will decode instead of encoding ''' context = cp.get_context(controller) decode = cp.get_option(options, 'decode', False) selection, range = cp.selection_and_range(context) # do nothing unless they selected something if range.length == 0: return if decode: try: text = base64.b64decode(selection) # base64decode raises a TypeError if the string doesn't have the right # padding, or if it's invalid base64... We'll just catch this error # and do nothing at all :) except TypeError: return # also, base64decode tends to return an empty string, which is also lame if range.length and not len(text): return else: text = base64.b64encode(selection) cp.insert_text(context, text, range)
def act(controller, bundle, options): ''' Required action method ''' context = cp.get_context(controller) from_lang = cp.get_option(options, 'from', 'markdown').lower() to_lang = cp.get_option(options, 'to', 'html').lower() selection, range = cp.selection_and_range(context) # grab the whole document if they haven't selected anything... if range.length == 0: selection = context.string() range = cp.new_range(0, len(selection)) # what are we coming from? if from_lang == 'markdown': html = markdown(selection) elif from_lang == 'textile': html = textile(selection) elif from_lang == 'rest': html = rest2html(selection) elif from_lang == 'html': html = selection else: return # what are we going to? if to_lang == 'markdown': text = html2text(html) elif to_lang == 'textile': text = html2textile(html) elif to_lang == 'rest': text = html2rest(html) elif to_lang == 'html': text = html else: return cp.insert_text(context, text, range)
def act(controller, bundle, options): ''' Required action method ''' context = cp.get_context(controller) selection, range = cp.selection_and_range(context) # do nothing unless they selected something if range.length == 0: return try: text = selection.encode('rot13') except LookupError: # for python 3... hopefully one of the two will do the trick. import rot13 text = rot13.rot13(selection) cp.insert_text(context, text, range)
def act(controller, bundle, options): ''' Required action method if supplied, message will be written instead of Hello World Setting replace=True replace the current selection instead of inserting ''' context = cp.get_context(controller) message = cp.get_option(options, 'message', 'Hello World') replace_selection = cp.get_option(options, 'replace', False) range = cp.get_range(context) if not replace_selection: range = cp.new_range(range.location, 0) cp.insert_text(context, message, range)
def act(controller, bundle, options): ''' Required action method Setting decode=True will decode instead of encoding ''' context = cp.get_context(controller) decode = cp.get_option(options, 'decode', False) selection, range = cp.selection_and_range(context) # do nothing unless they selected something if range.length == 0: return if decode: text = enc.htmlDecode(selection) else: text = enc.htmlEncode(selection) cp.insert_text(context, text, range)