Editorial Workflows

URLs to Links

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: Simple script to replace URLs in a document with Markdown links -- uses Mechanize to retrieve the titles.

Shared by: @olemoritz

Comments: Comment Feed (RSS)

There are no comments yet.

+ Add Comment

Workflow Preview
Run Python Script ?
Source Code
#coding: utf-8
import editor
import console
import re
from mechanize import Browser

text = editor.get_text()
chars = list(text)
matches = list(re.finditer(r'(https?://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?)', text))
for m in reversed(matches):
	url = m.group(0)
	try:
		br = Browser()
		br.set_handle_robots(False)
		br.open(url)
		title = br.title()
	except Exception, e:
		print 'Could not retrieve title for %s: %s' % (url, e)
		title = ''
	link = '[%s](%s)' % (title, url)
	chars[m.start(0):m.end(0)] = link

editor.replace_text(0, len(text), ''.join(chars))
console.hide_output()