From 37de5d484924799abb28c689da807eb3bd3e91b8 Mon Sep 17 00:00:00 2001 From: "Ryan C. Gordon" Date: Mon, 27 Feb 2023 23:07:43 -0500 Subject: [PATCH] wikiheaders: fixed wikilinks in Markdown code sections. (cherry picked from commit a4796334550da0963ed957829f41b9499a472d73) --- build-scripts/wikiheaders.pl | 32 +++++++++++++++++++++++++------- 1 file changed, 25 insertions(+), 7 deletions(-) diff --git a/build-scripts/wikiheaders.pl b/build-scripts/wikiheaders.pl index 31cac704a..528854714 100755 --- a/build-scripts/wikiheaders.pl +++ b/build-scripts/wikiheaders.pl @@ -260,12 +260,30 @@ sub wikify_chunk { $str .= "$code<\/syntaxhighlight>"; } } elsif ($wikitype eq 'md') { - # Convert obvious API things to wikilinks. You can't make links - # inside code blocks, but you _can_ make sure that single identifiers - # wrapped in backticks encode correctly. - if (defined $apiprefixregex) { - $str =~ s/(?\b|\`?)(?$apiprefixregex[a-zA-Z0-9_]+)(?\`?)/[$+{"LEFT"}$+{"SYM"}$+{"RIGHT"}]($+{"SYM"})/gms; + # convert `code` things first, so they aren't mistaken for other markdown items. + my $codedstr = ''; + while ($str =~ s/\A(.*?)(\`.*?\`)//ms) { + my $codeblock = $2; + $codedstr .= wikify_chunk($wikitype, $1, undef, undef); + if (defined $apiprefixregex) { + # Convert obvious API things to wikilinks, even inside `code` blocks, + # BUT ONLY IF the entire code block is the API thing, + # So something like "just call `SDL_Whatever`" will become + # "just call [`SDL_Whatever`](SDL_Whatever)", but + # "just call `SDL_Whatever(7)`" will not. It's just the safest + # way to do this without resorting to wrapping things in html tags. + $codeblock =~ s/\A\`($apiprefixregex[a-zA-Z0-9_]+)\`\Z/[`$1`]($1)/gms; + } + $codedstr .= $codeblock; } + + # Convert obvious API things to wikilinks. + if (defined $apiprefixregex) { + $str =~ s/\b($apiprefixregex[a-zA-Z0-9_]+)/[$1]($1)/gms; + } + + $str = $codedstr . $str; + if (defined $code) { $str .= "```$codelang$code```"; } @@ -335,7 +353,7 @@ sub dewikify_chunk { } elsif ($wikitype eq 'md') { # Dump obvious wikilinks. The rest can just passthrough. if (defined $apiprefixregex) { - $str =~ s/\[(?\`?)(?$apiprefixregex[a-zA-Z0-9_]+)(?\`?)\]\(($apiprefixregex[a-zA-Z0-9_]+)\)/$+{"LEFT"}$+{"SYM"}$+{"RIGHT"}/gms; + $str =~ s/\[(\`?$apiprefixregex[a-zA-Z0-9_]+\`?)\]\($apiprefixregex[a-zA-Z0-9_]+\)/$1/gms; } } @@ -370,7 +388,7 @@ sub dewikify_chunk { } elsif ($wikitype eq 'md') { # Dump obvious wikilinks. if (defined $apiprefixregex) { - $str =~ s/\[(?\`?)(?$apiprefixregex[a-zA-Z0-9_]+)(?\`?)\]\(($apiprefixregex[a-zA-Z0-9_]+)\)/\n.BR $+{"SYM"}\n/gms; + $str =~ s/\[(\`?$apiprefixregex[a-zA-Z0-9_]+\`?)\]\($apiprefixregex[a-zA-Z0-9_]+\)/\n.BR $1\n/gms; } # links