Skip to content

Commit a390f57

Browse files
krizejicculus
authored andcommitted
docs: improve man page generation
1 parent 93ac1e6 commit a390f57

File tree

1 file changed

+62
-15
lines changed

1 file changed

+62
-15
lines changed

build-scripts/wikiheaders.pl

+62-15
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,10 @@ sub dewikify_chunk {
424424
$str .= "\n```$codelang\n$code\n```\n";
425425
}
426426
} elsif ($dewikify_mode eq 'manpage') {
427-
$str =~ s/\./\\[char46]/gms; # make sure these can't become control codes.
427+
# make sure these can't become part of roff syntax.
428+
$str =~ s/\./\\[char46]/gms;
429+
$str =~ s/"/\\(dq/gms;
430+
428431
if ($wikitype eq 'mediawiki') {
429432
# Dump obvious wikilinks.
430433
if (defined $apiprefixregex) {
@@ -449,33 +452,52 @@ sub dewikify_chunk {
449452
# bullets
450453
$str =~ s/^\* /\n\\\(bu /gm;
451454
} elsif ($wikitype eq 'md') {
455+
# bullets
456+
$str =~ s/^\- /\n\\(bu /gm;
457+
# merge paragraphs
458+
$str =~ s/^[ \t]+//gm;
459+
$str =~ s/([^\-\n])\n([^\-\n])/$1 $2/g;
460+
$str =~ s/\n\n/\n.PP\n/g;
461+
452462
# Dump obvious wikilinks.
453463
if (defined $apiprefixregex) {
454-
$str =~ s/\[(\`?$apiprefixregex[a-zA-Z0-9_]+\`?)\]\($apiprefixregex[a-zA-Z0-9_]+\)/\n.BR $1\n/gms;
464+
my $apr = $apiprefixregex;
465+
if(!($apr =~ /\A\(.*\)\Z/s)) {
466+
# we're relying on the apiprefixregex having a capturing group.
467+
$apr = "(" . $apr . ")";
468+
}
469+
$str =~ s/(\S*?)\[\`?($apr[a-zA-Z0-9_]+)\`?\]\($apr[a-zA-Z0-9_]+\)(\S*)\s*/\n.BR "" "$1" "$2" "$5"\n/gm;
470+
# handle cases like "[x](x), [y](y), [z](z)" being separated.
471+
while($str =~ s/(\.BR[^\n]*)\n\n\.BR/$1\n.BR/gm) {}
455472
}
456473

457474
# links
458475
$str =~ s/\[(.*?)]\((https?\:\/\/.*?)\)/\n.URL "$2" "$1"\n/g;
459476

460477
# <code></code> is also popular. :/
461-
$str =~ s/\s*\`(.*?)\`\s*/\n.BR $1\n/gms;
478+
$str =~ s/\s*(\S*?)\`([^\n]*?)\`(\S*)\s*/\n.BR "" "$1" "$2" "$3"\n/gms;
462479

463480
# bold+italic (this looks bad, just make it bold).
464-
$str =~ s/\s*\*\*\*(.*?)\*\*\*\s*/\n.B $1\n/gms;
481+
$str =~ s/\s*(\S*?)\*\*\*([^\n]*?)\*\*\*(\S*)\s*/\n.BR "" "$1" "$2" "$3"\n/gms;
465482

466483
# bold
467-
$str =~ s/\s*\*\*(.*?)\*\*\s*/\n.B $1\n/gms;
484+
$str =~ s/\s*(\S*?)\*\*([^\n]*?)\*\*(\S*)\s*/\n.BR "" "$1" "$2" "$3"\n/gms;
468485

469486
# italic
470-
$str =~ s/\s*\*(.*?)\*\s*/\n.I $1\n/gms;
471-
472-
# bullets
473-
$str =~ s/^\- /\n\\\(bu /gm;
487+
$str =~ s/\s*(\S*?)\*([^\n]*?)\*(\S*)\s*/\n.IR "" "$1" "$2" "$3"\n/gms;
474488
}
475489

490+
# cleanup unnecessary quotes
491+
$str =~ s/(\.[IB]R?)(.*?) ""\n/$1$2\n/gm;
492+
$str =~ s/(\.[IB]R?) "" ""(.*?)\n/$1$2\n/gm;
493+
$str =~ s/"(\S+)"/$1/gm;
494+
# cleanup unnecessary whitespace
495+
$str =~ s/ +\n/\n/gm;
496+
476497
if (defined $code) {
477498
$code =~ s/\A\n+//gms;
478499
$code =~ s/\n+\Z//gms;
500+
$code =~ s/\\/\\(rs/gms;
479501
if ($dewikify_manpage_code_indent) {
480502
$str .= "\n.IP\n"
481503
} else {
@@ -580,7 +602,7 @@ sub dewikify {
580602
$retval .= dewikify_chunk($wikitype, $1, $2, $3);
581603
}
582604
} elsif ($wikitype eq 'md') {
583-
while ($str =~ s/\A(.*?)\n```(.*?)\n(.*?)\n```\n//ms) {
605+
while ($str =~ s/\A(.*?)\n?```(.*?)\n(.*?)\n```\n//ms) {
584606
$retval .= dewikify_chunk($wikitype, $1, $2, $3);
585607
}
586608
}
@@ -2765,14 +2787,30 @@ sub generate_quickref {
27652787
my $wikitype = $wikitypes{$sym};
27662788
my $sectionsref = $wikisyms{$sym};
27672789
my $remarks = $sectionsref->{'Remarks'};
2768-
my $params = $sectionsref->{'Function Parameters'};
27692790
my $returns = $sectionsref->{'Return Value'};
27702791
my $version = $sectionsref->{'Version'};
27712792
my $threadsafety = $sectionsref->{'Thread Safety'};
27722793
my $related = $sectionsref->{'See Also'};
27732794
my $examples = $sectionsref->{'Code Examples'};
27742795
my $deprecated = $sectionsref->{'Deprecated'};
27752796
my $headerfile = $manpageheaderfiletext;
2797+
2798+
my $params = undef;
2799+
2800+
if ($symtype == -1) { # category documentation block.
2801+
# nothing to be done here.
2802+
} elsif (($symtype == 1) || (($symtype == 5))) { # we'll assume a typedef (5) with a \param is a function pointer typedef.
2803+
$params = $sectionsref->{'Function Parameters'};
2804+
} elsif ($symtype == 2) {
2805+
$params = $sectionsref->{'Macro Parameters'};
2806+
} elsif ($symtype == 3) {
2807+
$params = $sectionsref->{'Fields'};
2808+
} elsif ($symtype == 4) {
2809+
$params = $sectionsref->{'Values'};
2810+
} else {
2811+
die("Unexpected symtype $symtype");
2812+
}
2813+
27762814
$headerfile =~ s/\%fname\%/$headersymslocation{$sym}/g;
27772815
$headerfile .= "\n";
27782816

@@ -2839,18 +2877,22 @@ sub generate_quickref {
28392877
$str .= dewikify($wikitype, $deprecated) . "\n";
28402878
}
28412879

2880+
my $incfile = $mainincludefname;
28422881
if (defined $headerfile) {
2843-
$str .= ".SH HEADER FILE\n";
2844-
$str .= dewikify($wikitype, $headerfile) . "\n";
2882+
if($headerfile =~ /Defined in (.*)/) {
2883+
$incfile = $1;
2884+
}
28452885
}
28462886

28472887
$str .= ".SH SYNOPSIS\n";
28482888
$str .= ".nf\n";
2849-
$str .= ".B #include \\(dq$mainincludefname\\(dq\n";
2889+
$str .= ".B #include <$incfile>\n";
28502890
$str .= ".PP\n";
28512891

28522892
my @decllines = split /\n/, $decl;
28532893
foreach (@decllines) {
2894+
$_ =~ s/\\/\\(rs/g; # fix multiline macro defs
2895+
$_ =~ s/"/\\(dq/g;
28542896
$str .= ".BI \"$_\n";
28552897
}
28562898
$str .= ".fi\n";
@@ -2938,8 +2980,11 @@ sub generate_quickref {
29382980
}
29392981

29402982
if (defined $returns) {
2983+
# Chop datatype in parentheses off the front.
2984+
if(!($returns =~ s/\A\([^\[]*\[[^\]]*\]\([^\)]*\)[^\)]*\) //ms)) {
2985+
$returns =~ s/\A\([^\)]*\) //ms;
2986+
}
29412987
$returns = dewikify($wikitype, $returns);
2942-
$returns =~ s/\A\(.*?\)\s*//; # Chop datatype in parentheses off the front.
29432988
$str .= ".SH RETURN VALUE\n";
29442989
$str .= "$returns\n";
29452990
}
@@ -2975,6 +3020,8 @@ sub generate_quickref {
29753020
s/\A\/*//;
29763021
s/\A\.BR\s+//; # dewikify added this, but we want to handle it.
29773022
s/\A\.I\s+//; # dewikify added this, but we want to handle it.
3023+
s/\A\.PP\s*//; # dewikify added this, but we want to handle it.
3024+
s/\\\(bu//; # dewikify added this, but we want to handle it.
29783025
s/\A\s*[\:\*\-]\s*//;
29793026
s/\A\s+//;
29803027
s/\s+\Z//;

0 commit comments

Comments
 (0)