# Strange Markup Language processor 2 - iterator 17mar14waw # Strange Markup Language (anyway, strange to me) processor 2. # iterator generator version. # refer to perlmonks node 1078356. package SML2; use warnings FATAL => 'all'; use strict; sub Iterator (&) { return $_[0]; } # syntactic sugar per mjd 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 tag_converter { # works -- iterator generator my (@tags, # tags to process in order of processing ) = @_; # verify existence of tags before making iterator. exists $xlate1{$_} or die qq{unknown tag '$_'} for @tags; return Iterator { my $n = 0; # total substitutions done for all tags for my $tag (@tags) { # processes tags in order $n += $_[0] =~ s{ \Q$tag\E \{ ($nobrackets) \} } {${ $xlate1{$tag}->($1) }}xmsg; } return $n; } } 1;