Skip to content

Commit a5a2b25

Browse files
add theme argument and template argument to use file path
1 parent 1dfac30 commit a5a2b25

File tree

1 file changed

+28
-19
lines changed

1 file changed

+28
-19
lines changed

index_generator/__main__.py

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@ def main():
1616
parser = argparse.ArgumentParser()
1717
parser.add_argument('--version', '-V', action='store_true', default=False,
1818
help='Print version infomation and quit.')
19-
parser.add_argument('--template', '-t', type=str, default='templates/default',
20-
help='Select template to generate html.')
19+
parser.add_argument('--theme', '-t', type=str, default='default', choices=['default'],
20+
help='Select builtin theme to generate html.')
21+
parser.add_argument('--template', '-T', type=str, default='', help='Custom template to generate html.')
2122
parser.add_argument('--no-recursive', action='store_true', default=False, help='Do not generate recursively.')
2223
parser.add_argument('--name', '-n', type=str, default='index.html',
2324
help='Default output filename.')
@@ -41,16 +42,24 @@ def app(args):
4142
sys.exit(0)
4243
if args.no_recursive:
4344
os.chdir(args.path)
44-
generate_once(args.template, '.', os.listdir('.'), args.name, args.print, base=args.root, human=args.human)
45+
generate_once(args.theme, '.', os.listdir('.'), args.name, args.print, base=args.root, human=args.human,
46+
template=args.template)
4547
else:
46-
generate_recursively(args.template, args.path, args.name, args.print, args.depth, base=args.root, human=args.human)
48+
generate_recursively(args.theme, args.path, args.name, args.print, args.depth, base=args.root, human=args.human,
49+
template=args.template)
4750

4851

49-
def generate_once(template_dir, root, files, name, if_print, base='/', human=False):
50-
environment = jinja2.Environment(
51-
loader=jinja2.PackageLoader('index_generator', template_dir),
52-
autoescape=jinja2.select_autoescape(['html', 'htm'])
53-
)
52+
def generate_once(theme, root, files, name, if_print, base='/', human=False, template=''):
53+
if not template:
54+
environment = jinja2.Environment(
55+
loader=jinja2.PackageLoader('index_generator', 'templates/' + theme),
56+
autoescape=jinja2.select_autoescape(['html', 'htm'])
57+
)
58+
else:
59+
environment = jinja2.Environment(
60+
loader=jinja2.FileSystemLoader(template),
61+
autoescape=jinja2.select_autoescape(['html', 'htm'])
62+
)
5463
template = environment.get_template(name)
5564

5665
entries = list(map(lambda f1: Entry(f1, root, base=base, human=human), files))
@@ -61,20 +70,20 @@ def generate_once(template_dir, root, files, name, if_print, base='/', human=Fal
6170
if entry.name in indexIgnore:
6271
continue
6372
filelist.append({
64-
'path': entry.path,
65-
'name': entry.name,
66-
'size': entry.size,
73+
'path': entry.path,
74+
'name': entry.name,
75+
'size': entry.size,
6776
'modified': time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(entry.modified)),
68-
'mime': entry.mime,
69-
'isDir': entry.isDir
77+
'mime': entry.mime,
78+
'isDir': entry.isDir
7079
})
7180
html = template.render(ig={
7281
'root': base + root.lstrip('.*/'),
7382
'files': filelist,
7483
'generator': {
75-
'name': APP_NAME,
84+
'name': APP_NAME,
7685
'version': APP_VERSION,
77-
'url': APP_URL
86+
'url': APP_URL
7887
}
7988
})
8089

@@ -85,7 +94,7 @@ def generate_once(template_dir, root, files, name, if_print, base='/', human=Fal
8594
print(html, file=f)
8695

8796

88-
def generate_recursively(template_dir, path, name, if_print, max_depth=0, base='/', human=False):
97+
def generate_recursively(theme, path, name, if_print, max_depth=0, base='/', human=False, template=''):
8998
os.chdir(path)
9099
for root, dirs, files in os.walk('.'):
91100
if max_depth != 0 and root.count(os.sep) >= max_depth:
@@ -97,12 +106,12 @@ def generate_recursively(template_dir, path, name, if_print, max_depth=0, base='
97106

98107
if if_print:
99108
print('-----------------------------------------')
100-
print('Path: '+root)
109+
print('Path: ' + root)
101110
print('dirs: {}'.format(dirs))
102111
print('files: {}'.format(files))
103112
print('-----------------------------------------')
104113

105-
generate_once(template_dir, root, dirs+files, name, if_print, base=base, human=human)
114+
generate_once(theme, root, dirs + files, name, if_print, base=base, human=human)
106115

107116

108117
if __name__ == '__main__':

0 commit comments

Comments
 (0)