Instantly share code, notes, and snippets.
Created
January 3, 2015 02:31
-
Star
0
(0)
You must be signed in to star a gist -
Fork
1
(1)
You must be signed in to fork a gist
-
Save bitoffdev/09af69fa6a2a8a0f9537 to your computer and use it in GitHub Desktop.
MarkdownMenu.py
This file contains hidden or 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
# Pythonista Markdown Action Menu | |
# By: EJM Software ---- http://ejm.cloudvent.net | |
import ui, console, editor, markdown | |
def format_italic(): | |
s = editor.get_selection() | |
if not s==None and s[1]-s[0]>0: | |
editor.replace_text(s[0], s[1], '*%s*'%editor.get_text()[s[0]:s[1]]) | |
def format_bold(): | |
s = editor.get_selection() | |
if not s==None and s[1]-s[0]>0: | |
editor.replace_text(s[0], s[1], '**%s**'%editor.get_text()[s[0]:s[1]]) | |
def format_code(): | |
s = editor.get_selection() | |
if not s==None and s[1]-s[0]>0: | |
editor.replace_text(s[0], s[1], '`%s`'%editor.get_text()[s[0]:s[1]]) | |
def format_codeblock(): | |
import re | |
COMMENT=' ' | |
i=editor.get_line_selection() | |
t=editor.get_text() | |
# replace every occurance of newline with newline plus COMMENT, except last newline | |
editor.replace_text(i[0],i[1]-1,COMMENT+re.sub(r'\n',r'\n'+COMMENT,t[i[0]:i[1]-1])) | |
#editor.set_selection(i[0],i[1]-len(t)+len(editor.get_text())) | |
def format_blockquote(): | |
import re | |
COMMENT='> ' | |
i=editor.get_line_selection() | |
t=editor.get_text() | |
# replace every occurance of newline with ewline plus COMMENT, except last newline | |
editor.replace_text(i[0],i[1]-1,COMMENT+re.sub(r'\n',r'\n'+COMMENT,t[i[0]:i[1]-1])) | |
#editor.set_selection(i[0],i[1]-len(t)+len(editor.get_text())) | |
@ui.in_background | |
def insert_link(): | |
s = editor.get_selection() | |
if not s==None: | |
text = editor.get_text() | |
selectedtext = text[s[0]:s[1]] | |
link = console.input_alert('link', '') | |
name = console.input_alert('name', '', selectedtext) | |
editor.replace_text(s[0], s[1], '[%s](%s)'%(name, link)) | |
@ui.in_background | |
def insert_image(): | |
s = editor.get_selection() | |
if not s==None: | |
text = editor.get_text() | |
selectedtext = text[s[0]:s[1]] | |
link = console.input_alert('link', '') | |
name = console.input_alert('name', '', selectedtext) | |
editor.replace_text(s[0], s[1], ''%(name, link)) | |
@ui.in_background | |
def new_file(): | |
filename = console.input_alert('Create MD File', 'File Name') | |
if filename.find('.')>-1: | |
filename = filename[:filename.rfind('.')] | |
filename += '.md' | |
fh = open(filename, 'a') | |
del fh | |
editor.open_file(filename) | |
def email_text(): | |
subject = 'subject='+urllib.quote(editor.get_path().split('/')[-1]) | |
body = 'body='+urllib.quote(editor.get_text()) | |
webbrowser.open('mailto:?'+subject+'&'+body) | |
@ui.in_background | |
def open_text_in(): | |
console.open_in(editor.get_path()) | |
def preview_html(): | |
mdviewwin = ui.View() | |
webview = ui.WebView(frame = (0, 0, mdviewwin.width, mdviewwin.height), flex="WH") | |
webview.load_html(markdown.markdown(editor.get_text())) | |
mdviewwin.add_subview(webview) | |
mdviewwin.present('fullscreen') | |
@ui.in_background | |
def save_html(): | |
filename = console.input_alert('Save HTML', 'File Name') | |
if filename.find('.')>-1: | |
filename = filename[:filename.rfind('.')] | |
filename += '.html' | |
fh = open(filename, 'w') | |
fh.write(markdown.markdown(editor.get_text())) | |
fh.close() | |
editor.open_file(filename) | |
def email_html(): | |
subject = 'subject='+urllib.quote(editor.get_path().split('/')[-1]) | |
body = 'body='+urllib.quote(markdown.markdown(editor.get_text())) | |
webbrowser.open('mailto:?'+subject+'&'+body) | |
@ui.in_background | |
def open_html_in(): | |
filename = 'htmltempfile' | |
fh = open(filename, 'w') | |
fh.write(markdown.markdown(editor.get_text())) | |
fh.close() | |
console.open_in(filename) | |
class MenuController (object): | |
SECTIONS = [ | |
{'name':'Format', | |
'titles':['Italic', 'Bold', 'Code', 'Blockquote'], | |
'actions':[format_italic, format_bold, format_code, format_blockquote]}, | |
#{'name':'Insert', | |
#'titles':['Link', 'Image'], | |
#'actions':[insert_link, insert_image]}, | |
{'name':'Text', | |
'titles':['New', 'Email', 'Open In'], | |
'actions':[new_file, email_text, open_text_in]}, | |
{'name':'HTML', | |
'titles':['Preview', 'Save', 'Email', 'Open In'], | |
'actions':[preview_html, save_html, email_html, open_html_in]} | |
] | |
def tableview_number_of_sections(self, tableview): | |
return len(self.SECTIONS) | |
def tableview_number_of_rows(self, tableview, section): | |
return len(self.SECTIONS[section]['titles']) | |
def tableview_cell_for_row(self, tableview, section, row): | |
cell = ui.TableViewCell() | |
cell.text_label.text = self.SECTIONS[section]['titles'][row] | |
return cell | |
def tableview_did_select(self, tableview, section, row): | |
self.SECTIONS[section]['actions'][row]() | |
tableview.reload() | |
def tableview_title_for_header(self, tableview, section): | |
return self.SECTIONS[section]['name'] | |
class MyView (ui.View): | |
def __init__(self): | |
self.background_color = (1.00, 1.00, 1.00) | |
self.border_color = 0.7, 0.7, 0.7 | |
self.border_width = 1 | |
self.width = 130 | |
control = MenuController() | |
self.table = ui.TableView() | |
self.table.flex = 'h' | |
self.table.frame = self.bounds | |
self.table.data_source = control | |
self.table.delegate = control | |
self.add_subview(self.table) | |
def keyboard_frame_will_change(self, frame): | |
self.table.height = self.bounds[3] - frame[3] | |
v = MyView() | |
v.present('sidebar') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment