txt2html: support extracting publish/update dates (now restored)
[~bandali/bndl.org] / txt2html
CommitLineData
d3adcff4
AB
1#!/usr/bin/env perl
2# txt2html --- simple script to convert my site's txt files to html
3
4# Copyright (C) 2014-2021 all contributors <meta@public-inbox.org>
5# Copyright (c) 2021 Amin Bandali <bandali@gnu.org>
6#
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.
11#
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.
16#
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/>.
19
20# This simple script borrows from the wonderful `txt2pre' from
21# public-inbox.git, under AGPLv3+, with a few additions of my own.
22
23
24use strict;
25use warnings 'all';
e02deb23
AB
26use Getopt::Long;
27
28my $opt_lang = 'en';
ce2fcdbc
AB
29my $opt_index;
30GetOptions ('lang=s' => \$opt_lang,
31 'index' => \$opt_index)
e02deb23 32 or die("bad command line arguments\n");
d3adcff4
AB
33
34my $link_re =
35 qr{([\('!])?\b((?:ftps?|https?|nntps?|imaps?|s?news|gopher)://
36 [\@:\w\.-]+(?:/
37 (?:[a-z0-9\-\._~!\$\&\';\(\)\*\+,;=:@/%]*)
38 (?:\?[a-z0-9\-\._~!\$\&\';\(\)\*\+,;=:@/%]+)?
39 (?:\#[a-z0-9\-\._~!\$\&\';\(\)\*\+,;=:@/%\?]+)?
40 )?
41 )}xi;
42
43my %pairs = (
44 "(" => qr/(\)[\.,;\+]?)\z/, # Markdown (,), Ruby (+) (, for arrays)
45 "'" => qr/('[\.,;\+]?)\z/, # Perl / Ruby
46 "!" => qr/(![\.,;\+]?)\z/, # Perl / Ruby
47);
48
49my %html_map = (
50 '&' => '&amp;',
51 '<' => '&lt;',
52 '>' => '&gt;',
53 # '"' => '&quot;',
54 # "'" => '&#39;',
55);
56
57sub html_esc {
58 my ($s) = @_;
59 $s =~ s/([&<>])/$html_map{$1}/sge;
60 $s;
61}
62
63sub linkify {
64 my ($s) = @_;
65 $s =~ s^$link_re^
66 my $beg = $1 || '';
67 my $url = $2;
68 my $end = '';
69
70 # it's fairly common to end URLs in messages with
71 # '.', ',' or ';' to denote the end of a statement;
72 # assume the intent was to end the statement/sentence
73 # in English
74 if (defined(my $re = $pairs{$beg})) {
75 if ($url =~ s/$re//) {
76 $end = $1;
77 }
78 } elsif ($url =~ s/(\))?([\.,;])\z//) {
79 $end = $2;
80 # require ')' to be paired with '('
81 if (defined $1) { # ')'
82 if (index($url, '(') < 0) {
83 $end = ")$end";
84 } else {
85 $url .= ')';
86 }
87 }
88 } elsif ($url !~ /\(/ && $url =~ s/\)\z//) {
89 $end = ')';
90 }
91
92 $beg . "<a href=\"$url\">$url</a>" . $end;
93 ^geo;
94 $s;
95}
96
97
98my $txt = do { local $/; <STDIN> };
212ec2e6 99
d3adcff4
AB
100my $title = html_esc($txt =~ /\A([^\n]+)/);
101$title =~ s/^\s+|\s+$//g;
e02deb23 102if ($opt_lang eq 'fa') {
d3adcff4
AB
103 $title .= ' &mdash; بندعلی' if $title !~ /بندعلی/;
104} else {
105 $title .= ' &mdash; bandali' if $title !~ /bandali/;
106}
212ec2e6
AB
107
108my ($upd, $pub, $url) = $txt =~ /(.*)\r?\n(.*)\r?\n(.*)\r?\n?\z/;
109($upd) = $upd =~ /(?:updated|ویرایش): (.*)/ if $upd;
110($pub) = $pub =~ /(?:published|انتشار): (.*)/ if $pub;
111($url) = $url =~ /(?:plain text|متن ساده): (.*)/ if $url;
ce2fcdbc
AB
112$url = 'https://bndl.org/bandali-cv.txt'
113 if (!$url and $title =~ /curriculum vitae/);
114$url = html_esc($url) if $url;
d3adcff4
AB
115
116$txt = linkify(html_esc($txt));
117
ce2fcdbc 118print("<!doctype html>",
e02deb23 119 qq(<html lang="$opt_lang"),
ce2fcdbc
AB
120 $opt_lang eq 'fa' ? ' dir="rtl"' : '',
121 ">",
122 qq(<head>
123<meta http-equiv="Content-Type"
124content="text/html; charset=utf-8" />\n),
125 "<title>$title</title>\n",
4236f6db 126 qq(<link rel="icon" href="data:,">\n),
ce2fcdbc
AB
127 $url ? qq(<link rel="alternate" href="$url"
128title="plain text" type="text/plain" />\n) : '',
129 ($opt_index and $opt_lang eq 'en')
130 ? qq(<link rel="alternate" href="https://bndl.org/fa/"
131hreflang="fa" title="persian" />\n)
132 : ($opt_index and $opt_lang eq 'fa')
133 ? qq(<link rel="alternate" href="https://bndl.org/"
134hreflang="en" title="english" />\n)
135 : '',
19580079
AB
136 qq(<style>\@media(prefers-color-scheme:dark){
137body{background:#1c1c1c;color:white;}a:link{color:#acdeff;}
138a:visited{color:#f8f;}a:active{color:#e00;}}),
e02deb23 139 $opt_lang eq 'fa'
19580079 140 ? qq(\n\@font-face{font-family:sahel;font-weight:normal;
d3adcff4 141src:local('Sahel WOL'),local('Sahel'),
19580079 142url('sahel.woff2')format('woff2');}pre{font-family:sahel})
d3adcff4 143 : '',
19580079 144 "</style>\n",
ce2fcdbc 145 "</head><body><pre>$txt</pre></body></html>\n");
d3adcff4 146STDOUT->flush;