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

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