Editorial Workflows

TP_Projects

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: Displays a list of all TaskPaper projects in all files in the currently active file directory. Select a project from the list to see a search result for the project term organized by file. Tap result to open and select.

::: By Macdrifter :::: v0.9

Shared by: Macdrifter.com

Comments: Comment Feed (RSS)

There are no comments yet.

+ Add Comment

Workflow Preview
Set Variable ?
Variable Name
projList
Value
Set Variable ?
Variable Name
todayDate
Value
A document must be open... ?
Run the block if
File Name
  • is Equal to
  • is Not Equal to
  • Contains
  • Doesn't Contain
  • Matches Regular Expression
Show Alert ?
Title
No Document Open
Message
You need to open a document to use this workflow. It uses the current document's path to determine where to search.
Button 1
OK
Output Value
Button 2
(don't show)
Output Value
Button 3
(don't show)
Output Value
Show Cancel Button
OFF
Stop ?
Stop
  • This Workflow
  • Repeat Block
Show HUD Alert
OFF
Message
Stopped
…End If
set CSS Light Variable ?
Variable Name
CSS Light
Value
body { font-family: 'Source Sans Pro'; color: #333; background-color: #fafafa; margin: 40px 60px 40px 60px; } a { text-decoration: none; color: #016396; } a.result-box { display: block; margin-bottom: 15px; -webkit-tap-highlight-color: rgba(0,0,0,0); color: #333; background-color: white; border: 1px solid #ccc; border-radius: 4px; padding: 10px; } a.result-box:active { color: #016396; background-color: #f0f0f0; } .highlight { background-color: yellow; } #help-text { color: gray; }
set CSS Dark Variable ?
Variable Name
CSS Dark
Value
body { font-family: 'Source Sans Pro'; color: #d6d6d6; background-color: #33393f; margin: 40px 60px 40px 60px; } a { text-decoration: none; color: #b2cae5; } a.result-box { display: block; margin-bottom: 15px; -webkit-tap-highlight-color: rgba(0,0,0,0); color: #d6d6d6; background-color: #33393f; border: 1px solid #111; border-radius: 4px; padding: 10px; } a.result-box:active { color: #b2cae5; background-color: #5a6470; } .highlight { background-color: #187b00; } #help-text { color: gray; }
set CSS Variable ?
Variable Name
CSS
Value
This will be set from the script, depending on the currently selected theme.
Gather All Projects (Python) ?
Source Code
import editor
import console
import os
import re
import sys
import codecs
import workflow
from StringIO import StringIO
import time

theme = editor.get_theme()
workflow.set_variable('CSS', workflow.get_variable('CSS Dark' if theme == 'Dark' else 'CSS Light'))

today_date = time.strftime('%Y-%m-%d')

workflow.set_variable("todayDate", today_date)

p = editor.get_path()

start_pattern = r'^\s*(\w+.+:\s*(\@[^\s]+(\(.*?\))?\s*)*$\n?)|^\s*---.{3,5}---+$'

term = start_pattern
pattern = re.compile(term, flags=re.IGNORECASE)

from urllib import quote
dir = os.path.split(p)[0]
valid_extensions = set(['.txt', '.md', '.markd', '.text', '.mdown', '.taskpaper'])
html = StringIO()
match_count = 0
found_snippets = []
for w in os.walk(dir):
	dir_path = w[0]
	filenames = w[2]
	for name in filenames:
		full_path = os.path.join(dir_path, name)
		ext = os.path.splitext(full_path)[1]
		if ext.lower() not in valid_extensions:
			continue
		
		i = 0
		try:
			with codecs.open(full_path, 'r', 'utf-8') as f:
				for line in f:
					for match in re.finditer(pattern, line):
						#print match.group(0)
						found_snippets.append(match.group(0).strip())
					i += len(line)
		except UnicodeDecodeError, e:
			pass
found_snippets = filter(None, found_snippets)
project_list = '\n'.join(found_snippets)
workflow.set_variable('projList', project_list)
			
Select from List ?
Title
Projects Available
List (Lines)
projList
Multiple Selection
OFF
Show in Popover
OFF
Escape for RegEx ?
Set Variable ?
Variable Name
selectionVal
Value
Input
Do the search (Python) ?
Source Code
import editor
import console
import os
import re
import sys
import codecs
import workflow
from StringIO import StringIO
import time

theme = editor.get_theme()
workflow.set_variable('CSS', workflow.get_variable('CSS Dark' if theme == 'Dark' else 'CSS Light'))


p = editor.get_path()

start_pattern = workflow.get_variable('selectionVal')

term = start_pattern
pattern = re.compile(term, flags=re.IGNORECASE)

from urllib import quote
dir = os.path.split(p)[0]
valid_extensions = set(['.txt', '.md', '.markd', '.text', '.mdown', '.taskpaper'])
html = StringIO()
match_count = 0
for w in os.walk(dir):
	dir_path = w[0]
	filenames = w[2]
	for name in filenames:
		full_path = os.path.join(dir_path, name)
		ext = os.path.splitext(full_path)[1]
		if ext.lower() not in valid_extensions:
			continue
		found_snippets = []
		i = 0
		try:
			with codecs.open(full_path, 'r', 'utf-8') as f:
				for line in f:
					for match in re.finditer(pattern, line):
						start = max(0, match.start(0) - 100)
						end = min(len(line)-1, match.end(0) + 100)
						snippet = (line[start:match.start(0)],
						           match.group(0),
						           line[match.end(0):end],
						           match.start(0) + i,
						           match.end(0) + i)
						found_snippets.append(snippet)
					i += len(line)
		except UnicodeDecodeError, e:
			pass
		if len(found_snippets) > 0:
			match_count += 1
			root, rel_path = editor.to_relative_path(full_path)
			ed_url = 'editorial://open/' + quote(rel_path) + '?root=' + root
			html.write('<h2><a href="' + ed_url + '">' + name + '</a></h2>')
			for snippet in found_snippets:
				start = snippet[3]
				end = snippet[4]
				select_url = 'editorial://open/' + quote(rel_path) + '?root=' + root
				select_url += '&selection=' + str(start) + '-' + str(end)
				html.write('<a class="result-box" href="' + select_url + '">' + snippet[0] + '<span class="highlight">' + snippet[1] + '</span>' + snippet[2] + '</a>')
if match_count == 0:
	html.write('<p>No matches found.</p>')

workflow.set_output(html.getvalue())
			
Show Results ?
HTML
<!DOCTYPE html> <html> <head><meta charset="utf-8"/> <style>CSS</style> </head> <body ontouchstart=""> <h1>Search Results for “selectionVal”</h1> <p id="help-text">Tap on a result to select it in the editor. Tap the <em>Done</em> button to get back to the Markdown preview.</p> Input </body> </html>
Title
Results
Base URL
None