#!/usr/bin/perl -w # encode_template.pl - Don Yang (uguu.org) # # ! = newline # # .. 7 = char # 8 .. [ = space # ^ .. } = char bitmask (implicit first bit) # # 07/07/07 use strict; my $char_start = ord('#'); my $char_end = ord('7'); my $space_start = $char_end + 1; my $space_end = ord('['); sub EncodeRun($$$) { my ($start, $end, $count) = @_; for(; $count > $end - $start + 1; $count -= $end - $start + 1) { print chr($end); } print chr($count + $start - 1); } while(my $line = <>) { chomp $line; $line =~ s/\S/X/g; while( $line ne '' ) { if( $line =~ s/^(\s+)(.*?)$/$2/ ) { # Consecutive space EncodeRun($space_start, $space_end, length($1)); next; } if( $line =~ s/^(X{7,})(.*)$/$2/ ) { # Long non-whitespace run EncodeRun($char_start, $char_end, length($1)); next; } if( length($line) < 6 ) { # Short non-whitespace run $line =~ s/^(X+)(.*?)$/$2/ or die; EncodeRun($char_start, $char_end, length($1)); next; } # 6 character set $line =~ s/^X(.)(.)(.)(.)(.)(.*)$/$6/ or die $!; my $bits = (($1 eq 'X') ? 1 : 0) | (($2 eq 'X') ? 2 : 0) | (($3 eq 'X') ? 4 : 0) | (($4 eq 'X') ? 8 : 0) | (($5 eq 'X') ? 16 : 0); print chr(ord('^') + $bits); } print "!"; } print "\n";