Created
November 2, 2014 02:50
-
-
Save aheadlead/30c3f311be3ad287b186 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
# Static Site Generator | |
# aheadlead | |
# 2014-01-30 | |
import os | |
import sys | |
from urllib import quote | |
site_title = u'aheadlead\'s sheetbook' | |
footer = u'<p class="footer">维护者邮箱:aheadlead@dlifep.com</p> <p class="footer">本站已拒绝搜索引擎收录。</p>' | |
reload(sys) | |
sys.setdefaultencoding('utf-8') | |
src_dir = './' | |
def get_html(foo): | |
raw = u'''<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>%s</title> <style type="text/css"> body { font: 100%%/1.4 Verdana, Arial, Helvetica, sans-serif; background-color: #42413C; margin: 0; padding: 0; color: #000; } ul, ol, dl { padding: 0; margin: 0; } h1, h2, h3, h4, h5, h6, p { margin-top: 0; padding-right: 15px; padding-left: 15px; } a img { border: none; } a:link { color: #42413C; text-decoration: none; } a:visited { color: #6E6C64; text-decoration: none; } a:hover, a:active, a:focus { color: #09C; text-decoration: none; } .container { width: 960px; background-color: #FFF; margin: 0 auto; } .content { padding: 10px 0; } .fltrt { float: right; margin-left: 8px; } .fltlft { float: left; margin-right: 8px; } .clearfloat { clear:both; height:0; font-size: 1px; line-height: 0px; } .footer { color: #DDD; text-decoration: none; } </style> </head> <script src="http://s13.cnzz.com/stat.php?id=5806937&web_id=5806937" language="JavaScript"></script> <body> <div class="container"> <div class="content"> <h1>%s</h1> <p> %s </p>%s</div> </div> </body> </html>''' | |
item_template = u'<font color=#CCC>%s</font> <a href="%s">%s</a><br>' | |
item = u'' | |
for i in foo: | |
item = item + \ | |
(item_template % (i['type'], \ | |
quote(i['name']) + ('/index.html' if i['type'] == 'DIR' else ''), \ | |
i['name'])) | |
raw = (raw % (site_title, \ | |
site_title, \ | |
item, \ | |
footer)) | |
return raw | |
def gen(src): | |
foo = [] | |
list_dirs = os.walk(src) | |
html = file(os.path.join(src, 'index.html'), 'w') | |
for root, dirs, files in list_dirs: | |
if root != src: | |
continue | |
if src != src_dir: | |
foo.append({'name':'..', 'type':'DIR'}) | |
for d in dirs: | |
foo.append({'name':d, 'type':'DIR'}) | |
gen(os.path.join(src, d)) | |
for f in files: | |
if f != 'index.html' and f[0] != '.': | |
foo.append({'name':f, 'type':'FILE'}) | |
html.write(get_html(foo)) | |
html.close(); | |
return | |
gen(src_dir) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment