-
Notifications
You must be signed in to change notification settings - Fork 0
/
printHTMLList.py
181 lines (175 loc) · 7 KB
/
printHTMLList.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# Please compile using python3.4
# http://stackoverflow.com/questions/898669/how-can-i-detect-if-a-file-is-binary-non-text-in-python
# http://stackoverflow.com/questions/2922783/how-do-you-walk-through-the-directories-using-python
import re
import os
import mimetypes # for finding type of file
import sys
import copy
htmlStringBegin = "<!DOCTYPE html>\n\
<html>\n\
<head>\n\
<title>Project - Index of Words</title>\n\
<link href='http://fonts.googleapis.com/css?family=Lato:400,700,900' rel='stylesheet' type='text/css'>\n\
<link href='http://fonts.googleapis.com/css?family=Roboto:400,700,900' rel='stylesheet' type='text/css'>\n\
<link rel=\"stylesheet\" href=\"styles/bootstrap/bootstrap.min.css\">\n\
<link rel=\"stylesheet\" href=\"styles/bootstrap/bootstrap-theme.min.css\">\n\
<link rel=\"stylesheet\" href=\"styles/myStyle.css\">\n\
<script type=\"text/javascript\" src=\"js/search.js\"></script>\n\
</head>\n\
<body>\n\
<div id=\"head-title\">\n\
<center>\n\
<p id=\"title\"><b>Index File </b>\n\
<!--<input type=\"text\" width=\"500px\"></input>\n\
<button id=\"search\">Search</button></p>-->\n\
<input id=\"search-word\" name=\"wordToSearch\" type=\"text\" width=\"500px\" onKeyDown=\"if(event.keyCode == 13){searchWord(this.value)};\"></input>\n\
<button id=\"search-button\" onclick=\"searchWord(document.getElementsByName('wordToSearch')[0].value)\">Search</button>\n\
<button id=\"disp-button\" onclick=\"printOrigDictionary()\">Original Index</button>\n\
</center>\n\
</div><br>\n\
<!--<hr>-->\n\
<br><br><center><div id=\"resultBox\"></div></center>\n\
<div id=\"content\">\n\
<!--<center><b>Index</b></center>-->\n\
<center>\n"
htmlStringEnd = " </center></div><center>\n\
<p id=\"file-title\"></p>\n\
<table id=\"codeTable\">\n\
</table><br><br><br>\n\
</center>\n\
\n\
<script src=\"js/jquery/jquery-1.11.3.min.js\"></script>\n\
<!-- Include all compiled plugins (below), or include individual files as needed -->\n\
<script src=\"js/bootstrap/bootstrap.min.js\"></script>\n\
<div id=\"footer\"> \n\
© 2015 Designed by <a href=\"http://www.cse.iitb.ac.in/~praneethas/\">Sona Praneeth</a> <a href=\"http://www.cse.iitb.ac.in/~siddarth/\">Siddharth A</a>\n\
</div>\n\
</body>\n\
</html>"
'''
Create fileIndex.html - HTML file containing indexlist
Input: Dictionary of words with key as word and value as dictionary of files as key and values as line nos
Output: [[listOfTextFiles],[listOfHTMLFiles]]
'''
def printToHTMLCompleteList(wordsList, outfile):
indexFile = open(outfile, 'w')
indexFile.write(htmlStringBegin)
indexFile.write("\t\t<table class=\"table\" id=\"indexWords\">\n")
indexFile.write("\t\t<thead>\n\t\t\t<tr id=\"head\">\n");
indexFile.write("<th>Word</th><th>File Name</th><th>Line Nos</th></tr></thead>");
noEntries=15
for wordInfo in wordsList:
word = wordInfo[0]
infoWordList = wordInfo[1]
lenInfoWordList = len(infoWordList)
for fileName in infoWordList:
unixFileName = "/".join(fileName[0].split('\\'))
# print(os.path.normpath(os.path.relpath(fileName[0], '.')))
relativeFilePath = os.path.relpath(unixFileName, '.')
normRelativeFilePath = os.path.normpath(relativeFilePath)
# print("Relative path: ", relativeFilePath)
# print("Relative normalize path: ", normRelativeFilePath)
baseFileNameArray = relativeFilePath.split('/');
baseFileName = baseFileNameArray[-1]
lineNoArray = fileName[1]
lenLineNoArray = len(lineNoArray)
k = 1
l = 1
for lineNo in lineNoArray:
if(l==1 and k!=lenLineNoArray):
indexFile.write("\t\t\t"+"<tr>"+"\n\t\t\t\t"+"<td id=\"word\">")
indexFile.write(word)
indexFile.write("</td>\n")
indexFile.write("\t\t\t\t<td id=\"file-name\"><a href=\"#codeTable\" onclick=\"populatePre('"+ normRelativeFilePath+"')\">")
indexFile.write(baseFileName)
indexFile.write("</a></td>\n")
indexFile.write("\t\t\t\t<td id=\"line-nos\">")
indexFile.write("<a href=\"#");
indexFile.write(str(lineNo))
indexFile.write("\" onclick=\"populatePre('"+ normRelativeFilePath+"')\">");
indexFile.write(str(lineNo))
indexFile.write("</a> ");
l += 1
k += 1
elif(l==1 and k==lenLineNoArray):
indexFile.write("\t\t\t"+"<tr>"+"\n\t\t\t\t"+"<td id=\"word\">")
indexFile.write(word)
indexFile.write("</td>\n")
indexFile.write("\t\t\t\t<td id=\"file-name\"><a href=\"#codeTable\" onclick=\"populatePre('"+normRelativeFilePath+"')\">")
indexFile.write(baseFileName)
indexFile.write("</a></td>\n")
indexFile.write("\t\t\t\t<td id=\"line-nos\">")
indexFile.write("<a href=\"#");
indexFile.write(str(lineNo))
indexFile.write("\" onclick=\"populatePre('"+normRelativeFilePath+"')\">");
indexFile.write(str(lineNo))
indexFile.write("</a>");
indexFile.write("</td>\n")
indexFile.write("\t\t\t</tr>\n")
l = 1
k = 1
elif(l%noEntries!=0 and k!=lenLineNoArray):
indexFile.write("<a href=\"#");
indexFile.write(str(lineNo))
indexFile.write("\" onclick=\"populatePre('"+ normRelativeFilePath+"')\">");
indexFile.write(str(lineNo))
indexFile.write("</a> ");
l += 1
k += 1
elif(l%noEntries!=0 and k==lenLineNoArray):
indexFile.write("<a href=\"#");
indexFile.write(str(lineNo))
indexFile.write("\" onclick=\"populatePre('"+ normRelativeFilePath+"')\">");
indexFile.write(str(lineNo))
indexFile.write("</a>");
indexFile.write("</td>\n")
indexFile.write("\t\t\t</tr>\n")
l += 1
k = 1
elif(l%noEntries==0):
indexFile.write("<a href=\"#");
indexFile.write(str(lineNo))
indexFile.write("\" onclick=\"populatePre('"+ normRelativeFilePath+"')\">");
indexFile.write(str(lineNo))
indexFile.write("</a>");
indexFile.write("</td>\n")
indexFile.write("\t\t\t</tr>\n")
l = 1
k += 1
indexFile.write("</table>")
indexFile.write(htmlStringEnd)
'''
Get List of all files in a directory and its subdirectories
Input: Root Directory
Output: [[listOfTextFiles],[listOfHTMLFiles]]
'''
def getAllFilesinDir(directory):
print('Reading from ', end='')
print(directory)
h = 0
t = 0
f = 0
textFilesToTraverse=[]
htmlFilesToTraverse=[]
filesToTraverse=[]
# traverse root directory, and list directories as dirs and files as files
for root, dirs, files in os.walk(directory):
for file in files:
mime = mimetypes.guess_type(file)
typeOfFile = mime[0]
if(typeOfFile is None):
pass
else:
type = typeOfFile.split('/')
if(type[0]=='text'):
if(type[1]=='plain'):
absPath = os.path.normpath(os.path.abspath(root+'/'+file))
textFilesToTraverse.append(absPath)
t=t+1
if(type[1]=='html'):
absPath = os.path.normpath(os.path.abspath(root+'/'+file))
htmlFilesToTraverse.append(absPath)
filesToTraverse.append(textFilesToTraverse)
filesToTraverse.append(htmlFilesToTraverse)
return filesToTraverse