in reply to Re: Group __DATA__ lines in a while?
in thread Group __DATA__ lines in a while?
I would like to wrap each section in an HTML <section>. Also, the subroutines (line, heading, and paragraph) used in story print immediately. It appears to get the table of contents, I will have to go through the lines twice.
sub story { my ($source,$author) = @_; my $tab = 3; my @toc; while (my $line = <$source>) { chomp($line); if ($line =~ /^2/) { my ($number,$text) = split(/ /,$line,2); push @toc, anchor($text, { href => '#'.idify($text) }); } } list($tab, 'u', \@toc, { class => 'two' }); # I ran $source above, so I can't have it here! /me head desks. while (my $line = <$source>) { chomp($line); # If a line starts with a bracket, just print the line. if ($line =~ m/^</) { line($tab,$line); } # If a line starts with a numeral 1-6, print a heading. elsif ($line =~ /^[1-6]\s/) { my ($heading,$text) = split(/ /,$line,2); my $id = idify($text); heading($tab,$heading,$text, { id => $id }); } # If a line is a break or horizontal rule, print the line in brack +ets. elsif ($line =~ /^[bh]r$/) { line($tab,"<$line>"); } # All other lines are paragraphs. else { paragraph($tab,$line); } } # If I wrote a story, I want people to know I wrote it at the bottom +. paragraph($tab,"written by $root_user", { class => 'author' }) if !$ +author; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: Group __DATA__ lines in a while?
by hdb (Monsignor) on Oct 21, 2013 at 07:01 UTC | |
by Lady_Aleena (Priest) on Oct 21, 2013 at 07:51 UTC | |
by hdb (Monsignor) on Oct 23, 2013 at 09:32 UTC | |
by Lady_Aleena (Priest) on Oct 24, 2013 at 04:48 UTC | |
by Lady_Aleena (Priest) on Nov 10, 2013 at 23:16 UTC | |
by hdb (Monsignor) on Nov 11, 2013 at 08:44 UTC | |
|