Editorial Workflows

Links 2 reference

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: Highlight markdown and format it into reference style links

* See the [ForMd docs](http://drbunsen.github.io/formd/) for more information:

The original script is can be launched with an argument via the command line/shell script etc so I've split the script into it's the seperate functions; covert all to reference links, convert all to inline links, flip link state. Further info in the GitHub docs.

Shared by: jm

Comments: Comment Feed (RSS)

There are no comments yet.

+ Add Comment

Workflow Preview
Document Text ?
Folded Text
  • Include
  • Replace with:
Run Python Script ?
Source Code
#!/usr/bin/env python
# encoding=utf8
Seth Brown

import editor
import workflow
from sys import stdin, stdout
import re
from collections import OrderedDict

class ForMd(object):
    """ Format Markdown text"""
    def __init__(self, text):
        super(ForMd, self).__init__()
        self.text = text
        self.match_links = re.compile(r"""(\[[^^]*?\])\s?             # text
                                      (\[.*?\]|\(.*?\r?\n?.*?\)\)?)   # ref/url
                                       """, re.MULTILINE | re.X)
        self.match_refs = re.compile(r'(?<=\n)\[[^^\r?\n]*?\]:\s?.*')
        self.data = []

    def _links(self):
        """ Find Markdown links"""
        links = re.findall(self.match_links, self.text)
        for (text, ref)  in links:
            ref = ref.replace('\n', '').replace('\r', '')
            yield (text, ref)

    def _refs(self):
        """ Find Markdown references"""
        refs = re.findall(self.match_refs, self.text)
        refs = OrderedDict(i.split(":", 1) for i in refs)
        return refs

    def _format(self):
        """ Process text"""
        links = (i for i in self._links())
        refs = self._refs()
        for n, link in enumerate(links):
            text, ref = link
            ref_num = ''.join(("[",str(n+1),"]: "))
            if ref in refs.keys():
                url = refs.get(ref).strip()
                formd_ref = ''.join((ref_num, url))
                formd_text = ''.join((text, ref_num))
                self.data.append([formd_text, formd_ref])
            elif text in refs.keys():
                url = refs.get(text).strip()
                formd_ref = ''.join((ref_num, url))
                formd_text = ''.join((text, ref_num))
                self.data.append([formd_text, formd_ref])
            elif ref not in refs.keys():
                # remove the leading/training parens
                parse_ref = ref[1:-1]
                formd_ref = ''.join((ref_num, parse_ref))
                formd_text = ''.join((text,ref_num))
                self.data.append([formd_text, formd_ref])

    def inline_md(self):
        """ Generate inline Markdown"""
        text_link = iter([''.join((_[0].split("][",1)[0],
            "](", _[1].split(":",1)[1].strip(), ")")) for _ in self.data])
        formd_text = self.match_links.sub(lambda _: next(text_link), self.text)
        formd_md = self.match_refs.sub('', formd_text).strip()
        return formd_md

    def ref_md(self):
        """ Generate referenced Markdown"""
        ref_nums = iter([_[0].rstrip(" :") for _ in self.data])
        formd_text = self.match_links.sub(lambda _: next(ref_nums), self.text)
        formd_refs = self.match_refs.sub('', formd_text).strip()
        references = (i[1] for i in self.data)
        formd_md = '\n'.join((formd_refs,
                              '\n', '\n'.join(i for i in references)))
        return formd_md

    def flip(self):
        """ Convert Markdown to the opposite style of the first text link"""
            first_match = re.search(self.match_links, self.text).group(0)
            if first_match is None or first_match == []:
                formd_md = self.text
            elif '(' and ')' in first_match:
                formd_md = self.ref_md()
                formd_md = self.inline_md()
        except AttributeError:
            formd_md = self.text
        yield formd_md

def editorial_ref_md():
	text = workflow.get_input()
	f = ForMd(text)
	ref_md = f.ref_md()
Replace Selected Text ?
Replacement Text