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