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> };
99 my $title = html_esc($txt =~ /\A([^\n]+)/);
100 $title =~ s/^\s+|\s+$//g;
101 if ($opt_lang eq 'fa') {
102 $title .= ' — بندعلی' if $title !~ /بندعلی/;
104 $title .= ' — bandali' if $title !~ /bandali/;
106 my ($url) = $txt =~ /(?:plain text|متن ساده): (.*)\r?\n?\z/;
107 $url = 'https://bndl.org/bandali-cv.txt'
108 if (!$url and $title =~ /curriculum vitae/);
109 $url = html_esc($url) if $url;
111 $txt = linkify(html_esc($txt));
113 print("<!doctype html
>",
114 qq(<html lang="$opt_lang"),
115 $opt_lang eq 'fa' ? ' dir="rtl
"' : '',
118 <meta http-equiv="Content
-Type
"
119 content="text
/html; charset=utf-8" />\n),
120 "<title>$title</title>\n",
121 $url ?
qq(<link rel
="alternate" href
="$url"
122 title
="plain text" type
="text/plain" />\n) : '',
123 ($opt_index and $opt_lang eq 'en')
124 ?
qq(<link rel
="alternate" href
="https://bndl.org/fa/"
125 hreflang
="fa" title
="persian" />\n)
126 : ($opt_index and $opt_lang eq 'fa')
127 ?
qq(<link rel
="alternate" href
="https://bndl.org/"
128 hreflang
="en" title
="english" />\n)
131 ?
qq(<style
>\
@font-face
{font
-family
:sahel
;font
-weight
:normal
;
132 src
:local('Sahel WOL'),local('Sahel'),
133 url
('sahel.woff2')format
('woff2');}pre
{font
-family
:sahel
}</style
>\n)
135 "</head><body><pre>$txt</pre></body></html>\n");