Skip to content

Commit 0de035b

Browse files
Merge branch 'master' of github.com:BruceZhang1993/index-generator
2 parents fdae0f2 + 09d4a5d commit 0de035b

File tree

3 files changed

+61
-3
lines changed

3 files changed

+61
-3
lines changed

index_generator/__main__.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
import sys
44
import time
55

6+
import os
7+
import os.path as path
8+
from datetime import datetime
9+
610
import jinja2
711
import argparse
812
import os
@@ -11,8 +15,11 @@
1115
from index_generator.models.exceptions import IndexGeneratorException
1216
from . import *
1317

18+
indexIgnore=['index.html', 'templates']
1419

1520
def main():
21+
global template
22+
global arguments
1623
parser = argparse.ArgumentParser()
1724
parser.add_argument('--version', '-V', action='store_true', default=False,
1825
help='Print version infomation and quit.')
@@ -63,6 +70,45 @@ def generate_once(template_dir, path='.', name='index.html', if_print=False):
6370
raise IndexGeneratorException(IndexGeneratorException.NOT_IMPLEMENTED)
6471

6572

73+
def generate(currentDir=''):
74+
filelist=[]
75+
dirlist=[]
76+
for file in os.listdir():
77+
if file in indexIgnore:
78+
continue
79+
if path.isdir(file):
80+
dirlist.append({
81+
'name': file,
82+
'modified': datetime.fromtimestamp(path.getmtime(file)).strftime('%Y-%m-%d %H:%M')
83+
})
84+
else:
85+
filelist.append({
86+
'name': file,
87+
'modified': datetime.fromtimestamp(path.getmtime(file)).strftime('%Y-%m-%d %H:%M'),
88+
'size': path.getsize(file)
89+
})
90+
91+
index = template.render(ig={
92+
'currentPath': currentDir,
93+
'dirs': dirlist,
94+
'files': filelist
95+
})
96+
if arguments.print:
97+
print(index)
98+
99+
with open('index.html', 'w') as f:
100+
print(index, file=f)
101+
102+
if not arguments.no_recursive and dirlist:
103+
for file in dirlist:
104+
if arguments.print:
105+
print('------------------------------------------------')
106+
os.chdir(file['name'])
107+
generate(currentDir+'/'+file['name'])
108+
109+
os.chdir('..')
110+
111+
66112
if __name__ == '__main__':
67113
try:
68114
main()

index_generator/templates/default/index.html

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,21 @@
1414
<th align="left">Last modified</th>
1515
<th align="left">Size</th>
1616
</tr>
17+
<tr>
18+
<td><a href="../">Parent directory/</a></td>
19+
<td>-</td>
20+
<td>-</td>
21+
</tr>
22+
{% for dir in ig.dirs %}
23+
<tr>
24+
<td><a href="{{ ig.currentPath }}/{{ dir.name }}">{{ dir.name }}/</a></td>
25+
<td>{{ dir.modified }}</td>
26+
<td>-</td>
27+
</tr>
28+
{% endfor %}
1729
{% for file in ig.files %}
1830
<tr>
19-
<td><a href="{{ file.path }}">{{ file.name }}</a></td>
31+
<td><a href="{{ ig.currentPath }}/{{ file.name }}">{{ file.name }}</a></td>
2032
<td>{{ file.modified }}</td>
2133
<td>{{ file.size }}</td>
2234
</tr>

index_generator/templates/default/layout.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22
<html lang="en">
33
<head>
44
<meta charset="UTF-8">
5-
<title>{% block title %}Files: {{ ig.currentPath }}{% endblock %}</title>
5+
<title>{% block title %}Files: {{ ig.currentPath }}/{% endblock %}</title>
66
{% block extra %}{% endblock %}
77
</head>
88
<body>
99
{% block head %}
10-
<h1>Index of {{ ig.currentPath }}</h1>
10+
<h1>Index of {{ ig.currentPath }}/</h1>
1111
<hr>
1212
{% block files %}
1313
{% endblock %}

0 commit comments

Comments
 (0)