diff --git a/distribusi/cli.py b/distribusi/cli.py index ac63c48..b55fa80 100644 --- a/distribusi/cli.py +++ b/distribusi/cli.py @@ -84,6 +84,11 @@ def build_argparser(): help="Append index.html to menu items to aid navigation", action="store_true") + parser.add_argument( + '--unfolding', + help="Enable unfolding detecting .unfolding", + action="store_true") + return parser diff --git a/distribusi/distribusi.py b/distribusi/distribusi.py index 67ec604..2882524 100644 --- a/distribusi/distribusi.py +++ b/distribusi/distribusi.py @@ -108,6 +108,90 @@ def write_index(args,index, html, html_head, html_footer): f.write(html_footer) +def render_dir(args, root): + html = [] + + for entry in os.listdir(root): + + if args.no_hidden: + if entry.startswith('.'): + continue + + name = entry + lv = root.split("/") + relative = lv[len(lv) - 1] + relative_path = "./{}/{}".format(relative, name) + + if os.path.isfile(root + '/' + entry): + + if 'index.html' not in name: + full_path = os.path.join(root, name) + mime = MIME_TYPE.from_file(full_path) + # example: MIME plain/text becomes 'type' plain 'subtype' text + type_, subtype = mime.split('/') + + caption = name + + if args.verbose: + print('Found file in dir ', name, 'as', mime) + + if type_ in FILE_TYPES: + a = FILE_TYPES[type_].format(relative_path, caption) + + # expansion for different kind of text files + if type_ == 'text': + if name.endswith('.html') or subtype == 'html': + subtype = 'html' + # what types of text files to expand + a = '
{}
'.format(name, open(full_path).read()) + elif subtype in CODE_TYPES or name.endswith('.txt'): + # if the plain text is code, + # which types do we wrap in pre-tags? + a = "
" + open(full_path).read() + "
" + else: + subtype = subtype + ' unkown-file' + a = "{}" + # a = FILE_TYPES[type_] + + if type_ == 'image': + a = FILE_TYPES[type_].format(relative_path, caption) + if args.thumbnail: + a = thumbnail(relative_path, relative_path, args) + if args.no_filenames: + caption = "" + if args.captions: + caption = caption(relative_path) + a = FILE_TYPES[type_].format(relative_path, caption) + + if subtype in SUB_TYPES: + a = SUB_TYPES[subtype] + + if type_ not in FILE_TYPES and subtype not in SUB_TYPES: + # catch exceptions not yet defined in FILE_TYPES or SUB_TYPES + a = "{}" + if args.verbose: + message = 'not in list of file types, adding as plain href: \n' + print(type_, subtype, message, name) + subtype = subtype + ' unkown-file' + + a = a.replace('{}', relative_path) + html.append(div(args, type_, subtype, a, name)) + + if os.path.isdir(root + '/' + entry): + + if args.menu_with_index: + a = "{}".replace('{}', relative_path) + else: + a = "{}/".replace('{}', relative_path) + html.append(div(args, 'dir', 'dir', a, 'folder')) + # html.insert(0, div(args, 'dir', 'dir', a, 'folder')) + + result = "" + for line in html: + result += line + "\n" + return result + + def distribusify(args, directory): # noqa for root, dirs, files in os.walk(directory): @@ -196,8 +280,12 @@ def distribusify(args, directory): # noqa a = "{}".replace('{}', name) else: a = "{}/".replace('{}', name) - - html.insert(0, div(args, 'dir', 'dir', a, 'folder')) + if args.unfolding: + rd = render_dir(args, "{}/{}".format(root, name)) + h = '
\n{}\n{}
'.format(name, a, rd) + html.append(h) + else: + html.insert(0, div(args, 'dir', 'dir', a, 'folder')) index = os.path.join(root, 'index.html') if os.path.exists(index): diff --git a/run.sh b/run.sh index dcd7875..bbf86de 100755 --- a/run.sh +++ b/run.sh @@ -1,4 +1,4 @@ #!/bin/bash pip install -r py3/requirements.txt -python run.py -d ./data/ -nf -s custom.css --no-hidden -v +python run.py -d ./data/ -nf -s custom.css --no-hidden --unfolding