transition to a simple txt-based setup, plus txt2html
[~bandali/bndl.org] / txt2html
diff --git a/txt2html b/txt2html
new file mode 100644 (file)
index 0000000..128995c
--- /dev/null
+++ b/txt2html
@@ -0,0 +1,118 @@
+#!/usr/bin/env perl
+# txt2html --- simple script to convert my site's txt files to html
+
+# Copyright (C) 2014-2021 all contributors <meta@public-inbox.org>
+# Copyright (c) 2021 Amin Bandali <bandali@gnu.org>
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as
+# published by the Free Software Foundation, either version 3 of the
+# License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU Affero General Public License for more details.
+#
+# You should have received a copy of the GNU Affero General Public License
+# along with this program.  If not, see <https://www.gnu.org/licenses/>.
+
+# This simple script borrows from the wonderful `txt2pre' from
+# public-inbox.git, under AGPLv3+, with a few additions of my own.
+
+
+use strict;
+use warnings 'all';
+
+my $link_re =
+    qr{([\('!])?\b((?:ftps?|https?|nntps?|imaps?|s?news|gopher)://
+       [\@:\w\.-]+(?:/
+         (?:[a-z0-9\-\._~!\$\&\';\(\)\*\+,;=:@/%]*)
+         (?:\?[a-z0-9\-\._~!\$\&\';\(\)\*\+,;=:@/%]+)?
+         (?:\#[a-z0-9\-\._~!\$\&\';\(\)\*\+,;=:@/%\?]+)?
+       )?
+    )}xi;
+
+my %pairs = (
+    "(" => qr/(\)[\.,;\+]?)\z/, # Markdown (,), Ruby (+) (, for arrays)
+    "'" => qr/('[\.,;\+]?)\z/, # Perl / Ruby
+    "!" => qr/(![\.,;\+]?)\z/, # Perl / Ruby
+);
+
+my %html_map = (
+    '&' => '&amp;',
+    '<' => '&lt;',
+    '>' => '&gt;',
+    # '"' => '&quot;',
+    # "'" => '&#39;',
+);
+
+sub html_esc {
+    my ($s) = @_;
+    $s =~ s/([&<>])/$html_map{$1}/sge;
+    $s;
+}
+
+sub linkify {
+    my ($s) = @_;
+    $s =~ s^$link_re^
+        my $beg = $1 || '';
+        my $url = $2;
+        my $end = '';
+
+        # it's fairly common to end URLs in messages with
+        # '.', ',' or ';' to denote the end of a statement;
+        # assume the intent was to end the statement/sentence
+        # in English
+        if (defined(my $re = $pairs{$beg})) {
+            if ($url =~ s/$re//) {
+                $end = $1;
+            }
+        } elsif ($url =~ s/(\))?([\.,;])\z//) {
+            $end = $2;
+            # require ')' to be paired with '('
+            if (defined $1) { # ')'
+                if (index($url, '(') < 0) {
+                    $end = ")$end";
+                } else {
+                    $url .= ')';
+                }
+            }
+        } elsif ($url !~ /\(/ && $url =~ s/\)\z//) {
+            $end = ')';
+        }
+
+        $beg . "<a href=\"$url\">$url</a>" . $end;
+    ^geo;
+    $s;
+}
+
+
+my $txt = do { local $/; <STDIN> };
+my $title = html_esc($txt =~ /\A([^\n]+)/);
+$title =~ s/^\s+|\s+$//g;
+if ($ARGV[0] and $ARGV[0] eq 'fa') {
+    $title .= ' &mdash; بندعلی' if $title !~ /بندعلی/;
+} else {
+    $title .= ' &mdash; bandali' if $title !~ /bandali/;
+}
+
+$txt = linkify(html_esc($txt));
+
+print('<!doctype html>',
+      ($ARGV[0] and $ARGV[0] eq 'fa')
+      ? '<html lang="fa" dir="rtl">'
+      : '<html>',
+      '<head>',
+      '<meta
+http-equiv="Content-Type"
+content="text/html; charset=utf-8"
+/>',
+      "<title>$title</title>",
+      ($ARGV[0] and $ARGV[0] eq 'fa')
+      ? "\n<style>\@font-face{font-family:sahel;font-weight:normal;
+src:local('Sahel WOL'),local('Sahel'),
+url('sahel.woff2')format('woff2');}pre{font-family:sahel}</style>\n"
+      : '',
+      '</head><body><pre>', $txt, '</pre></body></html>');
+STDOUT->flush;