summaryrefslogtreecommitdiff
path: root/localmarkdown2sms
diff options
context:
space:
mode:
authorJonas Smedegaard <dr@jones.dk>2009-08-08 17:45:26 +0200
committerJonas Smedegaard <dr@jones.dk>2009-08-08 17:45:26 +0200
commitad7103520d33390dcf9e4082c7c1ff7b3c386d3d (patch)
treeaf428ee2ea06d3cb52566813e6cff140ddba8b02 /localmarkdown2sms
parentc5d672ae830253ef23d020c54603955d260b8de4 (diff)
Tidy replies (poor-mans Markdown) and skip empty ones.
Diffstat (limited to 'localmarkdown2sms')
-rwxr-xr-xlocalmarkdown2sms26
1 files changed, 20 insertions, 6 deletions
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 {