gmi2html: fix lists

This commit is contained in:
Sam Greytalon 2023-04-15 17:50:16 -07:00
parent e6e0ee3b05
commit 40c9c0fd80
1 changed files with 42 additions and 26 deletions

View File

@ -51,14 +51,14 @@ print OUT <<"END_HTML";
END_HTML END_HTML
my $line = convert_gmi_line($old_title); my $line = convert_gmi_line($old_title);
print OUT " $line\n"; print OUT " $line";
foreach $line (<IN>) { foreach $line (<IN>) {
chomp $line; chomp $line;
my $out = convert_gmi_line($line); my $out = convert_gmi_line($line);
if (defined $out) { if (defined $out) {
print OUT " $out\n"; print OUT " $out";
} }
} }
@ -68,30 +68,50 @@ print OUT <<'END_HTML';
END_HTML END_HTML
sub convert_gmi_line { sub convert_gmi_line {
state $mode = 'normal'; state $pre = 0;
state $list = 0; state $list = 0;
my ($line) = @_; my ($line) = @_;
if ($mode eq 'pre') { if ($pre == 1) {
if ($line eq '```') { if ($line eq '```') {
$mode = 'normal'; $pre = 0;
return undef; return "</pre>\n";
} }
return $line; return " $line\n";
} }
if ($line =~ /^\* /) { if ($line eq '```') {
if ($list == 0) { $pre = 1;
return '<ul>';
if ($list == 1) {
$list = 0;
return "</ul>\n <pre>\n";
}
return "<pre>\n";
}
if ($list == 0) {
if ($line =~ /^\* /) {
$list = 1; $list = 1;
my $listitem = convert_gmi_line($line);
return "<ul>\n $listitem";
} }
} }
else { else {
if ($list == 1) { if (not $line =~ /^\* /) {
return '</ul>';
$list = 0; $list = 0;
my $nextline = convert_gmi_line($line);
if (defined $nextline) {
return "</ul>\n $nextline";
}
else {
return "</ul>\n";
}
} }
} }
@ -102,43 +122,39 @@ sub convert_gmi_line {
my $text = $2; my $text = $2;
$link =~ s/\.gmi/\.html/; $link =~ s/\.gmi/\.html/;
return "<p><a href=\"$link\">$text</a></p>"; return "<p><a href=\"$link\">$text</a></p>\n";
} }
when(/^#[^#]/) { when(/^#[^#]/) {
$line =~ s/^#\s*//; $line =~ s/^#\s*//;
return "<h1>$line</h1>"; return "<h1>$line</h1>\n";
} }
when(/^##[^#]/) { when(/^##[^#]/) {
$line =~ s/^##\s*//; $line =~ s/^##\s*//;
return "<h2>$line</h2>"; return "<h2>$line</h2>\n";
} }
when(/^###/) { when(/^###/) {
$line =~ s/^###\s*//; $line =~ s/^###\s*//;
return "<h3>$line</h3>"; return "<h3>$line</h3>\n";
} }
when(/^\*\s*/) { when(/^\*\s*/) {
$line =~ s/^\*\s*//; $line =~ s/^\*\s*//;
return "<li>$line</li>"; return " <li>$line</li>\n";
} }
when(/^>/) { when(/^>/) {
$line =~ s/^>//; $line =~ s/^>\s*//;
return "<blockquote>$line</blockquote>"; return "<blockquote>$line</blockquote>\n";
}
when('```') {
$mode = 'pre';
return undef;
} }
default { default {
if ($line eq '') { if ($line eq '') {
return undef; return undef;
} }
return "<p>$line</p>"; return "<p>$line</p>\n";
} }
} }
} }