If you are interested in a more compact style, you could be writing your @sections loop as
section($tab, sub {
for my $line (@$_) {
heading($tab,$1,$2, { id => idify($2) }), next if $line =~ /^(
+[1-6])\s+(.*)/;
line($tab,$line), next if $line =~ /^<
+/;
line($tab,"<$line>"), next if $line =~ /^[
+bh]r$/;
paragraph($tab,$line);
}
}) for @sections;
I like this for its tabular look where the action is on the left and the condition on the right. It also avoids the chain of elsifs. The danger is that it becomes difficult to stay the course when the actions become more complex...
| [reply] [d/l] [select] |
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' });
}
No matter how hysterical I get, my problems are not time sensitive. So, relax, have a cookie, and a very nice day!
Lady Aleena
| [reply] [d/l] [select] |
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/) {
my ($number,$text) = split(/ /,$line,2);
push @toc, anchor(textify($text), { href => '#'.idify($text) });
$inc++;
}
push @{$sections[$inc]}, $line;
}
$inc = 0;
for my $section (@sections) {
if ($section) {
section($tab, sub {
for my $line (@{$section}) {
line($tab, $line), next if $line =~ /^</;
line($tab, "<$line>"), next if $line =~ /^[bh]r$/;
$magic->{$1}->(), next if $line =~ /^&\s+(.*)/;
blockquote($tab, $1), next if $line =~ /^bq\s(.*)/;
item($tab, $1), next if $line =~ /^\*\s(.*)/;
item($tab, $2, { value => $1 }), next if $line =~ /^\*(\d+)\
+s(.*)/;
heading($tab, $1, $2, { id => idify($2) }), next if $line
+ =~ /^([1-6])\s+(.*)/;
paragraph($tab, $line, { class => 'author' }), next if $line
+ =~ /^by /;
paragraph($tab, $line);
}
});
}
if ($inc == 0 && @toc > 3) {
section($tab, sub {
my $class = @toc > 25 ? @toc > 50 ? 'four' : 'three' : 'two';
my $style = @toc > 50 ? 'font-size:smaller' : undef;
list($tab, 'u', \@toc, { class => $class, style => $style });
}, { class => 'contents'} );
}
$inc++;
}
# paragraph($tab,"written by $root_user", { class => 'author' });
}
No matter how hysterical I get, my problems are not time sensitive. So, relax, have a cookie, and a very nice day!
Lady Aleena
| [reply] [d/l] |
Thanks for letting me know.
Are these scripts called every time someone visits your website? Or are they part of the build process of the website?
If your scripts get ever more complicated it might make sense to cache the resulting HTML files rather than generating them every time dynamically. The script could check whether the HTML file is older than the input file and only then generate output.
| [reply] |