Skip to content

Commit 07858db

Browse files
committed
Add support for exporting to JSON with the "-t json" target flag
1 parent cd62eec commit 07858db

File tree

2 files changed

+36
-8
lines changed

2 files changed

+36
-8
lines changed

lib/tiddlywiki.py

+24-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
# that translate between Twee and TiddlyWiki output seamlessly.
1313
#
1414

15-
import re, datetime, time, os, sys
15+
import re, datetime, time, os, sys, json
1616
import PyRSS2Gen as rss
1717

1818
#
@@ -62,6 +62,13 @@ def toHtml (self, app = None, target = None, order = None):
6262
output += '</div></body></html>'
6363

6464
return output
65+
66+
def toJson (self):
67+
output = {"data": []}
68+
order = self.tiddlers.keys()
69+
for i in order:
70+
output['data'].append(self.tiddlers[i].toJson(self.author))
71+
return json.dumps(output)
6572

6673
def toRtf (self, order = None):
6774
"""Returns RTF source code for this TiddlyWiki."""
@@ -263,6 +270,22 @@ def toHtml (self, author = 'twee'):
263270
return output
264271

265272

273+
def toJson (self, author = 'twee'):
274+
now = time.localtime()
275+
ret = {}
276+
277+
ret['title'] = self.title
278+
ret['tags'] = []
279+
for tag in self.tags:
280+
ret['tags'].append(tag)
281+
ret['modified'] = encode_date(self.modified)
282+
ret['created'] = encode_date(self.created)
283+
ret['modifier'] = author
284+
ret['text'] = self.text
285+
286+
return ret
287+
288+
266289
def toTwee (self):
267290
"""Returns a Twee representation of this tiddler."""
268291
output = ':: ' + self.title

twee

+12-7
Original file line numberDiff line numberDiff line change
@@ -76,27 +76,32 @@ def main (argv):
7676

7777
# output the target header
7878

79-
if (target != 'none') and (target != 'plugin'):
79+
if (target != 'none') and (target != 'plugin') and (target != 'json'):
8080
file = open(scriptPath + os.sep + 'targets' + os.sep + target \
8181
+ os.sep + 'header.html')
8282
print(file.read())
8383
file.close()
8484

8585
# the tiddlers
8686

87-
print tw.toHtml()
87+
if (target == 'json'):
88+
print tw.toJson()
89+
else:
90+
print tw.toHtml()
8891

8992
# plugins
9093

9194
for plugin in plugins:
92-
file = open(scriptPath + os.sep + 'targets' + os.sep + target \
93-
+ os.sep + 'plugins' + os.sep + plugin + os.sep + 'compiled.html')
94-
print(file.read())
95-
file.close()
95+
if target != 'json':
96+
file = open(scriptPath + os.sep + 'targets' + os.sep + target \
97+
+ os.sep + 'plugins' + os.sep + plugin + os.sep + 'compiled.html')
98+
print(file.read())
99+
file.close()
96100

97101
# and close it up
98102

99-
print '</div></html>'
103+
if target != 'json':
104+
print '</div></html>'
100105

101106

102107
if __name__ == '__main__':

0 commit comments

Comments
 (0)