wikiheaders.pl 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398
  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. # Ignore "Category*" pages.
  500. next if ($dent =~ /\ACategory/);
  501. open(FH, '<', "$wikipath/$dent") or die("Can't open '$wikipath/$dent': $!\n");
  502. my $current_section = '[start]';
  503. my @section_order = ( $current_section );
  504. my $fn = $dent;
  505. $fn =~ s/\..*\Z//;
  506. my %sections = ();
  507. $sections{$current_section} = '';
  508. my $firstline = 1;
  509. while (<FH>) {
  510. chomp;
  511. my $orig = $_;
  512. s/\A\s*//;
  513. s/\s*\Z//;
  514. if ($type eq 'mediawiki') {
  515. if (defined($wikipreamble) && $firstline && /\A\=\=\=\=\=\= (.*?) \=\=\=\=\=\=\Z/ && ($1 eq $wikipreamble)) {
  516. $firstline = 0; # skip this.
  517. next;
  518. } elsif (/\A\= (.*?) \=\Z/) {
  519. $firstline = 0;
  520. $current_section = ($1 eq $fn) ? '[Brief]' : $1;
  521. die("Doubly-defined section '$current_section' in '$dent'!\n") if defined $sections{$current_section};
  522. push @section_order, $current_section;
  523. $sections{$current_section} = '';
  524. } elsif (/\A\=\= (.*?) \=\=\Z/) {
  525. $firstline = 0;
  526. $current_section = ($1 eq $fn) ? '[Brief]' : $1;
  527. die("Doubly-defined section '$current_section' in '$dent'!\n") if defined $sections{$current_section};
  528. push @section_order, $current_section;
  529. $sections{$current_section} = '';
  530. next;
  531. } elsif (/\A\-\-\-\-\Z/) {
  532. $firstline = 0;
  533. $current_section = '[footer]';
  534. die("Doubly-defined section '$current_section' in '$dent'!\n") if defined $sections{$current_section};
  535. push @section_order, $current_section;
  536. $sections{$current_section} = '';
  537. next;
  538. }
  539. } elsif ($type eq 'md') {
  540. if (defined($wikipreamble) && $firstline && /\A\#\#\#\#\#\# (.*?)\Z/ && ($1 eq $wikipreamble)) {
  541. $firstline = 0; # skip this.
  542. next;
  543. } elsif (/\A\#+ (.*?)\Z/) {
  544. $firstline = 0;
  545. $current_section = ($1 eq $fn) ? '[Brief]' : $1;
  546. die("Doubly-defined section '$current_section' in '$dent'!\n") if defined $sections{$current_section};
  547. push @section_order, $current_section;
  548. $sections{$current_section} = '';
  549. next;
  550. } elsif (/\A\-\-\-\-\Z/) {
  551. $firstline = 0;
  552. $current_section = '[footer]';
  553. die("Doubly-defined section '$current_section' in '$dent'!\n") if defined $sections{$current_section};
  554. push @section_order, $current_section;
  555. $sections{$current_section} = '';
  556. next;
  557. }
  558. } else {
  559. die("Unexpected wiki file type. Fixme!\n");
  560. }
  561. if ($firstline) {
  562. $firstline = ($_ ne '');
  563. }
  564. if (!$firstline) {
  565. $sections{$current_section} .= "$orig\n";
  566. }
  567. }
  568. close(FH);
  569. foreach (keys %sections) {
  570. $sections{$_} =~ s/\A\n+//;
  571. $sections{$_} =~ s/\n+\Z//;
  572. $sections{$_} .= "\n";
  573. }
  574. if (0) {
  575. foreach (@section_order) {
  576. print("$fn SECTION '$_':\n");
  577. print($sections{$_});
  578. print("\n\n");
  579. }
  580. }
  581. $wikitypes{$fn} = $type;
  582. $wikifuncs{$fn} = \%sections;
  583. $wikisectionorder{$fn} = \@section_order;
  584. }
  585. closedir(DH);
  586. if ($warn_about_missing) {
  587. foreach (keys %wikifuncs) {
  588. my $fn = $_;
  589. if (not defined $headerfuncs{$fn}) {
  590. print("WARNING: $fn defined in the wiki but not the headers!\n");
  591. }
  592. }
  593. foreach (keys %headerfuncs) {
  594. my $fn = $_;
  595. if (not defined $wikifuncs{$fn}) {
  596. print("WARNING: $fn defined in the headers but not the wiki!\n");
  597. }
  598. }
  599. }
  600. if ($copy_direction == 1) { # --copy-to-headers
  601. my %changed_headers = ();
  602. $dewikify_mode = 'md';
  603. $wordwrap_mode = 'md'; # the headers use Markdown format.
  604. foreach (keys %headerfuncs) {
  605. my $fn = $_;
  606. next if not defined $wikifuncs{$fn}; # don't have a page for that function, skip it.
  607. my $wikitype = $wikitypes{$fn};
  608. my $sectionsref = $wikifuncs{$fn};
  609. my $remarks = %$sectionsref{'Remarks'};
  610. my $params = %$sectionsref{'Function Parameters'};
  611. my $returns = %$sectionsref{'Return Value'};
  612. my $version = %$sectionsref{'Version'};
  613. my $related = %$sectionsref{'Related Functions'};
  614. my $deprecated = %$sectionsref{'Deprecated'};
  615. my $brief = %$sectionsref{'[Brief]'};
  616. my $addblank = 0;
  617. my $str = '';
  618. $headerfuncshasdoxygen{$fn} = 1; # Added/changed doxygen for this header.
  619. $brief = dewikify($wikitype, $brief);
  620. $brief =~ s/\A(.*?\.) /$1\n/; # \brief should only be one sentence, delimited by a period+space. Split if necessary.
  621. my @briefsplit = split /\n/, $brief;
  622. $brief = shift @briefsplit;
  623. if (defined $remarks) {
  624. $remarks = join("\n", @briefsplit) . dewikify($wikitype, $remarks);
  625. }
  626. if (defined $brief) {
  627. $str .= "\n" if $addblank; $addblank = 1;
  628. $str .= wordwrap($brief) . "\n";
  629. }
  630. if (defined $remarks) {
  631. $str .= "\n" if $addblank; $addblank = 1;
  632. $str .= wordwrap($remarks) . "\n";
  633. }
  634. if (defined $deprecated) {
  635. # !!! FIXME: lots of code duplication in all of these.
  636. $str .= "\n" if $addblank; $addblank = 1;
  637. my $v = dewikify($wikitype, $deprecated);
  638. my $whitespacelen = length("\\deprecated") + 1;
  639. my $whitespace = ' ' x $whitespacelen;
  640. $v = wordwrap($v, -$whitespacelen);
  641. my @desclines = split /\n/, $v;
  642. my $firstline = shift @desclines;
  643. $str .= "\\deprecated $firstline\n";
  644. foreach (@desclines) {
  645. $str .= "${whitespace}$_\n";
  646. }
  647. }
  648. if (defined $params) {
  649. $str .= "\n" if $addblank; $addblank = (defined $returns) ? 0 : 1;
  650. my @lines = split /\n/, dewikify($wikitype, $params);
  651. if ($wikitype eq 'mediawiki') {
  652. die("Unexpected data parsing MediaWiki table") if (shift @lines ne '{|'); # Dump the '{|' start
  653. while (scalar(@lines) >= 3) {
  654. my $name = shift @lines;
  655. my $desc = shift @lines;
  656. my $terminator = shift @lines; # the '|-' or '|}' line.
  657. last if ($terminator ne '|-') and ($terminator ne '|}'); # we seem to have run out of table.
  658. $name =~ s/\A\|\s*//;
  659. $name =~ s/\A\*\*(.*?)\*\*/$1/;
  660. $name =~ s/\A\'\'\'(.*?)\'\'\'/$1/;
  661. $desc =~ s/\A\|\s*//;
  662. #print STDERR "FN: $fn NAME: $name DESC: $desc TERM: $terminator\n";
  663. my $whitespacelen = length($name) + 8;
  664. my $whitespace = ' ' x $whitespacelen;
  665. $desc = wordwrap($desc, -$whitespacelen);
  666. my @desclines = split /\n/, $desc;
  667. my $firstline = shift @desclines;
  668. $str .= "\\param $name $firstline\n";
  669. foreach (@desclines) {
  670. $str .= "${whitespace}$_\n";
  671. }
  672. }
  673. } else {
  674. die("write me");
  675. }
  676. }
  677. if (defined $returns) {
  678. $str .= "\n" if $addblank; $addblank = 1;
  679. my $r = dewikify($wikitype, $returns);
  680. my $retstr = "\\returns";
  681. if ($r =~ s/\AReturn(s?) //) {
  682. $retstr = "\\return$1";
  683. }
  684. my $whitespacelen = length($retstr) + 1;
  685. my $whitespace = ' ' x $whitespacelen;
  686. $r = wordwrap($r, -$whitespacelen);
  687. my @desclines = split /\n/, $r;
  688. my $firstline = shift @desclines;
  689. $str .= "$retstr $firstline\n";
  690. foreach (@desclines) {
  691. $str .= "${whitespace}$_\n";
  692. }
  693. }
  694. if (defined $version) {
  695. # !!! FIXME: lots of code duplication in all of these.
  696. $str .= "\n" if $addblank; $addblank = 1;
  697. my $v = dewikify($wikitype, $version);
  698. my $whitespacelen = length("\\since") + 1;
  699. my $whitespace = ' ' x $whitespacelen;
  700. $v = wordwrap($v, -$whitespacelen);
  701. my @desclines = split /\n/, $v;
  702. my $firstline = shift @desclines;
  703. $str .= "\\since $firstline\n";
  704. foreach (@desclines) {
  705. $str .= "${whitespace}$_\n";
  706. }
  707. }
  708. if (defined $related) {
  709. # !!! FIXME: lots of code duplication in all of these.
  710. $str .= "\n" if $addblank; $addblank = 1;
  711. my $v = dewikify($wikitype, $related);
  712. my @desclines = split /\n/, $v;
  713. foreach (@desclines) {
  714. s/\A(\:|\* )//;
  715. s/\(\)\Z//; # Convert "SDL_Func()" to "SDL_Func"
  716. s/\A\/*//;
  717. $str .= "\\sa $_\n";
  718. }
  719. }
  720. my $header = $headerfuncslocation{$fn};
  721. my $contentsref = $headers{$header};
  722. my $chunk = $headerfuncschunk{$fn};
  723. my @lines = split /\n/, $str;
  724. my $addnewline = (($chunk > 0) && ($$contentsref[$chunk-1] ne '')) ? "\n" : '';
  725. my $output = "$addnewline/**\n";
  726. foreach (@lines) {
  727. chomp;
  728. s/\s*\Z//;
  729. if ($_ eq '') {
  730. $output .= " *\n";
  731. } else {
  732. $output .= " * $_\n";
  733. }
  734. }
  735. $output .= " */";
  736. #print("$fn:\n$output\n\n");
  737. $$contentsref[$chunk] = $output;
  738. #$$contentsref[$chunk+1] = $headerdecls{$fn};
  739. $changed_headers{$header} = 1;
  740. }
  741. foreach (keys %changed_headers) {
  742. my $header = $_;
  743. # this is kinda inefficient, but oh well.
  744. my @removelines = ();
  745. foreach (keys %headerfuncslocation) {
  746. my $fn = $_;
  747. next if $headerfuncshasdoxygen{$fn};
  748. next if $headerfuncslocation{$fn} ne $header;
  749. # 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.
  750. push @removelines, $headerfuncschunk{$fn};
  751. }
  752. my $contentsref = $headers{$header};
  753. foreach (@removelines) {
  754. delete $$contentsref[$_]; # delete DOES NOT RENUMBER existing elements!
  755. }
  756. my $path = "$incpath/$header.tmp";
  757. open(FH, '>', $path) or die("Can't open '$path': $!\n");
  758. foreach (@$contentsref) {
  759. print FH "$_\n" if defined $_;
  760. }
  761. close(FH);
  762. rename($path, "$incpath/$header") or die("Can't rename '$path' to '$incpath/$header': $!\n");
  763. }
  764. } elsif ($copy_direction == -1) { # --copy-to-wiki
  765. foreach (keys %headerfuncs) {
  766. my $fn = $_;
  767. next if not $headerfuncshasdoxygen{$fn};
  768. my $wikitype = defined $wikitypes{$fn} ? $wikitypes{$fn} : 'mediawiki'; # default to MediaWiki for new stuff FOR NOW.
  769. die("Unexpected wikitype '$wikitype'\n") if (($wikitype ne 'mediawiki') and ($wikitype ne 'md') and ($wikitype ne 'manpage'));
  770. #print("$fn\n"); next;
  771. $wordwrap_mode = $wikitype;
  772. my $raw = $headerfuncs{$fn}; # raw doxygen text with comment characters stripped from start/end and start of each line.
  773. next if not defined $raw;
  774. $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.
  775. my @doxygenlines = split /\n/, $raw;
  776. my $brief = '';
  777. while (@doxygenlines) {
  778. last if $doxygenlines[0] =~ /\A\\/; # some sort of doxygen command, assume we're past the general remarks.
  779. last if $doxygenlines[0] =~ /\A\s*\Z/; # blank line? End of paragraph, done.
  780. my $l = shift @doxygenlines;
  781. chomp($l);
  782. $l =~ s/\A\s*//;
  783. $l =~ s/\s*\Z//;
  784. $brief .= "$l ";
  785. }
  786. $brief =~ s/\A(.*?\.) /$1\n\n/; # \brief should only be one sentence, delimited by a period+space. Split if necessary.
  787. my @briefsplit = split /\n/, $brief;
  788. $brief = wikify($wikitype, shift @briefsplit) . "\n";
  789. @doxygenlines = (@briefsplit, @doxygenlines);
  790. my $remarks = '';
  791. # !!! FIXME: wordwrap and wikify might handle this, now.
  792. while (@doxygenlines) {
  793. last if $doxygenlines[0] =~ /\A\\/; # some sort of doxygen command, assume we're past the general remarks.
  794. my $l = shift @doxygenlines;
  795. if ($l =~ /\A\`\`\`/) { # syntax highlighting, don't reformat.
  796. $remarks .= "$l\n";
  797. while ((@doxygenlines) && (not $l =~ /\`\`\`\Z/)) {
  798. $l = shift @doxygenlines;
  799. $remarks .= "$l\n";
  800. }
  801. } else {
  802. $l =~ s/\A\s*//;
  803. $l =~ s/\s*\Z//;
  804. $remarks .= "$l\n";
  805. }
  806. }
  807. #print("REMARKS:\n\n $remarks\n\n");
  808. $remarks = wordwrap(wikify($wikitype, $remarks));
  809. $remarks =~ s/\A\s*//;
  810. $remarks =~ s/\s*\Z//;
  811. my $decl = $headerdecls{$fn};
  812. #$decl =~ s/\*\s+SDLCALL/ *SDLCALL/; # Try to make "void * Function" become "void *Function"
  813. #$decl =~ s/\A\s*extern\s+(SDL_DEPRECATED\s+|)DECLSPEC\s+(.*?)\s+(\*?)SDLCALL/$2$3/;
  814. my $syntax = '';
  815. if ($wikitype eq 'mediawiki') {
  816. $syntax = "<syntaxhighlight lang='c'>\n$decl</syntaxhighlight>\n";
  817. } elsif ($wikitype eq 'md') {
  818. $syntax = "```c\n$decl\n```\n";
  819. } else { die("Expected wikitype '$wikitype'\n"); }
  820. my %sections = ();
  821. $sections{'[Brief]'} = $brief; # include this section even if blank so we get a title line.
  822. $sections{'Remarks'} = "$remarks\n" if $remarks ne '';
  823. $sections{'Syntax'} = $syntax;
  824. my @params = (); # have to parse these and build up the wiki tables after, since Markdown needs to know the length of the largest string. :/
  825. while (@doxygenlines) {
  826. my $l = shift @doxygenlines;
  827. if ($l =~ /\A\\param\s+(.*?)\s+(.*)\Z/) {
  828. my $arg = $1;
  829. my $desc = $2;
  830. while (@doxygenlines) {
  831. my $subline = $doxygenlines[0];
  832. $subline =~ s/\A\s*//;
  833. last if $subline =~ /\A\\/; # some sort of doxygen command, assume we're past this thing.
  834. shift @doxygenlines; # dump this line from the array; we're using it.
  835. if ($subline eq '') { # empty line, make sure it keeps the newline char.
  836. $desc .= "\n";
  837. } else {
  838. $desc .= " $subline";
  839. }
  840. }
  841. $desc =~ s/[\s\n]+\Z//ms;
  842. # We need to know the length of the longest string to make Markdown tables, so we just store these off until everything is parsed.
  843. push @params, $arg;
  844. push @params, $desc;
  845. } elsif ($l =~ /\A\\r(eturns?)\s+(.*)\Z/) {
  846. my $retstr = "R$1"; # "Return" or "Returns"
  847. my $desc = $2;
  848. while (@doxygenlines) {
  849. my $subline = $doxygenlines[0];
  850. $subline =~ s/\A\s*//;
  851. last if $subline =~ /\A\\/; # some sort of doxygen command, assume we're past this thing.
  852. shift @doxygenlines; # dump this line from the array; we're using it.
  853. if ($subline eq '') { # empty line, make sure it keeps the newline char.
  854. $desc .= "\n";
  855. } else {
  856. $desc .= " $subline";
  857. }
  858. }
  859. $desc =~ s/[\s\n]+\Z//ms;
  860. $sections{'Return Value'} = wordwrap("$retstr " . wikify($wikitype, $desc)) . "\n";
  861. } elsif ($l =~ /\A\\deprecated\s+(.*)\Z/) {
  862. my $desc = $1;
  863. while (@doxygenlines) {
  864. my $subline = $doxygenlines[0];
  865. $subline =~ s/\A\s*//;
  866. last if $subline =~ /\A\\/; # some sort of doxygen command, assume we're past this thing.
  867. shift @doxygenlines; # dump this line from the array; we're using it.
  868. if ($subline eq '') { # empty line, make sure it keeps the newline char.
  869. $desc .= "\n";
  870. } else {
  871. $desc .= " $subline";
  872. }
  873. }
  874. $desc =~ s/[\s\n]+\Z//ms;
  875. $sections{'Deprecated'} = wordwrap(wikify($wikitype, $desc)) . "\n";
  876. } elsif ($l =~ /\A\\since\s+(.*)\Z/) {
  877. my $desc = $1;
  878. while (@doxygenlines) {
  879. my $subline = $doxygenlines[0];
  880. $subline =~ s/\A\s*//;
  881. last if $subline =~ /\A\\/; # some sort of doxygen command, assume we're past this thing.
  882. shift @doxygenlines; # dump this line from the array; we're using it.
  883. if ($subline eq '') { # empty line, make sure it keeps the newline char.
  884. $desc .= "\n";
  885. } else {
  886. $desc .= " $subline";
  887. }
  888. }
  889. $desc =~ s/[\s\n]+\Z//ms;
  890. $sections{'Version'} = wordwrap(wikify($wikitype, $desc)) . "\n";
  891. } elsif ($l =~ /\A\\sa\s+(.*)\Z/) {
  892. my $sa = $1;
  893. $sa =~ s/\(\)\Z//; # Convert "SDL_Func()" to "SDL_Func"
  894. $sections{'Related Functions'} = '' if not defined $sections{'Related Functions'};
  895. if ($wikitype eq 'mediawiki') {
  896. $sections{'Related Functions'} .= ":[[$sa]]\n";
  897. } elsif ($wikitype eq 'md') {
  898. $sections{'Related Functions'} .= "* [$sa]($sa)\n";
  899. } else { die("Expected wikitype '$wikitype'\n"); }
  900. }
  901. }
  902. # Make sure this ends with a double-newline.
  903. $sections{'Related Functions'} .= "\n" if defined $sections{'Related Functions'};
  904. # We can build the wiki table now that we have all the data.
  905. if (scalar(@params) > 0) {
  906. my $str = '';
  907. if ($wikitype eq 'mediawiki') {
  908. while (scalar(@params) > 0) {
  909. my $arg = shift @params;
  910. my $desc = wikify($wikitype, shift @params);
  911. $str .= ($str eq '') ? "{|\n" : "|-\n";
  912. $str .= "|'''$arg'''\n";
  913. $str .= "|$desc\n";
  914. }
  915. $str .= "|}\n";
  916. } elsif ($wikitype eq 'md') {
  917. my $longest_arg = 0;
  918. my $longest_desc = 0;
  919. my $which = 0;
  920. foreach (@params) {
  921. if ($which == 0) {
  922. my $len = length($_) + 4;
  923. $longest_arg = $len if ($len > $longest_arg);
  924. $which = 1;
  925. } else {
  926. my $len = length(wikify($wikitype, $_));
  927. $longest_desc = $len if ($len > $longest_desc);
  928. $which = 0;
  929. }
  930. }
  931. # Markdown tables are sort of obnoxious.
  932. $str .= '| ' . (' ' x ($longest_arg+4)) . ' | ' . (' ' x $longest_desc) . " |\n";
  933. $str .= '| ' . ('-' x ($longest_arg+4)) . ' | ' . ('-' x $longest_desc) . " |\n";
  934. while (@params) {
  935. my $arg = shift @params;
  936. my $desc = wikify($wikitype, shift @params);
  937. $str .= "| **$arg** " . (' ' x ($longest_arg - length($arg))) . "| $desc" . (' ' x ($longest_desc - length($desc))) . " |\n";
  938. }
  939. } else {
  940. die("Unexpected wikitype!\n"); # should have checked this elsewhere.
  941. }
  942. $sections{'Function Parameters'} = $str;
  943. }
  944. my $path = "$wikipath/$_.${wikitype}.tmp";
  945. open(FH, '>', $path) or die("Can't open '$path': $!\n");
  946. my $sectionsref = $wikifuncs{$fn};
  947. foreach (@standard_wiki_sections) {
  948. # drop sections we either replaced or removed from the original wiki's contents.
  949. if (not defined $only_wiki_sections{$_}) {
  950. delete($$sectionsref{$_});
  951. }
  952. }
  953. my $wikisectionorderref = $wikisectionorder{$fn};
  954. # Make sure there's a footer in the wiki that puts this function in CategoryAPI...
  955. if (not $$sectionsref{'[footer]'}) {
  956. $$sectionsref{'[footer]'} = '';
  957. push @$wikisectionorderref, '[footer]';
  958. }
  959. # !!! FIXME: This won't be CategoryAPI if we eventually handle things other than functions.
  960. my $footer = $$sectionsref{'[footer]'};
  961. if ($wikitype eq 'mediawiki') {
  962. $footer =~ s/\[\[CategoryAPI\]\],?\s*//g;
  963. $footer = '[[CategoryAPI]]' . (($footer eq '') ? "\n" : ", $footer");
  964. } elsif ($wikitype eq 'md') {
  965. $footer =~ s/\[CategoryAPI\]\(CategoryAPI\),?\s*//g;
  966. $footer = '[CategoryAPI](CategoryAPI)' . (($footer eq '') ? '' : ', ') . $footer;
  967. } else { die("Unexpected wikitype '$wikitype'\n"); }
  968. $$sectionsref{'[footer]'} = $footer;
  969. if (defined $wikipreamble) {
  970. if ($wikitype eq 'mediawiki') {
  971. print FH "====== $wikipreamble ======\n";
  972. } elsif ($wikitype eq 'md') {
  973. print FH "###### $wikipreamble\n";
  974. } else { die("Unexpected wikitype '$wikitype'\n"); }
  975. }
  976. my $prevsectstr = '';
  977. my @ordered_sections = (@standard_wiki_sections, defined $wikisectionorderref ? @$wikisectionorderref : ()); # this copies the arrays into one.
  978. foreach (@ordered_sections) {
  979. my $sect = $_;
  980. next if $sect eq '[start]';
  981. next if (not defined $sections{$sect} and not defined $$sectionsref{$sect});
  982. my $section = defined $sections{$sect} ? $sections{$sect} : $$sectionsref{$sect};
  983. if ($sect eq '[footer]') {
  984. # Make sure previous section ends with two newlines.
  985. if (substr($prevsectstr, -1) ne "\n") {
  986. print FH "\n\n";
  987. } elsif (substr($prevsectstr, -2) ne "\n\n") {
  988. print FH "\n";
  989. }
  990. print FH "----\n"; # It's the same in Markdown and MediaWiki.
  991. } elsif ($sect eq '[Brief]') {
  992. if ($wikitype eq 'mediawiki') {
  993. print FH "= $fn =\n\n";
  994. } elsif ($wikitype eq 'md') {
  995. print FH "# $fn\n\n";
  996. } else { die("Unexpected wikitype '$wikitype'\n"); }
  997. } else {
  998. if ($wikitype eq 'mediawiki') {
  999. print FH "\n== $sect ==\n\n";
  1000. } elsif ($wikitype eq 'md') {
  1001. print FH "\n## $sect\n\n";
  1002. } else { die("Unexpected wikitype '$wikitype'\n"); }
  1003. }
  1004. my $sectstr = defined $sections{$sect} ? $sections{$sect} : $$sectionsref{$sect};
  1005. print FH $sectstr;
  1006. $prevsectstr = $sectstr;
  1007. # make sure these don't show up twice.
  1008. delete($sections{$sect});
  1009. delete($$sectionsref{$sect});
  1010. }
  1011. print FH "\n\n";
  1012. close(FH);
  1013. rename($path, "$wikipath/$_.${wikitype}") or die("Can't rename '$path' to '$wikipath/$_.${wikitype}': $!\n");
  1014. }
  1015. } elsif ($copy_direction == -2) { # --copy-to-manpages
  1016. # This only takes from the wiki data, since it has sections we omit from the headers, like code examples.
  1017. my $manpath = "$srcpath/man";
  1018. mkdir($manpath);
  1019. $manpath .= "/man3";
  1020. mkdir($manpath);
  1021. $dewikify_mode = 'manpage';
  1022. $wordwrap_mode = 'manpage';
  1023. my $introtxt = '';
  1024. if (0) {
  1025. open(FH, '<', "$srcpath/LICENSE.txt") or die("Can't open '$srcpath/LICENSE.txt': $!\n");
  1026. while (<FH>) {
  1027. chomp;
  1028. $introtxt .= ".\\\" $_\n";
  1029. }
  1030. close(FH);
  1031. }
  1032. my $gitrev = `cd "$srcpath" ; git rev-list HEAD~..`;
  1033. chomp($gitrev);
  1034. # !!! FIXME
  1035. open(FH, '<', "$srcpath/$versionfname") or die("Can't open '$srcpath/$versionfname': $!\n");
  1036. my $majorver = 0;
  1037. my $minorver = 0;
  1038. my $patchver = 0;
  1039. while (<FH>) {
  1040. chomp;
  1041. if (/$versionmajorregex/) {
  1042. $majorver = int($1);
  1043. } elsif (/$versionminorregex/) {
  1044. $minorver = int($1);
  1045. } elsif (/$versionpatchregex/) {
  1046. $patchver = int($1);
  1047. }
  1048. }
  1049. close(FH);
  1050. my $fullversion = "$majorver.$minorver.$patchver";
  1051. foreach (keys %headerfuncs) {
  1052. my $fn = $_;
  1053. next if not defined $wikifuncs{$fn}; # don't have a page for that function, skip it.
  1054. my $wikitype = $wikitypes{$fn};
  1055. my $sectionsref = $wikifuncs{$fn};
  1056. my $remarks = %$sectionsref{'Remarks'};
  1057. my $params = %$sectionsref{'Function Parameters'};
  1058. my $returns = %$sectionsref{'Return Value'};
  1059. my $version = %$sectionsref{'Version'};
  1060. my $related = %$sectionsref{'Related Functions'};
  1061. my $examples = %$sectionsref{'Code Examples'};
  1062. my $deprecated = %$sectionsref{'Deprecated'};
  1063. my $brief = %$sectionsref{'[Brief]'};
  1064. my $decl = $headerdecls{$fn};
  1065. my $str = '';
  1066. $brief = "$brief";
  1067. $brief =~ s/\A[\s\n]*\= .*? \=\s*?\n+//ms;
  1068. $brief =~ s/\A[\s\n]*\=\= .*? \=\=\s*?\n+//ms;
  1069. $brief =~ s/\A(.*?\.) /$1\n/; # \brief should only be one sentence, delimited by a period+space. Split if necessary.
  1070. my @briefsplit = split /\n/, $brief;
  1071. $brief = shift @briefsplit;
  1072. $brief = dewikify($wikitype, $brief);
  1073. if (defined $remarks) {
  1074. $remarks = dewikify($wikitype, join("\n", @briefsplit) . $remarks);
  1075. }
  1076. $str .= $introtxt;
  1077. $str .= ".\\\" This manpage content is licensed under Creative Commons\n";
  1078. $str .= ".\\\" Attribution 4.0 International (CC BY 4.0)\n";
  1079. $str .= ".\\\" https://creativecommons.org/licenses/by/4.0/\n";
  1080. $str .= ".\\\" This manpage was generated from ${projectshortname}'s wiki page for $fn:\n";
  1081. $str .= ".\\\" $wikiurl/$fn\n";
  1082. $str .= ".\\\" Generated with SDL/build-scripts/wikiheaders.pl\n";
  1083. $str .= ".\\\" revision $gitrev\n" if $gitrev ne '';
  1084. $str .= ".\\\" Please report issues in this manpage's content at:\n";
  1085. $str .= ".\\\" $bugreporturl\n";
  1086. $str .= ".\\\" Please report issues in the generation of this manpage from the wiki at:\n";
  1087. $str .= ".\\\" https://github.com/libsdl-org/SDL/issues/new?title=Misgenerated%20manpage%20for%20$fn\n";
  1088. $str .= ".\\\" $projectshortname can be found at $projecturl\n";
  1089. # 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.
  1090. # This wizadry is from https://web.archive.org/web/20060102165607/http://people.debian.org/~branden/talks/wtfm/wtfm.pdf
  1091. $str .= ".de URL\n";
  1092. $str .= '\\$2 \(laURL: \\$1 \(ra\\$3' . "\n";
  1093. $str .= "..\n";
  1094. $str .= '.if \n[.g] .mso www.tmac' . "\n";
  1095. $str .= ".TH $fn 3 \"$projectshortname $fullversion\" \"$projectfullname\" \"$projectshortname$majorver FUNCTIONS\"\n";
  1096. $str .= ".SH NAME\n";
  1097. $str .= "$fn";
  1098. $str .= " \\- $brief" if (defined $brief);
  1099. $str .= "\n";
  1100. $str .= ".SH SYNOPSIS\n";
  1101. $str .= ".nf\n";
  1102. $str .= ".B #include \\(dq$mainincludefname\\(dq\n";
  1103. $str .= ".PP\n";
  1104. my @decllines = split /\n/, $decl;
  1105. foreach (@decllines) {
  1106. $str .= ".BI \"$_\n";
  1107. }
  1108. $str .= ".fi\n";
  1109. if (defined $remarks) {
  1110. $str .= ".SH DESCRIPTION\n";
  1111. $str .= $remarks . "\n";
  1112. }
  1113. if (defined $deprecated) {
  1114. $str .= ".SH DEPRECATED\n";
  1115. $str .= dewikify($wikitype, $deprecated) . "\n";
  1116. }
  1117. if (defined $params) {
  1118. $str .= ".SH FUNCTION PARAMETERS\n";
  1119. my @lines = split /\n/, $params;
  1120. if ($wikitype eq 'mediawiki') {
  1121. die("Unexpected data parsing MediaWiki table") if (shift @lines ne '{|'); # Dump the '{|' start
  1122. while (scalar(@lines) >= 3) {
  1123. my $name = shift @lines;
  1124. my $desc = shift @lines;
  1125. my $terminator = shift @lines; # the '|-' or '|}' line.
  1126. last if ($terminator ne '|-') and ($terminator ne '|}'); # we seem to have run out of table.
  1127. $name =~ s/\A\|\s*//;
  1128. $name =~ s/\A\*\*(.*?)\*\*/$1/;
  1129. $name =~ s/\A\'\'\'(.*?)\'\'\'/$1/;
  1130. $desc =~ s/\A\|\s*//;
  1131. $desc = dewikify($wikitype, $desc);
  1132. #print STDERR "FN: $fn NAME: $name DESC: $desc TERM: $terminator\n";
  1133. $str .= ".TP\n";
  1134. $str .= ".I $name\n";
  1135. $str .= "$desc\n";
  1136. }
  1137. } else {
  1138. die("write me");
  1139. }
  1140. }
  1141. if (defined $returns) {
  1142. $str .= ".SH RETURN VALUE\n";
  1143. $str .= dewikify($wikitype, $returns) . "\n";
  1144. }
  1145. if (defined $examples) {
  1146. $str .= ".SH CODE EXAMPLES\n";
  1147. $dewikify_manpage_code_indent = 0;
  1148. $str .= dewikify($wikitype, $examples) . "\n";
  1149. $dewikify_manpage_code_indent = 1;
  1150. }
  1151. if (defined $version) {
  1152. $str .= ".SH AVAILABILITY\n";
  1153. $str .= dewikify($wikitype, $version) . "\n";
  1154. }
  1155. if (defined $related) {
  1156. $str .= ".SH SEE ALSO\n";
  1157. # !!! FIXME: lots of code duplication in all of these.
  1158. my $v = dewikify($wikitype, $related);
  1159. my @desclines = split /\n/, $v;
  1160. my $nextstr = '';
  1161. foreach (@desclines) {
  1162. s/\A(\:|\* )//;
  1163. s/\(\)\Z//; # Convert "SDL_Func()" to "SDL_Func"
  1164. s/\A\/*//;
  1165. s/\A\.BR\s+//; # dewikify added this, but we want to handle it.
  1166. s/\A\s+//;
  1167. s/\s+\Z//;
  1168. next if $_ eq '';
  1169. $str .= "$nextstr.BR $_ (3)";
  1170. $nextstr = ",\n";
  1171. }
  1172. $str .= "\n";
  1173. }
  1174. if (0) {
  1175. $str .= ".SH COPYRIGHT\n";
  1176. $str .= "This manpage is licensed under\n";
  1177. $str .= ".UR https://creativecommons.org/licenses/by/4.0/\n";
  1178. $str .= "Creative Commons Attribution 4.0 International (CC BY 4.0)\n";
  1179. $str .= ".UE\n";
  1180. $str .= ".PP\n";
  1181. $str .= "This manpage was generated from\n";
  1182. $str .= ".UR $wikiurl/$fn\n";
  1183. $str .= "${projectshortname}'s wiki\n";
  1184. $str .= ".UE\n";
  1185. $str .= "using SDL/build-scripts/wikiheaders.pl";
  1186. $str .= " revision $gitrev" if $gitrev ne '';
  1187. $str .= ".\n";
  1188. $str .= "Please report issues in this manpage at\n";
  1189. $str .= ".UR $bugreporturl\n";
  1190. $str .= "our bugtracker!\n";
  1191. $str .= ".UE\n";
  1192. }
  1193. my $path = "$manpath/$_.3.tmp";
  1194. open(FH, '>', $path) or die("Can't open '$path': $!\n");
  1195. print FH $str;
  1196. close(FH);
  1197. rename($path, "$manpath/$_.3") or die("Can't rename '$path' to '$manpath/$_.3': $!\n");
  1198. }
  1199. }
  1200. # end of wikiheaders.pl ...