-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathbuildNotesList.py
More file actions
57 lines (52 loc) · 1.5 KB
/
buildNotesList.py
File metadata and controls
57 lines (52 loc) · 1.5 KB
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
#!/usr/bin/python
import os
# Get the titles of all the notes files in the directory. The
# title is assumed to be the first line of the file. Truncate
# the title at a word boundary if it's longer than maxlength,
# and turn it into a link in a list item.
def nameList(dir):
fileLI = []
maxlength = 35
allFiles = os.listdir(dir)
baseNames = [ f[:-3] for f in allFiles if f[-3:] == '.md' ]
# print baseNames
for fn in baseNames:
f = file(os.path.join(d, fn + '.md'))
top = f.readline()
title = top.strip('# \n')
if len(title) > maxlength:
words = title.split()
twords = []
count = 0
for w in words:
if count + len(w) > maxlength:
break
else:
twords.append(w)
count += len(w) + 1
title = ' '.join(twords) + "…"
f.close()
fileLI.append('<li><a href="\' + root + \'%s.html">%s</a></li>' % (os.path.join(d, fn),title))
return fileLI
# Find all the directories that have md files.
mdDirs = []
for root, dirs, files in os.walk('.'):
for f in files:
if f[-3:] == '.md':
mdDirs.append(root)
break
# Go through the directories and generate the list of links.
linkList = []
for d in mdDirs:
if d == '.':
linkList += nameList(d)
# print linkList
else:
linkList += ['<li>%s' % d[2:], '<ul>']
# print linkList
linkList += nameList(d)
# print linkList
linkList += ['</ul>', '</li>']
print '''function showNotesList(root){
document.write('%s')
}''' % ''.join(linkList)