# Strange Markup Language processor 16mar14waw # Strange Markup Language (anyway, strange to me) processor. # refer to perlmonks node 1078356. package SML; use warnings FATAL => 'all'; use strict; my $nobrackets = qr{ [^{}]+ }xms; # maybe use * quantifier?? use constant XLT => ( # basic translation mapping # captured tagged substrings represented by $ below # tag translates as '\textsuperscript' => ' startsuperscript $ endsuperscript ', '\textsubscript' => ' startsubscript $ endsubscript' , '\textit' => ' startitalic $ enditalic' , '\textcolor' => '' , '' => '($)' , ); my %xlate1 = XLT; # tag will be in separate scalar. # tagged string will be returned by anon. subroutine. # convert strings (and placeholders) of %xlate1 to anon. subs. for (values %xlate1) { # convert: # placeholder to function parameter; s{ \$ }'$_[0]'xms; # value to anon. sub returning string w/interpolated param $_ = eval qq{ sub { qq{$_} } }; } sub process_iter_1 { # works -- iterative, 1 capture, uses /e my ($passes, # number of processing passes to make $string, # string to be processed @tags, # tags to process in order of processing ) = @_; while ($passes-- >= 1) { # count the passes for ($string) { # aliases $string to $_ for s/// for my $tag (@tags) { # processes tags in order exists $xlate1{$tag} or die qq{unknown tag '$tag'}; s{ \Q$tag\E \{ ($nobrackets) \} } { $xlate1{$tag}->($1) }xmsge; } } } return $string; } my %xlate2 = XLT; # tag will be in separate scalar. # tagged string will be returned by anon. subroutine. # convert strings (and placeholders) of %xlate2 to anon. subs. for (values %xlate2) { # convert: # placeholder to function parameter; s{ \$ }'$_[0]'xms; # value to anon. sub returning ref. to string w/interpolated param $_ = eval qq{ sub { \\ qq{$_} } }; } sub process_iter_2 { # works -- iterative, 1 capture, no /e my ($passes, # number of processing passes to make $string, # string to be processed @tags, # tags to process in order of processing ) = @_; while ($passes-- >= 1) { # count the passes for ($string) { # aliases $string to $_ for s/// for my $tag (@tags) { # processes tags in order exists $xlate2{$tag} or die qq{unknown tag '$tag'}; s{ \Q$tag\E \{ ($nobrackets) \} } {${ $xlate2{$tag}->($1) }}xmsg; } } } return $string; } 1;