wikiheaders.pl 31 KB

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