Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
gen4idx
# -*- coding: utf-8 -*-
#!/usr/bin/env python
'''自动生成所有目录的 index.html
Version:
+ 13.4.18 增补meta 停息,以免乱码
+ 13.4.17 增补多层目录中的目录/文件链接 (7牛没有目录概念)
+ 13.4.11 可用
'''
import os
import sys
index_count = 0
pass_names = [".DS_Store", ".json", "index.html"]
def genall(dir, imgPath):
global index_count
footer = """<style type="text/css">
a, a:active {text-decoration: none; color: blue;}
a:visited {color: #48468F;}
a:hover, a:focus {text-decoration: underline; color: red;}
body {background-color: #F5F5F5;}
h2 {margin-bottom: 12px;}
table {margin-left: 12px;}
th, td { font: 90% monospace; text-align: left;}
th { font-weight: bold; padding-right: 14px; padding-bottom: 3px;}
td {padding-right: 14px;}
td.s, th.s {text-align: right;}
div.list { background-color: white; border-top: 1px solid #646464; border-bottom: 1px solid #646464; padding-top: 10px; padding-bottom: 14px;}
div.foot { font: 90% monospace; color: #787878; padding-top: 4px;}
</style>
<hr/>
<div class="foot">
Powerded by: <a href="http://qiniu.com/">七牛云</a>
<br />本站内容采用<a href="http://creativecommons.org/licenses/by-sa/3.0/cn/">知识共享署名-相同方式共享 3.0 中国大陆许可协议</a>进行许可.
</div>
"""
tpl_idx = """<!DOCTYPE html>
<html lang="zh-CN">
<head><meta charset="UTF-8" />
<meta name="generator" content="gen4idx.py v13.4.18" />
<title>Index of %(crt_root)s </title></head>
<body>
<h1>Index of %(crt_root)s</h1><hr>
<pre><a href="%(crt_root)s/..">../</a>
%(crt_files)s
</pre>
%(footer)s
</body></html>
"""
crt_root = dir[1:]
crt_dir = ""
crt_file = ""
imgPaths = imgPath.split(",")
for file_obj in os.listdir(dir):
file = os.path.join(dir, file_obj)
if os.path.isdir(file):
if file_obj not in imgPaths:
pass
else:
crt_dir += "<a href='%s'>%s/</a>\n" % (file[1:], file_obj)
genall(file, imgPath)
else:
if file_obj in pass_names:
pass
else:
fstat = os.lstat(file)
fsize = 1000000 < fstat.st_size and "%.2fM" % (
fstat.st_size / 1000000.0) or "%.2fk" % (fstat.st_size /
1024.0)
crt_file = "%s <a href='%s'>\n%- 79s % 20s" % (
crt_file, file_obj, "%s</a>" % file_obj, fsize)
pass
#print file
print (">>>", dir[1:])
crt_files = "%s%s" % (crt_dir, crt_file)
index_count += 1
open("%s/index.html" % dir, "w").write(tpl_idx % locals())
if __name__ == '__main__':
if 3 != len(sys.argv) :
print ('''Usage:
gen4idx.py /path/2/gen img,img1''')
else:
startPath = sys.argv[1]
imgPath = sys.argv[2]
genall(startPath, imgPath)
print ("gen %s index.html"% index_count)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment