wikiheaders.pl 29 KB

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