-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.py
123 lines (80 loc) · 2.61 KB
/
index.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# -*- coding: utf-8 -*-
import linecache
import os.path
import string
def index(dirs, excludes):
truncateIndex('.')
writeFrontMatterToIndex('.', '')
for dir in dirs:
indexDir(dir, excludes)
def indexDir(dir, excludes):
if (not os.path.exists(dir) or not os.path.isdir(dir)):
error('Directory not found: ' + dir)
return
info('Indexing: ' + path([dir]))
dirName = string.capwords(dir)
# Add dir title to root index
writeTitle('.', dirName);
for subDir in sorted(os.listdir(dir)):
indexSubDir(dir, dirName, subDir, excludes)
def indexSubDir(dir, dirName, subDir, excludes):
subDirPath = path([dir, subDir])
if (not os.path.isdir(subDirPath) or subDir in excludes):
return
info('Indexing: ' + subDirPath)
subDirName = string.capwords(subDir)
# Add sub dir entry to root index
writeEntry('.', subDirName, subDirPath + '/index')
truncateIndex(subDirPath)
title = dirName + ' / ' + subDirName
writeFrontMatterToIndex(subDirPath, '')
# Add sub dir title to sub dir index
writeTitle(subDirPath, title)
for file in sorted(os.listdir(subDirPath)):
if (not file.startswith('.')) and file.endswith('.org'):
# Expect title string on 2nd line and skip the first 9 chars
title = linecache.getline(subDirPath + '/' + file, 2)[9:].rstrip()
htmlFilePath = subDirPath + '/' + file.replace('.org', '.html')
# Add doc entry to sub dir index
writeEntry(subDirPath, title, htmlFilePath)
# Add Jekyll front matter to HTML file
writeFrontMatterToPage(htmlFilePath, title)
def truncateIndex(dirPath):
filePath = dirPath + '/index.md'
with open(filePath, 'w+') as file:
file.truncate()
def getFrontMatterString(title):
return '---\ntitle: "' + title + '"\n---\n'
def writeFrontMatterToIndex(dirPath, title):
# Set the title to be empty string
writeLine(dirPath, getFrontMatterString(title))
def writeFrontMatterToPage(filePath, title):
try:
with open(filePath, 'r+') as f:
body = f.read()
# If not already have front matter
# TODO: Make it replace existing front matter
if (not body.startswith('---')):
f.seek(0)
f.write(getFrontMatterString(title) + body)
except IOError:
return
def writeTitle(dirPath, title):
writeLine(dirPath, '\n## ' + title + '\n\n')
def writeEntry(dirPath, name, link):
writeLine(dirPath, '- [' + name + '](/pkb/' + link + ')\n')
def writeLine(dirPath, line):
filePath = dirPath + '/index.md'
with open(filePath, 'a+') as file:
file.write(line)
def path(dirs):
path = '/'.join(dirs)
return path
def error(message):
print('[ERROR] ' + message)
def info(message):
print('[INFO] ' + message)
index(
['programming', 'general'],
['images']
)