Skip to content

Instantly share code, notes, and snippets.

@scturtle
Last active December 10, 2015 01:19
Show Gist options
  • Save scturtle/4357467 to your computer and use it in GitHub Desktop.
Save scturtle/4357467 to your computer and use it in GitHub Desktop.
scan and generate static html files
#!/bin/env python3
import sys
if sys.version_info.major == 2:
print ('SORRY: scan.py works under python 3.')
exit(1)
from pathlib import Path
struct = '''\
<!doctype html>
<html><head>
<meta charset=utf-8>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<link href="http://netdna.bootstrapcdn.com/bootstrap/3.0.2/css/\
bootstrap.min.css" rel="stylesheet">
<style type="text/css">
body{{margin:auto;width:250px;font-size:18px;}}
ul{{list-style-type:none;}}
.sm{{color:gray;font-size:16px}}
span{{margin:5px 5px 0 0 !important;}}
</style>
<title>{path}</title>
</head><body>
<h4><span class='sm'>Directory list of</span> {path}</h4>
<hr><ul>
{lis}
</ul><hr>
<span class='sm'>
Powered by <a href='https://gist.github.com/4357467'>scan.py</a>
</span>
</body></html>'''
folderi = '<span class="glyphicon glyphicon-folder-close"></span>'
filei = '<span class="glyphicon glyphicon-file"></span>'
li = '<li>{}<a href="{}">{}</a>'
IGNORE_LIST = {'scan.py', 'dockbox', 'index.html', '.dropbox',
'.dropbox.attr', 'desktop.ini'}
def scan(cur=Path('.')):
lis = []
if cur != Path('.'):
lis.append(li.format(folderi, '../index.html', '..'))
for i in sorted(cur.iterdir()):
if i.name in IGNORE_LIST:
continue
url = name = i.name
if i.is_dir():
scan(i)
icon = folderi
name += '/'
url += '/index.html'
else:
icon = filei
lis.append(li.format(icon, url, name))
idx = cur / 'index.html'
if idx.exists() and 'scan.py' not in idx.open(encoding='utf8').read():
return
else:
lis = '\n'.join(lis)
open(str(idx), 'w', encoding='utf8').write(
struct.format(path=str(cur).replace('\\', '/'), lis=lis))
if __name__ == '__main__':
scan()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment