def write_output(self, rawdog, config): """Write out the current output file.""" bits = rawdog.get_main_template_bits(config) bits["items"] = self.f.getvalue() bits["num_items"] = str(len(rawdog.articles.values())) bits["dated_output_pages"] = self.generate_list() bits["dated_output_calendar"] = self.generate_calendar() bits["dated_output_calendars"] = self.generate_calendars() s = fill_template(rawdog.get_template(config), bits) fn = self.current_fn config.log("dated-output writing output file: ", fn) f = open(fn + ".new", "w") write_ascii(f, s, config) f.close() os.rename(fn + ".new", fn)
def output_write_files(rawdog, config, articles, article_dates): config.log("paged-output starting") outputfile = config["outputfile"] i = outputfile.rfind('.') if i != -1: prefix = outputfile[:i] suffix = outputfile[i:] else: prefix = outputfile suffix = "" chunks = [] while articles != []: chunks.append(articles[:articles_per_page]) articles = articles[articles_per_page:] fns = [] for i in range(len(chunks)): if i == 0: fn = prefix + suffix else: fn = "%s%d%s" % (prefix, i, suffix) fns.append(fn) for i in range(len(chunks)): fn = fns[i] date = format_time(article_dates[chunks[i][0]], config) config.log("paged-output writing ", fn, " (", date, ")") f = StringIO() dw = DayWriter(f, config) for article in chunks[i]: dw.time(article_dates[article]) rawdog.write_article(f, article, config) dw.close() bits = rawdog.get_main_template_bits(config) bits["items"] = f.getvalue() bits["num_items"] = str(len(rawdog.articles.values())) f = StringIO() f.write('<ul class="paged_output_pages">\n') for j in range(len(chunks)): latest_date = max([article_dates[a] for a in chunks[j]]) f.write('<li>') if i != j: f.write('<a href="' + os.path.basename(fns[j]) + '">') f.write(format_time(latest_date, config)) if i != j: f.write('</a>') f.write('</li>\n') f.write('</ul>\n') bits["paged_output_pages"] = f.getvalue() f = StringIO() def make_link(rel, page): if page >= 0 and page < len(chunks): f.write('<link rel="%s" href="%s">\n' % (rel, os.path.basename(fns[page]))) make_link("next", i - 1) make_link("prev", i + 1) bits["paged_output_head"] = f.getvalue() s = fill_template(rawdog.get_template(config), bits) f = open(fn + ".new", "w") write_ascii(f, s, config) f.close() os.rename(fn + ".new", fn) config.log("paged-output done") return False
def output_write_files(rawdog, config, articles, article_dates): f_hdr = StringIO() rawdoglib.plugins.call_hook("output_items_begin", rawdog, config, f_hdr) remaining = articles[:] groups = {} other_count = 0 for url, feed in rawdog.feeds.items(): if (feed.args.has_key("define_group")): group = feed.args["define_group"] group = group.replace("_", " ") if (groups.has_key(group)): groups[group]["feeds"].append(feed) else: groups[group] = {"count" : 0, "feeds" : [feed]} f = StringIO() for (key, group) in groups.items(): dw = DayWriter(f, config) f.write("<hr>\n") f.write('<h1><a name="' + key + '">' + key + '</a></h1>\n') for article in remaining[:]: for feed in group["feeds"]: if article.feed == feed.url: if not rawdoglib.plugins.call_hook("output_items_heading", rawdog, config, f, article, article_dates[article]): dw.time(article_dates[article]) rawdog.write_article(f, article, config) remaining.remove(article) group["count"] += 1 dw.close() f.write("<hr>\n") if remaining: dw = DayWriter(f, config) f.write('<h1><a name="Other">Other</a></h1>\n') for article in remaining: if not rawdoglib.plugins.call_hook("output_items_heading", rawdog, config, f, article, article_dates[article]): dw.time(article_dates[article]) rawdog.write_article(f, article, config) other_count += 1 f_hdr.write("<ul>\n") for (key, group) in groups.items(): if group["count"]: f_hdr.write('<li><a href="#%(key)s">%(key)s (%(count)d items)</a></li>\n' % {'key' : key, 'count' : group["count"]}) if other_count: f_hdr.write('<li><a href="#%(key)s">%(key)s (%(count)d items)</a></li>\n' % {'key' : 'Other', 'count' : other_count}) f_hdr.write("</ul>\n") rawdoglib.plugins.call_hook("output_items_end", rawdog, config, f) bits = rawdog.get_main_template_bits(config) bits["items"] = f_hdr.getvalue() + f.getvalue() bits["num_items"] = str(len(rawdog.articles)) rawdoglib.plugins.call_hook("output_bits", rawdog, config, bits) s = fill_template(rawdog.get_template(config), bits) outputfile = config["outputfile"] if outputfile == "-": write_ascii(sys.stdout, s, config) else: config.log("Writing output file: ", outputfile) f = open(outputfile + ".new", "w") write_ascii(f, s, config) f.close() os.rename(outputfile + ".new", outputfile) return False