wikiheaders.pl 30 KB

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