wikiheaders.pl 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291
  1. #!/usr/bin/perl -w
  2. use warnings;
  3. use strict;
  4. use Text::Wrap;
  5. $Text::Wrap::huge = 'overflow';
  6. my $srcpath = undef;
  7. my $wikipath = undef;
  8. my $warn_about_missing = 0;
  9. my $copy_direction = 0;
  10. foreach (@ARGV) {
  11. $warn_about_missing = 1, next if $_ eq '--warn-about-missing';
  12. $copy_direction = 1, next if $_ eq '--copy-to-headers';
  13. $copy_direction = 1, next if $_ eq '--copy-to-header';
  14. $copy_direction = -1, next if $_ eq '--copy-to-wiki';
  15. $copy_direction = -2, next if $_ eq '--copy-to-manpages';
  16. $srcpath = $_, next if not defined $srcpath;
  17. $wikipath = $_, next if not defined $wikipath;
  18. }
  19. my $wordwrap_mode = 'mediawiki';
  20. sub wordwrap_atom { # don't call this directly.
  21. my $str = shift;
  22. my $retval = '';
  23. # wordwrap but leave links intact, even if they overflow.
  24. if ($wordwrap_mode eq 'mediawiki') {
  25. while ($str =~ s/(.*?)\s*(\[https?\:\/\/.*?\s+.*?\])\s*//ms) {
  26. $retval .= fill('', '', $1); # wrap it.
  27. $retval .= "\n$2\n"; # don't wrap it.
  28. }
  29. } elsif ($wordwrap_mode eq 'md') {
  30. while ($str =~ s/(.*?)\s*(\[.*?\]\(https?\:\/\/.*?\))\s*//ms) {
  31. $retval .= fill('', '', $1); # wrap it.
  32. $retval .= "\n$2\n"; # don't wrap it.
  33. }
  34. }
  35. return $retval . fill('', '', $str);
  36. }
  37. sub wordwrap_with_bullet_indent { # don't call this directly.
  38. my $bullet = shift;
  39. my $str = shift;
  40. my $retval = '';
  41. #print("WORDWRAP BULLET ('$bullet'):\n\n$str\n\n");
  42. # You _can't_ (at least with Pandoc) have a bullet item with a newline in
  43. # MediaWiki, so _remove_ wrapping!
  44. if ($wordwrap_mode eq 'mediawiki') {
  45. $retval = "$bullet$str";
  46. $retval =~ s/\n/ /gms;
  47. $retval =~ s/\s+$//gms;
  48. #print("WORDWRAP BULLET DONE:\n\n$retval\n\n");
  49. return "$retval\n";
  50. }
  51. my $bulletlen = length($bullet);
  52. # wrap it and then indent each line to be under the bullet.
  53. $Text::Wrap::columns -= $bulletlen;
  54. my @wrappedlines = split /\n/, wordwrap_atom($str);
  55. $Text::Wrap::columns += $bulletlen;
  56. my $prefix = $bullet;
  57. my $usual_prefix = ' ' x $bulletlen;
  58. foreach (@wrappedlines) {
  59. $retval .= "$prefix$_\n";
  60. $prefix = $usual_prefix;
  61. }
  62. return $retval;
  63. }
  64. sub wordwrap_one_paragraph { # don't call this directly.
  65. my $retval = '';
  66. my $p = shift;
  67. #print "\n\n\nPARAGRAPH: [$p]\n\n\n";
  68. if ($p =~ s/\A([\*\-] )//) { # bullet list, starts with "* " or "- ".
  69. my $bullet = $1;
  70. my $item = '';
  71. my @items = split /\n/, $p;
  72. foreach (@items) {
  73. if (s/\A([\*\-] )//) {
  74. $retval .= wordwrap_with_bullet_indent($bullet, $item);
  75. $item = '';
  76. }
  77. s/\A\s*//;
  78. $item .= "$_\n"; # accumulate lines until we hit the end or another bullet.
  79. }
  80. if ($item ne '') {
  81. $retval .= wordwrap_with_bullet_indent($bullet, $item);
  82. }
  83. } else {
  84. $retval = wordwrap_atom($p) . "\n";
  85. }
  86. return $retval;
  87. }
  88. sub wordwrap_paragraphs { # don't call this directly.
  89. my $str = shift;
  90. my $retval = '';
  91. my @paragraphs = split /\n\n/, $str;
  92. foreach (@paragraphs) {
  93. next if $_ eq '';
  94. $retval .= wordwrap_one_paragraph($_);
  95. $retval .= "\n";
  96. }
  97. return $retval;
  98. }
  99. my $wordwrap_default_columns = 76;
  100. sub wordwrap {
  101. my $str = shift;
  102. my $columns = shift;
  103. $columns = $wordwrap_default_columns if not defined $columns;
  104. $columns += $wordwrap_default_columns if $columns < 0;
  105. $Text::Wrap::columns = $columns;
  106. my $retval = '';
  107. #print("\n\nWORDWRAP:\n\n$str\n\n\n");
  108. $str =~ s/\A\n+//ms;
  109. while ($str =~ s/(.*?)(\`\`\`.*?\`\`\`|\<syntaxhighlight.*?\<\/syntaxhighlight\>)//ms) {
  110. #print("\n\nWORDWRAP BLOCK:\n\n$1\n\n ===\n\n$2\n\n\n");
  111. $retval .= wordwrap_paragraphs($1); # wrap it.
  112. $retval .= "$2\n\n"; # don't wrap it.
  113. }
  114. $retval .= wordwrap_paragraphs($str); # wrap what's left.
  115. $retval =~ s/\n+\Z//ms;
  116. #print("\n\nWORDWRAP DONE:\n\n$retval\n\n\n");
  117. return $retval;
  118. }
  119. # This assumes you're moving from Markdown (in the Doxygen data) to Wiki, which
  120. # is why the 'md' section is so sparse.
  121. sub wikify_chunk {
  122. my $wikitype = shift;
  123. my $str = shift;
  124. my $codelang = shift;
  125. my $code = shift;
  126. #print("\n\nWIKIFY CHUNK:\n\n$str\n\n\n");
  127. if ($wikitype eq 'mediawiki') {
  128. # convert `code` things first, so they aren't mistaken for other markdown items.
  129. my $codedstr = '';
  130. while ($str =~ s/\A(.*?)\`(.*?)\`//ms) {
  131. my $codeblock = $2;
  132. $codedstr .= wikify_chunk($wikitype, $1, undef, undef);
  133. # Convert obvious SDL things to wikilinks, even inside `code` blocks.
  134. $codeblock =~ s/\b(SDL_[a-zA-Z0-9_]+)/[[$1]]/gms;
  135. $codedstr .= "<code>$codeblock</code>";
  136. }
  137. # Convert obvious SDL things to wikilinks.
  138. $str =~ s/\b(SDL_[a-zA-Z0-9_]+)/[[$1]]/gms;
  139. # Make some Markdown things into MediaWiki...
  140. # links
  141. $str =~ s/\[(.*?)\]\((https?\:\/\/.*?)\)/\[$2 $1\]/g;
  142. # bold+italic
  143. $str =~ s/\*\*\*(.*?)\*\*\*/'''''$1'''''/gms;
  144. # bold
  145. $str =~ s/\*\*(.*?)\*\*/'''$1'''/gms;
  146. # italic
  147. $str =~ s/\*(.*?)\*/''$1''/gms;
  148. # bullets
  149. $str =~ s/^\- /* /gm;
  150. $str = $codedstr . $str;
  151. if (defined $code) {
  152. $str .= "<syntaxhighlight lang='$codelang'>$code<\/syntaxhighlight>";
  153. }
  154. } elsif ($wikitype eq 'md') {
  155. # Convert obvious SDL things to wikilinks.
  156. $str =~ s/\b(SDL_[a-zA-Z0-9_]+)/[$1]($1)/gms;
  157. if (defined $code) {
  158. $str .= "```$codelang$code```";
  159. }
  160. }
  161. #print("\n\nWIKIFY CHUNK DONE:\n\n$str\n\n\n");
  162. return $str;
  163. }
  164. sub wikify {
  165. my $wikitype = shift;
  166. my $str = shift;
  167. my $retval = '';
  168. #print("WIKIFY WHOLE:\n\n$str\n\n\n");
  169. while ($str =~ s/\A(.*?)\`\`\`(c\+\+|c)(.*?)\`\`\`//ms) {
  170. $retval .= wikify_chunk($wikitype, $1, $2, $3);
  171. }
  172. $retval .= wikify_chunk($wikitype, $str, undef, undef);
  173. #print("WIKIFY WHOLE DONE:\n\n$retval\n\n\n");
  174. return $retval;
  175. }
  176. my $dewikify_mode = 'md';
  177. my $dewikify_manpage_code_indent = 1;
  178. sub dewikify_chunk {
  179. my $wikitype = shift;
  180. my $str = shift;
  181. my $codelang = shift;
  182. my $code = shift;
  183. #print("\n\nDEWIKIFY CHUNK:\n\n$str\n\n\n");
  184. if ($dewikify_mode eq 'md') {
  185. if ($wikitype eq 'mediawiki') {
  186. # Doxygen supports Markdown (and it just simply looks better than MediaWiki
  187. # when looking at the raw headers), so do some conversions here as necessary.
  188. $str =~ s/\[\[(SDL_[a-zA-Z0-9_]+)\]\]/$1/gms; # Dump obvious wikilinks.
  189. # links
  190. $str =~ s/\[(https?\:\/\/.*?)\s+(.*?)\]/\[$2\]\($1\)/g;
  191. # <code></code> is also popular. :/
  192. $str =~ s/\<code>(.*?)<\/code>/`$1`/gms;
  193. # bold+italic
  194. $str =~ s/'''''(.*?)'''''/***$1***/gms;
  195. # bold
  196. $str =~ s/'''(.*?)'''/**$1**/gms;
  197. # italic
  198. $str =~ s/''(.*?)''/*$1*/gms;
  199. # bullets
  200. $str =~ s/^\* /- /gm;
  201. }
  202. if (defined $code) {
  203. $str .= "```$codelang$code```";
  204. }
  205. } elsif ($dewikify_mode eq 'manpage') {
  206. $str =~ s/\./\\[char46]/gms; # make sure these can't become control codes.
  207. if ($wikitype eq 'mediawiki') {
  208. $str =~ s/\s*\[\[(SDL_[a-zA-Z0-9_]+)\]\]\s*/\n.BR $1\n/gms; # Dump obvious wikilinks.
  209. # links
  210. $str =~ s/\[(https?\:\/\/.*?)\s+(.*?)\]/\n.URL "$1" "$2"\n/g;
  211. # <code></code> is also popular. :/
  212. $str =~ s/\s*\<code>(.*?)<\/code>\s*/\n.BR $1\n/gms;
  213. # bold+italic
  214. $str =~ s/\s*'''''(.*?)'''''\s*/\n.BI $1\n/gms;
  215. # bold
  216. $str =~ s/\s*'''(.*?)'''\s*/\n.B $1\n/gms;
  217. # italic
  218. $str =~ s/\s*''(.*?)''\s*/\n.I $1\n/gms;
  219. # bullets
  220. $str =~ s/^\* /\n\\\(bu /gm;
  221. } else {
  222. die("Unexpected wikitype when converting to manpages\n"); # !!! FIXME: need to handle Markdown wiki pages.
  223. }
  224. if (defined $code) {
  225. $code =~ s/\A\n+//gms;
  226. $code =~ s/\n+\Z//gms;
  227. if ($dewikify_manpage_code_indent) {
  228. $str .= "\n.IP\n"
  229. } else {
  230. $str .= "\n.PP\n"
  231. }
  232. $str .= ".EX\n$code\n.EE\n.PP\n";
  233. }
  234. } else {
  235. die("Unexpected dewikify_mode\n");
  236. }
  237. #print("\n\nDEWIKIFY CHUNK DONE:\n\n$str\n\n\n");
  238. return $str;
  239. }
  240. sub dewikify {
  241. my $wikitype = shift;
  242. my $str = shift;
  243. return '' if not defined $str;
  244. #print("DEWIKIFY WHOLE:\n\n$str\n\n\n");
  245. $str =~ s/\A[\s\n]*\= .*? \=\s*?\n+//ms;
  246. $str =~ s/\A[\s\n]*\=\= .*? \=\=\s*?\n+//ms;
  247. my $retval = '';
  248. while ($str =~ s/\A(.*?)<syntaxhighlight lang='?(.*?)'?>(.*?)<\/syntaxhighlight\>//ms) {
  249. $retval .= dewikify_chunk($wikitype, $1, $2, $3);
  250. }
  251. $retval .= dewikify_chunk($wikitype, $str, undef, undef);
  252. #print("DEWIKIFY WHOLE DONE:\n\n$retval\n\n\n");
  253. return $retval;
  254. }
  255. sub usage {
  256. die("USAGE: $0 <source code git clone path> <wiki git clone path> [--copy-to-headers|--copy-to-wiki|--copy-to-manpages] [--warn-about-missing]\n\n");
  257. }
  258. usage() if not defined $srcpath;
  259. usage() if not defined $wikipath;
  260. #usage() if $copy_direction == 0;
  261. my @standard_wiki_sections = (
  262. 'Draft',
  263. '[Brief]',
  264. 'Deprecated',
  265. 'Syntax',
  266. 'Function Parameters',
  267. 'Return Value',
  268. 'Remarks',
  269. 'Version',
  270. 'Code Examples',
  271. 'Related Functions'
  272. );
  273. # Sections that only ever exist in the wiki and shouldn't be deleted when
  274. # not found in the headers.
  275. my %only_wiki_sections = ( # The ones don't mean anything, I just need to check for key existence.
  276. 'Draft', 1,
  277. 'Code Examples', 1
  278. );
  279. my %headers = (); # $headers{"SDL_audio.h"} -> reference to an array of all lines of text in SDL_audio.h.
  280. my %headerfuncs = (); # $headerfuncs{"SDL_OpenAudio"} -> string of header documentation for SDL_OpenAudio, with comment '*' bits stripped from the start. Newlines embedded!
  281. my %headerdecls = ();
  282. my %headerfuncslocation = (); # $headerfuncslocation{"SDL_OpenAudio"} -> name of header holding SDL_OpenAudio define ("SDL_audio.h" in this case).
  283. my %headerfuncschunk = (); # $headerfuncschunk{"SDL_OpenAudio"} -> offset in array in %headers that should be replaced for this function.
  284. my %headerfuncshasdoxygen = (); # $headerfuncschunk{"SDL_OpenAudio"} -> 1 if there was no existing doxygen for this function.
  285. my $incpath = "$srcpath/include";
  286. opendir(DH, $incpath) or die("Can't opendir '$incpath': $!\n");
  287. while (readdir(DH)) {
  288. my $dent = $_;
  289. next if not $dent =~ /\ASDL.*?\.h\Z/; # just SDL*.h headers.
  290. open(FH, '<', "$incpath/$dent") or die("Can't open '$incpath/$dent': $!\n");
  291. my @contents = ();
  292. while (<FH>) {
  293. chomp;
  294. my $decl;
  295. my @templines;
  296. my $str;
  297. my $has_doxygen = 1;
  298. if (/\A\s*extern\s+(SDL_DEPRECATED\s+|)DECLSPEC/) { # a function declaration without a doxygen comment?
  299. @templines = ();
  300. $decl = $_;
  301. $str = '';
  302. $has_doxygen = 0;
  303. } elsif (not /\A\/\*\*\s*\Z/) { # not doxygen comment start?
  304. push @contents, $_;
  305. next;
  306. } else { # Start of a doxygen comment, parse it out.
  307. @templines = ( $_ );
  308. while (<FH>) {
  309. chomp;
  310. push @templines, $_;
  311. last if /\A\s*\*\/\Z/;
  312. 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...
  313. $str .= "$_\n";
  314. while (<FH>) {
  315. chomp;
  316. push @templines, $_;
  317. s/\A\s*\*\s?//;
  318. if (s/\A\s*\`\`\`/```/) {
  319. $str .= "$_\n";
  320. last;
  321. } else {
  322. $str .= "$_\n";
  323. }
  324. }
  325. } else {
  326. s/\A\s*\*\s*//;
  327. $str .= "$_\n";
  328. }
  329. }
  330. $decl = <FH>;
  331. $decl = '' if not defined $decl;
  332. chomp($decl);
  333. if (not $decl =~ /\A\s*extern\s+(SDL_DEPRECATED\s+|)DECLSPEC/) {
  334. #print "Found doxygen but no function sig:\n$str\n\n";
  335. foreach (@templines) {
  336. push @contents, $_;
  337. }
  338. push @contents, $decl;
  339. next;
  340. }
  341. }
  342. my @decllines = ( $decl );
  343. if (not $decl =~ /\)\s*;/) {
  344. while (<FH>) {
  345. chomp;
  346. push @decllines, $_;
  347. s/\A\s+//;
  348. s/\s+\Z//;
  349. $decl .= " $_";
  350. last if /\)\s*;/;
  351. }
  352. }
  353. $decl =~ s/\s+\);\Z/);/;
  354. $decl =~ s/\s+\Z//;
  355. #print("DECL: [$decl]\n");
  356. my $fn = '';
  357. if ($decl =~ /\A\s*extern\s+(SDL_DEPRECATED\s+|)DECLSPEC\s+(const\s+|)(unsigned\s+|)(.*?)\s*(\*?)\s*SDLCALL\s+(.*?)\s*\((.*?)\);/) {
  358. $fn = $6;
  359. #$decl =~ s/\A\s*extern\s+DECLSPEC\s+(.*?)\s+SDLCALL/$1/;
  360. } else {
  361. #print "Found doxygen but no function sig:\n$str\n\n";
  362. foreach (@templines) {
  363. push @contents, $_;
  364. }
  365. foreach (@decllines) {
  366. push @contents, $_;
  367. }
  368. next;
  369. }
  370. $decl = ''; # build this with the line breaks, since it looks better for syntax highlighting.
  371. foreach (@decllines) {
  372. if ($decl eq '') {
  373. $decl = $_;
  374. $decl =~ s/\Aextern\s+(SDL_DEPRECATED\s+|)DECLSPEC\s+(.*?)\s+(\*?)SDLCALL\s+/$2$3 /;
  375. } else {
  376. my $trimmed = $_;
  377. # !!! FIXME: trim space for SDL_DEPRECATED if it was used, too.
  378. $trimmed =~ s/\A\s{24}//; # 24 for shrinking to match the removed "extern DECLSPEC SDLCALL "
  379. $decl .= $trimmed;
  380. }
  381. $decl .= "\n";
  382. }
  383. #print("$fn:\n$str\n\n");
  384. # There might be multiple declarations of a function due to #ifdefs,
  385. # and only one of them will have documentation. If we hit an
  386. # undocumented one before, delete the placeholder line we left for
  387. # it so it doesn't accumulate a new blank line on each run.
  388. my $skipfn = 0;
  389. if (defined $headerfuncshasdoxygen{$fn}) {
  390. if ($headerfuncshasdoxygen{$fn} == 0) { # An undocumented declaration already exists, nuke its placeholder line.
  391. delete $contents[$headerfuncschunk{$fn}]; # delete DOES NOT RENUMBER existing elements!
  392. } else { # documented function already existed?
  393. $skipfn = 1; # don't add this copy to the list of functions.
  394. if ($has_doxygen) {
  395. print STDERR "WARNING: Function '$fn' appears to be documented in multiple locations. Only keeping the first one we saw!\n";
  396. }
  397. push @contents, join("\n", @decllines); # just put the existing declation in as-is.
  398. }
  399. }
  400. if (!$skipfn) {
  401. $headerfuncs{$fn} = $str;
  402. $headerdecls{$fn} = $decl;
  403. $headerfuncslocation{$fn} = $dent;
  404. $headerfuncschunk{$fn} = scalar(@contents);
  405. $headerfuncshasdoxygen{$fn} = $has_doxygen;
  406. push @contents, join("\n", @templines);
  407. push @contents, join("\n", @decllines);
  408. }
  409. }
  410. close(FH);
  411. $headers{$dent} = \@contents;
  412. }
  413. closedir(DH);
  414. # !!! FIXME: we need to parse enums and typedefs and structs and defines and and and and and...
  415. # !!! FIXME: (but functions are good enough for now.)
  416. my %wikitypes = (); # contains string of wiki page extension, like $wikitypes{"SDL_OpenAudio"} == 'mediawiki'
  417. 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"}.
  418. 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'
  419. opendir(DH, $wikipath) or die("Can't opendir '$wikipath': $!\n");
  420. while (readdir(DH)) {
  421. my $dent = $_;
  422. my $type = '';
  423. if ($dent =~ /\ASDL.*?\.(md|mediawiki)\Z/) {
  424. $type = $1;
  425. } else {
  426. next; # only dealing with wiki pages.
  427. }
  428. open(FH, '<', "$wikipath/$dent") or die("Can't open '$wikipath/$dent': $!\n");
  429. my $current_section = '[start]';
  430. my @section_order = ( $current_section );
  431. my $fn = $dent;
  432. $fn =~ s/\..*\Z//;
  433. my %sections = ();
  434. $sections{$current_section} = '';
  435. while (<FH>) {
  436. chomp;
  437. my $orig = $_;
  438. s/\A\s*//;
  439. s/\s*\Z//;
  440. if ($type eq 'mediawiki') {
  441. if (/\A\= (.*?) \=\Z/) {
  442. $current_section = ($1 eq $fn) ? '[Brief]' : $1;
  443. die("Doubly-defined section '$current_section' in '$dent'!\n") if defined $sections{$current_section};
  444. push @section_order, $current_section;
  445. $sections{$current_section} = '';
  446. } elsif (/\A\=\= (.*?) \=\=\Z/) {
  447. $current_section = ($1 eq $fn) ? '[Brief]' : $1;
  448. die("Doubly-defined section '$current_section' in '$dent'!\n") if defined $sections{$current_section};
  449. push @section_order, $current_section;
  450. $sections{$current_section} = '';
  451. next;
  452. } elsif (/\A\-\-\-\-\Z/) {
  453. $current_section = '[footer]';
  454. die("Doubly-defined section '$current_section' in '$dent'!\n") if defined $sections{$current_section};
  455. push @section_order, $current_section;
  456. $sections{$current_section} = '';
  457. next;
  458. }
  459. } elsif ($type eq 'md') {
  460. if (/\A\#+ (.*?)\Z/) {
  461. $current_section = ($1 eq $fn) ? '[Brief]' : $1;
  462. die("Doubly-defined section '$current_section' in '$dent'!\n") if defined $sections{$current_section};
  463. push @section_order, $current_section;
  464. $sections{$current_section} = '';
  465. next;
  466. } elsif (/\A\-\-\-\-\Z/) {
  467. $current_section = '[footer]';
  468. die("Doubly-defined section '$current_section' in '$dent'!\n") if defined $sections{$current_section};
  469. push @section_order, $current_section;
  470. $sections{$current_section} = '';
  471. next;
  472. }
  473. } else {
  474. die("Unexpected wiki file type. Fixme!\n");
  475. }
  476. $sections{$current_section} .= "$orig\n";
  477. }
  478. close(FH);
  479. foreach (keys %sections) {
  480. $sections{$_} =~ s/\A\n+//;
  481. $sections{$_} =~ s/\n+\Z//;
  482. $sections{$_} .= "\n";
  483. }
  484. if (0) {
  485. foreach (@section_order) {
  486. print("$fn SECTION '$_':\n");
  487. print($sections{$_});
  488. print("\n\n");
  489. }
  490. }
  491. $wikitypes{$fn} = $type;
  492. $wikifuncs{$fn} = \%sections;
  493. $wikisectionorder{$fn} = \@section_order;
  494. }
  495. closedir(DH);
  496. if ($warn_about_missing) {
  497. foreach (keys %wikifuncs) {
  498. my $fn = $_;
  499. if (not defined $headerfuncs{$fn}) {
  500. print("WARNING: $fn defined in the wiki but not the headers!\n");
  501. }
  502. }
  503. foreach (keys %headerfuncs) {
  504. my $fn = $_;
  505. if (not defined $wikifuncs{$fn}) {
  506. print("WARNING: $fn defined in the headers but not the wiki!\n");
  507. }
  508. }
  509. }
  510. if ($copy_direction == 1) { # --copy-to-headers
  511. my %changed_headers = ();
  512. $dewikify_mode = 'md';
  513. $wordwrap_mode = 'md'; # the headers use Markdown format.
  514. foreach (keys %headerfuncs) {
  515. my $fn = $_;
  516. next if not defined $wikifuncs{$fn}; # don't have a page for that function, skip it.
  517. my $wikitype = $wikitypes{$fn};
  518. my $sectionsref = $wikifuncs{$fn};
  519. my $remarks = %$sectionsref{'Remarks'};
  520. my $params = %$sectionsref{'Function Parameters'};
  521. my $returns = %$sectionsref{'Return Value'};
  522. my $version = %$sectionsref{'Version'};
  523. my $related = %$sectionsref{'Related Functions'};
  524. my $deprecated = %$sectionsref{'Deprecated'};
  525. my $brief = %$sectionsref{'[Brief]'};
  526. my $addblank = 0;
  527. my $str = '';
  528. $headerfuncshasdoxygen{$fn} = 1; # Added/changed doxygen for this header.
  529. $brief = dewikify($wikitype, $brief);
  530. $brief =~ s/\A(.*?\.) /$1\n/; # \brief should only be one sentence, delimited by a period+space. Split if necessary.
  531. my @briefsplit = split /\n/, $brief;
  532. $brief = shift @briefsplit;
  533. if (defined $remarks) {
  534. $remarks = join("\n", @briefsplit) . dewikify($wikitype, $remarks);
  535. }
  536. if (defined $brief) {
  537. $str .= "\n" if $addblank; $addblank = 1;
  538. $str .= wordwrap($brief) . "\n";
  539. }
  540. if (defined $remarks) {
  541. $str .= "\n" if $addblank; $addblank = 1;
  542. $str .= wordwrap($remarks) . "\n";
  543. }
  544. if (defined $deprecated) {
  545. # !!! FIXME: lots of code duplication in all of these.
  546. $str .= "\n" if $addblank; $addblank = 1;
  547. my $v = dewikify($wikitype, $deprecated);
  548. my $whitespacelen = length("\\deprecated") + 1;
  549. my $whitespace = ' ' x $whitespacelen;
  550. $v = wordwrap($v, -$whitespacelen);
  551. my @desclines = split /\n/, $v;
  552. my $firstline = shift @desclines;
  553. $str .= "\\deprecated $firstline\n";
  554. foreach (@desclines) {
  555. $str .= "${whitespace}$_\n";
  556. }
  557. }
  558. if (defined $params) {
  559. $str .= "\n" if $addblank; $addblank = (defined $returns) ? 0 : 1;
  560. my @lines = split /\n/, dewikify($wikitype, $params);
  561. if ($wikitype eq 'mediawiki') {
  562. die("Unexpected data parsing MediaWiki table") if (shift @lines ne '{|'); # Dump the '{|' start
  563. while (scalar(@lines) >= 3) {
  564. my $name = shift @lines;
  565. my $desc = shift @lines;
  566. my $terminator = shift @lines; # the '|-' or '|}' line.
  567. last if ($terminator ne '|-') and ($terminator ne '|}'); # we seem to have run out of table.
  568. $name =~ s/\A\|\s*//;
  569. $name =~ s/\A\*\*(.*?)\*\*/$1/;
  570. $name =~ s/\A\'\'\'(.*?)\'\'\'/$1/;
  571. $desc =~ s/\A\|\s*//;
  572. #print STDERR "FN: $fn NAME: $name DESC: $desc TERM: $terminator\n";
  573. my $whitespacelen = length($name) + 8;
  574. my $whitespace = ' ' x $whitespacelen;
  575. $desc = wordwrap($desc, -$whitespacelen);
  576. my @desclines = split /\n/, $desc;
  577. my $firstline = shift @desclines;
  578. $str .= "\\param $name $firstline\n";
  579. foreach (@desclines) {
  580. $str .= "${whitespace}$_\n";
  581. }
  582. }
  583. } else {
  584. die("write me");
  585. }
  586. }
  587. if (defined $returns) {
  588. $str .= "\n" if $addblank; $addblank = 1;
  589. my $r = dewikify($wikitype, $returns);
  590. my $retstr = "\\returns";
  591. if ($r =~ s/\AReturn(s?) //) {
  592. $retstr = "\\return$1";
  593. }
  594. my $whitespacelen = length($retstr) + 1;
  595. my $whitespace = ' ' x $whitespacelen;
  596. $r = wordwrap($r, -$whitespacelen);
  597. my @desclines = split /\n/, $r;
  598. my $firstline = shift @desclines;
  599. $str .= "$retstr $firstline\n";
  600. foreach (@desclines) {
  601. $str .= "${whitespace}$_\n";
  602. }
  603. }
  604. if (defined $version) {
  605. # !!! FIXME: lots of code duplication in all of these.
  606. $str .= "\n" if $addblank; $addblank = 1;
  607. my $v = dewikify($wikitype, $version);
  608. my $whitespacelen = length("\\since") + 1;
  609. my $whitespace = ' ' x $whitespacelen;
  610. $v = wordwrap($v, -$whitespacelen);
  611. my @desclines = split /\n/, $v;
  612. my $firstline = shift @desclines;
  613. $str .= "\\since $firstline\n";
  614. foreach (@desclines) {
  615. $str .= "${whitespace}$_\n";
  616. }
  617. }
  618. if (defined $related) {
  619. # !!! FIXME: lots of code duplication in all of these.
  620. $str .= "\n" if $addblank; $addblank = 1;
  621. my $v = dewikify($wikitype, $related);
  622. my @desclines = split /\n/, $v;
  623. foreach (@desclines) {
  624. s/\A(\:|\* )//;
  625. s/\(\)\Z//; # Convert "SDL_Func()" to "SDL_Func"
  626. $str .= "\\sa $_\n";
  627. }
  628. }
  629. my $header = $headerfuncslocation{$fn};
  630. my $contentsref = $headers{$header};
  631. my $chunk = $headerfuncschunk{$fn};
  632. my @lines = split /\n/, $str;
  633. my $addnewline = (($chunk > 0) && ($$contentsref[$chunk-1] ne '')) ? "\n" : '';
  634. my $output = "$addnewline/**\n";
  635. foreach (@lines) {
  636. chomp;
  637. s/\s*\Z//;
  638. if ($_ eq '') {
  639. $output .= " *\n";
  640. } else {
  641. $output .= " * $_\n";
  642. }
  643. }
  644. $output .= " */";
  645. #print("$fn:\n$output\n\n");
  646. $$contentsref[$chunk] = $output;
  647. #$$contentsref[$chunk+1] = $headerdecls{$fn};
  648. $changed_headers{$header} = 1;
  649. }
  650. foreach (keys %changed_headers) {
  651. my $header = $_;
  652. # this is kinda inefficient, but oh well.
  653. my @removelines = ();
  654. foreach (keys %headerfuncslocation) {
  655. my $fn = $_;
  656. next if $headerfuncshasdoxygen{$fn};
  657. next if $headerfuncslocation{$fn} ne $header;
  658. # 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.
  659. push @removelines, $headerfuncschunk{$fn};
  660. }
  661. my $contentsref = $headers{$header};
  662. foreach (@removelines) {
  663. delete $$contentsref[$_]; # delete DOES NOT RENUMBER existing elements!
  664. }
  665. my $path = "$incpath/$header.tmp";
  666. open(FH, '>', $path) or die("Can't open '$path': $!\n");
  667. foreach (@$contentsref) {
  668. print FH "$_\n" if defined $_;
  669. }
  670. close(FH);
  671. rename($path, "$incpath/$header") or die("Can't rename '$path' to '$incpath/$header': $!\n");
  672. }
  673. } elsif ($copy_direction == -1) { # --copy-to-wiki
  674. foreach (keys %headerfuncs) {
  675. my $fn = $_;
  676. next if not $headerfuncshasdoxygen{$fn};
  677. my $wikitype = defined $wikitypes{$fn} ? $wikitypes{$fn} : 'mediawiki'; # default to MediaWiki for new stuff FOR NOW.
  678. die("Unexpected wikitype '$wikitype'\n") if (($wikitype ne 'mediawiki') and ($wikitype ne 'md') and ($wikitype ne 'manpage'));
  679. #print("$fn\n"); next;
  680. $wordwrap_mode = $wikitype;
  681. my $raw = $headerfuncs{$fn}; # raw doxygen text with comment characters stripped from start/end and start of each line.
  682. next if not defined $raw;
  683. $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.
  684. my @doxygenlines = split /\n/, $raw;
  685. my $brief = '';
  686. while (@doxygenlines) {
  687. last if $doxygenlines[0] =~ /\A\\/; # some sort of doxygen command, assume we're past the general remarks.
  688. last if $doxygenlines[0] =~ /\A\s*\Z/; # blank line? End of paragraph, done.
  689. my $l = shift @doxygenlines;
  690. chomp($l);
  691. $l =~ s/\A\s*//;
  692. $l =~ s/\s*\Z//;
  693. $brief .= "$l ";
  694. }
  695. $brief =~ s/\A(.*?\.) /$1\n\n/; # \brief should only be one sentence, delimited by a period+space. Split if necessary.
  696. my @briefsplit = split /\n/, $brief;
  697. $brief = wikify($wikitype, shift @briefsplit) . "\n";
  698. @doxygenlines = (@briefsplit, @doxygenlines);
  699. my $remarks = '';
  700. # !!! FIXME: wordwrap and wikify might handle this, now.
  701. while (@doxygenlines) {
  702. last if $doxygenlines[0] =~ /\A\\/; # some sort of doxygen command, assume we're past the general remarks.
  703. my $l = shift @doxygenlines;
  704. if ($l =~ /\A\`\`\`/) { # syntax highlighting, don't reformat.
  705. $remarks .= "$l\n";
  706. while ((@doxygenlines) && (not $l =~ /\`\`\`\Z/)) {
  707. $l = shift @doxygenlines;
  708. $remarks .= "$l\n";
  709. }
  710. } else {
  711. $l =~ s/\A\s*//;
  712. $l =~ s/\s*\Z//;
  713. $remarks .= "$l\n";
  714. }
  715. }
  716. #print("REMARKS:\n\n $remarks\n\n");
  717. $remarks = wordwrap(wikify($wikitype, $remarks));
  718. $remarks =~ s/\A\s*//;
  719. $remarks =~ s/\s*\Z//;
  720. my $decl = $headerdecls{$fn};
  721. #$decl =~ s/\*\s+SDLCALL/ *SDLCALL/; # Try to make "void * Function" become "void *Function"
  722. #$decl =~ s/\A\s*extern\s+(SDL_DEPRECATED\s+|)DECLSPEC\s+(.*?)\s+(\*?)SDLCALL/$2$3/;
  723. my $syntax = '';
  724. if ($wikitype eq 'mediawiki') {
  725. $syntax = "<syntaxhighlight lang='c'>\n$decl</syntaxhighlight>\n";
  726. } elsif ($wikitype eq 'md') {
  727. $syntax = "```c\n$decl\n```\n";
  728. } else { die("Expected wikitype '$wikitype'\n"); }
  729. my %sections = ();
  730. $sections{'[Brief]'} = $brief; # include this section even if blank so we get a title line.
  731. $sections{'Remarks'} = "$remarks\n" if $remarks ne '';
  732. $sections{'Syntax'} = $syntax;
  733. my @params = (); # have to parse these and build up the wiki tables after, since Markdown needs to know the length of the largest string. :/
  734. while (@doxygenlines) {
  735. my $l = shift @doxygenlines;
  736. if ($l =~ /\A\\param\s+(.*?)\s+(.*)\Z/) {
  737. my $arg = $1;
  738. my $desc = $2;
  739. while (@doxygenlines) {
  740. my $subline = $doxygenlines[0];
  741. $subline =~ s/\A\s*//;
  742. last if $subline =~ /\A\\/; # some sort of doxygen command, assume we're past this thing.
  743. shift @doxygenlines; # dump this line from the array; we're using it.
  744. if ($subline eq '') { # empty line, make sure it keeps the newline char.
  745. $desc .= "\n";
  746. } else {
  747. $desc .= " $subline";
  748. }
  749. }
  750. $desc =~ s/[\s\n]+\Z//ms;
  751. # We need to know the length of the longest string to make Markdown tables, so we just store these off until everything is parsed.
  752. push @params, $arg;
  753. push @params, $desc;
  754. } elsif ($l =~ /\A\\r(eturns?)\s+(.*)\Z/) {
  755. my $retstr = "R$1"; # "Return" or "Returns"
  756. my $desc = $2;
  757. while (@doxygenlines) {
  758. my $subline = $doxygenlines[0];
  759. $subline =~ s/\A\s*//;
  760. last if $subline =~ /\A\\/; # some sort of doxygen command, assume we're past this thing.
  761. shift @doxygenlines; # dump this line from the array; we're using it.
  762. if ($subline eq '') { # empty line, make sure it keeps the newline char.
  763. $desc .= "\n";
  764. } else {
  765. $desc .= " $subline";
  766. }
  767. }
  768. $desc =~ s/[\s\n]+\Z//ms;
  769. $sections{'Return Value'} = wordwrap("$retstr " . wikify($wikitype, $desc)) . "\n";
  770. } elsif ($l =~ /\A\\deprecated\s+(.*)\Z/) {
  771. my $desc = $1;
  772. while (@doxygenlines) {
  773. my $subline = $doxygenlines[0];
  774. $subline =~ s/\A\s*//;
  775. last if $subline =~ /\A\\/; # some sort of doxygen command, assume we're past this thing.
  776. shift @doxygenlines; # dump this line from the array; we're using it.
  777. if ($subline eq '') { # empty line, make sure it keeps the newline char.
  778. $desc .= "\n";
  779. } else {
  780. $desc .= " $subline";
  781. }
  782. }
  783. $desc =~ s/[\s\n]+\Z//ms;
  784. $sections{'Deprecated'} = wordwrap(wikify($wikitype, $desc)) . "\n";
  785. } elsif ($l =~ /\A\\since\s+(.*)\Z/) {
  786. my $desc = $1;
  787. while (@doxygenlines) {
  788. my $subline = $doxygenlines[0];
  789. $subline =~ s/\A\s*//;
  790. last if $subline =~ /\A\\/; # some sort of doxygen command, assume we're past this thing.
  791. shift @doxygenlines; # dump this line from the array; we're using it.
  792. if ($subline eq '') { # empty line, make sure it keeps the newline char.
  793. $desc .= "\n";
  794. } else {
  795. $desc .= " $subline";
  796. }
  797. }
  798. $desc =~ s/[\s\n]+\Z//ms;
  799. $sections{'Version'} = wordwrap(wikify($wikitype, $desc)) . "\n";
  800. } elsif ($l =~ /\A\\sa\s+(.*)\Z/) {
  801. my $sa = $1;
  802. $sa =~ s/\(\)\Z//; # Convert "SDL_Func()" to "SDL_Func"
  803. $sections{'Related Functions'} = '' if not defined $sections{'Related Functions'};
  804. if ($wikitype eq 'mediawiki') {
  805. $sections{'Related Functions'} .= ":[[$sa]]\n";
  806. } elsif ($wikitype eq 'md') {
  807. $sections{'Related Functions'} .= "* [$sa](/$sa)\n";
  808. } else { die("Expected wikitype '$wikitype'\n"); }
  809. }
  810. }
  811. # Make sure this ends with a double-newline.
  812. $sections{'Related Functions'} .= "\n" if defined $sections{'Related Functions'};
  813. # We can build the wiki table now that we have all the data.
  814. if (scalar(@params) > 0) {
  815. my $str = '';
  816. if ($wikitype eq 'mediawiki') {
  817. while (scalar(@params) > 0) {
  818. my $arg = shift @params;
  819. my $desc = wikify($wikitype, shift @params);
  820. $str .= ($str eq '') ? "{|\n" : "|-\n";
  821. $str .= "|'''$arg'''\n";
  822. $str .= "|$desc\n";
  823. }
  824. $str .= "|}\n";
  825. } elsif ($wikitype eq 'md') {
  826. my $longest_arg = 0;
  827. my $longest_desc = 0;
  828. my $which = 0;
  829. foreach (@params) {
  830. if ($which == 0) {
  831. my $len = length($_) + 4;
  832. $longest_arg = $len if ($len > $longest_arg);
  833. $which = 1;
  834. } else {
  835. my $len = length(wikify($wikitype, $_));
  836. $longest_desc = $len if ($len > $longest_desc);
  837. $which = 0;
  838. }
  839. }
  840. # Markdown tables are sort of obnoxious.
  841. $str .= '| ' . (' ' x ($longest_arg+4)) . ' | ' . (' ' x $longest_desc) . " |\n";
  842. $str .= '| ' . ('-' x ($longest_arg+4)) . ' | ' . ('-' x $longest_desc) . " |\n";
  843. while (@params) {
  844. my $arg = shift @params;
  845. my $desc = wikify($wikitype, shift @params);
  846. $str .= "| **$arg** " . (' ' x ($longest_arg - length($arg))) . "| $desc" . (' ' x ($longest_desc - length($desc))) . " |\n";
  847. }
  848. } else {
  849. die("Unexpected wikitype!\n"); # should have checked this elsewhere.
  850. }
  851. $sections{'Function Parameters'} = $str;
  852. }
  853. my $path = "$wikipath/$_.${wikitype}.tmp";
  854. open(FH, '>', $path) or die("Can't open '$path': $!\n");
  855. my $sectionsref = $wikifuncs{$fn};
  856. foreach (@standard_wiki_sections) {
  857. # drop sections we either replaced or removed from the original wiki's contents.
  858. if (not defined $only_wiki_sections{$_}) {
  859. delete($$sectionsref{$_});
  860. }
  861. }
  862. my $wikisectionorderref = $wikisectionorder{$fn};
  863. # Make sure there's a footer in the wiki that puts this function in CategoryAPI...
  864. if (not $$sectionsref{'[footer]'}) {
  865. $$sectionsref{'[footer]'} = '';
  866. push @$wikisectionorderref, '[footer]';
  867. }
  868. # !!! FIXME: This won't be CategoryAPI if we eventually handle things other than functions.
  869. my $footer = $$sectionsref{'[footer]'};
  870. if ($wikitype eq 'mediawiki') {
  871. $footer =~ s/\[\[CategoryAPI\]\],?\s*//g;
  872. $footer = '[[CategoryAPI]]' . (($footer eq '') ? "\n" : ", $footer");
  873. } elsif ($wikitype eq 'md') {
  874. $footer =~ s/\[CategoryAPI\]\(CategoryAPI\),?\s*//g;
  875. $footer = '[CategoryAPI](CategoryAPI)' . (($footer eq '') ? '' : ', ') . $footer;
  876. } else { die("Unexpected wikitype '$wikitype'\n"); }
  877. $$sectionsref{'[footer]'} = $footer;
  878. my $prevsectstr = '';
  879. my @ordered_sections = (@standard_wiki_sections, defined $wikisectionorderref ? @$wikisectionorderref : ()); # this copies the arrays into one.
  880. foreach (@ordered_sections) {
  881. my $sect = $_;
  882. next if $sect eq '[start]';
  883. next if (not defined $sections{$sect} and not defined $$sectionsref{$sect});
  884. my $section = defined $sections{$sect} ? $sections{$sect} : $$sectionsref{$sect};
  885. if ($sect eq '[footer]') {
  886. # Make sure previous section ends with two newlines.
  887. if (substr($prevsectstr, -1) ne "\n") {
  888. print FH "\n\n";
  889. } elsif (substr($prevsectstr, -2) ne "\n\n") {
  890. print FH "\n";
  891. }
  892. print FH "----\n"; # It's the same in Markdown and MediaWiki.
  893. } elsif ($sect eq '[Brief]') {
  894. if ($wikitype eq 'mediawiki') {
  895. print FH "= $fn =\n\n";
  896. } elsif ($wikitype eq 'md') {
  897. print FH "# $fn\n\n";
  898. } else { die("Unexpected wikitype '$wikitype'\n"); }
  899. } else {
  900. if ($wikitype eq 'mediawiki') {
  901. print FH "\n== $sect ==\n\n";
  902. } elsif ($wikitype eq 'md') {
  903. print FH "\n## $sect\n\n";
  904. } else { die("Unexpected wikitype '$wikitype'\n"); }
  905. }
  906. my $sectstr = defined $sections{$sect} ? $sections{$sect} : $$sectionsref{$sect};
  907. print FH $sectstr;
  908. $prevsectstr = $sectstr;
  909. # make sure these don't show up twice.
  910. delete($sections{$sect});
  911. delete($$sectionsref{$sect});
  912. }
  913. print FH "\n\n";
  914. close(FH);
  915. rename($path, "$wikipath/$_.${wikitype}") or die("Can't rename '$path' to '$wikipath/$_.${wikitype}': $!\n");
  916. }
  917. } elsif ($copy_direction == -2) { # --copy-to-manpages
  918. # This only takes from the wiki data, since it has sections we omit from the headers, like code examples.
  919. my $manpath = "$srcpath/man";
  920. mkdir($manpath);
  921. $manpath .= "/man3";
  922. mkdir($manpath);
  923. $dewikify_mode = 'manpage';
  924. $wordwrap_mode = 'manpage';
  925. my $introtxt = '';
  926. if (0) {
  927. open(FH, '<', "$srcpath/LICENSE.txt") or die("Can't open '$srcpath/LICENSE.txt': $!\n");
  928. while (<FH>) {
  929. chomp;
  930. $introtxt .= ".\\\" $_\n";
  931. }
  932. close(FH);
  933. }
  934. my $gitrev = `cd "$srcpath" ; git rev-list HEAD~..`;
  935. chomp($gitrev);
  936. open(FH, '<', "$srcpath/include/SDL_version.h") or die("Can't open '$srcpath/include/SDL_version.h': $!\n");
  937. my $majorver = 0;
  938. my $minorver = 0;
  939. my $patchver = 0;
  940. while (<FH>) {
  941. chomp;
  942. if (/\A\#define SDL_MAJOR_VERSION\s+(\d+)\Z/) {
  943. $majorver = int($1);
  944. } elsif (/\A\#define SDL_MINOR_VERSION\s+(\d+)\Z/) {
  945. $minorver = int($1);
  946. } elsif (/\A\#define SDL_PATCHLEVEL\s+(\d+)\Z/) {
  947. $patchver = int($1);
  948. }
  949. }
  950. close(FH);
  951. my $sdlversion = "$majorver.$minorver.$patchver";
  952. foreach (keys %headerfuncs) {
  953. my $fn = $_;
  954. next if not defined $wikifuncs{$fn}; # don't have a page for that function, skip it.
  955. my $wikitype = $wikitypes{$fn};
  956. my $sectionsref = $wikifuncs{$fn};
  957. my $remarks = %$sectionsref{'Remarks'};
  958. my $params = %$sectionsref{'Function Parameters'};
  959. my $returns = %$sectionsref{'Return Value'};
  960. my $version = %$sectionsref{'Version'};
  961. my $related = %$sectionsref{'Related Functions'};
  962. my $examples = %$sectionsref{'Code Examples'};
  963. my $deprecated = %$sectionsref{'Deprecated'};
  964. my $brief = %$sectionsref{'[Brief]'};
  965. my $decl = $headerdecls{$fn};
  966. my $str = '';
  967. $brief = "$brief";
  968. $brief =~ s/\A[\s\n]*\= .*? \=\s*?\n+//ms;
  969. $brief =~ s/\A[\s\n]*\=\= .*? \=\=\s*?\n+//ms;
  970. $brief =~ s/\A(.*?\.) /$1\n/; # \brief should only be one sentence, delimited by a period+space. Split if necessary.
  971. my @briefsplit = split /\n/, $brief;
  972. $brief = shift @briefsplit;
  973. $brief = dewikify($wikitype, $brief);
  974. if (defined $remarks) {
  975. $remarks = dewikify($wikitype, join("\n", @briefsplit) . $remarks);
  976. }
  977. $str .= $introtxt;
  978. $str .= ".\\\" This manpage content is licensed under Creative Commons\n";
  979. $str .= ".\\\" Attribution 4.0 International (CC BY 4.0)\n";
  980. $str .= ".\\\" https://creativecommons.org/licenses/by/4.0/\n";
  981. $str .= ".\\\" This manpage was generated from SDL's wiki page for $fn:\n";
  982. $str .= ".\\\" https://wiki.libsdl.org/$fn\n";
  983. $str .= ".\\\" Generated with SDL/build-scripts/wikiheaders.pl\n";
  984. $str .= ".\\\" revision $gitrev\n" if $gitrev ne '';
  985. $str .= ".\\\" Please report issues in this manpage's content at:\n";
  986. $str .= ".\\\" https://github.com/libsdl-org/sdlwiki/issues/new?title=Feedback%20on%20page%20$fn\n";
  987. $str .= ".\\\" Please report issues in the generation of this manpage from the wiki at:\n";
  988. $str .= ".\\\" https://github.com/libsdl-org/SDL/issues/new?title=Misgenerated%20manpage%20for%20$fn\n";
  989. $str .= ".\\\" SDL can be found at https://libsdl.org/\n";
  990. # Define a .URL macro. The "www.tmac" thing decides if we're using GNU roff (which has a .URL macro already), and if so, overrides the macro we just created.
  991. # This wizadry is from https://web.archive.org/web/20060102165607/http://people.debian.org/~branden/talks/wtfm/wtfm.pdf
  992. $str .= ".de URL\n";
  993. $str .= '\\$2 \(laURL: \\$1 \(ra\\$3' . "\n";
  994. $str .= "..\n";
  995. $str .= '.if \n[.g] .mso www.tmac' . "\n";
  996. $str .= ".TH $fn 3 \"SDL $sdlversion\" \"Simple Directmedia Layer\" \"SDL$majorver FUNCTIONS\"\n";
  997. $str .= ".SH NAME\n";
  998. $str .= "$fn";
  999. $str .= " \\- $brief" if (defined $brief);
  1000. $str .= "\n";
  1001. $str .= ".SH SYNOPSIS\n";
  1002. $str .= ".nf\n";
  1003. $str .= ".B #include \\(dqSDL.h\\(dq\n";
  1004. $str .= ".PP\n";
  1005. my @decllines = split /\n/, $decl;
  1006. foreach (@decllines) {
  1007. $str .= ".BI \"$_\n";
  1008. }
  1009. $str .= ".fi\n";
  1010. if (defined $remarks) {
  1011. $str .= ".SH DESCRIPTION\n";
  1012. $str .= $remarks . "\n";
  1013. }
  1014. if (defined $deprecated) {
  1015. $str .= ".SH DEPRECATED\n";
  1016. $str .= dewikify($wikitype, $deprecated) . "\n";
  1017. }
  1018. if (defined $params) {
  1019. $str .= ".SH FUNCTION PARAMETERS\n";
  1020. my @lines = split /\n/, $params;
  1021. if ($wikitype eq 'mediawiki') {
  1022. die("Unexpected data parsing MediaWiki table") if (shift @lines ne '{|'); # Dump the '{|' start
  1023. while (scalar(@lines) >= 3) {
  1024. my $name = shift @lines;
  1025. my $desc = shift @lines;
  1026. my $terminator = shift @lines; # the '|-' or '|}' line.
  1027. last if ($terminator ne '|-') and ($terminator ne '|}'); # we seem to have run out of table.
  1028. $name =~ s/\A\|\s*//;
  1029. $name =~ s/\A\*\*(.*?)\*\*/$1/;
  1030. $name =~ s/\A\'\'\'(.*?)\'\'\'/$1/;
  1031. $desc =~ s/\A\|\s*//;
  1032. $desc = dewikify($wikitype, $desc);
  1033. #print STDERR "FN: $fn NAME: $name DESC: $desc TERM: $terminator\n";
  1034. $str .= ".TP\n";
  1035. $str .= ".I $name\n";
  1036. $str .= "$desc\n";
  1037. }
  1038. } else {
  1039. die("write me");
  1040. }
  1041. }
  1042. if (defined $returns) {
  1043. $str .= ".SH RETURN VALUE\n";
  1044. $str .= dewikify($wikitype, $returns) . "\n";
  1045. }
  1046. if (defined $examples) {
  1047. $str .= ".SH CODE EXAMPLES\n";
  1048. $dewikify_manpage_code_indent = 0;
  1049. $str .= dewikify($wikitype, $examples) . "\n";
  1050. $dewikify_manpage_code_indent = 1;
  1051. }
  1052. if (defined $version) {
  1053. $str .= ".SH AVAILABILITY\n";
  1054. $str .= dewikify($wikitype, $version) . "\n";
  1055. }
  1056. if (defined $related) {
  1057. $str .= ".SH SEE ALSO\n";
  1058. # !!! FIXME: lots of code duplication in all of these.
  1059. my $v = dewikify($wikitype, $related);
  1060. my @desclines = split /\n/, $v;
  1061. my $nextstr = '';
  1062. foreach (@desclines) {
  1063. s/\A(\:|\* )//;
  1064. s/\(\)\Z//; # Convert "SDL_Func()" to "SDL_Func"
  1065. s/\A\.BR\s+//; # dewikify added this, but we want to handle it.
  1066. s/\A\s+//;
  1067. s/\s+\Z//;
  1068. next if $_ eq '';
  1069. $str .= "$nextstr.BR $_ (3)";
  1070. $nextstr = ",\n";
  1071. }
  1072. $str .= "\n";
  1073. }
  1074. if (0) {
  1075. $str .= ".SH COPYRIGHT\n";
  1076. $str .= "This manpage is licensed under\n";
  1077. $str .= ".UR https://creativecommons.org/licenses/by/4.0/\n";
  1078. $str .= "Creative Commons Attribution 4.0 International (CC BY 4.0)\n";
  1079. $str .= ".UE\n";
  1080. $str .= ".PP\n";
  1081. $str .= "This manpage was generated from\n";
  1082. $str .= ".UR https://wiki.libsdl.org/$fn\n";
  1083. $str .= "SDL's wiki\n";
  1084. $str .= ".UE\n";
  1085. $str .= "using SDL/build-scripts/wikiheaders.pl";
  1086. $str .= " revision $gitrev" if $gitrev ne '';
  1087. $str .= ".\n";
  1088. $str .= "Please report issues in this manpage at\n";
  1089. $str .= ".UR https://github.com/libsdl-org/sdlwiki/issues/new\n";
  1090. $str .= "our bugtracker!\n";
  1091. $str .= ".UE\n";
  1092. }
  1093. my $path = "$manpath/$_.3.tmp";
  1094. open(FH, '>', $path) or die("Can't open '$path': $!\n");
  1095. print FH $str;
  1096. close(FH);
  1097. rename($path, "$manpath/$_.3") or die("Can't rename '$path' to '$manpath/$_.3': $!\n");
  1098. }
  1099. }
  1100. # end of wikiheaders.pl ...