X-Git-Url: https://git.shemshak.org/~bandali/bndl.org/blobdiff_plain/4ce760d0e6a8e2a939e45afce78045bf6ead9828..76b32a1423b69a094d42f50c0a7906bece44fb8e:/txt2pre diff --git a/txt2pre b/txt2pre index e288a69..f25cbcf 100644 --- a/txt2pre +++ b/txt2pre @@ -1,8 +1,8 @@ #!/usr/bin/env perl -# txt2pre --- convert my site's txt files to `pre'-based html +# txt2pre --- convert my site's txt files to `pre'-based atom/rss/html # Copyright (C) 2014-2021 all contributors -# Copyright (c) 2021 Amin Bandali +# Copyright (c) 2021 bandali # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU Affero General Public License as @@ -21,17 +21,48 @@ # wonderful public-inbox project, under AGPLv3+, with additions of # my own. +# Update (2021-11-01): this script isn't currently used for generating +# my site's pages anymore; but kept for future reference. + use strict; use warnings 'all'; use Getopt::Long; -my $opt_lang = 'en'; -my $opt_index; -GetOptions ('lang=s' => \$opt_lang, - 'index' => \$opt_index) +my $format = 'html'; +my $lang = 'en'; +my $index = ''; +my $header = ''; +my $footer = ''; + +GetOptions ('format=s' => \$format, + 'lang=s' => \$lang, + 'index' => \$index, + 'header' => \$header, + 'footer' => \$footer) or die("bad command line arguments\n"); +my $author = + $lang eq 'en' ? 'bandali' + : $lang eq 'fa' ? 'بندعلی' + : ''; +my $site_title = + $lang eq 'en' ? "${author}'s personal site" + : $lang eq 'fa' ? "سایت شخصی $author" + : ''; +my $site_desc = + $lang eq 'en' ? "notes and blog posts by $author" + : $lang eq 'fa' ? "نوشته‌ها و بلاگ پست‌های $author" + : ''; +my $site_url = + ($lang eq 'en') ? 'https://bndl.org' + : ($lang eq 'fa') ? 'https://bndl.org/fa/' + : ''; +my $feed_id = + ($lang eq 'en') ? "tag:bndl.org,2020:notes.$format" + : ($lang eq 'fa') ? "tag:bndl.org,2020:fa/notes.$format" + : ''; + my $link_re = qr{([\('!])?\b((?:ftps?|https?|nntps?|imaps?|s?news|gopher):// [\@:\w\.-]+(?:/ @@ -96,19 +127,83 @@ sub linkify { } +my $out = ''; + +# atom/rss feed header and footer +if ($index and ($format eq 'atom' or $format eq 'rss')) { + if ($header) { + my $now_iso8601 = `date -Iseconds -u | tr -d \\\\n`; + my $now_rfc5322 = `date -uR | tr -d \\\\n`; + my $atom_rel = $format eq 'atom' ? 'self' : 'alternate'; + my $rss_rel = $format eq 'rss' ? 'self' : 'alternate'; + my $link = $format eq 'atom' ? 'link' : 'atom:link'; + my $links = ''; + if ($lang eq 'en') { + $links = qq( +<$link hreflang="fa" href="https://bndl.org/fa/notes.atom" rel="alternate" type="application/atom+xml" /> +<$link hreflang="fa" href="https://bndl.org/fa/notes.rss" rel="alternate" type="application/rss+xml" /> +<$link hreflang="fa" href="https://bndl.org/fa/bandali.fa.txt" rel="alternate" type="text/plain" /> +<$link hreflang="fa" href="https://bndl.org/fa/" rel="alternate" type="text/html" /> +<$link href="https://bndl.org/notes.atom" rel="$atom_rel" type="application/atom+xml" /> +<$link href="https://bndl.org/notes.rss" rel="$rss_rel" type="application/rss+xml" /> +<$link href="https://bndl.org/bandali.txt" rel="alternate" type="text/plain" /> +<$link href="https://bndl.org" rel="alternate" type="text/html" />); + } elsif ($lang eq 'fa') { + $links = qq( + + + + + + + +); + } + $links =~ s/^\n//; + + $out .= ''; + $out .= ($format eq 'atom') ? qq( + +$site_title +$site_desc +$feed_id +$links +$now_iso8601) + : ($format eq 'rss') ? qq( + + +$site_title +$site_desc +$site_url +$lang +$now_rfc5322 +$now_rfc5322 +1800 +$links) + : ''; + } elsif ($footer) { + $out .= ($format eq 'atom') ? '' + : ($format eq 'rss') ? '' + : ''; + } + + # we're done + goto PRINT; +} + + my $txt = do { local $/; }; my $title = html_esc($txt =~ /\A([^\n]+)/); $title =~ s/^\s+|\s+$//g; -if ($opt_lang eq 'fa') { - $title .= ' — بندعلی' if $title !~ /بندعلی/; -} else { - $title .= ' — bandali' if $title !~ /bandali/; -} +$title .= " — $author" if $title !~ /$author/; my ($upd, $pub, $url) = $txt =~ /(.*)\r?\n(.*)\r?\n(.*)\r?\n?\z/; ($upd) = $upd =~ /(?:updated|ویرایش): (.*)/ if $upd; ($pub) = $pub =~ /(?:published|انتشار): (.*)/ if $pub; +$upd = $pub if (!$upd); ($url) = $url =~ /(?:plain text|متن ساده): (.*)/ if $url; $url = 'https://bndl.org/bandali-cv.txt' if (!$url and $title =~ /curriculum vitae/); @@ -116,32 +211,86 @@ $url = html_esc($url) if $url; $txt = linkify(html_esc($txt)); -print("", - qq(", - qq( + +my $upd_iso8601 = `date -Iseconds -ud '$upd' | tr -d \\\\n` if $upd; +my $pub_iso8601 = `date -Iseconds -ud '$pub' | tr -d \\\\n` if $pub; +my $pub_rfc5322 = `date -uRd '$pub' | tr -d \\\\n` if $pub; +my $url_html = $url =~ s/(?:[.]$lang)?[.]txt$/.html/r if $url; +$url_html =~ s|/bandali-(.*)|/$1| if $url_html; +my $slug = $url_html =~ s|.*/(.*)[.]html$|$1|r if $url_html; +my $note_id = "$feed_id:$slug" if $url_html; + +# note header +if ($format eq 'html') { + $out .= + '' + . qq(') + . qq( \n), - "$title\n", - qq(\n), - $url ? qq(\n) : '', - ($opt_index and $opt_lang eq 'en') - ? qq(\n) + . "$title\n" + . qq(\n) + . ($url + ? qq(\n) + : '') + . (($index and $lang eq 'en') + ? qq(\n) - : ($opt_index and $opt_lang eq 'fa') - ? qq(\n) - : '', - qq(\n", - "
$txt
\n"); + : '') + . "\n" + . '
';
+} elsif ($format eq 'atom' or $format eq 'rss') {
+    my $atom_updated =
+        ($format eq 'atom') ? 'updated'
+        : ($format eq 'rss') ? 'atom:updated'
+        : '';
+    my $updated =
+        "<$atom_updated>$upd_iso8601\n" if $upd;
+    $out .= ($format eq 'atom') ? qq(
+
+$author
+$note_id
+$pub_iso8601\n)
+. ($updated ? $updated : '') .
+qq(
+
+$title
+)
+        : ($format eq 'rss') ? qq(
+
+$title
+$url_html
+$note_id
+$pub_rfc5322\n)
+. (($updated and $pub ne $upd) ? $updated : '') .
+qq()
+        : '';
+}
+# note body
+$out .= $txt;
+# note footer
+if ($format eq 'html') {
+    $out .= '
'; +} elsif ($format eq 'atom') { + $out .= "]]>"; +} elsif ($format eq 'rss') { + $out .= "]]>"; +} + + PRINT: +print("$out\n"); STDOUT->flush;