Created
November 5, 2013 07:04
-
-
Save stubevan/7315027 to your computer and use it in GitHub Desktop.
Editorial workflow to simplify the use of hyperlinks in markdown. Uses a combination of the selected region and clipboard to generate the appropriate code. It then puts the cursor in the correct place
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#coding: utf-8 | |
''' | |
Editorial Macro | |
Construct a markdown URL string - sensitive to the contents of the clipboard and selected text | |
- Ask whether the clipboard is to be used - you dont know what's in there | |
- check whether it's a URL and react accordingly | |
- if an area is selected and there is also something in the clipboard then the clipboard | |
takes precedence as it's been chosen | |
''' | |
import workflow | |
import clipboard | |
import editor | |
import console | |
debug = False | |
def is_url(text) : | |
''' | |
Checks to see whether the passed string is a URL | |
Eventually this will be more sophisticated but for the moment just check whether | |
it starts with http:// or https:// | |
''' | |
rc = False | |
if len(text) > len('https://') : | |
if text[:4] == 'http' : | |
rc = True | |
return rc | |
def construct_url_string(text, optional_text = '') : | |
''' | |
Construct a url string from the passed text | |
''' | |
cursor_pos = None | |
url_text = '' | |
if is_url(text) : | |
url_text = '[' + optional_text + '](' + text + ')' | |
if optional_text == '': | |
cursor_pos = 1 | |
else : | |
if optional_text == '': | |
url_text = '[' + text + '](http://' + optional_text + ')' | |
cursor_pos = 10 + len(text) | |
else : | |
url_text = '[' + text + '](' + optional_text + ')' | |
# final text for cursor pos for both text set | |
if cursor_pos is None: | |
cursor_pos = len(url_text) | |
return url_text, cursor_pos | |
# start of main execution | |
selected_text = editor.get_selected_text() | |
# make sure a document is actually open - if not then not a lot we can do | |
if selected_text is None : | |
console.alert('No Document is open') | |
workflow.stop() | |
# Get the rest of the inputs | |
selected_area_start, selected_area_end = editor.get_selection() | |
clipboard_text = clipboard.get() | |
if clipboard_text is not '' : | |
# ask whether clipboard is to be used | |
use_clipboard = console.alert("Use clipboard?", clipboard_text, "Yes", "No") | |
if use_clipboard != 1 : | |
clipboard_text = '' | |
url_text = None | |
cursor_pos = None | |
# We have everything - now run through the options | |
if clipboard_text == '' : | |
# nothing in the clipboard and possibly no selected text | |
url_text, cursor_pos = construct_url_string(selected_text) | |
elif selected_text == "" : | |
# Just clipboard text | |
url_text, cursor_pos = construct_url_string(clipboard_text) | |
else : | |
# we have text in both - give the clipboard text precedence as that has been | |
# conciously selected | |
if is_url(clipboard_text) : | |
url_text, cursor_pos = construct_url_string(clipboard_text, selected_text) | |
else : | |
url_text, cursor_pos = construct_url_string(selected_text, clipboard_text) | |
if debug : | |
print "selected_area -> " + str(selected_area_start) + ", " + str(selected_area_end) | |
print "selected_text -> '" + selected_text + "'" | |
print "Clipboard -> '" + clipboard_text + "'" | |
print "cursor_pos -> " + str(cursor_pos) | |
# do a sanity check to avoid weirdness | |
if url_text is None or cursor_pos is None : | |
console.alert('Error - url text not set') | |
workflow.stop() | |
# We've done the magic now set the results. set the text and then set the cursor position | |
editor.replace_text(selected_area_start, selected_area_end, url_text) | |
editor.set_selection(selected_area_start + cursor_pos, selected_area_start + cursor_pos) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment