]> Sergey Matveev's repositories - public-inbox.git/blob - Documentation/mknews.perl
wwwstream: reduce object graph depth
[public-inbox.git] / Documentation / mknews.perl
1 #!/usr/bin/perl -w
2 # Copyright (C) 2019-2020 all contributors <meta@public-inbox.org>
3 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 # Generates NEWS, NEWS.atom, and NEWS.html files using release emails
5 # this uses unstable internal APIs of public-inbox, and this script
6 # needs to be updated if they change.
7 use strict;
8 use PublicInbox::Eml;
9 use PublicInbox::View;
10 use PublicInbox::MsgTime qw(msg_datestamp);
11 use PublicInbox::MID qw(mids mid_escape);
12 END { $INC{'Plack/Util.pm'} and warn "$0 should not have loaded Plack::Util\n" }
13 my $dst = shift @ARGV or die "Usage: $0 <NEWS|NEWS.atom|NEWS.html>";
14
15 # newest to oldest
16 my @releases = @ARGV;
17 my $dir = 'Documentation/RelNotes';
18 my $base_url = 'https://public-inbox.org/meta';
19 my $html_url = 'https://public-inbox.org/NEWS.html';
20 my $atom_url = 'https://public-inbox.org/NEWS.atom';
21 my $addr = 'meta@public-inbox.org';
22
23 my $latest = shift(@releases) or die 'no releases?';
24 my $mtime;
25 my $mime_latest = release2mime($latest, \$mtime);
26 my $tmp = "$dst+";
27 my $out;
28 if ($dst eq 'NEWS') {
29         open $out, '>:encoding(utf8)', $tmp or die;
30         mime2txt($out, $mime_latest);
31         for my $v (@releases) {
32                 print $out "\n" or die;
33                 mime2txt($out, release2mime($v));
34         }
35 } elsif ($dst eq 'NEWS.atom' || $dst eq 'NEWS.html') {
36         open $out, '>', $tmp or die;
37         my $ibx = My::MockObject->new(
38                 description => 'public-inbox releases',
39                 over => undef,
40                 search => 1, # for WwwStream:_html_top
41                 base_url => "$base_url/",
42         );
43         $ibx->{-primary_address} = $addr;
44         my $ctx = {
45                 -inbox => $ibx,
46                 -upfx => "$base_url/",
47                 -hr => 1,
48         };
49         if ($dst eq 'NEWS.html') {
50                 html_start($out, $ctx);
51                 mime2html($out, $mime_latest, $ctx);
52                 while (defined(my $v = shift(@releases))) {
53                         mime2html($out, release2mime($v), $ctx);
54                 }
55                 html_end($out, $ctx);
56         } elsif ($dst eq 'NEWS.atom') {
57                 my $astream = atom_start($out, $ctx, $mtime);
58                 for my $v (reverse(@releases)) {
59                         mime2atom($out, $astream, release2mime($v), $ctx);
60                 }
61                 mime2atom($out, $astream, $mime_latest, $ctx);
62                 print $out '</feed>' or die;
63         } else {
64                 die "BUG: Unrecognized $dst\n";
65         }
66 } else {
67         die "Unrecognized $dst\n";
68 }
69
70 close($out) or die;
71 utime($mtime, $mtime, $tmp) or die;
72 rename($tmp, $dst) or die;
73 exit 0;
74
75 sub release2mime {
76         my ($release, $mtime_ref) = @_;
77         my $f = "$dir/$release.eml";
78         open(my $fh, '<', $f) or die "open($f): $!";
79         my $mime = PublicInbox::Eml->new(\(do { local $/; <$fh> }));
80         # Documentation/include.mk relies on mtimes of each .eml file
81         # to trigger rebuild, so make sure we sync the mtime to the Date:
82         # header in the .eml
83         my $mtime = msg_datestamp($mime->header_obj);
84         utime($mtime, $mtime, $fh) or warn "futimes $f: $!";
85         $$mtime_ref = $mtime if $mtime_ref;
86         $mime;
87 }
88
89 sub mime2txt {
90         my ($out, $mime) = @_;
91         my $title = $mime->header('Subject');
92         $title =~ s/^\s*\[\w+\]\s*//g; # [ANNOUNCE] or [ANN]
93         my $dtime = msg_datestamp($mime->header_obj);
94         $title .= ' - ' . PublicInbox::View::fmt_ts($dtime) . ' UTC';
95         print $out $title, "\n" or die;
96         my $uline = '=' x length($title);
97         print $out $uline, "\n\n" or die;
98
99         my $mid = mids($mime)->[0];
100         print $out 'Link: ', $base_url, '/', mid_escape($mid), "/\n\n" or die;
101         print $out $mime->body_str or die;
102 }
103
104 sub mime2html {
105         my ($out, $eml, $ctx) = @_;
106         my $smsg = bless {}, 'PublicInbox::Smsg';
107         $smsg->populate($eml);
108         print $out PublicInbox::View::eml_entry($ctx, $smsg, $eml, 1) or die;
109 }
110
111 sub html_start {
112         my ($out, $ctx) = @_;
113         require PublicInbox::WwwStream;
114         $ctx->{www} = My::MockObject->new(style => '');
115         my $www_stream = PublicInbox::WwwStream::init($ctx);
116         print $out $www_stream->_html_top, '<pre>' or die;
117 }
118
119 sub html_end {
120         print $out <<EOF or die;
121         git clone $PublicInbox::WwwStream::CODE_URL
122 </pre></body></html>
123 EOF
124 }
125
126 sub atom_start {
127         my ($out, $ctx, $mtime) = @_;
128         require PublicInbox::WwwAtomStream;
129         # WwwAtomStream stats this dir for mtime
130         my $astream = PublicInbox::WwwAtomStream->new($ctx);
131         delete $astream->{emit_header};
132         my $ibx = $ctx->{-inbox};
133         my $title = PublicInbox::WwwAtomStream::title_tag($ibx->description);
134         my $updated = PublicInbox::WwwAtomStream::feed_updated($mtime);
135         print $out <<EOF or die;
136 <?xml version="1.0" encoding="us-ascii"?>
137 <feed
138 xmlns="http://www.w3.org/2005/Atom"
139 xmlns:thr="http://purl.org/syndication/thread/1.0">$title<link
140 rel="alternate"
141 type="text/html"
142 href="$html_url"/><link
143 rel="self"
144 href="$atom_url"/><id>$atom_url</id>$updated
145 EOF
146         $astream;
147 }
148
149 sub mime2atom  {
150         my ($out, $astream, $eml, $ctx) = @_;
151         my $smsg = bless {}, 'PublicInbox::Smsg';
152         $smsg->populate($eml);
153         if (defined(my $str = $astream->feed_entry($smsg, $eml))) {
154                 print $out $str or die;
155         }
156 }
157 package My::MockObject;
158 use strict;
159 our $AUTOLOAD;
160
161 sub new {
162         my ($class, %values) = @_;
163         bless \%values, $class;
164 }
165
166 sub AUTOLOAD {
167         my ($self) = @_;
168         my $attr = (split(/::/, $AUTOLOAD))[-1];
169         $self->{$attr};
170 }
171
172 1;