Editorial Workflows

Sidebar Preview

public workflow

Install Workflow...

This workflow contains at least one Python script. Only use it if you trust the person who shared this with you, and if you know exactly what it does.

I understand, install the workflow!

This is a workflow for Editorial, a Markdown and plain text editor for iOS. To download it, you need to view this page on a device that has the app installed.

Description: NOTE: This workflow is designed for iPad only.

Shows a Markdown preview of the current document in a sidebar panel, next to the editor.

The preview is updated automatically as you type, and it preserves its scroll position.

Links that you tap on in the preview are opened in the browser panel.

Shared by: @olemoritz

Comments: Comment Feed (RSS)

anonymous — 02 Jun 2014
Hi, it's probably a stupid question, but how can I switch back to the default UI?
anonymous — 02 Jun 2014
Aha, I see. I can simply close the sidebar.

+ Add Comment

Workflow Preview
Run Python Script ?
Source Code
#coding: utf-8
import ui
import editor

# Used to make links open in the browser panel:
class WebViewDelegate (object):
	def webview_should_start_load(self, webview, url, nav_type):
		if nav_type == 'link_clicked':
			import webbrowser
			webbrowser.open(url)
			return False
		return True
		
class SidebarView (ui.View):
	@ui.in_background
	def refresh(self):
		if not self.active:
			# Sidebar was already closed, don't refresh anymore
			return
		# Import these again because it's possible that another
		# script has run in the meantime:
		import ui, editor
		import markdown2
		text = editor.get_text()
		# Only refresh if the text has changed since last time:
		if text != self.current_text:
			html = markdown2.markdown(text)
			if self.first_run:
				# In the first run, just load the template...
				html = '<!DOCTYPE html><html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><style>body {font-family:helvetica; font-size: 16px;}h1 {font-size: 24px;} h2 {font-size: 22px;} h3 {font-size: 20px*;}</style></head><body><div id="content">Loading...</div><div style="height:400px"></div></body></html>'
				self['webview1'].load_html(html)
			else:
				# ...afterwards, replace the HTML via JavaScript, so that the
				# scroll position is preserved:
				html = html.replace('"', '\\"').replace('\n', '\\n')
				js = 'document.getElementById("content").innerHTML = "%s";' % html
				self['webview1'].evaluate_javascript(js)
				self.current_text = text
		ui.delay(self.refresh, 0.5 if self.first_run else 2.5)
		self.first_run = False
	
	def did_load(self):
		self.active = True
		self.first_run = True
		self.current_text = ''
		self.refresh()
	
	def will_close(self):
		self.active = False

def main():
	import platform
	if not platform.machine().startswith('iPad'):
		import console 
		console.alert('iPad Required',
			            'Sorry, this workflow is designed for iPad only.', 'OK',
			            hide_cancel_button=True)
		return
	v = ui.load_view()
	v['webview1'].delegate = WebViewDelegate()
	v.present('sidebar')

main()