From cc044905ae1f3016b465009564f5ffa159a98731 Mon Sep 17 00:00:00 2001 From: "Kyle J. McKay" Date: Tue, 2 Mar 2021 21:16:43 -0700 Subject: [PATCH] Markdown.pl: include yaml table lines for error msg lineno The line number mentioned in any error message gets generated by counting from the beginning of the non-yaml output. Of course, the final output will include any yaml table if generated. Adjust the line number in any error messages by the number of lines of preceding yaml table that will be included in the output. Signed-off-by: Kyle J. McKay --- Markdown.pl | 47 +++++++++++++++++++++++++---------------------- 1 file changed, 25 insertions(+), 22 deletions(-) diff --git a/Markdown.pl b/Markdown.pl index af15b20..f8e212b 100755 --- a/Markdown.pl +++ b/Markdown.pl @@ -1153,6 +1153,7 @@ sub _SanitizeOpts { my $o = shift; # hashref ref($o) eq "HASH" or return; + $o->{firstline} = 0; $o->{keep_named_character_entities} = 0 unless defined($o->{keep_named_character_entities}) && $o->{keep_named_character_entities} eq "1"; $o->{xmlcheck} = looks_like_number($o->{xmlcheck}) && $o->{xmlcheck} == 0 ? 0 : 2; @@ -1317,6 +1318,28 @@ sub Markdown { my $yaml; ($text, $yaml) = _PrepareInput($text, $opt{yamlmode}); _ApplyYAMLOpts($yaml, \%opt) if ref($yaml) eq "HASH" && $opt{yamlmode} > 0; + my $yamltable = ""; + if (ref($yaml) eq "HASH" && %$yaml && $opt{yamlmode} && $opt{yamlvis}) { + if ($opt{yamlvis} > 0 || _HasUnknownYAMLOptions($yaml)) { + my ($hrows, $drows) = ("", ""); + foreach (sort(keys(%$yaml))) { + my $v = $yaml->{$_}; + my $rspn = ''; + if (defined($v)) { + $v =~ s/&/&/g; + $v =~ s/" . $v . "\n"; + } else { + $rspn = " class=\"$opt{style_prefix}yaml-undef-value\" rowspan=\"2\" valign=\"top\""; + } + $hrows .= "" . $_ . "\n"; + } + $yamltable = "\n" . + "\n$hrows\n\n$drows\n
\n"; + $opt{firstline} = scalar(@{[$yamltable =~ /\n/g]}); + } + } # Clear the globals. If we don't clear these, you get conflicts # from other articles when generating a page which contains more than @@ -1382,27 +1405,7 @@ sub Markdown { } ${$_[0]}{yaml} = $yaml if ref($yaml) eq "HASH"; } - my $yamltable = ""; - if (ref($yaml) eq "HASH" && %$yaml && $opt{yamlmode} && $opt{yamlvis}) { - if ($opt{yamlvis} > 0 || _HasUnknownYAMLOptions($yaml)) { - my ($hrows, $drows) = ("", ""); - foreach (sort(keys(%$yaml))) { - my $v = $yaml->{$_}; - my $rspn = ''; - if (defined($v)) { - $v =~ s/&/&/g; - $v =~ s/" . $v . "\n"; - } else { - $rspn = " class=\"$opt{style_prefix}yaml-undef-value\" rowspan=\"2\" valign=\"top\""; - } - $hrows .= "" . $_ . "\n"; - } - $yamltable = "\n" . - "\n$hrows\n\n$drows\n
\n"; - } - } + # Eliminate known named character entities $opt{keep_named_character_entities} or do { $yamltable = ConvertNamedCharacterEntities($yamltable); @@ -3599,7 +3602,7 @@ sub _linecol { my ($pos, $txt) = @_; pos($txt) = 0; my ($l, $p); - $l = 1; + $l = 1 + $opt{firstline}; ++$l while ($p = pos($txt)), $txt =~ /\G[^\n]*\n/gc && pos($txt) <= $pos; return "line $l col " . (1 + ($pos - $p)); }