-
Notifications
You must be signed in to change notification settings - Fork 0
/
doittomorrow.py
81 lines (67 loc) · 2.64 KB
/
doittomorrow.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Copyright (c) Rex Tsai 2013
#
# Usage: doittomorrow.py %s %n
#
# This script move all un-checked task to tomorrow page,
# and xchecked the tasks in currenet page.
import sys
import pprint
import copy
import os
from datetime import date, timedelta
from zim.formats import ParseTree, Element, Visitor, get_format, UNCHECKED_BOX, CHECKED_BOX, XCHECKED_BOX
try:
import xml.etree.cElementTree as ElementTreeModule
except: #pragma: no cover
logger.warn('Could not load cElementTree, defaulting to ElementTreeModuleementTree')
import xml.etree.ElementTree as ElementTreeModule
def main(orig_file, notebook_path):
assert(os.path.isdir(notebook_path))
assert(os.path.isfile(orig_file))
dumper = get_format('wiki').Dumper()
parser = get_format('wiki').Parser()
text = open(orig_file).read()
parsetree = parser.parse(text)
# Title
newtree = ParseTree().fromstring("<zim-tree></zim-tree>")
title = parsetree.find("h")[0];
if(title is not None):
# newtree.set_heading(title, 1)
h = ElementTreeModule.Element('h', {'level': 1})
h.text = title
newtree._etree.getroot().append(h)
p = ElementTreeModule.Element('p')
p.text = "\n"
newtree._etree.getroot().append(p)
for para in parsetree._etree.getiterator('p'):
p = ElementTreeModule.Element('p')
for node in flatten_list(para, para):
p.append(node)
newtree._etree.getroot().append(p)
# new todo list
text = ''.join(dumper.dump(newtree)).encode('utf-8')
tomorrow = date.today() + timedelta(1)
directory = os.path.join(notebook_path, "Calendar", str("%04d" % tomorrow.year), str("%02d" % tomorrow.month), str("%02d" % tomorrow.day))
filename = "stash.txt"
if (not os.path.exists(directory)): os.makedirs(directory)
# write tasks to tomorrow page.
with open(os.path.join(directory, filename), 'a+') as the_file:
the_file.write(text)
# update original wiki page.
text = ''.join(dumper.dump(parsetree) ).encode('utf-8')
with open(orig_file, 'w') as the_file:
the_file.write(text)
def flatten_list(node, parent = None, copied = False):
for child in node.getchildren():
if child.tag in ('li') and (child.get('bullet') not in (CHECKED_BOX, XCHECKED_BOX)):
if copied is False:
yield copy.deepcopy(parent)
copied = True
# makr the bullet xchecked as record.
child.set('bullet', XCHECKED_BOX)
for i in flatten_list(child, node, copied):
yield i
if __name__ == "__main__":
main(sys.argv[1], sys.argv[2])