Skip to content

Instantly share code, notes, and snippets.

@scateu
Last active May 11, 2020 16:24
Show Gist options
  • Save scateu/a0a3a724b67b1f72dacd907ee3a09bbb to your computer and use it in GitHub Desktop.
Save scateu/a0a3a724b67b1f72dacd907ee3a09bbb to your computer and use it in GitHub Desktop.
Convert todo.txt or Taskpaper(Omnifocus) to .eml file. Then import into alpine.
# -*- coding: utf-8
"""
Manually Pre-process
## Omnifocus 拷贝为Taskpaper
把Taskpaper转成Todo.txt
VIM COMMANDS:
:%g/^$/d
:%s/@autodone(false)//g
:%s/@parallel(true)//g
:%s/@context(\(.*\))/@\1/g
:%s/@defer(\(.*\) .*)/t:\1 /g
:%s/@due(\(.*\) .*)/due:\1 /g
"""
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
import sys
import re
with open(sys.argv[-1]) as f:
lines = f.readlines()
_subject = ""
_comment = []
for i, l in enumerate(lines):
if l.startswith("- "):
_subject = re.sub(r'^- ', '', l)
_subject = _subject.replace('\r','').replace('\n','')
elif l.startswith((" ","\t")):
_comment.append(l.strip().decode('utf-8'))
if i+1 < len(lines):
if lines[i+1].startswith("- "):
_output = True
else:
_output = False #not yet, wait for it..
else: #the last line, output anyway
_output = True
if _output:
_email = MIMEMultipart('alternative')
_email['Subject']=_subject.decode('utf-8')
_email['From']="todo@scateu.me"
_part= MIMEText(u'\n\r'.join(_comment)+u"\n\r-- \n\rImported from Taskpaper","plain","utf-8")
_email.attach(_part)
print _email
_subject = ""
_comment = []
# -*- coding: utf-8
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
import sys
with open(sys.argv[-1]) as f:
lines = f.readlines()
for l in lines:
_email = MIMEMultipart('alternative')
_email['Subject']=l.replace('\r','').replace('\n','').decode('utf-8')
_email['From']="todo@scateu.me"
_part= MIMEText(u"Imported from todo.txt","plain","utf-8")
_email.attach(_part)
print _email
sed=gsed
if [ ! $# -eq 1 ]
then
echo "Usage: preprocess_taskpaper.sh <todo.taskpaper>"
exit -1
fi
$sed -i "/^$/d" $1
$sed -i "s/@autodone(false)//" $1
$sed -i "s/@parallel(true)//" $1
$sed -i "s/@context(\(.*\))/@\1/" $1
$sed -i "s/@defer(\(.*\) .*)/t:\1 /" $1
$sed -i "s/@due(\(.*\) .*)/due:\1 /" $1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment