| 1 | #!/usr/bin/env perl | ||
| 2 | =head1 Article Wrap | ||
| 3 | |||
| 4 | Wrap news articles or mail files. That is to say, don't wrap the header lines at | ||
| 5 | the top but do wrap the rest of the file. Also don't wrap "source code" which is | ||
| 6 | fenced by Markdown code fences: trible backticks. Don't wrap short lines (10 | ||
| 7 | characters or less). Don't wrap lines with whitespace at the end. Don't wrap | ||
| 8 | empty lines. | ||
| 9 | |||
| 10 | This is useful when post-processing a message written using ed for posting using | ||
| 11 | tin, for example. | ||
| 12 | =cut | ||
| 13 | 0 | ▶ | use Modern::Perl; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 14 | die "This filter wraps news posts.\n" if @ARGV; | ||
| 15 | binmode(STDIN, ':utf8'); | ||
| 16 | binmode(STDOUT, ':utf8'); | ||
| 17 | # headers | ||
| 18 | while (<STDIN>) { | ||
| 19 | chomp; | ||
| 20 | last if not $_; # end of headers | ||
| 21 | say; # header line | ||
| 22 | } | ||
| 23 | print "\n"; # empty line after headers | ||
| 24 | my $max = 72; | ||
| 25 | my $buffer; | ||
| 26 | my $prefix = ''; | ||
| 27 | my $wrap = 1; | ||
| 28 | while (<STDIN>) { | ||
| 29 | chomp; | ||
| 30 | my ($new_prefix) = /([> ]*)/; | ||
| 31 | # empty lines don't get wrapped, nor lines with a space at the end, nor indented lines | ||
| 32 | my $empty = length() == 0 || /^$prefix\s*$/ || /\s$/ || /^\s/ || /^$prefix.{0,10}$/; | ||
| 33 | # ``` toggles wrap | ||
| 34 | $wrap = not $wrap if /^$prefix\s*```$/; | ||
| 35 | # end old paragraph with the old prefix if the prefix changed, an empty line, | ||
| 36 | # or not wrapping anymore | ||
| 37 | if ($buffer and ($new_prefix ne $prefix or $empty or not $wrap)) { | ||
| 38 | say $prefix . $buffer; | ||
| 39 | $buffer = ''; | ||
| 40 | } | ||
| 41 | # print empty lines or not wrapped lines without stripping trailing whitespace | ||
| 42 | if ($empty or not $wrap) { | ||
| 43 | say $_; | ||
| 44 | next; | ||
| 45 | } | ||
| 46 | # continue old paragraph | ||
| 47 | $buffer .= " " if $buffer; | ||
| 48 | # strip the prefix | ||
| 49 | $prefix = $new_prefix; | ||
| 50 | $buffer .= substr($_, length($prefix)); | ||
| 51 | # wrap what we have | ||
| 52 | while (length($buffer) > $max) { | ||
| 53 | # this is the max line + 1 | ||
| 54 | my $test_line = substr($buffer, 0, $max - length($prefix) + 1); | ||
| 55 | # if there's a word that reaches into that last character, break before | ||
| 56 | if ($test_line =~ /(\s+(\S+)\S)$/) { | ||
| 57 | # $1 is the last word: strip it, print prefix and stripped line | ||
| 58 | say $prefix . substr($buffer, 0, $max - length($prefix) - length($1) + 1); | ||
| 59 | # the new buffer starts with the word just stripped | ||
| 60 | $buffer = substr($buffer, $max - length($prefix) - length($2)); | ||
| 61 | } else { | ||
| 62 | # we know that there is no word at the boundary, so cut there | ||
| 63 | my $line = substr($buffer, 0, $max - length($prefix)); | ||
| 64 | # strip trailing whitespace and print it | ||
| 65 | $line =~ s/\s+$//; | ||
| 66 | say $prefix . $line; | ||
| 67 | # the new buffer starts where we did the cut, strip leading whitespace | ||
| 68 | $buffer = substr($buffer, $max - length($prefix)); | ||
| 69 | $buffer =~ s/^\s+//; | ||
| 70 | } | ||
| 71 | } | ||
| 72 | } | ||
| 73 | say $prefix . $buffer if $buffer; | ||
| 74 | 1; | ||