From ad7103520d33390dcf9e4082c7c1ff7b3c386d3d Mon Sep 17 00:00:00 2001 From: Jonas Smedegaard Date: Sat, 8 Aug 2009 17:45:26 +0200 Subject: Tidy replies (poor-mans Markdown) and skip empty ones. --- localmarkdown2sms | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) (limited to 'localmarkdown2sms') diff --git a/localmarkdown2sms b/localmarkdown2sms index f768f90..6466eb9 100755 --- a/localmarkdown2sms +++ b/localmarkdown2sms @@ -44,6 +44,10 @@ foreach my $file (read_dir( $path )) { my $content; # headline if ($line =~ /^(#+)\s*(.*?)\s*$/) { + # tidy latest reply (TODO: use sub) + if (defined($i) and defined($reply{$key}[$i])) { + $reply{$key}[$i] =~ s/^\s*(\w.*?)\s*$/$1/s || delete $reply{$key}[$i]; + } my $level = length($1); $content = $2; # key @@ -77,33 +81,43 @@ foreach my $file (read_dir( $path )) { } else { warn( "skipping non-parsable headline: \"$line\""); } - # content + # reply } else { + $content = $reply{$key}[$i] if (defined($reply{$key}[$i])); + $content .= $line . "\n"; + $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 if ( defined( $key )) { - $reply{$key}[$i] .= $line . "\n"; + $reply{$key}[$i] = $content; } else { warn( "skipping non-key'ed content: \"$1\"" ); } } } + # tidy latest reply (TODO: use sub) + if (defined($i) and defined($reply{$key}[$i])) { + $reply{$key}[$i] =~ s/^\s*(\w.*?)\s*$/$1/s || delete $reply{$key}[$i]; + } } my ($phone) = shift @ARGV; my ($key) = shift @ARGV; -my $num_children = $#{ $reply{$key} }; # How many children we'll create +my $num_children = $#{ $reply{$key} } + 1; # How many children we'll create $SIG{CHLD} = 'IGNORE'; # Don't worry about reaping zombies # Spawn off some children if ($DEBUG) { print STDERR "queueing $num_children replies:"; - for my $num ( 1 .. $num_children ) { + for my $num ( 0 .. $num_children - 1 ) { print STDERR " [" . $delay{$key}[$num] . "s]"; } print STDERR "\n"; } -for my $num ( 1 .. $num_children ) { - $reply{$key}[$num] =~ s/^\s*(.*?)\s*$/$1/s; +for my $num ( 0 .. $num_children - 1 ) { run_fork { child { -- cgit v1.2.3