From 40c9c0fd801e44b046f7d732a33a9614fb98e4c6 Mon Sep 17 00:00:00 2001 From: Sam Talonborn Date: Sat, 15 Apr 2023 17:50:16 -0700 Subject: [PATCH] gmi2html: fix lists --- bin/gmi2html | 68 ++++++++++++++++++++++++++++++++-------------------- 1 file changed, 42 insertions(+), 26 deletions(-) diff --git a/bin/gmi2html b/bin/gmi2html index 898e0d5..69ea693 100755 --- a/bin/gmi2html +++ b/bin/gmi2html @@ -51,14 +51,14 @@ print OUT <<"END_HTML"; END_HTML my $line = convert_gmi_line($old_title); -print OUT " $line\n"; +print OUT " $line"; foreach $line () { chomp $line; my $out = convert_gmi_line($line); if (defined $out) { - print OUT " $out\n"; + print OUT " $out"; } } @@ -68,30 +68,50 @@ print OUT <<'END_HTML'; END_HTML sub convert_gmi_line { - state $mode = 'normal'; + state $pre = 0; state $list = 0; my ($line) = @_; - - if ($mode eq 'pre') { + + if ($pre == 1) { if ($line eq '```') { - $mode = 'normal'; - return undef; + $pre = 0; + return "\n"; } - return $line; + return " $line\n"; } - - if ($line =~ /^\* /) { - if ($list == 0) { - return '\n
\n";
+        }
+
+        return "
\n";
+    }
+
+    if ($list == 0) {
+        if ($line =~ /^\* /) {
             $list = 1;
+            my $listitem = convert_gmi_line($line);
+
+            return "
    \n $listitem"; } } else { - if ($list == 1) { - return '
'; + if (not $line =~ /^\* /) { $list = 0; + my $nextline = convert_gmi_line($line); + + if (defined $nextline) { + return "\n $nextline"; + } + else { + return "\n"; + } } } @@ -102,43 +122,39 @@ sub convert_gmi_line { my $text = $2; $link =~ s/\.gmi/\.html/; - return "

$text

"; + return "

$text

\n"; } when(/^#[^#]/) { $line =~ s/^#\s*//; - return "

$line

"; + return "

$line

\n"; } when(/^##[^#]/) { $line =~ s/^##\s*//; - return "

$line

"; + return "

$line

\n"; } when(/^###/) { $line =~ s/^###\s*//; - return "

$line

"; + return "

$line

\n"; } when(/^\*\s*/) { $line =~ s/^\*\s*//; - return "
  • $line
  • "; + return "
  • $line
  • \n"; } when(/^>/) { - $line =~ s/^>//; + $line =~ s/^>\s*//; - return "
    $line
    "; - } - when('```') { - $mode = 'pre'; - return undef; + return "
    $line
    \n"; } default { if ($line eq '') { return undef; } - return "

    $line

    "; + return "

    $line

    \n"; } } }