I recently needed a way to translate simple markup into (uhm, another markup) HTML. The simple markup could be anything, but had to provide a way to format text entries. I figured POD was good enough.

Plain Old Documentation markup is a Perl standard, so it had that going for it, and it seemed pretty simple. Make some text bold by wrapping it in a B<> tag; I for italic, C code, and so on. Writing a regex to match and translate those into HTML seemed easy enough, to until I tried to handle nested tags.

The solution is a nice demonstration on why Perl is such a good language for this sort of job. It's not exactly a one-liner, though it might be possible...

my $string = 'Symmetry B<is I<overrated>. Overrated is> symmetry.';

my %mkup = (I=>"em", B=>"strong", C=>"pre");

my @stack = ();
$string =~
    s{
        ([IBC])<|(>)
    }
    {
        if ($1) { push(@stack, $1), "<$mkup{$1}>" } # opener
        elsif ($2) { '</'.$mkup{pop(@stack)}.'>' }  # closer
    }gex;

print $string;