1f04eb48050400946d060028b0b32947ec0c430a
2 # txt2pre --- convert my site's txt files to `pre'-based atom/rss/html
4 # Copyright (C) 2014-2021 all contributors <meta@public-inbox.org>
5 # Copyright (c) 2021 Amin Bandali <bandali@gnu.org>
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU Affero General Public License as
9 # published by the Free Software Foundation, either version 3 of the
10 # License, or (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU Affero General Public License for more details.
17 # You should have received a copy of the GNU Affero General Public License
18 # along with this program. If not, see <https://www.gnu.org/licenses/>.
20 # This simple script borrows from a script of the same name from the
21 # wonderful public-inbox project, under AGPLv3+, with additions of
35 GetOptions
('format=s' => \
$format,
40 or die("bad command line arguments\n");
43 $lang eq 'en' ?
'bandali'
44 : $lang eq 'fa' ?
'بندعلی'
47 $lang eq 'en' ?
"${author}'s personal site"
48 : $lang eq 'fa' ?
"سایت شخصی $author"
51 $lang eq 'en' ?
"notes and blog posts by $author"
52 : $lang eq 'fa' ?
"نوشتهها و بلاگ پستهای $author"
55 ($lang eq 'en') ?
'https://bndl.org'
56 : ($lang eq 'fa') ?
'https://bndl.org/fa/'
59 ($lang eq 'en') ?
"tag:bndl.org,2020:notes.$format"
60 : ($lang eq 'fa') ?
"tag:bndl.org,2020:fa/notes.$format"
64 qr{([\
('!])?\b((?:ftps?|https?|nntps?|imaps?|s?news|gopher)://
66 (?:[a-z0-9\-\._~!\$\&\';\(\)\*\+,;=:@/%]*)
67 (?:\?[a-z0-9\-\._~!\$\&\';\(\)\*\+,;=:@/%]+)?
68 (?:\#[a-z0-9\-\._~!\$\&\';\(\)\*\+,;=:@/%\?]+)?
73 "(" => qr/(\)[\.,;\+]?)\z/, # Markdown (,), Ruby (+) (, for arrays)
74 "'" => qr/('[\.,;\+]?)\z/, # Perl / Ruby
75 "!" => qr/(![\.,;\+]?)\z/, # Perl / Ruby
88 $s =~ s/([&<>])/$html_map{$1}/sge;
99 # it's fairly common to end URLs in messages with
100 # '.', ',' or ';' to denote the end of a statement;
101 # assume the intent was to end the statement/sentence
103 if (defined(my $re = $pairs{$beg})) {
104 if ($url =~ s/$re//) {
107 } elsif ($url =~ s/(\))?([\.,;])\z//) {
109 # require ')' to be paired with '('
110 if (defined $1) { # ')'
111 if (index($url, '(') < 0) {
117 } elsif ($url !~ /\(/ && $url =~ s/\)\z//) {
121 $beg . "<a href
=\"$url\">$url</a
>" . $end;
129 # atom/rss feed header and footer
130 if ($index and ($format eq 'atom' or $format eq 'rss')) {
132 my $now_iso8601 = `date -Iseconds -u | tr -d \\\\n`;
133 my $now_rfc5322 = `date -uR | tr -d \\\\n`;
134 my $atom_rel = $format eq 'atom' ? 'self' : 'alternate';
135 my $rss_rel = $format eq 'rss' ? 'self' : 'alternate';
136 my $link = $format eq 'atom' ? 'link' : 'atom:link';
140 <$link hreflang="fa
" href="https
://bndl
.org
/fa/notes
.atom
" rel="alternate
" type="application
/atom+xml" />
141 <$link hreflang
="fa" href
="https://bndl.org/fa/notes.rss" rel
="alternate" type
="application/rss+xml" />
142 <$link hreflang
="fa" href
="https://bndl.org/fa/bandali.fa.txt" rel
="alternate" type
="text/plain" />
143 <$link hreflang
="fa" href
="https://bndl.org/fa/" rel
="alternate" type
="text/html" />
144 <$link href
="https://bndl.org/notes.atom" rel
="$atom_rel" type
="application/atom+xml" />
145 <$link href
="https://bndl.org/notes.rss" rel
="$rss_rel" type
="application/rss+xml" />
146 <$link href
="https://bndl.org/bandali.txt" rel
="alternate" type
="text/plain" />
147 <$link href
="https://bndl.org" rel
="alternate" type
="text/html" />);
148 } elsif ($lang eq 'fa') {
150 <link hreflang
="en" href
="https://bndl.org/notes.atom" rel
="alternate" type
="application/atom+xml" />
151 <link hreflang
="en" href
="https://bndl.org/notes.rss" rel
="alternate" type
="application/rss+xml" />
152 <link hreflang
="en" href
="https://bndl.org/bandali.txt" rel
="alternate" type
="text/plain" />
153 <link hreflang
="en" href
="https://bndl.org" rel
="alternate" type
="text/html" />
154 <link href
="https://bndl.org/fa/notes.atom" rel
="$atom_rel" type
="application/atom+xml" />
155 <link href
="https://bndl.org/fa/notes.rss" rel
="$rss_rel" type
="application/rss+xml" />
156 <link href
="https://bndl.org/fa/bandali.fa.txt" rel
="alternate" type
="text/plain" />
157 <link href
="https://bndl.org/fa/" rel
="alternate" type
="text/html" />);
161 $out .= '<?xml version="1.0" encoding="UTF-8" ?>';
162 $out .= ($format eq 'atom') ?
qq(
163 <feed xml
:lang
="$lang" xmlns
="http://www.w3.org/2005/Atom">
164 <title
>$site_title</title
>
165 <subtitle
>$site_desc</subtitle
>
168 <updated
>$now_iso8601</updated
>)
169 : ($format eq 'rss') ?
qq(
171 xmlns
:atom
="http://www.w3.org/2005/Atom"
172 xmlns
:content
="http://purl.org/rss/1.0/modules/content/">
174 <title
>$site_title</title
>
175 <description
>$site_desc</description
>
176 <link>$site_url</link>
177 <language
>$lang</language
>
178 <lastBuildDate
>$now_rfc5322</lastBuildDate
>
179 <pubDate
>$now_rfc5322</pubDate
>
184 $out .= ($format eq 'atom') ?
'</feed>'
185 : ($format eq 'rss') ?
'</channel></rss>'
194 my $txt = do { local $/; <STDIN
> };
196 my $title = html_esc
($txt =~ /\A([^\n]+)/);
197 $title =~ s/^\s+|\s+$//g;
198 $title .= " — $author" if $title !~ /$author/;
200 my ($upd, $pub, $url) = $txt =~ /(.*)\r?\n(.*)\r?\n(.*)\r?\n?\z/;
201 ($upd) = $upd =~ /(?:updated|ویرایش): (.*)/ if $upd;
202 ($pub) = $pub =~ /(?:published|انتشار): (.*)/ if $pub;
203 $upd = $pub if (!$upd);
204 ($url) = $url =~ /(?:plain text|متن ساده): (.*)/ if $url;
205 $url = 'https://bndl.org/bandali-cv.txt'
206 if (!$url and $title =~ /curriculum vitae/);
207 $url = html_esc
($url) if $url;
209 $txt = linkify
(html_esc
($txt));
212 my $upd_iso8601 = `date -Iseconds -ud '$upd' | tr -d \\\\n` if $upd;
213 my $pub_iso8601 = `date -Iseconds -ud '$pub' | tr -d \\\\n` if $pub;
214 my $pub_rfc5322 = `date -uRd '$pub' | tr -d \\\\n` if $pub;
215 my $url_html = $url =~ s/(?:[.]$lang)?[.]txt$/.html/r if $url;
216 $url_html =~ s
|/bandali-(.*)|/$1| if $url_html;
217 my $slug = $url_html =~ s
|.*/(.*)[.]html
$|$1|r
if $url_html;
218 my $note_id = "$feed_id:$slug" if $url_html;
221 if ($format eq 'html') {
224 . qq(<html lang
="$lang") . ($lang eq 'fa'
228 <meta http
-equiv
="Content-Type"
229 content
="text/html; charset=utf-8" />\n)
230 . "<title>$title</title>\n"
231 . qq(<link rel
="icon" href
="data:,">\n)
233 ?
qq(<link rel
="alternate" href
="$url"
234 title
="plain text" type
="text/plain" />\n)
236 . (($index and $lang eq 'en')
237 ?
qq(<link rel
="alternate" href
="https://bndl.org/fa/"
238 hreflang
="fa" title
="persian" />\n)
239 : ($index and $lang eq 'fa')
240 ?
qq(<link rel
="alternate" href
="https://bndl.org/"
241 hreflang
="en" title
="english" />\n)
243 . qq(<style
>\
@media(prefers
-color
-scheme
:dark
){
244 body
{background
:#1c1c1c;color:white;}a:link{color:#acdeff;}
245 a
:visited
{color
:#f8f;}a:active{color:#e00;}})
247 ?
qq(\n\
@font-face
{font
-family
:sahel
;font
-weight
:normal
;
248 src
:local('Sahel WOL'),local('Sahel'),
249 url
('sahel.woff2')format
('woff2');}pre
{font
-family
:sahel
})
252 . '</head><body><pre>';
253 } elsif ($format eq 'atom' or $format eq 'rss') {
255 ($format eq 'atom') ?
'updated'
256 : ($format eq 'rss') ?
'atom:updated'
259 "<$atom_updated>$upd_iso8601</$atom_updated>\n" if $upd;
260 $out .= ($format eq 'atom') ?
qq(
261 <entry xml
:base
="$site_url">
262 <author
><name
>$author</name></author
>
264 <published
>$pub_iso8601</published
>\n)
265 . ($updated ?
$updated : '') .
266 qq(<link href
="$url" rel
="alternate" type
="text/plain" />
267 <link href
="$url_html" rel
="alternate" type
="text/html" />
268 <title
>$title</title
>
269 <content type
="html"><![CDATA
[<pre
>)
270 : ($format eq 'rss') ?
qq(
272 <title
>$title</title
>
273 <link>$url_html</link>
274 <guid isPermaLink
="false">$note_id</guid
>
275 <pubDate
>$pub_rfc5322</pubDate
>\n)
276 . (($updated and $pub ne $upd) ?
$updated : '') .
277 qq(<content
:encoded
><![CDATA
[<pre
>)
283 if ($format eq 'html') {
284 $out .= '</pre></body></html>';
285 } elsif ($format eq 'atom') {
286 $out .= "</pre>]]></content></entry>";
287 } elsif ($format eq 'rss') {
288 $out .= "</pre>]]></content:encoded></item>";