Editorial Workflows

Diff with Clipboard

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: Shows a diff that compares the selected document with text in the clipboard. The diff is shown in the Preview panel.

Shared by: yt

Comments: Comment Feed (RSS)

There are no comments yet.

+ Add Comment

Workflow Preview
Run Python Script ?
Source Code
#coding: utf-8
import workflow
import difflib
import clipboard
import re
import editor
import console

def main():
	text1 = clipboard.get()
	if not text1:
		console.hud_alert('Clipboard Empty', 'error')
		workflow.stop()
		return
	text2 = editor.get_text()
	if not text2:
		console.hud_alert('No Text in Editor', 'error')
		workflow.stop()
		return
	d = difflib.Differ()
	lines1 = text1.splitlines()
	lines2 = text2.splitlines()
	out_lines = []
	diff_lines = list(d.compare(lines1, lines2))
	for i, line in enumerate(diff_lines):
		if line.startswith('+'):
			out_lines.append('<pre class="diffline added">%s</pre>' % line[2:])
		elif line.startswith('-'):
			out_lines.append('<pre class="diffline deleted">%s</pre>' % line[2:])
		elif line.startswith('?') and i > 0:
			prev_line = diff_lines[i-1]
			ranges = [(m.start(), m.end()) for m in re.finditer(r'\++|-+', line)]
			for start, end in reversed(ranges):
				span_tag = '<span class="%s-highlight">' % ('deleted' if line[start] == '-' else 'added')
				prev_line = prev_line[:start] + span_tag + prev_line[start:end] + '</span>' + prev_line[end:]
			pre_tag = '<pre class="diffline %s">' % ('added' if prev_line.startswith('+') else 'deleted')
			out_lines[i-1] = '%s%s</pre>' % (pre_tag, prev_line[2:],)
			out_lines.append('')
		else:
			out_lines.append('<pre class="common">%s</pre>' % line[2:])
	
	workflow.set_output('\n'.join(out_lines))

main()
Show HTML ?
HTML
<!DOCTYPE html> <html> <head><meta charset="utf-8"/> <style type="text/css"> body { background-color: #f6f6f6; color: #333; margin: 16px; } pre { font-family: Menlo, monospace; white-space: pre-wrap; margin-top: 0; margin-bottom: 0; overflow: hidden; } pre.diffline { border-width: 0 0 0 8px; margin-left: -16px; margin-right: -16px; padding-right: 16px; padding-left: 8px; border-style: solid; } .added { color: green; background-color: #f4fbef; border-color: #c9ffb3; } .deleted { color: #820e03; background-color: #fbefef; border-color: #ffb3b4; } .deleted-highlight { background-color: #ffb3b4; border-radius: 3px; } .added-highlight { background-color: #c9ffb3; border-radius: 3px; } .common { color: #999; } </style> </head> <body> Input </body> </html>
Title
Diff
Base URL
None