wikiheaders.pl 31 KB

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