#!/usr/bin/perl use v5.12; use warnings; use HTML::Entities qw/encode_entities/; use Text::ParseWords qw/quotewords/; # 1 Definitions # 1.1 Global program state my $empty = ''; # currently buffered empty lines my $close = ''; # buffered closing tag for currently opened block element my $postclose = ''; # buffered extra closing tag for opened contiguous element my $opel = ''; # currently opened block element # 1.2 Elements # 1.2.1 Contiguous elements my %ctels = ( Qp_0 => '
{

%

}
', ); # 1.2.2 Block elements my %blels = ( Pp_0 => '

%

', Pr_0 => '
%
', Sh_0 => '

%

', Sh_1 => '%', Ti_0 => '%', ); # 1.2.3 Inline elements my %inels = ( Au_1 => '', Bd_1 => '\\$1', Bd_2 => '\\$1\\$2', Bd_3 => '\\$3\\$1\\$2', br_0 => '
', Cd_1 => '\\$1', Cd_2 => '\\$1\\$2', Cd_3 => '\\$3\\$1\\$2', Cs_1 => '', Em_1 => '\\$1', Em_2 => '\\$1\\$2', Em_3 => '\\$3\\$1\\$2', Hy_2 => '\\$1', Im_2 => '\\$2', It_1 => '\\$1', It_2 => '\\$1\\$2', It_3 => '\\$3\\$1\\$2', St_1 => '\\$1', St_2 => '\\$1\\$2', St_3 => '\\$3\\$1\\$2', Tt_1 => '\\$1', Tt_2 => '\\$1\\$2', Tt_3 => '\\$3\\$1\\$2', Ul_1 => '\\$1', Ul_2 => '\\$1\\$2', Ul_3 => '\\$3\\$1\\$2', ); # 1.3 Subroutines # 1.3.1 Handle element request sub request { my ($el, $args) = @_; my @argv = quotewords('\s+', 0, $args); my $n = @argv; my $elkey = "${el}_$n"; if (exists $blels{$elkey} or exists $ctels{$elkey}) { # Clear empty line buffer $empty = ''; # Close currently open block element, open new my ($base, $prestart, $newpostclose) = ('', '', ''); if (exists $ctels{$elkey}) { $base = $ctels{$elkey}; $prestart = prestart($base); $newpostclose = postclose($base); $base = inner($base); } else { $base = $blels{$elkey}; } my $start = start($base); my $newclose = interpol(_close($base), @argv) . "\n"; print $close; $close = $newclose; print $postclose if $el ne $opel; $postclose = $newpostclose; print $prestart if $el ne $opel; print interpol($start, @argv) . "\n"; $opel = $el; } elsif (exists $inels{$elkey}) { print interpol($inels{$elkey}, @argv) . "\n"; } else { print STDERR "Error: $el/$n not implemented\n"; exit 1; } } # 1.3.2 Interpolate \$n parameters sub interpol { my $s = shift; my $i = 1; my $arg; while ($arg = shift) { $s =~ s/\\\$$i/$arg/g; $i++; } return $s; } # 1.3.3 Retrieve opening tag of block element string sub start { return (split '%', shift)[0]; } # 1.3.4 Retrieve closing tag of block element string sub _close { return (split '%', shift)[1]; } # 1.3.5 Retrieve extra opening tag of block element string sub prestart { return (split '{', shift)[0]; } # 1.3.6 Retrieve extra closing tag of block element string sub postclose { return (split '}', shift)[-1]; } # 1.3.7 Retrieve inner block of contiguous tag sub inner { my $s = shift; $s =~ s/^.*?\{//; $s =~ s/}.*?$//; return $s } # 2 Program # 2.1 Translate source text to HTML while (<>) { chomp; if (/^\.([A-Za-z][a-z])\s*(.*)/) { request($1, $2); } elsif ($_ eq '') { $empty .= "\n"; } else { print $empty; $empty = ''; print encode_entities($_) . "\n"; } } # 2.2 Close currently open block element print $close;