in reply to Re^5: Group __DATA__ lines in a while?
in thread Group __DATA__ lines in a while?
hdb, it got a little more complicated than it was. Here is the the most recent version. There are a few more elsif statements. I am almost content with it now. :)
sub story { my ($source,$magic) = @_; my $tab = 3; my $inc = 0; my @sections; my @toc; while (my $line = <$source>) { chomp($line); next if !$line; if ($line =~ /^2/) { $inc++; my ($number,$text) = split(/ /,$line,2); push @toc, anchor(textify($text), { href => '#'.idify($text) }); } push @{$sections[$inc]}, $line; } $inc = 0; for my $section (@sections) { if ($section) { section($tab, sub { for my $line (@{$section}) { if ($line =~ m/^</) { line($tab,$line); } elsif ($line =~ /^& /) { my ($symbol,$magic_word) = split(/ /,$line,2); $magic->{$magic_word}->(); } elsif ($line =~ /^[1-6]\s/) { my ($heading,$text) = split(/ /,$line,2); my $id = idify($text); heading($tab,$heading,$text, { id => $id }); } elsif ($line =~ /^bq\s/) { my ($symbol,$text) = split(/ /,$line,2); blockquote($tab,$text); } elsif ($line =~ /^[bh]r$/) { line($tab,"<$line>"); } else { paragraph($tab,$line); } } }); } if ($inc == 0 && @toc > 3) { section($tab, sub { list($tab, 'u', \@toc, { class => 'two' }); }, { class => 'contents'} ); } $inc++; } # paragraph($tab,"written by $root_user", { class => 'author' }); }
|
|---|