Editorial Workflows

jump

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:
This workflow uses the Markdown link syntax to let jump between files or create files in editorial. The idea is that you can navigate to the files refrenced in a document more easily and (optionally) can create folders and files be simply referencing them in the document you are currently writing.

To use the workflow place your cursor within the bounds of Markdown link with a relative file path and run the workflow. It will then open the referenced file for you. Any link with a specific protocol like http: or file: will be ignored by the workflow. The workflow also does deliberately ignore file extensions so that you can write Markdown files that have working links in their rendered html files. Any link will be interpreted as a link to a Markdown file with the same name.

It can handle folders and the command "../" in its file path. If either the referenced folder or file do not exist it will ask you if you want it to create them for you. Newly created files will be empty apart from a link to the file they were created from.

Examples:
=======

* The link [blah](file_a.md) will open the file `file_a.md` in the folder of this document.
* The link [](file_b..html) will open the file `file_b.md` in the folder of this document.
* The link [)](notes/june/file_c.txt) will open the file `file_c.md` in the sub folder `notes/june/` of this document.
* The link [](../file_d) will open the file `file_d.md` in the folder one folder above the folder of this file. If you are in `Documents` this will raise an alert.
* The link [](../../costs/file_e) will open the file `file_e.md` in the folder `costs`which does lie two folder above the folder of this file. If you are not at least two levels below `Documents` this will raise an alert.

Shared by: zipit

Comments: Comment Feed (RSS)

There are no comments yet.

+ Add Comment

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

import console
import editor
import os
import re
import workflow

EXPR_LINK = '\[.*?\]\(([^:]*?)\)'


def get_uris():
    ''' Retrievs the relative path of link uri currently under the cursor in
        relation to editorials root documents folder.

    returns:
        tuple of (str, str, str, str): (absolute path, relative path,
        file name, and back link) or None * 4 if no uri has been found.
    '''
    text = editor.get_text()
    select = editor.get_selection()
    link = None
    for match in re.finditer(EXPR_LINK, text, re.IGNORECASE):
        span = match.span()
        if span[0] <= select[0] and span[1] >= select[1]:
            link = match.groups()[0]
    if link is not None:
        link_path, link_file = os.path.split(link)
        link_file = os.path.splitext(link_file)[0] + os.extsep + 'md'
        base, file_dir = os.path.dirname(editor.get_path()).split('Documents')
        file_dir = file_dir.strip()
        base = os.path.join(base, 'Documents')
        blnk = []
        for folder in link_path.split(os.sep):
            if folder == '..':
                if file_dir == '':
                    msg = ('The relative path "{}" exceeds the bounds of ' +
                           'editorials "Documents" folder.').format(link)
                    console.alert('', msg, 'Ok', hide_cancel_button=True)
                    return None, None, None, None
                else:
                    blnk.append(os.path.split(file_dir)[1])
                    file_dir = os.path.dirname(file_dir)
            elif folder != '':
                blnk.append('..')
                if file_dir == '':
                     file_dir = os.sep
                file_dir = os.path.join(file_dir, folder)
        if len(file_dir) > 0 and file_dir[0] == os.sep:
            file_dir = file_dir[1:]
        path = os.path.join(base, file_dir)
        blnk = os.sep.join(reversed(blnk))
        return path, file_dir, link_file, blnk
    return None, None, None, None


def validate_uri(path, folder, file, back_link):
    ''' Validates the existance of the passed path and file. If the path or file
    does not exist, a dialog will be raised asking for permission to create them.

    args:
        path(str): The absolute folder path for the file the file is loctaed in.
        folder(str): The folder path relative to editorials 'Documents' folder.
        file(str): The file name and the extension of the file.
        back_link(str): A back link path for new files.
    returns:
        str | None: The relative uri or None when the file does not exist.
    '''
    if file is None:
        return False
    uri = os.path.join(path, file)
    b0, b1 = 'Cancel', 'Create'
    msg_path = 'The path "{}" does not exist.'.format(folder)
    msg_file = 'The file "{}" does not exist.'.format(file)
    if not os.path.exists(path):
        if console.alert('', msg_path, b0, b1, hide_cancel_button=True) == 2:
            try:
                os.makedirs(path)
            except BaseException:
                console.alert('', 'Could not create folder.', 'Ok',
                               hide_cancel_button=True)
                return None
        else:
            return None
    if not os.path.exists(uri):
        if console.alert('', msg_file, b0, b1, hide_cancel_button=True) == 2:
            msg = 'The file [{0}]({1}) is pointing to this file.'
            name = os.path.split(editor.get_path())[1]
            back_link = os.path.join(back_link, name)
            msg = msg.format(name, back_link)
            try:
                with open(uri, 'w+') as f:
                    f.write(msg)
                editor.reload_files()
            except BaseException:
                console.alert('', 'Could not write file.', 'Ok', 
                              hide_cancel_button=True)
                return None
        else:
            return None
    return os.path.join(folder, file)


path, rel_folder, file, back_link = get_uris()
output = validate_uri(path, rel_folder, file, back_link) or 'None'
workflow.set_output(output)
If… ?
Run the block if
Input
  • is Equal to
  • is Not Equal to
  • Contains
  • Doesn't Contain
  • Matches Regular Expression
None
Open Document ?
File Name
%var:Input
In Dropbox
OFF
…End If