sub parseLists { my $self = shift; my $text = \$self->{_text}; # So we want to loop through the text line by line # and be able to modify some lines, # but we don't want to rebuild/copy the whole text. my $lf = "\n"; # linebreak my $lflen = length $lf; # 1 my $pos1 = 0; # left line offset my $pos2 = 0; # right line offset (1st char after line) my $len = 0; # line length my $lendif = 0; # line length difference my $inlist; open my $fh, "<:utf8", $text; # Note how we open in UTF-8 mode # while (<$fh>) # Gets confused when line length changes # Using seek() is risky, because it reads bytes, not chars! # However, substr() always counts chars, not bytes. while (<$fh>) { # Get line string without newline character my $line = substr $_, 0, -$lflen; my $oldline = $line; # Calculate offsets $len = length $line; $pos1 = $pos2; $pos2 += $len + $lflen; # Modify line # START (not part of loop structure) my $isasterisk = $line =~ m/^\* /; my $isindented = $line =~ m/^\ /; my $isfirst; if (!$inlist) { if ($isasterisk) { $isfirst = 1; $inlist = 1; } } if ($inlist) { if (!$isindented && !$isasterisk) { substr $line, 0, 0, "\n"; $inlist = 0; } elsif ($isindented) { $line =~ s/^\ (.*)/
  • $1<\/li>/; } elsif ($isasterisk) { $line =~ s/^\* (.*)/
  • $1<\/li>/; substr $line, 0, 0, "