wikiheaders.pl 29 KB

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