2 # txt2html --- simple script to convert my site's txt files to 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 the wonderful `txt2pre' from
21 # public-inbox.git, under AGPLv3+, with a few additions of my own.
30 GetOptions
('lang=s' => \
$opt_lang,
31 'index' => \
$opt_index)
32 or die("bad command line arguments\n");
35 qr{([\
('!])?\b((?:ftps?|https?|nntps?|imaps?|s?news|gopher)://
37 (?:[a-z0-9\-\._~!\$\&\';\(\)\*\+,;=:@/%]*)
38 (?:\?[a-z0-9\-\._~!\$\&\';\(\)\*\+,;=:@/%]+)?
39 (?:\#[a-z0-9\-\._~!\$\&\';\(\)\*\+,;=:@/%\?]+)?
44 "(" => qr/(\)[\.,;\+]?)\z/, # Markdown (,), Ruby (+) (, for arrays)
45 "'" => qr/('[\.,;\+]?)\z/, # Perl / Ruby
46 "!" => qr/(![\.,;\+]?)\z/, # Perl / Ruby
59 $s =~ s/([&<>])/$html_map{$1}/sge;
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
74 if (defined(my $re = $pairs{$beg})) {
75 if ($url =~ s/$re//) {
78 } elsif ($url =~ s/(\))?([\.,;])\z//) {
80 # require ')' to be paired with '('
81 if (defined $1) { # ')'
82 if (index($url, '(') < 0) {
88 } elsif ($url !~ /\(/ && $url =~ s/\)\z//) {
92 $beg . "<a href
=\"$url\">$url</a
>" . $end;
98 my $txt = do { local $/; <STDIN> };
100 my $title = html_esc($txt =~ /\A([^\n]+)/);
101 $title =~ s/^\s+|\s+$//g;
102 if ($opt_lang eq 'fa') {
103 $title .= ' — بندعلی' if $title !~ /بندعلی/;
105 $title .= ' — bandali' if $title !~ /bandali/;
108 my ($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;
112 $url = 'https://bndl.org/bandali-cv.txt'
113 if (!$url and $title =~ /curriculum vitae/);
114 $url = html_esc($url) if $url;
116 $txt = linkify(html_esc($txt));
118 print("<!doctype html
>",
119 qq(<html lang="$opt_lang"),
120 $opt_lang eq 'fa' ? ' dir="rtl
"' : '',
123 <meta http-equiv="Content
-Type
"
124 content="text
/html; charset=utf-8" />\n),
125 "<title>$title</title>\n",
126 qq(<link rel
="icon" href
="data:,">\n),
127 $url ?
qq(<link rel
="alternate" href
="$url"
128 title
="plain text" type
="text/plain" />\n) : '',
129 ($opt_index and $opt_lang eq 'en')
130 ?
qq(<link rel
="alternate" href
="https://bndl.org/fa/"
131 hreflang
="fa" title
="persian" />\n)
132 : ($opt_index and $opt_lang eq 'fa')
133 ?
qq(<link rel
="alternate" href
="https://bndl.org/"
134 hreflang
="en" title
="english" />\n)
136 qq(<style
>\
@media(prefers
-color
-scheme
:dark
){
137 body
{background
:#1c1c1c;color:white;}a:link{color:#acdeff;}
138 a
:visited
{color
:#f8f;}a:active{color:#e00;}}),
140 ?
qq(\n\
@font-face
{font
-family
:sahel
;font
-weight
:normal
;
141 src
:local('Sahel WOL'),local('Sahel'),
142 url
('sahel.woff2')format
('woff2');}pre
{font
-family
:sahel
})
145 "</head><body><pre>$txt</pre></body></html>\n");