| 1 | # Copyright (C) 2023 Alex Schroeder <alex@gnu.org> | ||||||||||||||||||||||||||||||||||||||||||
| 2 | # | ||||||||||||||||||||||||||||||||||||||||||
| 3 | # This program is free software; you can redistribute it and/or modify | ||||||||||||||||||||||||||||||||||||||||||
| 4 | # it under the terms of the GNU General Public License as published by | ||||||||||||||||||||||||||||||||||||||||||
| 5 | # the Free Software Foundation; either version 3 of the License, or | ||||||||||||||||||||||||||||||||||||||||||
| 6 | # (at your option) any later version. | ||||||||||||||||||||||||||||||||||||||||||
| 7 | # | ||||||||||||||||||||||||||||||||||||||||||
| 8 | # This program is distributed in the hope that it will be useful, | ||||||||||||||||||||||||||||||||||||||||||
| 9 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||||||||||||||||||||||||||||||||||||||||
| 10 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||||||||||||||||||||||||||||||||||||||||
| 11 | # GNU General Public License for more details. | ||||||||||||||||||||||||||||||||||||||||||
| 12 | # | ||||||||||||||||||||||||||||||||||||||||||
| 13 | # You should have received a copy of the GNU General Public License | ||||||||||||||||||||||||||||||||||||||||||
| 14 | # along with this program. If not, see <http://www.gnu.org/licenses/>. | ||||||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||||||
| 16 | package App::news; | ||||||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||||||
| 18 | 8 | ▶ | use Modern::Perl '2018'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||||||
| 20 | 8 | require Exporter; | |||||||||||||||||||||||||||||||||||||||||
| 21 | 8 | our @ISA = qw(Exporter); | |||||||||||||||||||||||||||||||||||||||||
| 22 | 8 | our @EXPORT_OK = qw(wrap html_unwrap ranges sranges); | |||||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||||||
| 24 | 8 | our $VERSION = 1.10; | |||||||||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||||||||||
| 26 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||||||||
| 28 | App::news - a web front-end for a news server | ||||||||||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||||||||||
| 30 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||||||||||
| 32 | This is a collection of functions for F<script/news>, which see. | ||||||||||||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||||||||||||
| 34 | use App::news qw(wrap); | ||||||||||||||||||||||||||||||||||||||||||
| 35 | $body = wrap($body); | ||||||||||||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||||||||||||
| 37 | B<wrap> does text wrapping appropriate for plain text message bodies as used in | ||||||||||||||||||||||||||||||||||||||||||
| 38 | mail and news articles. | ||||||||||||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||||||||||||
| 40 | If a line is shorter than 50 characters, it is not wrapped. | ||||||||||||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||||||||||||
| 42 | Lines are wrapped to be 72 characters or shorter. | ||||||||||||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||||||||||||
| 44 | Quotes are handled as long as only ">" is used for quoting. | ||||||||||||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||||||||||||
| 46 | B<ranges> translates a list of message numbers into an array of numbers or | ||||||||||||||||||||||||||||||||||||||||||
| 47 | arrays suitable for XOVER. | ||||||||||||||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||||||||||||
| 49 | B<sranges> translates the output of I<ranges> into a string for humans to read, | ||||||||||||||||||||||||||||||||||||||||||
| 50 | i.e. "1-2,4". | ||||||||||||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||||||||||||
| 52 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||||||||||||
| 54 | Alex Schroeder | ||||||||||||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||||||||||||
| 56 | =head1 LICENSE | ||||||||||||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||||||||||||
| 58 | GNU Affero General Public License | ||||||||||||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||||||||||
| 60 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||||||||||||
| 62 | # Some US-ASCII coded characters 00-1F and 7F hexadecimal are excluded; the | ||||||||||||||||||||||||||||||||||||||||||
| 63 | # space character and all whitespace is excluded; angle-bracket "<" and ">" and | ||||||||||||||||||||||||||||||||||||||||||
| 64 | # double-quote (") characters are excluded; and the "unwise" characters are | ||||||||||||||||||||||||||||||||||||||||||
| 65 | # excluded. This regular expression is case-sensitive, so the scheme most be | ||||||||||||||||||||||||||||||||||||||||||
| 66 | # lower-case! | ||||||||||||||||||||||||||||||||||||||||||
| 67 | 8 | my $iri_re = qr((\b[a-z]+:[^\x00-\x1F\x7F[:space:]<>"{}|\\^\[\]`]+)); | |||||||||||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||||||||||||
| 69 | 4 | ▶ | sub wrap { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
wrap: called CC=11 SCAR=25.1
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 70 | 4 | my @lines = split(/\n/, shift); | |||||||||||||||||||||||||||||||||||||||||
| 71 | 4 | my @result; | |||||||||||||||||||||||||||||||||||||||||
| 72 | 4 | my $min = 50; | |||||||||||||||||||||||||||||||||||||||||
| 73 | 4 | my $max = 72; | |||||||||||||||||||||||||||||||||||||||||
| 74 | 4 | my $buffer; | |||||||||||||||||||||||||||||||||||||||||
| 75 | 4 | my $prefix = ''; | |||||||||||||||||||||||||||||||||||||||||
| 76 | 4 | for (@lines) { | |||||||||||||||||||||||||||||||||||||||||
| 77 | 14 | my ($new_prefix) = /^([> ]*)/; | |||||||||||||||||||||||||||||||||||||||||
| 78 | 14 | ▶ | my $no_wrap = (/^$prefix\s*$/ or length() < $min); | ||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
/^$prefix\s*$/u || length $_ < $min
MC/DC50%1/2
/^$prefix\s*$/u || length $_ < $min
/^$prefix\s*$/u
length $_ < $min
Truth table66%2/3
A || B
A= /^$prefix\s*$/uB= length $_ < $min
| |||||||||||||||||||||||||||||||||||||||||||
| 79 | # end old paragraph with the old prefix if the prefix changed or a short line | ||||||||||||||||||||||||||||||||||||||||||
| 80 | # came up | ||||||||||||||||||||||||||||||||||||||||||
| 81 | 14 | ▶ | if ($buffer and ($new_prefix ne $prefix or $no_wrap)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%3/6
$buffer and $new_prefix ne $prefix || $no_wrap
$new_prefix ne $prefix or $no_wrap
MC/DC33%1/3
$buffer and $new_prefix ne $prefix || $no_wrap
$buffer
$new_prefix ne $prefix
$no_wrap
Truth table50%2/4
A and B or C
A= $bufferB= $new_prefix ne $prefixC= $no_wrap
| |||||||||||||||||||||||||||||||||||||||||||
| 82 | 1 | push(@result, $prefix . $buffer); | |||||||||||||||||||||||||||||||||||||||||
| 83 | 1 | $buffer = ''; | |||||||||||||||||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||||||||||||||||
| 85 | # set new prefix | ||||||||||||||||||||||||||||||||||||||||||
| 86 | 14 | $prefix = $new_prefix; | |||||||||||||||||||||||||||||||||||||||||
| 87 | # print short lines without stripping trailing whitespace | ||||||||||||||||||||||||||||||||||||||||||
| 88 | 14 | ▶ | if ($no_wrap) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 89 | 11 | push(@result, $_); | |||||||||||||||||||||||||||||||||||||||||
| 90 | 11 | next; | |||||||||||||||||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||||||||||||||||
| 92 | # continue old paragraph | ||||||||||||||||||||||||||||||||||||||||||
| 93 | 3 | ▶ | $buffer .= " " if $buffer; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 94 | # strip the prefix | ||||||||||||||||||||||||||||||||||||||||||
| 95 | 3 | $buffer .= substr($_, length($prefix)); | |||||||||||||||||||||||||||||||||||||||||
| 96 | # wrap what we have | ||||||||||||||||||||||||||||||||||||||||||
| 97 | 3 | ▶ | while (length($buffer) > $max) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 98 | # if there's a word boundary at $max, break before | ||||||||||||||||||||||||||||||||||||||||||
| 99 | 2 | ▶ | if (substr($buffer, 0, $max - length($prefix) + 1) =~ /(\s+(\S+))\S$/) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 100 | 2 | push(@result, $prefix . substr($buffer, 0, $max - length($prefix) - length($1))); | |||||||||||||||||||||||||||||||||||||||||
| 101 | 2 | $buffer = substr($buffer, $max - length($prefix) - length($2)); | |||||||||||||||||||||||||||||||||||||||||
| 102 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 103 | 0 | my $line = substr($buffer, 0, $max - length($prefix)); | |||||||||||||||||||||||||||||||||||||||||
| 104 | 0 | $line =~ s/\s+$//; | |||||||||||||||||||||||||||||||||||||||||
| 105 | 0 | push(@result, $prefix . $line); | |||||||||||||||||||||||||||||||||||||||||
| 106 | 0 | $buffer = substr($buffer, $max - length($prefix)); | |||||||||||||||||||||||||||||||||||||||||
| 107 | 0 | $buffer =~ s/^\s+//; | |||||||||||||||||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||||||||||||||||
| 111 | 4 | ▶ | push(@result, $prefix . $buffer) if $buffer; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 112 | 4 | return join("\n", @result) . "\n"; | |||||||||||||||||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||||||||||||||
| 115 | 3 | ▶ | sub html_unwrap { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
html_unwrap: called CC=7 SCAR=19.5
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 116 | 3 | my @lines = split(/\n/, shift); | |||||||||||||||||||||||||||||||||||||||||
| 117 | 3 | my $result; | |||||||||||||||||||||||||||||||||||||||||
| 118 | 3 | my $depth = 0; | |||||||||||||||||||||||||||||||||||||||||
| 119 | 3 | my @escapes; | |||||||||||||||||||||||||||||||||||||||||
| 120 | 3 | for (@lines) { | |||||||||||||||||||||||||||||||||||||||||
| 121 | 16 | chomp; | |||||||||||||||||||||||||||||||||||||||||
| 122 | 16 | s!$iri_re!push(@escapes, qq(<a href="$1">$1</a>)); "\x1e$#escapes\x1e"!ge; | |||||||||||||||||||||||||||||||||||||||||
| 123 | 16 | my ($prefix) = /^([> ]*)/; | |||||||||||||||||||||||||||||||||||||||||
| 124 | 16 | my $new_depth = () = $prefix =~ />/g; | |||||||||||||||||||||||||||||||||||||||||
| 125 | 16 | s/^([> ]*)//; | |||||||||||||||||||||||||||||||||||||||||
| 126 | 16 | my $closed = 0; | |||||||||||||||||||||||||||||||||||||||||
| 127 | 16 | ▶ | while ($new_depth < $depth) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 128 | 1 | $result .= "</blockquote>"; | |||||||||||||||||||||||||||||||||||||||||
| 129 | 1 | $depth--; | |||||||||||||||||||||||||||||||||||||||||
| 130 | 1 | $closed = 1; | |||||||||||||||||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||||||||||||||||
| 132 | 16 | ▶ | $result .= "\n" unless $closed; # closing blockquote already added a break | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 133 | 16 | ▶ | while ($depth < $new_depth) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 134 | 1 | $result .= '<blockquote>'; | |||||||||||||||||||||||||||||||||||||||||
| 135 | 1 | $depth++; | |||||||||||||||||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||||||||||||||||
| 137 | 16 | s/&/&/g; | |||||||||||||||||||||||||||||||||||||||||
| 138 | 16 | s/</</g; | |||||||||||||||||||||||||||||||||||||||||
| 139 | 16 | s/>/>/g; | |||||||||||||||||||||||||||||||||||||||||
| 140 | 16 | $result .= $_; | |||||||||||||||||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||||||||||||||||
| 142 | 3 | ▶ | while ($depth > 0) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 143 | 0 | $result .= "</blockquote>"; | |||||||||||||||||||||||||||||||||||||||||
| 144 | 0 | $depth--; | |||||||||||||||||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||||||||||||||||
| 146 | 3 | for my $i (0 .. $#escapes) { | |||||||||||||||||||||||||||||||||||||||||
| 147 | 1 | $result =~ s/\x1e$i\x1e/$escapes[$i]/; | |||||||||||||||||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||||||||||||||||
| 149 | 3 | return $result; | |||||||||||||||||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||||||||||||||||
| 152 | 9 | ▶ | sub ranges { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
ranges: called CC=6 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 153 | 9 | ▶ | return [] unless @_; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 154 | 9 | my $last = shift; | |||||||||||||||||||||||||||||||||||||||||
| 155 | 9 | my $curr = $last; | |||||||||||||||||||||||||||||||||||||||||
| 156 | 9 | my $ranges = []; | |||||||||||||||||||||||||||||||||||||||||
| 157 | 9 | for my $n (@_) { | |||||||||||||||||||||||||||||||||||||||||
| 158 | 17 | ▶ | if ($n == $curr + 1) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 159 | 11 | $curr = $n; | |||||||||||||||||||||||||||||||||||||||||
| 160 | } elsif ($last == $curr) { | ||||||||||||||||||||||||||||||||||||||||||
| 161 | 3 | push(@$ranges, $last); | |||||||||||||||||||||||||||||||||||||||||
| 162 | 3 | $last = $curr = $n; | |||||||||||||||||||||||||||||||||||||||||
| 163 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 164 | 3 | push(@$ranges, [$last, $curr]); | |||||||||||||||||||||||||||||||||||||||||
| 165 | 3 | $last = $curr = $n; | |||||||||||||||||||||||||||||||||||||||||
| 166 | } | ||||||||||||||||||||||||||||||||||||||||||
| 167 | } | ||||||||||||||||||||||||||||||||||||||||||
| 168 | 9 | ▶ | if ($curr > $last) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 169 | 5 | push(@$ranges, [$last, $curr]); | |||||||||||||||||||||||||||||||||||||||||
| 170 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 171 | 4 | push(@$ranges, $curr); | |||||||||||||||||||||||||||||||||||||||||
| 172 | } | ||||||||||||||||||||||||||||||||||||||||||
| 173 | 9 | return $ranges; | |||||||||||||||||||||||||||||||||||||||||
| 174 | } | ||||||||||||||||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||||||||||||||||
| 176 | 8 | ▶ | sub sranges { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
sranges: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 177 | 8 | my $ranges = shift; | |||||||||||||||||||||||||||||||||||||||||
| 178 | 8 | ▶ | join(",", map { ref ? join("-", @$_) : $_ } @$ranges); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 179 | } | ||||||||||||||||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||||||||||||||||
| 181 | 8 | 1; | |||||||||||||||||||||||||||||||||||||||||