#!/usr/bin/perl -w # encode_template.pl - Don Yang (uguu.org) # # # .. [ = space # ] .. | = 6 bit char (implicit first bit) # } = 1 char # ~ = 2 char # ! = newline # # 07/07/07 use strict; sub Str2bits($) { my ($text) = @_; my ($index, $bits) = (0, 0); for(; $text ne ''; $text = substr($text, 1)) { if( substr($text, 0, 1) eq 'X' ) { $bits |= 1 << $index; } $index++; } return chr($bits + ord(']')); } sub EncodeSpace($) { my ($count) = @_; my $max_run = ord('[') - ord('#') + 1; my $enc = ""; for(; $count > $max_run; $count -= $max_run) { $enc .= '['; } return $enc . chr($count + ord('#') - 1); } while(my $line = <>) { chomp $line; $line =~ s/\S/X/g; $line =~ s/\s*$//; while( $line ne '' ) { # Consecutive spaces if( $line =~ s/^(\s+)(.*?)$/$2/ ) { print EncodeSpace(length($1)); next; } # 6 character sequences beginning with non-whitespace if( $line =~ s/^X(.....)(.*)$/$2/ ) { print Str2bits($1); next; } # Trailing characters if( $line =~ s/^XX(.*)$/$1/ ) { print '~'; next; } $line =~ s/^X(.*)$/$1/ or die "Can not encode line $.\n"; print '}'; } print '!'; } print "\n";