Editorial Workflows

Auto-Linker

unlisted 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: Auto-links words on the current line or selection based on the current document and a Common References.md file.

Also warns you about missing reference links and takes you to them.

Comments: Comment Feed (RSS)

There are no comments yet.

+ Add Comment

Workflow Preview
Are we editing a file? ?
Run the block if
File Name
  • is Equal to
  • is Not Equal to
  • Contains
  • Doesn't Contain
  • Matches Regular Expression
Load Common References and link definitions ?
File Name
Common References.md
In Dropbox
ON
If File Does Not Exist
  • Empty Output
  • Stop Workflow
Store those in a variable ?
Variable Name
References
Value
Input
Extend the selection to encompass the whole line if necessary ?
Direction
  • Backward
  • Forward
  • Both
Unit
  • Start/End of Document
  • Start/End of Line
  • Number of Characters...
1
Run Python Script ?
Source Code
#coding: utf-8

# Markdown auto-linker - auto-links words in a post based on what's already 
# been linked or on a common reference document.
#
# So if, like me, you keep linking to the same URLs time and again, all you
# need to do is set up a reference document with:
#
# [Editorial][edapp]
# [edapp]: http://omz-software.com/editorial/
#
# And the word "Editorial" will be replaced accordingly, and link references
# added to your new document if necessary

import workflow, console, editor, re

console.show_activity("Parsing files...")

def extract_references(buffer):
	"""Naive URL and reference link extractor"""
	buffer = unicode(buffer)
	urls = re.findall("\n\[(\w+)\]\:\s+(.+)",  buffer, re.MULTILINE | re.IGNORECASE | re.UNICODE)
	refs = re.findall("\[([\w']+)\]\[(\w+)\]", buffer, re.MULTILINE | re.IGNORECASE | re.UNICODE)
	return urls, refs

# Load common link references if available
references = workflow.get_variable('References')

# Get a sane selection range and buffer
range = editor.get_selection()

if not range:
	buffer = editor.get_text()
	range = (0,len(buffer))
else:
	buffer = editor.get_selected_text()

# Pad out the selection
selection = unicode(u"\n%s\n" % buffer)

# Extract references
urls, refs = extract_references(references)
post_urls, post_refs = extract_references(editor.get_text())

# Merge them
all_refs = dict(refs)
all_refs.update(dict(post_refs))

# Characters we'll allow around references
surround = "[\s\"\'\-\:\;\,\.\_\*]+"

# now find all words that link to those references and replace them
for word in all_refs:
	raw_ref = all_refs[word]
	ref     = re.escape(raw_ref)
	selection = re.sub(r"(%(surround)s)%(word)s(%(surround)s)" % locals(), 
	                   r"\1[%(word)s][%(ref)s]\2" % locals(), selection)

editor.replace_text(range[0],range[1],selection.strip())

# Now that we've replaced things, let's see what references we need to add
post_urls, post_refs = extract_references(editor.get_text())
post_urls = dict(post_urls)

# This reverses the tuples and sets up a nice dictionary
used_refs = dict(map(lambda x: x[::-1], post_refs))

# Build a dictionary with all the URLs we know of
all_urls = dict(urls)
all_urls.update(post_urls)

# Now add each new reference at the end of the document
output = []
for ref in used_refs:
	if ref not in post_urls:
		try:
			end = len(editor.get_text())
			url = all_urls[ref]
			editor.replace_text(end, end,"\n[%(ref)s]: %(url)s" % locals())
		except KeyError:
			word = used_refs[ref]
			output.append("[%(word)s][%(ref)s]" % locals())
			pass

workflow.set_output("\n".join(output))
Any missing references? ?
Run the block if
Input
  • is Equal to
  • is Not Equal to
  • Contains
  • Doesn't Contain
  • Matches Regular Expression
Display missing references ?
Title
Missing References
List (Lines)
Input
Multiple Selection
OFF
Show in Popover
ON
Set Variable ?
Variable Name
Missing Reference
Value
Input
Document Text ?
Folded Text
  • Include
  • Replace with:
Find ?
Search for
Missing Reference
Match Group (RegEx)
0 (entire match)
Search Type
  • Regular Expression
  • Case-insensitive Search (A = a)
  • Case-sensitive Search (A ≠ a)
Output
  • Found Text
  • Range of Text (from:to)
Select Range ?
Range (from:to)
Input
Relative to
  • Entire Document
  • Current Selection
…End If
…End If