wikiheaders.pl 51 KB

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