def run_indices(threads, args): "builds the index files" single_template = load_template(args.templates + "index.html.jst") grouped_template = load_template(args.templates + "grouped-index.html.jst") messages = [ NS([("from", simple_from(m["headers"]["from"]))] + list(m.items())) for _, messages in threads for m in messages ] # build simple indices for index in INDICES: data = dict(title=index.name(args), **index.partitioner(messages)) if "groups" in data: html = grouped_template.expand(data) else: html = single_template.expand(data) with open(path.join(args.output, index.filename(args)), mode="w", encoding="utf-8") as out: out.write(html) partials = {} if args.partials.isdir(): partials = PartialManager(args.partials) index_template = load_template(args.templates + "thread-index.html.jst", more_formatters=partials) partial_template = load_template(args.partials + "thread.html.jst", more_formatters=partials) # flatten the threads messages = [message for _, messages in threads for message in messages] def preprocess(message): from_ = simple_from(message.headers.get("from", "")) or None return dict(list(message.items()) + [("from", from_)]) # recursively render the threads here so the expanding doesn't # hit the recursion limit def render_thread(thread): return partial_template.expand( message=preprocess(thread.message), children=[render_thread(child) for child in thread.children] ) with open(path.join(args.output, args.thread_index), mode="w", encoding="utf-8") as out: out.write( index_template.expand( title=args.thread_title, threads=[ dict(message=preprocess(t.message), children=[render_thread(c) for c in t.children]) for t in normalize_threads(build_threads(messages)) ], ) )
def preprocess(message): from_ = simple_from(message.headers.get("from", "")) or None return dict(list(message.items()) + [("from", from_)])