Skip to content

Instantly share code, notes, and snippets.

@dave-cross
Created November 21, 2013 14:52
Show Gist options
  • Save dave-cross/7582929 to your computer and use it in GitHub Desktop.
Save dave-cross/7582929 to your computer and use it in GitHub Desktop.
A script for Taskpaper files that sets tasks as @done if all subtasks are complete. This is purely a test. It would have to be added to in order to grab and parse *.taskpaper files.
#! /usr/bin/env python
# -*- coding: UTF-8 -*-
import re
# imput string sample data
targ = ''' - Task 09
This is a comment
- Subtask 9.01 @done
This is a subtask comment
- Subtask 9.02
Another comment
- Subsubtask 9.01 @done
- Subsubtask 9.02 @done
Why so many comments?
- Subsubtask 9.03
- Subsubsubtask 9.01 @done
Just another comment to break your code
- Subsubsubtask 9.02
Break, break, break
- Subsubsubsubtask 9.01 @done
This is the last comment, I promise
- Subsubsubtask 9.03 @done
I lied.
- This is the task of interest
- Subsubsubtask 9.03 @done
I love lying
- Subsubtask 9.05 @done
- Subtask 9.03 @done
Ok, this is truly the last comment.
Nah.'''
# original format from @philgr
# targ = '\t- Task 09\n\tThis is a comment\n\t\t- Subtask 9.01 @done\n\t\tThis is a subtask comment\n\t\t- Subtask 9.02\n\t\tAnother comment\n\t\t\t- Subsubtask 9.01 @done\n\t\t\t- Subsubtask 9.02 @done\n\t\t\tWhy so many comments?\n\t\t\t- Subsubtask 9.03\n\t\t\t\t- Subsubsubtask 9.01 @done\n\t\t\t\tJust another comment to break your code\n\t\t\t\t- Subsubsubtask 9.02\n\t\t\t\tBreak, break, break\n\t\t\t\t\t- Subsubsubsubtask 9.01 @done\n\t\t\t\t\tThis is the last comment, I promise\n\t\t\t\t- Subsubsubtask 9.03 @done\n\t\t\t\tI lied.\n\t\t\t- This is the task of interest\n\t\t\t\t- Subsubsubtask 9.03 @done\n\t\t\t\tI love lying\n\t\t\t- Subsubtask 9.05 @done\n\t\t- Subtask 9.03 @done\n\t\tOk, this is truly the last comment.\n\t\tNah.'
revtarg = targ.split('\n')[::-1] # targ is split and reversed
leading_spaces = 0 # set a variable for leading whitespace
# set a list of True booleans. This is how we track subtask completion.
# if a subtask isn't complete, the boolean at that tab count will be set to False.
# since this is Taskpaper, assume tab indents. If leading white space is spaces, increase the number to 50 or more.
tabtracker = [True]*20
# iterate over each line. Remember that they are in reverse order.
for i in range(len(revtarg)):
# get the white space count
thisline_spaces = len(revtarg[i]) - len(revtarg[i].lstrip())
# only work on tasks
if re.search("^\t*- ",revtarg[i]):
# If task isn't done, track it.
if not re.search("@done",revtarg[i]):
# if the line is outdented AND the subtasks are done, set task to done.
if (leading_spaces > thisline_spaces) and tabtracker[leading_spaces]==True:
revtarg[i] = revtarg[i] + " @done"
else: # anything that doesn't match should be marked as False
tabtracker[thisline_spaces] = False
# get the current leading space before starting the next line.
leading_spaces = thisline_spaces
print '\n'.join(revtarg[::-1])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment