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.
29 GetOptions
('lang=s' => \
$opt_lang)
30 or die("bad command line arguments\n");
33 qr{([\
('!])?\b((?:ftps?|https?|nntps?|imaps?|s?news|gopher)://
35 (?:[a-z0-9\-\._~!\$\&\';\(\)\*\+,;=:@/%]*)
36 (?:\?[a-z0-9\-\._~!\$\&\';\(\)\*\+,;=:@/%]+)?
37 (?:\#[a-z0-9\-\._~!\$\&\';\(\)\*\+,;=:@/%\?]+)?
42 "(" => qr/(\)[\.,;\+]?)\z/, # Markdown (,), Ruby (+) (, for arrays)
43 "'" => qr/('[\.,;\+]?)\z/, # Perl / Ruby
44 "!" => qr/(![\.,;\+]?)\z/, # Perl / Ruby
57 $s =~ s/([&<>])/$html_map{$1}/sge;
68 # it's fairly common to end URLs in messages with
69 # '.', ',' or ';' to denote the end of a statement;
70 # assume the intent was to end the statement/sentence
72 if (defined(my $re = $pairs{$beg})) {
73 if ($url =~ s/$re//) {
76 } elsif ($url =~ s/(\))?([\.,;])\z//) {
78 # require ')' to be paired with '('
79 if (defined $1) { # ')'
80 if (index($url, '(') < 0) {
86 } elsif ($url !~ /\(/ && $url =~ s/\)\z//) {
90 $beg . "<a href
=\"$url\">$url</a
>" . $end;
96 my $txt = do { local $/; <STDIN> };
97 my $title = html_esc($txt =~ /\A([^\n]+)/);
98 $title =~ s/^\s+|\s+$//g;
99 if ($opt_lang eq 'fa') {
100 $title .= ' — بندعلی' if $title !~ /بندعلی/;
102 $title .= ' — bandali' if $title !~ /bandali/;
105 $txt = linkify(html_esc($txt));
107 print('<!doctype html>',
108 qq(<html lang="$opt_lang"),
109 $opt_lang eq 'fa' ? ' dir="rtl
"' : '', '>',
111 http-equiv="Content
-Type
"
112 content="text
/html
; charset
=utf
-8"
114 "<title
>$title</title
>",
116 ? "\n<style
>\
@font-face
{font
-family
:sahel
;font
-weight
:normal
;
117 src
:local('Sahel WOL'),local('Sahel'),
118 url
('sahel.woff2')format
('woff2');}pre
{font
-family
:sahel
}</style
>\n"
120 '</head><body><pre>', $txt, '</pre></body></html>');