- my ($class, $args) = @_;
- require XML::Atom::SimpleFeed;
- require POSIX;
- my $max = $args->{max} || MAX_PER_PAGE;
-
- my $feed_opts = get_feedopts($args);
- my $addr = $feed_opts->{address};
- $addr = $addr->[0] if ref($addr);
- my $feed = XML::Atom::SimpleFeed->new(
- title => $feed_opts->{description} || "unnamed feed",
- link => $feed_opts->{url} || "http://example.com/",
- link => {
- rel => 'self',
- href => $feed_opts->{atomurl} ||
- "http://example.com/atom.xml",
- },
- id => 'mailto:' . ($addr || 'public-inbox@example.com'),
- updated => POSIX::strftime(DATEFMT, gmtime),
- );
-
- my $git = PublicInbox::GitCatFile->new($args->{git_dir});
- each_recent_blob($args, sub {
- my ($add) = @_;
- add_to_feed($feed_opts, $feed, $add, $git);
+ my ($ctx) = @_;
+ sub { emit_atom($_[0], $ctx) };
+}
+
+sub generate_thread_atom {
+ my ($ctx) = @_;
+ sub { emit_atom_thread($_[0], $ctx) };
+}
+
+sub generate_html_index {
+ my ($ctx) = @_;
+ sub { emit_html_index($_[0], $ctx) };
+}
+
+# private subs
+
+sub title_tag {
+ my ($title) = @_;
+ $title =~ tr/\t\n / /s; # squeeze spaces
+ # try to avoid the type attribute in title:
+ $title =~ ascii_html($title);
+ my $type = index($title, '&') >= 0 ? "\ntype=\"html\"" : '';
+ "<title$type>$title</title>";
+}
+
+sub atom_header {
+ my ($feed_opts, $title) = @_;
+
+ $title = title_tag($feed_opts->{description}) unless (defined $title);
+
+ qq(<?xml version="1.0" encoding="us-ascii"?>\n) .
+ qq{<feed\nxmlns="http://www.w3.org/2005/Atom">} .
+ qq{$title} .
+ qq(<link\nrel="alternate"\ntype="text/html") .
+ qq(\nhref="$feed_opts->{url}"/>) .
+ qq(<link\nrel="self"\nhref="$feed_opts->{atomurl}"/>) .
+ qq(<id>mailto:$feed_opts->{id_addr}</id>);
+}
+
+sub emit_atom {
+ my ($cb, $ctx) = @_;
+ my $fh = $cb->([ 200, ['Content-Type' => 'application/atom+xml']]);
+ my $max = $ctx->{max} || MAX_PER_PAGE;
+ my $feed_opts = get_feedopts($ctx);
+ my $x = atom_header($feed_opts);
+ my $git = $ctx->{git} ||= PublicInbox::Git->new($ctx->{git_dir});
+ each_recent_blob($ctx, sub {
+ my ($path, undef, $ts) = @_;
+ if (defined $x) {
+ $fh->write($x . feed_updated(undef, $ts));
+ $x = undef;
+ }
+ add_to_feed($feed_opts, $fh, $path, $git);