forked from tweecode/twine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
twee
executable file
·97 lines (71 loc) · 2.53 KB
/
twee
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#!/usr/bin/env python2
import sys, os.path, getopt, glob
from header import Header
from tiddlywiki import TiddlyWiki
from version import versionString
def usage():
print >> sys.stderr, 'usage: twee [-a author] [-t target] [-s startpassage] source1 [source2..]'
print >> sys.stderr, ' (source directories are recursively scanned for .tw/.twee files)'
class TweeApp(object):
NAME = 'Twee'
VERSION = versionString
def __init__ (self, path):
"""Initializes the application."""
self.builtinTargetsPath = path + os.sep + 'targets' + os.sep
def displayError(self, activity):
activity = activity.strip()
print >> sys.stderr, 'Error while ' + activity
exceptionValue = sys.exc_info()[1]
if exceptionValue is not None:
print >> sys.stderr, exceptionValue
print >> sys.stderr, ''
def main(argv):
if not argv:
usage()
sys.exit(2)
# defaults
author = 'twee'
target = 'sugarcane'
start = ''
# read command line switches
try:
opts, args = getopt.getopt(argv, 'a:s:t:', ['author=', 'start=', 'target='])
except getopt.GetoptError:
usage()
sys.exit(2)
for opt, arg in opts:
if (opt in ('-a', '--author')):
author = arg
elif (opt in ('-s', '--start')):
start = arg
elif (opt in ('-t', '--target')):
target = arg
sources = []
for arg in args:
for source in glob.glob(arg):
if os.path.isdir(source):
for dirpath, dirnames, filenames in os.walk(source):
for filename in filenames:
if filename.endswith('.tw') or filename.endswith('.twee'):
sources.append(os.path.join(dirpath, filename))
else:
sources.append(source)
if len(sources) == 0:
print >> sys.stderr, 'twee: no source files specified'
sys.exit(2)
scriptPath = os.path.dirname(os.path.realpath(sys.argv[0]))
app = TweeApp(scriptPath)
header = Header.factory(target, app.builtinTargetsPath + target + os.sep, app.builtinTargetsPath)
# populate a TiddlyWiki
tw = TiddlyWiki(author)
order = []
for filename in sources:
order += tw.addTweeFromFilename(filename)
# generate output
output = tw.toHtml(app, header, order, startAt = start)
if sys.stdout.isatty():
print output
else:
print output.encode('utf_8_sig')
if __name__ == '__main__':
main(sys.argv[1:])