123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107 |
- #!/usr/bin/env python
- import os
- def dirbasename(s, sep='/'):
- if s[-1] == sep:
- s = s[:-1]
- if not sep in s:
- return '.', s
- else:
- idx = s.rindex(sep)
- return s[:idx], s[idx+1:]
- def defaultFilt(dirEntry):
- pathname = dirEntry.path
- filename = dirEntry.name
- if filename[0] in '_.':
- return (False, None, None)
- if not dirEntry.is_dir():
- if len(filename) > 4 and filename[-4:] in ['.rst', '.htm']:
- pathname = pathname[:-4] + '.htm'
- filename = filename[:-4]
- elif len(filename) > 3 and filename[-3:] == '.md':
- pathname = pathname[:-3] + '.htm'
- filename = filename[:-3]
- else:
- return (False, None, None)
- if filename == "index":
- return (False, None, None)
- else:
- pathname = pathname + '/'
- filename = filename + '/'
- if pathname[0:2] == './':
- pathname = pathname[2:]
- return (True, pathname, filename)
-
- def navgen(base, path, filterConv = None, sub=None):
- curdir, fn = dirbasename(path)
- basePathLen = len(base)
- if len(curdir) < len(base):
- curdir = base
- fn = ""
- files = []
- for d in os.scandir(curdir):
- if filterConv is not None:
- visible, newpath, newname = filterConv(d)
- if not visible:
- continue
- else:
- newpath = d.path
- newname = d.name
- if newpath[0:basePathLen] == base:
- newpath = newpath[basePathLen:]
- if newpath[0] == '/':
- newpath = newpath[1:]
- thisPath = (d.name == fn)
- if thisPath:
- sublist = sub
- else:
- sublist = None
- files.append((newpath, newname, thisPath, sublist))
- if curdir == base:
- return files
- else:
- return navgen(base, curdir, filterConv, files)
- def emitPath(path, name, thisPath):
- if thisPath:
- sym = '»'
- append = ' class="thisPage"'
- else:
- sym = '›'
- append = ''
- print('<li><a href="/{}"{}>{} {}</a></li>'.format(path, append, sym, name))
- def printnav(navlist):
- print('<ul>')
- for (Path, Name, thisPath, Sub) in navlist:
- if thisPath:
- emitPath(Path, Name, True)
- if Sub is not None:
- print('<li>')
- printnav(Sub)
- print('</li>')
- else:
- emitPath(Path, Name, False)
- print('</ul>')
- if __name__ == '__main__':
- import sys
- nav = navgen(sys.argv[1], sys.argv[2], defaultFilt)
- printnav(nav)
|