wikiheaders.pl 37 KB

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