From 1d536eaeadcdfe74ec1a172d4ae17772fbc8dca4 Mon Sep 17 00:00:00 2001 From: Jonas Smedegaard Date: Thu, 13 Aug 2009 20:07:21 +0200 Subject: Fix newline handling using tidymsg() subroutine. --- localmarkdown2sms | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-) (limited to 'localmarkdown2sms') diff --git a/localmarkdown2sms b/localmarkdown2sms index 82547d3..0ca6abb 100755 --- a/localmarkdown2sms +++ b/localmarkdown2sms @@ -62,9 +62,10 @@ foreach my $file (read_dir( $path )) { my $content; # headline if ($line =~ /^(#+)\s*(.*?)\s*$/) { - # tidy latest reply (TODO: use sub) + # tidy latest reply if (defined($key) and defined($reply{$key}[$i])) { - $reply{$key}[$i] =~ s/^\s*(\w.*?)\s*$/$1/s || delete $reply{$key}[$i]; + $reply{$key}[$i] = &tidymsg($reply{$key}[$i]); + ($reply{$key}[$i]) || delete $reply{$key}[$i]; } my $level = length($1); $content = $2; @@ -121,13 +122,7 @@ foreach my $file (read_dir( $path )) { $directive_string =~ /^\s*\!(tag|taglink)\s*((\s*?\b\w+)+)/ and $file{$file}{'directive'}{'tag'} = [ split /\s+/, $2 ]; }; if ( defined( $key ) and not ($skipcontent)) { - $content = $reply{$key}[$i] . $content if (defined($reply{$key}[$i])); - $content =~ s/^\h*$//g; # clean virtually empty lines - $content =~ s/(\S)\h$/$1/g; # strip single trailing space - $content =~ s/\n\n+/\n\n/g; # strip excess newlines -# $content =~ s/(\S)\n([^\n])/$1 $2/g; # convert newline to space - $content =~ s/\h*$//g; # strip all trailing spaces - $reply{$key}[$i] = $content; + $reply{$key}[$i] .= $content; } elsif ($skipkey or $skipcontent) { # skipping - already warned about it... } else { @@ -136,12 +131,24 @@ foreach my $file (read_dir( $path )) { } } } - # tidy latest reply (TODO: use sub) + # tidy latest reply if (defined($key) and defined($reply{$key}[$i])) { - $reply{$key}[$i] =~ s/^\s*(\w.*?)\s*$/$1/s || delete $reply{$key}[$i]; + $reply{$key}[$i] = &tidymsg($reply{$key}[$i]); + ($reply{$key}[$i]) || delete $reply{$key}[$i]; } } +sub tidymsg { + my $msg = shift @_; + $msg =~ s/^\h*$//g; # clean virtually empty lines + $msg =~ s/(\S)\h$/$1/g; # strip single trailing space + $msg =~ s/\n\n+/\n\n/g; # strip excess newlines + $msg =~ s/(\S)\n([^\n])/$1 $2/g; # convert newline to space + $msg =~ s/\h*$//g; # strip all trailing spaces + $msg =~ s/^\s*(\w*?.*?)\s*$/$1/s; # strip surrounding space + return $msg; +} + sub sendmsg { my ($phone, $desc, $msg) = @_; -- cgit v1.2.3