🛠️🐜 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.

1404 lines
51 KiB

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