Editorial Workflows

TF: Filter

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: Filter taskpaper files using folding with query syntax same as Taskpaper.app

Requires Python packages:
- Todoflow: https://github.com/bevesce/TodoFlow
- datedrop: https://github.com/bevesce/datedrop
- ply: https://github.com/dabeaz/ply
They are installed on first run so it can take a while.

Shared by: @bevesce

Comments: Comment Feed (RSS)

Jeremy — 30 Nov 2016
Looks like the script is now erroring on installing the python modules after:

> downloading https://github.com/bevesce/datedrop/archive/master.zip
> extracting <module 'zipfile'="" from="" '="" var="" containers="" bundle="" application="" ffac3728-7d04-45d3-9432-10051f7f85b6="" editorial.app="" pylib="" zipfile.py'="">

It's a "BadZipfile" error in "line 12"
Jeremy — 30 Nov 2016
Further investigation reveals that "datedrop" is no longer hosted under the repositories at https://github.com/bevesce. Anyone have an idea where this went?
JSLR — 09 Apr 2017
Would love to have this up and running again, or hear about alternatives for Taskpaper queries in Editorial. Any suggestions, anyone?

+ Add Comment

Workflow Preview
select query ?
Title
Query
List (Lines)
Today @start <= {today} or @due <= {today} or @today Not done not @done ⌨
Multiple Selection
OFF
Show in Popover
OFF
if selected ⌨ ask for query ?
Run the block if
Input
  • is Equal to
  • is Not Equal to
  • Contains
  • Doesn't Contain
  • Matches Regular Expression
Request Text Input ?
Title
Query
Initial Text
  • Single Line
  • Multiple Lines
Keyboard Options:
…End If
set query ?
Variable Name
query
Value
Input
maybe install todoflow ?
Source Code
def install_todoflow():
    import urllib
    import zipfile
    import shutil
    import os

    def download_package(github_url, name, dir_name, before_dir_removal=lambda dir_name, name: None):
        zip_file = name + '-temp.zip'
        print 'downloading', github_url
        urllib.urlretrieve(github_url, zip_file)
        print 'extracting', zipfile
        z = zipfile.ZipFile(zip_file)
        z.extractall()
        try:
            shutil.rmtree(name)
        except OSError:
            pass
        os.remove(zip_file)
        print 'installing', name
        shutil.move(os.path.join(dir_name, name), name)
        before_dir_removal(dir_name, name)
        print 'cleaning'
        shutil.rmtree(dir_name)
        print 'done'

    def copy_parsetab(dir_name, name):
        newtab = 'todoflow_parsetab.pickle'
        oldtab = os.path.join(dir_name, newtab)
        try:
            os.remove(newtab)
        except:
            pass
        try:
            shutil.move(oldtab, newtab)
        except:
            pass

    download_package(
        'https://github.com/dabeaz/ply/archive/master.zip',
        'ply',
        'ply-master'
    )
    download_package(
        'https://github.com/bevesce/TodoFlow/archive/master.zip',
        'todoflow',
        'TodoFlow-master',
        copy_parsetab
    )
    download_package(
        'https://github.com/bevesce/datedrop/archive/master.zip',
        'datedrop.py',
        'datedrop-master'
    )
    def fix_file(path, fixes):
        t = open(path).read()
        for tofix, replacement in fixes:
            t = t.replace(tofix, replacement)
        open(path, 'w').write(t)

    # its seems like editorial cant handle absolute or somerhing like that
    d = 'todoflow'
    for filename in os.listdir(d):
        if filename.endswith('py'):
            fix_file(
                os.path.join(d, filename), [
                    ['from __future__ import absolute_import', ''],
                    ['from . ', ''],
                    ['from .', 'from ']
                ]
            )
    # change location of parsetab pickle
    fix_file(
        'todoflow/querying_parser.py', (
            ("os.path.expanduser('~/.", "('" + os.path.abspath('.') + '/'),
        )
    )

try:
	import todoflow
	import ply
	import datedrop
except:
	install_todoflow()
prepare query ?
Source Code
#coding: utf-8
import workflow
import datetime
import re
import datedrop

query = workflow.get_variable('query')
query = re.sub(
	r'{([^}]*)}',
	lambda m: datedrop.expand(m.group(1), '%Y-%m-%d', '%Y-%m-%d %H:%M'),
	query
)
workflow.set_output(query)
do the folding ?
Source Code
#coding: utf-8
import editor
import workflow
import todoflow as tf

editor.unfold_all()
text = editor.get_text()
query = workflow.get_input()


def fold_not_matching_query(text, query):
    todolist = tf.from_text(text)
    matching_ranges = []
    for i in todolist.filter(query):
        matching_ranges.append((i.start, i.end))
    ranges_to_fold = calculate_ranges_to_fold(len(text), matching_ranges)
    for start, end in ranges_to_fold[::-1]:
        editor.fold_range(start, end)


def calculate_ranges_to_fold(document_length, ranges_not_to_fold):
    range_start = 0
    ranges = []
    if not ranges_not_to_fold:
        return ((0, document_length), )
    if ranges_not_to_fold[0][0] == 0:
        range_start = ranges_not_to_fold[0][1]
        ranges_not_to_fold = ranges_not_to_fold[1:]

    for s, e in ranges_not_to_fold:
        if range_start + 1 == s:
            range_start = e
            continue
        ranges.append((range_start + 1, s - 1))
        range_start = e
    if e != document_length:
        ranges.append((e + 1, document_length))
    return ranges


fold_not_matching_query(text, query)