🛠️🐜 Antkeeper superbuild with dependencies included https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1258 lines
46 KiB

  1. #!/usr/bin/perl -w
  2. use warnings;
  3. use strict;
  4. use Text::Wrap;
  5. my $srcpath = undef;
  6. my $wikipath = undef;
  7. my $warn_about_missing = 0;
  8. my $copy_direction = 0;
  9. foreach (@ARGV) {
  10. $warn_about_missing = 1, next if $_ eq '--warn-about-missing';
  11. $copy_direction = 1, next if $_ eq '--copy-to-headers';
  12. $copy_direction = 1, next if $_ eq '--copy-to-header';
  13. $copy_direction = -1, next if $_ eq '--copy-to-wiki';
  14. $copy_direction = -2, next if $_ eq '--copy-to-manpages';
  15. $srcpath = $_, next if not defined $srcpath;
  16. $wikipath = $_, next if not defined $wikipath;
  17. }
  18. my $wordwrap_mode = 'mediawiki';
  19. sub wordwrap_atom { # don't call this directly.
  20. my $str = shift;
  21. return fill('', '', $str);
  22. }
  23. sub wordwrap_with_bullet_indent { # don't call this directly.
  24. my $bullet = shift;
  25. my $str = shift;
  26. my $retval = '';
  27. #print("WORDWRAP BULLET ('$bullet'):\n\n$str\n\n");
  28. # You _can't_ (at least with Pandoc) have a bullet item with a newline in
  29. # MediaWiki, so _remove_ wrapping!
  30. if ($wordwrap_mode eq 'mediawiki') {
  31. $retval = "$bullet$str";
  32. $retval =~ s/\n/ /gms;
  33. $retval =~ s/\s+$//gms;
  34. #print("WORDWRAP BULLET DONE:\n\n$retval\n\n");
  35. return "$retval\n";
  36. }
  37. my $bulletlen = length($bullet);
  38. # wrap it and then indent each line to be under the bullet.
  39. $Text::Wrap::columns -= $bulletlen;
  40. my @wrappedlines = split /\n/, wordwrap_atom($str);
  41. $Text::Wrap::columns += $bulletlen;
  42. my $prefix = $bullet;
  43. my $usual_prefix = ' ' x $bulletlen;
  44. foreach (@wrappedlines) {
  45. $retval .= "$prefix$_\n";
  46. $prefix = $usual_prefix;
  47. }
  48. return $retval;
  49. }
  50. sub wordwrap_one_paragraph { # don't call this directly.
  51. my $retval = '';
  52. my $p = shift;
  53. #print "\n\n\nPARAGRAPH: [$p]\n\n\n";
  54. if ($p =~ s/\A([\*\-] )//) { # bullet list, starts with "* " or "- ".
  55. my $bullet = $1;
  56. my $item = '';
  57. my @items = split /\n/, $p;
  58. foreach (@items) {
  59. if (s/\A([\*\-] )//) {
  60. $retval .= wordwrap_with_bullet_indent($bullet, $item);
  61. $item = '';
  62. }
  63. s/\A\s*//;
  64. $item .= "$_\n"; # accumulate lines until we hit the end or another bullet.
  65. }
  66. if ($item ne '') {
  67. $retval .= wordwrap_with_bullet_indent($bullet, $item);
  68. }
  69. } else {
  70. $retval = wordwrap_atom($p) . "\n";
  71. }
  72. return $retval;
  73. }
  74. sub wordwrap_paragraphs { # don't call this directly.
  75. my $str = shift;
  76. my $retval = '';
  77. my @paragraphs = split /\n\n/, $str;
  78. foreach (@paragraphs) {
  79. next if $_ eq '';
  80. $retval .= wordwrap_one_paragraph($_);
  81. $retval .= "\n";
  82. }
  83. return $retval;
  84. }
  85. my $wordwrap_default_columns = 76;
  86. sub wordwrap {
  87. my $str = shift;
  88. my $columns = shift;
  89. $columns = $wordwrap_default_columns if not defined $columns;
  90. $columns += $wordwrap_default_columns if $columns < 0;
  91. $Text::Wrap::columns = $columns;
  92. my $retval = '';
  93. #print("\n\nWORDWRAP:\n\n$str\n\n\n");
  94. $str =~ s/\A\n+//ms;
  95. while ($str =~ s/(.*?)(\`\`\`.*?\`\`\`|\<syntaxhighlight.*?\<\/syntaxhighlight\>)//ms) {
  96. #print("\n\nWORDWRAP BLOCK:\n\n$1\n\n ===\n\n$2\n\n\n");
  97. $retval .= wordwrap_paragraphs($1); # wrap it.
  98. $retval .= "$2\n\n"; # don't wrap it.
  99. }
  100. $retval .= wordwrap_paragraphs($str); # wrap what's left.
  101. $retval =~ s/\n+\Z//ms;
  102. #print("\n\nWORDWRAP DONE:\n\n$retval\n\n\n");
  103. return $retval;
  104. }
  105. # This assumes you're moving from Markdown (in the Doxygen data) to Wiki, which
  106. # is why the 'md' section is so sparse.
  107. sub wikify_chunk {
  108. my $wikitype = shift;
  109. my $str = shift;
  110. my $codelang = shift;
  111. my $code = shift;
  112. #print("\n\nWIKIFY CHUNK:\n\n$str\n\n\n");
  113. if ($wikitype eq 'mediawiki') {
  114. # convert `code` things first, so they aren't mistaken for other markdown items.
  115. my $codedstr = '';
  116. while ($str =~ s/\A(.*?)\`(.*?)\`//ms) {
  117. my $codeblock = $2;
  118. $codedstr .= wikify_chunk($wikitype, $1, undef, undef);
  119. # Convert obvious SDL things to wikilinks, even inside `code` blocks.
  120. $codeblock =~ s/\b(SDL_[a-zA-Z0-9_]+)/[[$1]]/gms;
  121. $codedstr .= "<code>$codeblock</code>";
  122. }
  123. # Convert obvious SDL things to wikilinks.
  124. $str =~ s/\b(SDL_[a-zA-Z0-9_]+)/[[$1]]/gms;
  125. # Make some Markdown things into MediaWiki...
  126. # bold+italic
  127. $str =~ s/\*\*\*(.*?)\*\*\*/'''''$1'''''/gms;
  128. # bold
  129. $str =~ s/\*\*(.*?)\*\*/'''$1'''/gms;
  130. # italic
  131. $str =~ s/\*(.*?)\*/''$1''/gms;
  132. # bullets
  133. $str =~ s/^\- /* /gm;
  134. $str = $codedstr . $str;
  135. if (defined $code) {
  136. $str .= "<syntaxhighlight lang='$codelang'>$code<\/syntaxhighlight>";
  137. }
  138. } elsif ($wikitype eq 'md') {
  139. # Convert obvious SDL things to wikilinks.
  140. $str =~ s/\b(SDL_[a-zA-Z0-9_]+)/[$1]($1)/gms;
  141. if (defined $code) {
  142. $str .= "```$codelang$code```";
  143. }
  144. }
  145. #print("\n\nWIKIFY CHUNK DONE:\n\n$str\n\n\n");
  146. return $str;
  147. }
  148. sub wikify {
  149. my $wikitype = shift;
  150. my $str = shift;
  151. my $retval = '';
  152. #print("WIKIFY WHOLE:\n\n$str\n\n\n");
  153. while ($str =~ s/\A(.*?)\`\`\`(c\+\+|c)(.*?)\`\`\`//ms) {
  154. $retval .= wikify_chunk($wikitype, $1, $2, $3);
  155. }
  156. $retval .= wikify_chunk($wikitype, $str, undef, undef);
  157. #print("WIKIFY WHOLE DONE:\n\n$retval\n\n\n");
  158. return $retval;
  159. }
  160. my $dewikify_mode = 'md';
  161. my $dewikify_manpage_code_indent = 1;
  162. sub dewikify_chunk {
  163. my $wikitype = shift;
  164. my $str = shift;
  165. my $codelang = shift;
  166. my $code = shift;
  167. #print("\n\nDEWIKIFY CHUNK:\n\n$str\n\n\n");
  168. if ($dewikify_mode eq 'md') {
  169. if ($wikitype eq 'mediawiki') {
  170. # Doxygen supports Markdown (and it just simply looks better than MediaWiki
  171. # when looking at the raw headers), so do some conversions here as necessary.
  172. $str =~ s/\[\[(SDL_[a-zA-Z0-9_]+)\]\]/$1/gms; # Dump obvious wikilinks.
  173. # <code></code> is also popular. :/
  174. $str =~ s/\<code>(.*?)<\/code>/`$1`/gms;
  175. # bold+italic
  176. $str =~ s/'''''(.*?)'''''/***$1***/gms;
  177. # bold
  178. $str =~ s/'''(.*?)'''/**$1**/gms;
  179. # italic
  180. $str =~ s/''(.*?)''/*$1*/gms;
  181. # bullets
  182. $str =~ s/^\* /- /gm;
  183. }
  184. if (defined $code) {
  185. $str .= "```$codelang$code```";
  186. }
  187. } elsif ($dewikify_mode eq 'manpage') {
  188. $str =~ s/\./\\[char46]/gms; # make sure these can't become control codes.
  189. if ($wikitype eq 'mediawiki') {
  190. $str =~ s/\s*\[\[(SDL_[a-zA-Z0-9_]+)\]\]\s*/\n.BR $1\n/gms; # Dump obvious wikilinks.
  191. # <code></code> is also popular. :/
  192. $str =~ s/\s*\<code>(.*?)<\/code>\s*/\n.BR $1\n/gms;
  193. # bold+italic
  194. $str =~ s/\s*'''''(.*?)'''''\s*/\n.BI $1\n/gms;
  195. # bold
  196. $str =~ s/\s*'''(.*?)'''\s*/\n.B $1\n/gms;
  197. # italic
  198. $str =~ s/\s*''(.*?)''\s*/\n.I $1\n/gms;
  199. # bullets
  200. $str =~ s/^\* /\n\\\(bu /gm;
  201. } else {
  202. die("Unexpected wikitype when converting to manpages\n"); # !!! FIXME: need to handle Markdown wiki pages.
  203. }
  204. if (defined $code) {
  205. $code =~ s/\A\n+//gms;
  206. $code =~ s/\n+\Z//gms;
  207. if ($dewikify_manpage_code_indent) {
  208. $str .= "\n.IP\n"
  209. } else {
  210. $str .= "\n.PP\n"
  211. }
  212. $str .= ".EX\n$code\n.EE\n.PP\n";
  213. }
  214. } else {
  215. die("Unexpected dewikify_mode\n");
  216. }
  217. #print("\n\nDEWIKIFY CHUNK DONE:\n\n$str\n\n\n");
  218. return $str;
  219. }
  220. sub dewikify {
  221. my $wikitype = shift;
  222. my $str = shift;
  223. return '' if not defined $str;
  224. #print("DEWIKIFY WHOLE:\n\n$str\n\n\n");
  225. $str =~ s/\A[\s\n]*\= .*? \=\s*?\n+//ms;
  226. $str =~ s/\A[\s\n]*\=\= .*? \=\=\s*?\n+//ms;
  227. my $retval = '';
  228. while ($str =~ s/\A(.*?)<syntaxhighlight lang='?(.*?)'?>(.*?)<\/syntaxhighlight\>//ms) {
  229. $retval .= dewikify_chunk($wikitype, $1, $2, $3);
  230. }
  231. $retval .= dewikify_chunk($wikitype, $str, undef, undef);
  232. #print("DEWIKIFY WHOLE DONE:\n\n$retval\n\n\n");
  233. return $retval;
  234. }
  235. sub usage {
  236. die("USAGE: $0 <source code git clone path> <wiki git clone path> [--copy-to-headers|--copy-to-wiki|--copy-to-manpages] [--warn-about-missing]\n\n");
  237. }
  238. usage() if not defined $srcpath;
  239. usage() if not defined $wikipath;
  240. #usage() if $copy_direction == 0;
  241. my @standard_wiki_sections = (
  242. 'Draft',
  243. '[Brief]',
  244. 'Deprecated',
  245. 'Syntax',
  246. 'Function Parameters',
  247. 'Return Value',
  248. 'Remarks',
  249. 'Version',
  250. 'Code Examples',
  251. 'Related Functions'
  252. );
  253. # Sections that only ever exist in the wiki and shouldn't be deleted when
  254. # not found in the headers.
  255. my %only_wiki_sections = ( # The ones don't mean anything, I just need to check for key existence.
  256. 'Draft', 1,
  257. 'Code Examples', 1
  258. );
  259. my %headers = (); # $headers{"SDL_audio.h"} -> reference to an array of all lines of text in SDL_audio.h.
  260. my %headerfuncs = (); # $headerfuncs{"SDL_OpenAudio"} -> string of header documentation for SDL_OpenAudio, with comment '*' bits stripped from the start. Newlines embedded!
  261. my %headerdecls = ();
  262. my %headerfuncslocation = (); # $headerfuncslocation{"SDL_OpenAudio"} -> name of header holding SDL_OpenAudio define ("SDL_audio.h" in this case).
  263. my %headerfuncschunk = (); # $headerfuncschunk{"SDL_OpenAudio"} -> offset in array in %headers that should be replaced for this function.
  264. my %headerfuncshasdoxygen = (); # $headerfuncschunk{"SDL_OpenAudio"} -> 1 if there was no existing doxygen for this function.
  265. my $incpath = "$srcpath/include";
  266. opendir(DH, $incpath) or die("Can't opendir '$incpath': $!\n");
  267. while (readdir(DH)) {
  268. my $dent = $_;
  269. next if not $dent =~ /\ASDL.*?\.h\Z/; # just SDL*.h headers.
  270. open(FH, '<', "$incpath/$dent") or die("Can't open '$incpath/$dent': $!\n");
  271. my @contents = ();
  272. while (<FH>) {
  273. chomp;
  274. my $decl;
  275. my @templines;
  276. my $str;
  277. my $has_doxygen = 1;
  278. if (/\A\s*extern\s+(SDL_DEPRECATED\s+|)DECLSPEC/) { # a function declaration without a doxygen comment?
  279. @templines = ();
  280. $decl = $_;
  281. $str = '';
  282. $has_doxygen = 0;
  283. } elsif (not /\A\/\*\*\s*\Z/) { # not doxygen comment start?
  284. push @contents, $_;
  285. next;
  286. } else { # Start of a doxygen comment, parse it out.
  287. @templines = ( $_ );
  288. while (<FH>) {
  289. chomp;
  290. push @templines, $_;
  291. last if /\A\s*\*\/\Z/;
  292. if (s/\A\s*\*\s*\`\`\`/```/) { # this is a hack, but a lot of other code relies on the whitespace being trimmed, but we can't trim it in code blocks...
  293. $str .= "$_\n";
  294. while (<FH>) {
  295. chomp;
  296. push @templines, $_;
  297. s/\A\s*\*\s?//;
  298. if (s/\A\s*\`\`\`/```/) {
  299. $str .= "$_\n";
  300. last;
  301. } else {
  302. $str .= "$_\n";
  303. }
  304. }
  305. } else {
  306. s/\A\s*\*\s*//;
  307. $str .= "$_\n";
  308. }
  309. }
  310. $decl = <FH>;
  311. $decl = '' if not defined $decl;
  312. chomp($decl);
  313. if (not $decl =~ /\A\s*extern\s+(SDL_DEPRECATED\s+|)DECLSPEC/) {
  314. #print "Found doxygen but no function sig:\n$str\n\n";
  315. foreach (@templines) {
  316. push @contents, $_;
  317. }
  318. push @contents, $decl;
  319. next;
  320. }
  321. }
  322. my @decllines = ( $decl );
  323. if (not $decl =~ /\)\s*;/) {
  324. while (<FH>) {
  325. chomp;
  326. push @decllines, $_;
  327. s/\A\s+//;
  328. s/\s+\Z//;
  329. $decl .= " $_";
  330. last if /\)\s*;/;
  331. }
  332. }
  333. $decl =~ s/\s+\);\Z/);/;
  334. $decl =~ s/\s+\Z//;
  335. #print("DECL: [$decl]\n");
  336. my $fn = '';
  337. if ($decl =~ /\A\s*extern\s+(SDL_DEPRECATED\s+|)DECLSPEC\s+(const\s+|)(unsigned\s+|)(.*?)\s*(\*?)\s*SDLCALL\s+(.*?)\s*\((.*?)\);/) {
  338. $fn = $6;
  339. #$decl =~ s/\A\s*extern\s+DECLSPEC\s+(.*?)\s+SDLCALL/$1/;
  340. } else {
  341. #print "Found doxygen but no function sig:\n$str\n\n";
  342. foreach (@templines) {
  343. push @contents, $_;
  344. }
  345. foreach (@decllines) {
  346. push @contents, $_;
  347. }
  348. next;
  349. }
  350. $decl = ''; # build this with the line breaks, since it looks better for syntax highlighting.
  351. foreach (@decllines) {
  352. if ($decl eq '') {
  353. $decl = $_;
  354. $decl =~ s/\Aextern\s+(SDL_DEPRECATED\s+|)DECLSPEC\s+(.*?)\s+(\*?)SDLCALL\s+/$2$3 /;
  355. } else {
  356. my $trimmed = $_;
  357. # !!! FIXME: trim space for SDL_DEPRECATED if it was used, too.
  358. $trimmed =~ s/\A\s{24}//; # 24 for shrinking to match the removed "extern DECLSPEC SDLCALL "
  359. $decl .= $trimmed;
  360. }
  361. $decl .= "\n";
  362. }
  363. #print("$fn:\n$str\n\n");
  364. # There might be multiple declarations of a function due to #ifdefs,
  365. # and only one of them will have documentation. If we hit an
  366. # undocumented one before, delete the placeholder line we left for
  367. # it so it doesn't accumulate a new blank line on each run.
  368. my $skipfn = 0;
  369. if (defined $headerfuncshasdoxygen{$fn}) {
  370. if ($headerfuncshasdoxygen{$fn} == 0) { # An undocumented declaration already exists, nuke its placeholder line.
  371. delete $contents[$headerfuncschunk{$fn}]; # delete DOES NOT RENUMBER existing elements!
  372. } else { # documented function already existed?
  373. $skipfn = 1; # don't add this copy to the list of functions.
  374. if ($has_doxygen) {
  375. print STDERR "WARNING: Function '$fn' appears to be documented in multiple locations. Only keeping the first one we saw!\n";
  376. }
  377. push @contents, join("\n", @decllines); # just put the existing declation in as-is.
  378. }
  379. }
  380. if (!$skipfn) {
  381. $headerfuncs{$fn} = $str;
  382. $headerdecls{$fn} = $decl;
  383. $headerfuncslocation{$fn} = $dent;
  384. $headerfuncschunk{$fn} = scalar(@contents);
  385. $headerfuncshasdoxygen{$fn} = $has_doxygen;
  386. push @contents, join("\n", @templines);
  387. push @contents, join("\n", @decllines);
  388. }
  389. }
  390. close(FH);
  391. $headers{$dent} = \@contents;
  392. }
  393. closedir(DH);
  394. # !!! FIXME: we need to parse enums and typedefs and structs and defines and and and and and...
  395. # !!! FIXME: (but functions are good enough for now.)
  396. my %wikitypes = (); # contains string of wiki page extension, like $wikitypes{"SDL_OpenAudio"} == 'mediawiki'
  397. my %wikifuncs = (); # contains references to hash of strings, each string being the full contents of a section of a wiki page, like $wikifuncs{"SDL_OpenAudio"}{"Remarks"}.
  398. my %wikisectionorder = (); # contains references to array, each array item being a key to a wikipage section in the correct order, like $wikisectionorder{"SDL_OpenAudio"}[2] == 'Remarks'
  399. opendir(DH, $wikipath) or die("Can't opendir '$wikipath': $!\n");
  400. while (readdir(DH)) {
  401. my $dent = $_;
  402. my $type = '';
  403. if ($dent =~ /\ASDL.*?\.(md|mediawiki)\Z/) {
  404. $type = $1;
  405. } else {
  406. next; # only dealing with wiki pages.
  407. }
  408. open(FH, '<', "$wikipath/$dent") or die("Can't open '$wikipath/$dent': $!\n");
  409. my $current_section = '[start]';
  410. my @section_order = ( $current_section );
  411. my $fn = $dent;
  412. $fn =~ s/\..*\Z//;
  413. my %sections = ();
  414. $sections{$current_section} = '';
  415. while (<FH>) {
  416. chomp;
  417. my $orig = $_;
  418. s/\A\s*//;
  419. s/\s*\Z//;
  420. if ($type eq 'mediawiki') {
  421. if (/\A\= (.*?) \=\Z/) {
  422. $current_section = ($1 eq $fn) ? '[Brief]' : $1;
  423. die("Doubly-defined section '$current_section' in '$dent'!\n") if defined $sections{$current_section};
  424. push @section_order, $current_section;
  425. $sections{$current_section} = '';
  426. } elsif (/\A\=\= (.*?) \=\=\Z/) {
  427. $current_section = ($1 eq $fn) ? '[Brief]' : $1;
  428. die("Doubly-defined section '$current_section' in '$dent'!\n") if defined $sections{$current_section};
  429. push @section_order, $current_section;
  430. $sections{$current_section} = '';
  431. next;
  432. } elsif (/\A\-\-\-\-\Z/) {
  433. $current_section = '[footer]';
  434. die("Doubly-defined section '$current_section' in '$dent'!\n") if defined $sections{$current_section};
  435. push @section_order, $current_section;
  436. $sections{$current_section} = '';
  437. next;
  438. }
  439. } elsif ($type eq 'md') {
  440. if (/\A\#+ (.*?)\Z/) {
  441. $current_section = ($1 eq $fn) ? '[Brief]' : $1;
  442. die("Doubly-defined section '$current_section' in '$dent'!\n") if defined $sections{$current_section};
  443. push @section_order, $current_section;
  444. $sections{$current_section} = '';
  445. next;
  446. } elsif (/\A\-\-\-\-\Z/) {
  447. $current_section = '[footer]';
  448. die("Doubly-defined section '$current_section' in '$dent'!\n") if defined $sections{$current_section};
  449. push @section_order, $current_section;
  450. $sections{$current_section} = '';
  451. next;
  452. }
  453. } else {
  454. die("Unexpected wiki file type. Fixme!\n");
  455. }
  456. $sections{$current_section} .= "$orig\n";
  457. }
  458. close(FH);
  459. foreach (keys %sections) {
  460. $sections{$_} =~ s/\A\n+//;
  461. $sections{$_} =~ s/\n+\Z//;
  462. $sections{$_} .= "\n";
  463. }
  464. if (0) {
  465. foreach (@section_order) {
  466. print("$fn SECTION '$_':\n");
  467. print($sections{$_});
  468. print("\n\n");
  469. }
  470. }
  471. $wikitypes{$fn} = $type;
  472. $wikifuncs{$fn} = \%sections;
  473. $wikisectionorder{$fn} = \@section_order;
  474. }
  475. closedir(DH);
  476. if ($warn_about_missing) {
  477. foreach (keys %wikifuncs) {
  478. my $fn = $_;
  479. if (not defined $headerfuncs{$fn}) {
  480. print("WARNING: $fn defined in the wiki but not the headers!\n");
  481. }
  482. }
  483. foreach (keys %headerfuncs) {
  484. my $fn = $_;
  485. if (not defined $wikifuncs{$fn}) {
  486. print("WARNING: $fn defined in the headers but not the wiki!\n");
  487. }
  488. }
  489. }
  490. if ($copy_direction == 1) { # --copy-to-headers
  491. my %changed_headers = ();
  492. $dewikify_mode = 'md';
  493. $wordwrap_mode = 'md'; # the headers use Markdown format.
  494. foreach (keys %headerfuncs) {
  495. my $fn = $_;
  496. next if not defined $wikifuncs{$fn}; # don't have a page for that function, skip it.
  497. my $wikitype = $wikitypes{$fn};
  498. my $sectionsref = $wikifuncs{$fn};
  499. my $remarks = %$sectionsref{'Remarks'};
  500. my $params = %$sectionsref{'Function Parameters'};
  501. my $returns = %$sectionsref{'Return Value'};
  502. my $version = %$sectionsref{'Version'};
  503. my $related = %$sectionsref{'Related Functions'};
  504. my $deprecated = %$sectionsref{'Deprecated'};
  505. my $brief = %$sectionsref{'[Brief]'};
  506. my $addblank = 0;
  507. my $str = '';
  508. $headerfuncshasdoxygen{$fn} = 1; # Added/changed doxygen for this header.
  509. $brief = dewikify($wikitype, $brief);
  510. $brief =~ s/\A(.*?\.) /$1\n/; # \brief should only be one sentence, delimited by a period+space. Split if necessary.
  511. my @briefsplit = split /\n/, $brief;
  512. $brief = shift @briefsplit;
  513. if (defined $remarks) {
  514. $remarks = join("\n", @briefsplit) . dewikify($wikitype, $remarks);
  515. }
  516. if (defined $brief) {
  517. $str .= "\n" if $addblank; $addblank = 1;
  518. $str .= wordwrap($brief) . "\n";
  519. }
  520. if (defined $remarks) {
  521. $str .= "\n" if $addblank; $addblank = 1;
  522. $str .= wordwrap($remarks) . "\n";
  523. }
  524. if (defined $deprecated) {
  525. # !!! FIXME: lots of code duplication in all of these.
  526. $str .= "\n" if $addblank; $addblank = 1;
  527. my $v = dewikify($wikitype, $deprecated);
  528. my $whitespacelen = length("\\deprecated") + 1;
  529. my $whitespace = ' ' x $whitespacelen;
  530. $v = wordwrap($v, -$whitespacelen);
  531. my @desclines = split /\n/, $v;
  532. my $firstline = shift @desclines;
  533. $str .= "\\deprecated $firstline\n";
  534. foreach (@desclines) {
  535. $str .= "${whitespace}$_\n";
  536. }
  537. }
  538. if (defined $params) {
  539. $str .= "\n" if $addblank; $addblank = (defined $returns) ? 0 : 1;
  540. my @lines = split /\n/, dewikify($wikitype, $params);
  541. if ($wikitype eq 'mediawiki') {
  542. die("Unexpected data parsing MediaWiki table") if (shift @lines ne '{|'); # Dump the '{|' start
  543. while (scalar(@lines) >= 3) {
  544. my $name = shift @lines;
  545. my $desc = shift @lines;
  546. my $terminator = shift @lines; # the '|-' or '|}' line.
  547. last if ($terminator ne '|-') and ($terminator ne '|}'); # we seem to have run out of table.
  548. $name =~ s/\A\|\s*//;
  549. $name =~ s/\A\*\*(.*?)\*\*/$1/;
  550. $name =~ s/\A\'\'\'(.*?)\'\'\'/$1/;
  551. $desc =~ s/\A\|\s*//;
  552. #print STDERR "FN: $fn NAME: $name DESC: $desc TERM: $terminator\n";
  553. my $whitespacelen = length($name) + 8;
  554. my $whitespace = ' ' x $whitespacelen;
  555. $desc = wordwrap($desc, -$whitespacelen);
  556. my @desclines = split /\n/, $desc;
  557. my $firstline = shift @desclines;
  558. $str .= "\\param $name $firstline\n";
  559. foreach (@desclines) {
  560. $str .= "${whitespace}$_\n";
  561. }
  562. }
  563. } else {
  564. die("write me");
  565. }
  566. }
  567. if (defined $returns) {
  568. $str .= "\n" if $addblank; $addblank = 1;
  569. my $r = dewikify($wikitype, $returns);
  570. my $retstr = "\\returns";
  571. if ($r =~ s/\AReturn(s?) //) {
  572. $retstr = "\\return$1";
  573. }
  574. my $whitespacelen = length($retstr) + 1;
  575. my $whitespace = ' ' x $whitespacelen;
  576. $r = wordwrap($r, -$whitespacelen);
  577. my @desclines = split /\n/, $r;
  578. my $firstline = shift @desclines;
  579. $str .= "$retstr $firstline\n";
  580. foreach (@desclines) {
  581. $str .= "${whitespace}$_\n";
  582. }
  583. }
  584. if (defined $version) {
  585. # !!! FIXME: lots of code duplication in all of these.
  586. $str .= "\n" if $addblank; $addblank = 1;
  587. my $v = dewikify($wikitype, $version);
  588. my $whitespacelen = length("\\since") + 1;
  589. my $whitespace = ' ' x $whitespacelen;
  590. $v = wordwrap($v, -$whitespacelen);
  591. my @desclines = split /\n/, $v;
  592. my $firstline = shift @desclines;
  593. $str .= "\\since $firstline\n";
  594. foreach (@desclines) {
  595. $str .= "${whitespace}$_\n";
  596. }
  597. }
  598. if (defined $related) {
  599. # !!! FIXME: lots of code duplication in all of these.
  600. $str .= "\n" if $addblank; $addblank = 1;
  601. my $v = dewikify($wikitype, $related);
  602. my @desclines = split /\n/, $v;
  603. foreach (@desclines) {
  604. s/\A(\:|\* )//;
  605. s/\(\)\Z//; # Convert "SDL_Func()" to "SDL_Func"
  606. $str .= "\\sa $_\n";
  607. }
  608. }
  609. my $header = $headerfuncslocation{$fn};
  610. my $contentsref = $headers{$header};
  611. my $chunk = $headerfuncschunk{$fn};
  612. my @lines = split /\n/, $str;
  613. my $addnewline = (($chunk > 0) && ($$contentsref[$chunk-1] ne '')) ? "\n" : '';
  614. my $output = "$addnewline/**\n";
  615. foreach (@lines) {
  616. chomp;
  617. s/\s*\Z//;
  618. if ($_ eq '') {
  619. $output .= " *\n";
  620. } else {
  621. $output .= " * $_\n";
  622. }
  623. }
  624. $output .= " */";
  625. #print("$fn:\n$output\n\n");
  626. $$contentsref[$chunk] = $output;
  627. #$$contentsref[$chunk+1] = $headerdecls{$fn};
  628. $changed_headers{$header} = 1;
  629. }
  630. foreach (keys %changed_headers) {
  631. my $header = $_;
  632. # this is kinda inefficient, but oh well.
  633. my @removelines = ();
  634. foreach (keys %headerfuncslocation) {
  635. my $fn = $_;
  636. next if $headerfuncshasdoxygen{$fn};
  637. next if $headerfuncslocation{$fn} ne $header;
  638. # the index of the blank line we put before the function declaration in case we needed to replace it with new content from the wiki.
  639. push @removelines, $headerfuncschunk{$fn};
  640. }
  641. my $contentsref = $headers{$header};
  642. foreach (@removelines) {
  643. delete $$contentsref[$_]; # delete DOES NOT RENUMBER existing elements!
  644. }
  645. my $path = "$incpath/$header.tmp";
  646. open(FH, '>', $path) or die("Can't open '$path': $!\n");
  647. foreach (@$contentsref) {
  648. print FH "$_\n" if defined $_;
  649. }
  650. close(FH);
  651. rename($path, "$incpath/$header") or die("Can't rename '$path' to '$incpath/$header': $!\n");
  652. }
  653. } elsif ($copy_direction == -1) { # --copy-to-wiki
  654. foreach (keys %headerfuncs) {
  655. my $fn = $_;
  656. next if not $headerfuncshasdoxygen{$fn};
  657. my $wikitype = defined $wikitypes{$fn} ? $wikitypes{$fn} : 'mediawiki'; # default to MediaWiki for new stuff FOR NOW.
  658. die("Unexpected wikitype '$wikitype'\n") if (($wikitype ne 'mediawiki') and ($wikitype ne 'md') and ($wikitype ne 'manpage'));
  659. #print("$fn\n"); next;
  660. $wordwrap_mode = $wikitype;
  661. my $raw = $headerfuncs{$fn}; # raw doxygen text with comment characters stripped from start/end and start of each line.
  662. next if not defined $raw;
  663. $raw =~ s/\A\s*\\brief\s+//; # Technically we don't need \brief (please turn on JAVADOC_AUTOBRIEF if you use Doxygen), so just in case one is present, strip it.
  664. my @doxygenlines = split /\n/, $raw;
  665. my $brief = '';
  666. while (@doxygenlines) {
  667. last if $doxygenlines[0] =~ /\A\\/; # some sort of doxygen command, assume we're past the general remarks.
  668. last if $doxygenlines[0] =~ /\A\s*\Z/; # blank line? End of paragraph, done.
  669. my $l = shift @doxygenlines;
  670. chomp($l);
  671. $l =~ s/\A\s*//;
  672. $l =~ s/\s*\Z//;
  673. $brief .= "$l ";
  674. }
  675. $brief =~ s/\A(.*?\.) /$1\n\n/; # \brief should only be one sentence, delimited by a period+space. Split if necessary.
  676. my @briefsplit = split /\n/, $brief;
  677. $brief = wikify($wikitype, shift @briefsplit) . "\n";
  678. @doxygenlines = (@briefsplit, @doxygenlines);
  679. my $remarks = '';
  680. # !!! FIXME: wordwrap and wikify might handle this, now.
  681. while (@doxygenlines) {
  682. last if $doxygenlines[0] =~ /\A\\/; # some sort of doxygen command, assume we're past the general remarks.
  683. my $l = shift @doxygenlines;
  684. if ($l =~ /\A\`\`\`/) { # syntax highlighting, don't reformat.
  685. $remarks .= "$l\n";
  686. while ((@doxygenlines) && (not $l =~ /\`\`\`\Z/)) {
  687. $l = shift @doxygenlines;
  688. $remarks .= "$l\n";
  689. }
  690. } else {
  691. $l =~ s/\A\s*//;
  692. $l =~ s/\s*\Z//;
  693. $remarks .= "$l\n";
  694. }
  695. }
  696. #print("REMARKS:\n\n $remarks\n\n");
  697. $remarks = wordwrap(wikify($wikitype, $remarks));
  698. $remarks =~ s/\A\s*//;
  699. $remarks =~ s/\s*\Z//;
  700. my $decl = $headerdecls{$fn};
  701. #$decl =~ s/\*\s+SDLCALL/ *SDLCALL/; # Try to make "void * Function" become "void *Function"
  702. #$decl =~ s/\A\s*extern\s+(SDL_DEPRECATED\s+|)DECLSPEC\s+(.*?)\s+(\*?)SDLCALL/$2$3/;
  703. my $syntax = '';
  704. if ($wikitype eq 'mediawiki') {
  705. $syntax = "<syntaxhighlight lang='c'>\n$decl</syntaxhighlight>\n";
  706. } elsif ($wikitype eq 'md') {
  707. $syntax = "```c\n$decl\n```\n";
  708. } else { die("Expected wikitype '$wikitype'\n"); }
  709. my %sections = ();
  710. $sections{'[Brief]'} = $brief; # include this section even if blank so we get a title line.
  711. $sections{'Remarks'} = "$remarks\n" if $remarks ne '';
  712. $sections{'Syntax'} = $syntax;
  713. my @params = (); # have to parse these and build up the wiki tables after, since Markdown needs to know the length of the largest string. :/
  714. while (@doxygenlines) {
  715. my $l = shift @doxygenlines;
  716. if ($l =~ /\A\\param\s+(.*?)\s+(.*)\Z/) {
  717. my $arg = $1;
  718. my $desc = $2;
  719. while (@doxygenlines) {
  720. my $subline = $doxygenlines[0];
  721. $subline =~ s/\A\s*//;
  722. last if $subline =~ /\A\\/; # some sort of doxygen command, assume we're past this thing.
  723. shift @doxygenlines; # dump this line from the array; we're using it.
  724. if ($subline eq '') { # empty line, make sure it keeps the newline char.
  725. $desc .= "\n";
  726. } else {
  727. $desc .= " $subline";
  728. }
  729. }
  730. $desc =~ s/[\s\n]+\Z//ms;
  731. # We need to know the length of the longest string to make Markdown tables, so we just store these off until everything is parsed.
  732. push @params, $arg;
  733. push @params, $desc;
  734. } elsif ($l =~ /\A\\r(eturns?)\s+(.*)\Z/) {
  735. my $retstr = "R$1"; # "Return" or "Returns"
  736. my $desc = $2;
  737. while (@doxygenlines) {
  738. my $subline = $doxygenlines[0];
  739. $subline =~ s/\A\s*//;
  740. last if $subline =~ /\A\\/; # some sort of doxygen command, assume we're past this thing.
  741. shift @doxygenlines; # dump this line from the array; we're using it.
  742. if ($subline eq '') { # empty line, make sure it keeps the newline char.
  743. $desc .= "\n";
  744. } else {
  745. $desc .= " $subline";
  746. }
  747. }
  748. $desc =~ s/[\s\n]+\Z//ms;
  749. $sections{'Return Value'} = wordwrap("$retstr " . wikify($wikitype, $desc)) . "\n";
  750. } elsif ($l =~ /\A\\deprecated\s+(.*)\Z/) {
  751. my $desc = $1;
  752. while (@doxygenlines) {
  753. my $subline = $doxygenlines[0];
  754. $subline =~ s/\A\s*//;
  755. last if $subline =~ /\A\\/; # some sort of doxygen command, assume we're past this thing.
  756. shift @doxygenlines; # dump this line from the array; we're using it.
  757. if ($subline eq '') { # empty line, make sure it keeps the newline char.
  758. $desc .= "\n";
  759. } else {
  760. $desc .= " $subline";
  761. }
  762. }
  763. $desc =~ s/[\s\n]+\Z//ms;
  764. $sections{'Deprecated'} = wordwrap(wikify($wikitype, $desc)) . "\n";
  765. } elsif ($l =~ /\A\\since\s+(.*)\Z/) {
  766. my $desc = $1;
  767. while (@doxygenlines) {
  768. my $subline = $doxygenlines[0];
  769. $subline =~ s/\A\s*//;
  770. last if $subline =~ /\A\\/; # some sort of doxygen command, assume we're past this thing.
  771. shift @doxygenlines; # dump this line from the array; we're using it.
  772. if ($subline eq '') { # empty line, make sure it keeps the newline char.
  773. $desc .= "\n";
  774. } else {
  775. $desc .= " $subline";
  776. }
  777. }
  778. $desc =~ s/[\s\n]+\Z//ms;
  779. $sections{'Version'} = wordwrap(wikify($wikitype, $desc)) . "\n";
  780. } elsif ($l =~ /\A\\sa\s+(.*)\Z/) {
  781. my $sa = $1;
  782. $sa =~ s/\(\)\Z//; # Convert "SDL_Func()" to "SDL_Func"
  783. $sections{'Related Functions'} = '' if not defined $sections{'Related Functions'};
  784. if ($wikitype eq 'mediawiki') {
  785. $sections{'Related Functions'} .= ":[[$sa]]\n";
  786. } elsif ($wikitype eq 'md') {
  787. $sections{'Related Functions'} .= "* [$sa](/$sa)\n";
  788. } else { die("Expected wikitype '$wikitype'\n"); }
  789. }
  790. }
  791. # Make sure this ends with a double-newline.
  792. $sections{'Related Functions'} .= "\n" if defined $sections{'Related Functions'};
  793. # We can build the wiki table now that we have all the data.
  794. if (scalar(@params) > 0) {
  795. my $str = '';
  796. if ($wikitype eq 'mediawiki') {
  797. while (scalar(@params) > 0) {
  798. my $arg = shift @params;
  799. my $desc = wikify($wikitype, shift @params);
  800. $str .= ($str eq '') ? "{|\n" : "|-\n";
  801. $str .= "|'''$arg'''\n";
  802. $str .= "|$desc\n";
  803. }
  804. $str .= "|}\n";
  805. } elsif ($wikitype eq 'md') {
  806. my $longest_arg = 0;
  807. my $longest_desc = 0;
  808. my $which = 0;
  809. foreach (@params) {
  810. if ($which == 0) {
  811. my $len = length($_) + 4;
  812. $longest_arg = $len if ($len > $longest_arg);
  813. $which = 1;
  814. } else {
  815. my $len = length(wikify($wikitype, $_));
  816. $longest_desc = $len if ($len > $longest_desc);
  817. $which = 0;
  818. }
  819. }
  820. # Markdown tables are sort of obnoxious.
  821. $str .= '| ' . (' ' x ($longest_arg+4)) . ' | ' . (' ' x $longest_desc) . " |\n";
  822. $str .= '| ' . ('-' x ($longest_arg+4)) . ' | ' . ('-' x $longest_desc) . " |\n";
  823. while (@params) {
  824. my $arg = shift @params;
  825. my $desc = wikify($wikitype, shift @params);
  826. $str .= "| **$arg** " . (' ' x ($longest_arg - length($arg))) . "| $desc" . (' ' x ($longest_desc - length($desc))) . " |\n";
  827. }
  828. } else {
  829. die("Unexpected wikitype!\n"); # should have checked this elsewhere.
  830. }
  831. $sections{'Function Parameters'} = $str;
  832. }
  833. my $path = "$wikipath/$_.${wikitype}.tmp";
  834. open(FH, '>', $path) or die("Can't open '$path': $!\n");
  835. my $sectionsref = $wikifuncs{$fn};
  836. foreach (@standard_wiki_sections) {
  837. # drop sections we either replaced or removed from the original wiki's contents.
  838. if (not defined $only_wiki_sections{$_}) {
  839. delete($$sectionsref{$_});
  840. }
  841. }
  842. my $wikisectionorderref = $wikisectionorder{$fn};
  843. # Make sure there's a footer in the wiki that puts this function in CategoryAPI...
  844. if (not $$sectionsref{'[footer]'}) {
  845. $$sectionsref{'[footer]'} = '';
  846. push @$wikisectionorderref, '[footer]';
  847. }
  848. # !!! FIXME: This won't be CategoryAPI if we eventually handle things other than functions.
  849. my $footer = $$sectionsref{'[footer]'};
  850. if ($wikitype eq 'mediawiki') {
  851. $footer =~ s/\[\[CategoryAPI\]\],?\s*//g;
  852. $footer = '[[CategoryAPI]]' . (($footer eq '') ? "\n" : ", $footer");
  853. } elsif ($wikitype eq 'md') {
  854. $footer =~ s/\[CategoryAPI\]\(CategoryAPI\),?\s*//g;
  855. $footer = '[CategoryAPI](CategoryAPI)' . (($footer eq '') ? '' : ', ') . $footer;
  856. } else { die("Unexpected wikitype '$wikitype'\n"); }
  857. $$sectionsref{'[footer]'} = $footer;
  858. my $prevsectstr = '';
  859. my @ordered_sections = (@standard_wiki_sections, defined $wikisectionorderref ? @$wikisectionorderref : ()); # this copies the arrays into one.
  860. foreach (@ordered_sections) {
  861. my $sect = $_;
  862. next if $sect eq '[start]';
  863. next if (not defined $sections{$sect} and not defined $$sectionsref{$sect});
  864. my $section = defined $sections{$sect} ? $sections{$sect} : $$sectionsref{$sect};
  865. if ($sect eq '[footer]') {
  866. # Make sure previous section ends with two newlines.
  867. if (substr($prevsectstr, -1) ne "\n") {
  868. print FH "\n\n";
  869. } elsif (substr($prevsectstr, -2) ne "\n\n") {
  870. print FH "\n";
  871. }
  872. print FH "----\n"; # It's the same in Markdown and MediaWiki.
  873. } elsif ($sect eq '[Brief]') {
  874. if ($wikitype eq 'mediawiki') {
  875. print FH "= $fn =\n\n";
  876. } elsif ($wikitype eq 'md') {
  877. print FH "# $fn\n\n";
  878. } else { die("Unexpected wikitype '$wikitype'\n"); }
  879. } else {
  880. if ($wikitype eq 'mediawiki') {
  881. print FH "\n== $sect ==\n\n";
  882. } elsif ($wikitype eq 'md') {
  883. print FH "\n## $sect\n\n";
  884. } else { die("Unexpected wikitype '$wikitype'\n"); }
  885. }
  886. my $sectstr = defined $sections{$sect} ? $sections{$sect} : $$sectionsref{$sect};
  887. print FH $sectstr;
  888. $prevsectstr = $sectstr;
  889. # make sure these don't show up twice.
  890. delete($sections{$sect});
  891. delete($$sectionsref{$sect});
  892. }
  893. print FH "\n\n";
  894. close(FH);
  895. rename($path, "$wikipath/$_.${wikitype}") or die("Can't rename '$path' to '$wikipath/$_.${wikitype}': $!\n");
  896. }
  897. } elsif ($copy_direction == -2) { # --copy-to-manpages
  898. # This only takes from the wiki data, since it has sections we omit from the headers, like code examples.
  899. my $manpath = "$srcpath/man";
  900. mkdir($manpath);
  901. $manpath .= "/man3";
  902. mkdir($manpath);
  903. $dewikify_mode = 'manpage';
  904. $wordwrap_mode = 'manpage';
  905. my $introtxt = '';
  906. if (0) {
  907. open(FH, '<', "$srcpath/LICENSE.txt") or die("Can't open '$srcpath/LICENSE.txt': $!\n");
  908. while (<FH>) {
  909. chomp;
  910. $introtxt .= ".\\\" $_\n";
  911. }
  912. close(FH);
  913. }
  914. my $gitrev = `cd "$srcpath" ; git rev-list HEAD~..`;
  915. chomp($gitrev);
  916. open(FH, '<', "$srcpath/include/SDL_version.h") or die("Can't open '$srcpath/include/SDL_version.h': $!\n");
  917. my $majorver = 0;
  918. my $minorver = 0;
  919. my $patchver = 0;
  920. while (<FH>) {
  921. chomp;
  922. if (/\A\#define SDL_MAJOR_VERSION\s+(\d+)\Z/) {
  923. $majorver = int($1);
  924. } elsif (/\A\#define SDL_MINOR_VERSION\s+(\d+)\Z/) {
  925. $minorver = int($1);
  926. } elsif (/\A\#define SDL_PATCHLEVEL\s+(\d+)\Z/) {
  927. $patchver = int($1);
  928. }
  929. }
  930. close(FH);
  931. my $sdlversion = "$majorver.$minorver.$patchver";
  932. foreach (keys %headerfuncs) {
  933. my $fn = $_;
  934. next if not defined $wikifuncs{$fn}; # don't have a page for that function, skip it.
  935. my $wikitype = $wikitypes{$fn};
  936. my $sectionsref = $wikifuncs{$fn};
  937. my $remarks = %$sectionsref{'Remarks'};
  938. my $params = %$sectionsref{'Function Parameters'};
  939. my $returns = %$sectionsref{'Return Value'};
  940. my $version = %$sectionsref{'Version'};
  941. my $related = %$sectionsref{'Related Functions'};
  942. my $examples = %$sectionsref{'Code Examples'};
  943. my $deprecated = %$sectionsref{'Deprecated'};
  944. my $brief = %$sectionsref{'[Brief]'};
  945. my $decl = $headerdecls{$fn};
  946. my $str = '';
  947. $brief = "$brief";
  948. $brief =~ s/\A[\s\n]*\= .*? \=\s*?\n+//ms;
  949. $brief =~ s/\A[\s\n]*\=\= .*? \=\=\s*?\n+//ms;
  950. $brief =~ s/\A(.*?\.) /$1\n/; # \brief should only be one sentence, delimited by a period+space. Split if necessary.
  951. my @briefsplit = split /\n/, $brief;
  952. $brief = shift @briefsplit;
  953. $brief = dewikify($wikitype, $brief);
  954. if (defined $remarks) {
  955. $remarks = dewikify($wikitype, join("\n", @briefsplit) . $remarks);
  956. }
  957. $str .= $introtxt;
  958. $str .= ".\\\" This manpage content is licensed under Creative Commons\n";
  959. $str .= ".\\\" Attribution 4.0 International (CC BY 4.0)\n";
  960. $str .= ".\\\" https://creativecommons.org/licenses/by/4.0/\n";
  961. $str .= ".\\\" This manpage was generated from SDL's wiki page for $fn:\n";
  962. $str .= ".\\\" https://wiki.libsdl.org/$fn\n";
  963. $str .= ".\\\" Generated with SDL/build-scripts/wikiheaders.pl\n";
  964. $str .= ".\\\" revision $gitrev\n" if $gitrev ne '';
  965. $str .= ".\\\" Please report issues in this manpage's content at:\n";
  966. $str .= ".\\\" https://github.com/libsdl-org/sdlwiki/issues/new?title=Feedback%20on%20page%20$fn\n";
  967. $str .= ".\\\" Please report issues in the generation of this manpage from the wiki at:\n";
  968. $str .= ".\\\" https://github.com/libsdl-org/SDL/issues/new?title=Misgenerated%20manpage%20for%20$fn\n";
  969. $str .= ".\\\" SDL can be found at https://libsdl.org/\n";
  970. $str .= ".TH $fn 3 \"SDL $sdlversion\" \"Simple Directmedia Layer\" \"SDL$majorver FUNCTIONS\"\n";
  971. $str .= ".SH NAME\n";
  972. $str .= "$fn";
  973. $str .= " \\- $brief" if (defined $brief);
  974. $str .= "\n";
  975. $str .= ".SH SYNOPSIS\n";
  976. $str .= ".nf\n";
  977. $str .= ".B #include \\(dqSDL.h\\(dq\n";
  978. $str .= ".PP\n";
  979. my @decllines = split /\n/, $decl;
  980. foreach (@decllines) {
  981. $str .= ".BI \"$_\n";
  982. }
  983. $str .= ".fi\n";
  984. if (defined $remarks) {
  985. $str .= ".SH DESCRIPTION\n";
  986. $str .= $remarks . "\n";
  987. }
  988. if (defined $deprecated) {
  989. $str .= ".SH DEPRECATED\n";
  990. $str .= dewikify($wikitype, $deprecated) . "\n";
  991. }
  992. if (defined $params) {
  993. $str .= ".SH FUNCTION PARAMETERS\n";
  994. my @lines = split /\n/, $params;
  995. if ($wikitype eq 'mediawiki') {
  996. die("Unexpected data parsing MediaWiki table") if (shift @lines ne '{|'); # Dump the '{|' start
  997. while (scalar(@lines) >= 3) {
  998. my $name = shift @lines;
  999. my $desc = shift @lines;
  1000. my $terminator = shift @lines; # the '|-' or '|}' line.
  1001. last if ($terminator ne '|-') and ($terminator ne '|}'); # we seem to have run out of table.
  1002. $name =~ s/\A\|\s*//;
  1003. $name =~ s/\A\*\*(.*?)\*\*/$1/;
  1004. $name =~ s/\A\'\'\'(.*?)\'\'\'/$1/;
  1005. $desc =~ s/\A\|\s*//;
  1006. $desc = dewikify($wikitype, $desc);
  1007. #print STDERR "FN: $fn NAME: $name DESC: $desc TERM: $terminator\n";
  1008. $str .= ".TP\n";
  1009. $str .= ".I $name\n";
  1010. $str .= "$desc\n";
  1011. }
  1012. } else {
  1013. die("write me");
  1014. }
  1015. }
  1016. if (defined $returns) {
  1017. $str .= ".SH RETURN VALUE\n";
  1018. $str .= dewikify($wikitype, $returns) . "\n";
  1019. }
  1020. if (defined $examples) {
  1021. $str .= ".SH CODE EXAMPLES\n";
  1022. $dewikify_manpage_code_indent = 0;
  1023. $str .= dewikify($wikitype, $examples) . "\n";
  1024. $dewikify_manpage_code_indent = 1;
  1025. }
  1026. if (defined $version) {
  1027. $str .= ".SH AVAILABILITY\n";
  1028. $str .= dewikify($wikitype, $version) . "\n";
  1029. }
  1030. if (defined $related) {
  1031. $str .= ".SH SEE ALSO\n";
  1032. # !!! FIXME: lots of code duplication in all of these.
  1033. my $v = dewikify($wikitype, $related);
  1034. my @desclines = split /\n/, $v;
  1035. my $nextstr = '';
  1036. foreach (@desclines) {
  1037. s/\A(\:|\* )//;
  1038. s/\(\)\Z//; # Convert "SDL_Func()" to "SDL_Func"
  1039. s/\A\.BR\s+//; # dewikify added this, but we want to handle it.
  1040. s/\A\s+//;
  1041. s/\s+\Z//;
  1042. next if $_ eq '';
  1043. $str .= "$nextstr.BR $_ (3)";
  1044. $nextstr = ",\n";
  1045. }
  1046. $str .= "\n";
  1047. }
  1048. if (0) {
  1049. $str .= ".SH COPYRIGHT\n";
  1050. $str .= "This manpage is licensed under\n";
  1051. $str .= ".UR https://creativecommons.org/licenses/by/4.0/\n";
  1052. $str .= "Creative Commons Attribution 4.0 International (CC BY 4.0)\n";
  1053. $str .= ".UE\n";
  1054. $str .= ".PP\n";
  1055. $str .= "This manpage was generated from\n";
  1056. $str .= ".UR https://wiki.libsdl.org/$fn\n";
  1057. $str .= "SDL's wiki\n";
  1058. $str .= ".UE\n";
  1059. $str .= "using SDL/build-scripts/wikiheaders.pl";
  1060. $str .= " revision $gitrev" if $gitrev ne '';
  1061. $str .= ".\n";
  1062. $str .= "Please report issues in this manpage at\n";
  1063. $str .= ".UR https://github.com/libsdl-org/sdlwiki/issues/new\n";
  1064. $str .= "our bugtracker!\n";
  1065. $str .= ".UE\n";
  1066. }
  1067. my $path = "$manpath/$_.3.tmp";
  1068. open(FH, '>', $path) or die("Can't open '$path': $!\n");
  1069. print FH $str;
  1070. close(FH);
  1071. rename($path, "$manpath/$_.3") or die("Can't rename '$path' to '$manpath/$_.3': $!\n");
  1072. }
  1073. }
  1074. # end of wikiheaders.pl ...