wikiheaders.pl 33 KB

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