# # Translate a simple langauge into HTML. # Blank lines become

# _stuff_ becomes stuff # @stuff@ becomes stuff # {stuff} becomes stuff # [stuff] becomes stuff # [[stuff]] becomes

header. # [:stuff:] becomes

header and centered. # A line of 3 or more --- becomes an
. # A >>> at the start of a line becomes       # Other stuff is retained, with & < > " being translated into # approprite stuff. The \ escapes substitutions. # Generally, \ is removed. Use \\ to add a single one. # use strict; # Set of plain replacements. Order is important. my @plain = ( '\&', '&', '^\s*\>\>\>\s*', '     ', '\<', '<', '\>', '>', '\"', '"', '\[\[', '

', '\]\]', '

', '\[\:', '

', '\:\]', '

', '\{', '', '\}', '', '\[', '', '\]', '', '^\s*$', '

', '^\-\-\-+$', '


' ); # Set of replacements which toggle. Replacement will be the open # or closing version of the tag. my @toggle = ( '\_', 'u', '\@', 'tt' ); my %toggle_status = ( '\_' => '', '\@' => '' ); # Prefix. print '', "\n"; # Read the input. while(defined(my $line = )) { chomp $line; # Do the simple replacements. my @rs = @plain; while(my $r = shift @rs) { my $rt = shift @rs; $line =~ s/(^|[^\\])$r/$1$rt/g; } # Work through the toggles. @rs = @toggle; while(my $r = shift @rs) { my $rt = shift @rs; # Accumulate the new line here. my $newline = ''; # Take each left-most match for $r. The *? construct # is a * that matches as few as possible. while($line =~ /^(.*?(^|[^\\]))$r(.*)$/) { # Move the left part to newline and add the # replacement, then continue with the right part. $newline .= "$1<$toggle_status{$r}$rt>"; $line = $3; # Toggle the status of the symbol. $toggle_status{$r} = $toggle_status{$r} ? '' : '/'; } $line = $newline . $line; } # Reduce escapes. $line =~ s/\\(.)/$1/g; print "$line\n"; } print "\n";