wikiheaders.pl 35 KB

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