wikiheaders.pl 28 KB

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