2 # txt2pre --- convert my site's txt files to `pre'-based 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 a script of the same name from the
21 # wonderful public-inbox project, under AGPLv3+, with additions of
31 GetOptions
('lang=s' => \
$opt_lang,
32 'index' => \
$opt_index)
33 or die("bad command line arguments\n");
36 qr{([\
('!])?\b((?:ftps?|https?|nntps?|imaps?|s?news|gopher)://
38 (?:[a-z0-9\-\._~!\$\&\';\(\)\*\+,;=:@/%]*)
39 (?:\?[a-z0-9\-\._~!\$\&\';\(\)\*\+,;=:@/%]+)?
40 (?:\#[a-z0-9\-\._~!\$\&\';\(\)\*\+,;=:@/%\?]+)?
45 "(" => qr/(\)[\.,;\+]?)\z/, # Markdown (,), Ruby (+) (, for arrays)
46 "'" => qr/('[\.,;\+]?)\z/, # Perl / Ruby
47 "!" => qr/(![\.,;\+]?)\z/, # Perl / Ruby
60 $s =~ s/([&<>])/$html_map{$1}/sge;
71 # it's fairly common to end URLs in messages with
72 # '.', ',' or ';' to denote the end of a statement;
73 # assume the intent was to end the statement/sentence
75 if (defined(my $re = $pairs{$beg})) {
76 if ($url =~ s/$re//) {
79 } elsif ($url =~ s/(\))?([\.,;])\z//) {
81 # require ')' to be paired with '('
82 if (defined $1) { # ')'
83 if (index($url, '(') < 0) {
89 } elsif ($url !~ /\(/ && $url =~ s/\)\z//) {
93 $beg . "<a href
=\"$url\">$url</a
>" . $end;
99 my $txt = do { local $/; <STDIN> };
101 my $title = html_esc($txt =~ /\A([^\n]+)/);
102 $title =~ s/^\s+|\s+$//g;
103 if ($opt_lang eq 'fa') {
104 $title .= ' — بندعلی' if $title !~ /بندعلی/;
106 $title .= ' — bandali' if $title !~ /bandali/;
109 my ($upd, $pub, $url) = $txt =~ /(.*)\r?\n(.*)\r?\n(.*)\r?\n?\z/;
110 ($upd) = $upd =~ /(?:updated|ویرایش): (.*)/ if $upd;
111 ($pub) = $pub =~ /(?:published|انتشار): (.*)/ if $pub;
112 ($url) = $url =~ /(?:plain text|متن ساده): (.*)/ if $url;
113 $url = 'https://bndl.org/bandali-cv.txt'
114 if (!$url and $title =~ /curriculum vitae/);
115 $url = html_esc($url) if $url;
117 $txt = linkify(html_esc($txt));
119 print("<!doctype html
>",
120 qq(<html lang="$opt_lang"),
121 $opt_lang eq 'fa' ? ' dir="rtl
"' : '',
124 <meta http-equiv="Content
-Type
"
125 content="text
/html; charset=utf-8" />\n),
126 "<title>$title</title>\n",
127 qq(<link rel
="icon" href
="data:,">\n),
128 $url ?
qq(<link rel
="alternate" href
="$url"
129 title
="plain text" type
="text/plain" />\n) : '',
130 ($opt_index and $opt_lang eq 'en')
131 ?
qq(<link rel
="alternate" href
="https://bndl.org/fa/"
132 hreflang
="fa" title
="persian" />\n)
133 : ($opt_index and $opt_lang eq 'fa')
134 ?
qq(<link rel
="alternate" href
="https://bndl.org/"
135 hreflang
="en" title
="english" />\n)
137 qq(<style
>\
@media(prefers
-color
-scheme
:dark
){
138 body
{background
:#1c1c1c;color:white;}a:link{color:#acdeff;}
139 a
:visited
{color
:#f8f;}a:active{color:#e00;}}),
141 ?
qq(\n\
@font-face
{font
-family
:sahel
;font
-weight
:normal
;
142 src
:local('Sahel WOL'),local('Sahel'),
143 url
('sahel.woff2')format
('woff2');}pre
{font
-family
:sahel
})
146 "</head><body><pre>$txt</pre></body></html>\n");