wikiheaders.pl 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443
  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. 'Thread Safety',
  340. 'Version',
  341. 'Code Examples',
  342. 'Related Functions'
  343. );
  344. # Sections that only ever exist in the wiki and shouldn't be deleted when
  345. # not found in the headers.
  346. my %only_wiki_sections = ( # The ones don't mean anything, I just need to check for key existence.
  347. 'Draft', 1,
  348. 'Code Examples', 1
  349. );
  350. my %headers = (); # $headers{"SDL_audio.h"} -> reference to an array of all lines of text in SDL_audio.h.
  351. my %headerfuncs = (); # $headerfuncs{"SDL_OpenAudio"} -> string of header documentation for SDL_OpenAudio, with comment '*' bits stripped from the start. Newlines embedded!
  352. my %headerdecls = ();
  353. my %headerfuncslocation = (); # $headerfuncslocation{"SDL_OpenAudio"} -> name of header holding SDL_OpenAudio define ("SDL_audio.h" in this case).
  354. my %headerfuncschunk = (); # $headerfuncschunk{"SDL_OpenAudio"} -> offset in array in %headers that should be replaced for this function.
  355. my %headerfuncshasdoxygen = (); # $headerfuncschunk{"SDL_OpenAudio"} -> 1 if there was no existing doxygen for this function.
  356. my $incpath = "$srcpath";
  357. $incpath .= "/$incsubdir" if $incsubdir ne '';
  358. opendir(DH, $incpath) or die("Can't opendir '$incpath': $!\n");
  359. while (readdir(DH)) {
  360. my $dent = $_;
  361. next if not $dent =~ /$selectheaderregex/; # just selected headers.
  362. open(FH, '<', "$incpath/$dent") or die("Can't open '$incpath/$dent': $!\n");
  363. my @contents = ();
  364. while (<FH>) {
  365. chomp;
  366. my $decl;
  367. my @templines;
  368. my $str;
  369. my $has_doxygen = 1;
  370. if (/\A\s*extern\s+(SDL_DEPRECATED\s+|)DECLSPEC/) { # a function declaration without a doxygen comment?
  371. @templines = ();
  372. $decl = $_;
  373. $str = '';
  374. $has_doxygen = 0;
  375. } elsif (not /\A\/\*\*\s*\Z/) { # not doxygen comment start?
  376. push @contents, $_;
  377. next;
  378. } else { # Start of a doxygen comment, parse it out.
  379. @templines = ( $_ );
  380. while (<FH>) {
  381. chomp;
  382. push @templines, $_;
  383. last if /\A\s*\*\/\Z/;
  384. 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...
  385. $str .= "$_\n";
  386. while (<FH>) {
  387. chomp;
  388. push @templines, $_;
  389. s/\A\s*\*\s?//;
  390. if (s/\A\s*\`\`\`/```/) {
  391. $str .= "$_\n";
  392. last;
  393. } else {
  394. $str .= "$_\n";
  395. }
  396. }
  397. } else {
  398. s/\A\s*\*\s*//;
  399. $str .= "$_\n";
  400. }
  401. }
  402. $decl = <FH>;
  403. $decl = '' if not defined $decl;
  404. chomp($decl);
  405. if (not $decl =~ /\A\s*extern\s+(SDL_DEPRECATED\s+|)DECLSPEC/) {
  406. #print "Found doxygen but no function sig:\n$str\n\n";
  407. foreach (@templines) {
  408. push @contents, $_;
  409. }
  410. push @contents, $decl;
  411. next;
  412. }
  413. }
  414. my @decllines = ( $decl );
  415. if (not $decl =~ /\)\s*;/) {
  416. while (<FH>) {
  417. chomp;
  418. push @decllines, $_;
  419. s/\A\s+//;
  420. s/\s+\Z//;
  421. $decl .= " $_";
  422. last if /\)\s*;/;
  423. }
  424. }
  425. $decl =~ s/\s+\);\Z/);/;
  426. $decl =~ s/\s+\Z//;
  427. #print("DECL: [$decl]\n");
  428. my $fn = '';
  429. if ($decl =~ /\A\s*extern\s+(SDL_DEPRECATED\s+|)DECLSPEC\s+(const\s+|)(unsigned\s+|)(.*?)\s*(\*?)\s*SDLCALL\s+(.*?)\s*\((.*?)\);/) {
  430. $fn = $6;
  431. #$decl =~ s/\A\s*extern\s+DECLSPEC\s+(.*?)\s+SDLCALL/$1/;
  432. } else {
  433. #print "Found doxygen but no function sig:\n$str\n\n";
  434. foreach (@templines) {
  435. push @contents, $_;
  436. }
  437. foreach (@decllines) {
  438. push @contents, $_;
  439. }
  440. next;
  441. }
  442. $decl = ''; # build this with the line breaks, since it looks better for syntax highlighting.
  443. foreach (@decllines) {
  444. if ($decl eq '') {
  445. $decl = $_;
  446. $decl =~ s/\Aextern\s+(SDL_DEPRECATED\s+|)DECLSPEC\s+(.*?)\s+(\*?)SDLCALL\s+/$2$3 /;
  447. } else {
  448. my $trimmed = $_;
  449. # !!! FIXME: trim space for SDL_DEPRECATED if it was used, too.
  450. $trimmed =~ s/\A\s{24}//; # 24 for shrinking to match the removed "extern DECLSPEC SDLCALL "
  451. $decl .= $trimmed;
  452. }
  453. $decl .= "\n";
  454. }
  455. #print("$fn:\n$str\n\n");
  456. # There might be multiple declarations of a function due to #ifdefs,
  457. # and only one of them will have documentation. If we hit an
  458. # undocumented one before, delete the placeholder line we left for
  459. # it so it doesn't accumulate a new blank line on each run.
  460. my $skipfn = 0;
  461. if (defined $headerfuncshasdoxygen{$fn}) {
  462. if ($headerfuncshasdoxygen{$fn} == 0) { # An undocumented declaration already exists, nuke its placeholder line.
  463. delete $contents[$headerfuncschunk{$fn}]; # delete DOES NOT RENUMBER existing elements!
  464. } else { # documented function already existed?
  465. $skipfn = 1; # don't add this copy to the list of functions.
  466. if ($has_doxygen) {
  467. print STDERR "WARNING: Function '$fn' appears to be documented in multiple locations. Only keeping the first one we saw!\n";
  468. }
  469. push @contents, join("\n", @decllines); # just put the existing declation in as-is.
  470. }
  471. }
  472. if (!$skipfn) {
  473. $headerfuncs{$fn} = $str;
  474. $headerdecls{$fn} = $decl;
  475. $headerfuncslocation{$fn} = $dent;
  476. $headerfuncschunk{$fn} = scalar(@contents);
  477. $headerfuncshasdoxygen{$fn} = $has_doxygen;
  478. push @contents, join("\n", @templines);
  479. push @contents, join("\n", @decllines);
  480. }
  481. }
  482. close(FH);
  483. $headers{$dent} = \@contents;
  484. }
  485. closedir(DH);
  486. # !!! FIXME: we need to parse enums and typedefs and structs and defines and and and and and...
  487. # !!! FIXME: (but functions are good enough for now.)
  488. my %wikitypes = (); # contains string of wiki page extension, like $wikitypes{"SDL_OpenAudio"} == 'mediawiki'
  489. 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"}.
  490. 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'
  491. opendir(DH, $wikipath) or die("Can't opendir '$wikipath': $!\n");
  492. while (readdir(DH)) {
  493. my $dent = $_;
  494. my $type = '';
  495. if ($dent =~ /\.(md|mediawiki)\Z/) {
  496. $type = $1;
  497. } else {
  498. next; # only dealing with wiki pages.
  499. }
  500. my $fn = $dent;
  501. $fn =~ s/\..*\Z//;
  502. # Ignore FrontPage.
  503. next if $fn eq 'FrontPage';
  504. # Ignore "Category*" pages.
  505. next if ($fn =~ /\ACategory/);
  506. open(FH, '<', "$wikipath/$dent") or die("Can't open '$wikipath/$dent': $!\n");
  507. my $current_section = '[start]';
  508. my @section_order = ( $current_section );
  509. my %sections = ();
  510. $sections{$current_section} = '';
  511. my $firstline = 1;
  512. while (<FH>) {
  513. chomp;
  514. my $orig = $_;
  515. s/\A\s*//;
  516. s/\s*\Z//;
  517. if ($type eq 'mediawiki') {
  518. if (defined($wikipreamble) && $firstline && /\A\=\=\=\=\=\= (.*?) \=\=\=\=\=\=\Z/ && ($1 eq $wikipreamble)) {
  519. $firstline = 0; # skip this.
  520. next;
  521. } elsif (/\A\= (.*?) \=\Z/) {
  522. $firstline = 0;
  523. $current_section = ($1 eq $fn) ? '[Brief]' : $1;
  524. die("Doubly-defined section '$current_section' in '$dent'!\n") if defined $sections{$current_section};
  525. push @section_order, $current_section;
  526. $sections{$current_section} = '';
  527. } elsif (/\A\=\= (.*?) \=\=\Z/) {
  528. $firstline = 0;
  529. $current_section = ($1 eq $fn) ? '[Brief]' : $1;
  530. die("Doubly-defined section '$current_section' in '$dent'!\n") if defined $sections{$current_section};
  531. push @section_order, $current_section;
  532. $sections{$current_section} = '';
  533. next;
  534. } elsif (/\A\-\-\-\-\Z/) {
  535. $firstline = 0;
  536. $current_section = '[footer]';
  537. die("Doubly-defined section '$current_section' in '$dent'!\n") if defined $sections{$current_section};
  538. push @section_order, $current_section;
  539. $sections{$current_section} = '';
  540. next;
  541. }
  542. } elsif ($type eq 'md') {
  543. if (defined($wikipreamble) && $firstline && /\A\#\#\#\#\#\# (.*?)\Z/ && ($1 eq $wikipreamble)) {
  544. $firstline = 0; # skip this.
  545. next;
  546. } elsif (/\A\#+ (.*?)\Z/) {
  547. $firstline = 0;
  548. $current_section = ($1 eq $fn) ? '[Brief]' : $1;
  549. die("Doubly-defined section '$current_section' in '$dent'!\n") if defined $sections{$current_section};
  550. push @section_order, $current_section;
  551. $sections{$current_section} = '';
  552. next;
  553. } elsif (/\A\-\-\-\-\Z/) {
  554. $firstline = 0;
  555. $current_section = '[footer]';
  556. die("Doubly-defined section '$current_section' in '$dent'!\n") if defined $sections{$current_section};
  557. push @section_order, $current_section;
  558. $sections{$current_section} = '';
  559. next;
  560. }
  561. } else {
  562. die("Unexpected wiki file type. Fixme!\n");
  563. }
  564. if ($firstline) {
  565. $firstline = ($_ ne '');
  566. }
  567. if (!$firstline) {
  568. $sections{$current_section} .= "$orig\n";
  569. }
  570. }
  571. close(FH);
  572. foreach (keys %sections) {
  573. $sections{$_} =~ s/\A\n+//;
  574. $sections{$_} =~ s/\n+\Z//;
  575. $sections{$_} .= "\n";
  576. }
  577. if (0) {
  578. foreach (@section_order) {
  579. print("$fn SECTION '$_':\n");
  580. print($sections{$_});
  581. print("\n\n");
  582. }
  583. }
  584. $wikitypes{$fn} = $type;
  585. $wikifuncs{$fn} = \%sections;
  586. $wikisectionorder{$fn} = \@section_order;
  587. }
  588. closedir(DH);
  589. if ($warn_about_missing) {
  590. foreach (keys %wikifuncs) {
  591. my $fn = $_;
  592. if (not defined $headerfuncs{$fn}) {
  593. print("WARNING: $fn defined in the wiki but not the headers!\n");
  594. }
  595. }
  596. foreach (keys %headerfuncs) {
  597. my $fn = $_;
  598. if (not defined $wikifuncs{$fn}) {
  599. print("WARNING: $fn defined in the headers but not the wiki!\n");
  600. }
  601. }
  602. }
  603. if ($copy_direction == 1) { # --copy-to-headers
  604. my %changed_headers = ();
  605. $dewikify_mode = 'md';
  606. $wordwrap_mode = 'md'; # the headers use Markdown format.
  607. foreach (keys %headerfuncs) {
  608. my $fn = $_;
  609. next if not defined $wikifuncs{$fn}; # don't have a page for that function, skip it.
  610. my $wikitype = $wikitypes{$fn};
  611. my $sectionsref = $wikifuncs{$fn};
  612. my $remarks = %$sectionsref{'Remarks'};
  613. my $params = %$sectionsref{'Function Parameters'};
  614. my $returns = %$sectionsref{'Return Value'};
  615. my $threadsafety = %$sectionsref{'Thread Safety'};
  616. my $version = %$sectionsref{'Version'};
  617. my $related = %$sectionsref{'Related Functions'};
  618. my $deprecated = %$sectionsref{'Deprecated'};
  619. my $brief = %$sectionsref{'[Brief]'};
  620. my $addblank = 0;
  621. my $str = '';
  622. $headerfuncshasdoxygen{$fn} = 1; # Added/changed doxygen for this header.
  623. $brief = dewikify($wikitype, $brief);
  624. $brief =~ s/\A(.*?\.) /$1\n/; # \brief should only be one sentence, delimited by a period+space. Split if necessary.
  625. my @briefsplit = split /\n/, $brief;
  626. $brief = shift @briefsplit;
  627. if (defined $remarks) {
  628. $remarks = join("\n", @briefsplit) . dewikify($wikitype, $remarks);
  629. }
  630. if (defined $brief) {
  631. $str .= "\n" if $addblank; $addblank = 1;
  632. $str .= wordwrap($brief) . "\n";
  633. }
  634. if (defined $remarks) {
  635. $str .= "\n" if $addblank; $addblank = 1;
  636. $str .= wordwrap($remarks) . "\n";
  637. }
  638. if (defined $deprecated) {
  639. # !!! FIXME: lots of code duplication in all of these.
  640. $str .= "\n" if $addblank; $addblank = 1;
  641. my $v = dewikify($wikitype, $deprecated);
  642. my $whitespacelen = length("\\deprecated") + 1;
  643. my $whitespace = ' ' x $whitespacelen;
  644. $v = wordwrap($v, -$whitespacelen);
  645. my @desclines = split /\n/, $v;
  646. my $firstline = shift @desclines;
  647. $str .= "\\deprecated $firstline\n";
  648. foreach (@desclines) {
  649. $str .= "${whitespace}$_\n";
  650. }
  651. }
  652. if (defined $params) {
  653. $str .= "\n" if $addblank; $addblank = (defined $returns) ? 0 : 1;
  654. my @lines = split /\n/, dewikify($wikitype, $params);
  655. if ($wikitype eq 'mediawiki') {
  656. die("Unexpected data parsing MediaWiki table") if (shift @lines ne '{|'); # Dump the '{|' start
  657. while (scalar(@lines) >= 3) {
  658. my $name = shift @lines;
  659. my $desc = shift @lines;
  660. my $terminator = shift @lines; # the '|-' or '|}' line.
  661. last if ($terminator ne '|-') and ($terminator ne '|}'); # we seem to have run out of table.
  662. $name =~ s/\A\|\s*//;
  663. $name =~ s/\A\*\*(.*?)\*\*/$1/;
  664. $name =~ s/\A\'\'\'(.*?)\'\'\'/$1/;
  665. $desc =~ s/\A\|\s*//;
  666. #print STDERR "FN: $fn NAME: $name DESC: $desc TERM: $terminator\n";
  667. my $whitespacelen = length($name) + 8;
  668. my $whitespace = ' ' x $whitespacelen;
  669. $desc = wordwrap($desc, -$whitespacelen);
  670. my @desclines = split /\n/, $desc;
  671. my $firstline = shift @desclines;
  672. $str .= "\\param $name $firstline\n";
  673. foreach (@desclines) {
  674. $str .= "${whitespace}$_\n";
  675. }
  676. }
  677. } else {
  678. die("write me");
  679. }
  680. }
  681. if (defined $returns) {
  682. $str .= "\n" if $addblank; $addblank = 1;
  683. my $r = dewikify($wikitype, $returns);
  684. my $retstr = "\\returns";
  685. if ($r =~ s/\AReturn(s?) //) {
  686. $retstr = "\\return$1";
  687. }
  688. my $whitespacelen = length($retstr) + 1;
  689. my $whitespace = ' ' x $whitespacelen;
  690. $r = wordwrap($r, -$whitespacelen);
  691. my @desclines = split /\n/, $r;
  692. my $firstline = shift @desclines;
  693. $str .= "$retstr $firstline\n";
  694. foreach (@desclines) {
  695. $str .= "${whitespace}$_\n";
  696. }
  697. }
  698. if (defined $threadsafety) {
  699. # !!! FIXME: lots of code duplication in all of these.
  700. $str .= "\n" if $addblank; $addblank = 1;
  701. my $v = dewikify($wikitype, $threadsafety);
  702. my $whitespacelen = length("\\threadsafety") + 1;
  703. my $whitespace = ' ' x $whitespacelen;
  704. $v = wordwrap($v, -$whitespacelen);
  705. my @desclines = split /\n/, $v;
  706. my $firstline = shift @desclines;
  707. $str .= "\\threadsafety $firstline\n";
  708. foreach (@desclines) {
  709. $str .= "${whitespace}$_\n";
  710. }
  711. }
  712. if (defined $version) {
  713. # !!! FIXME: lots of code duplication in all of these.
  714. $str .= "\n" if $addblank; $addblank = 1;
  715. my $v = dewikify($wikitype, $version);
  716. my $whitespacelen = length("\\since") + 1;
  717. my $whitespace = ' ' x $whitespacelen;
  718. $v = wordwrap($v, -$whitespacelen);
  719. my @desclines = split /\n/, $v;
  720. my $firstline = shift @desclines;
  721. $str .= "\\since $firstline\n";
  722. foreach (@desclines) {
  723. $str .= "${whitespace}$_\n";
  724. }
  725. }
  726. if (defined $related) {
  727. # !!! FIXME: lots of code duplication in all of these.
  728. $str .= "\n" if $addblank; $addblank = 1;
  729. my $v = dewikify($wikitype, $related);
  730. my @desclines = split /\n/, $v;
  731. foreach (@desclines) {
  732. s/\A(\:|\* )//;
  733. s/\(\)\Z//; # Convert "SDL_Func()" to "SDL_Func"
  734. s/\[\[(.*?)\]\]/$1/; # in case some wikilinks remain.
  735. s/\A\/*//;
  736. $str .= "\\sa $_\n";
  737. }
  738. }
  739. my $header = $headerfuncslocation{$fn};
  740. my $contentsref = $headers{$header};
  741. my $chunk = $headerfuncschunk{$fn};
  742. my @lines = split /\n/, $str;
  743. my $addnewline = (($chunk > 0) && ($$contentsref[$chunk-1] ne '')) ? "\n" : '';
  744. my $output = "$addnewline/**\n";
  745. foreach (@lines) {
  746. chomp;
  747. s/\s*\Z//;
  748. if ($_ eq '') {
  749. $output .= " *\n";
  750. } else {
  751. $output .= " * $_\n";
  752. }
  753. }
  754. $output .= " */";
  755. #print("$fn:\n$output\n\n");
  756. $$contentsref[$chunk] = $output;
  757. #$$contentsref[$chunk+1] = $headerdecls{$fn};
  758. $changed_headers{$header} = 1;
  759. }
  760. foreach (keys %changed_headers) {
  761. my $header = $_;
  762. # this is kinda inefficient, but oh well.
  763. my @removelines = ();
  764. foreach (keys %headerfuncslocation) {
  765. my $fn = $_;
  766. next if $headerfuncshasdoxygen{$fn};
  767. next if $headerfuncslocation{$fn} ne $header;
  768. # 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.
  769. push @removelines, $headerfuncschunk{$fn};
  770. }
  771. my $contentsref = $headers{$header};
  772. foreach (@removelines) {
  773. delete $$contentsref[$_]; # delete DOES NOT RENUMBER existing elements!
  774. }
  775. my $path = "$incpath/$header.tmp";
  776. open(FH, '>', $path) or die("Can't open '$path': $!\n");
  777. foreach (@$contentsref) {
  778. print FH "$_\n" if defined $_;
  779. }
  780. close(FH);
  781. rename($path, "$incpath/$header") or die("Can't rename '$path' to '$incpath/$header': $!\n");
  782. }
  783. } elsif ($copy_direction == -1) { # --copy-to-wiki
  784. foreach (keys %headerfuncs) {
  785. my $fn = $_;
  786. next if not $headerfuncshasdoxygen{$fn};
  787. my $wikitype = defined $wikitypes{$fn} ? $wikitypes{$fn} : 'mediawiki'; # default to MediaWiki for new stuff FOR NOW.
  788. die("Unexpected wikitype '$wikitype'\n") if (($wikitype ne 'mediawiki') and ($wikitype ne 'md') and ($wikitype ne 'manpage'));
  789. #print("$fn\n"); next;
  790. $wordwrap_mode = $wikitype;
  791. my $raw = $headerfuncs{$fn}; # raw doxygen text with comment characters stripped from start/end and start of each line.
  792. next if not defined $raw;
  793. $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.
  794. my @doxygenlines = split /\n/, $raw;
  795. my $brief = '';
  796. while (@doxygenlines) {
  797. last if $doxygenlines[0] =~ /\A\\/; # some sort of doxygen command, assume we're past the general remarks.
  798. last if $doxygenlines[0] =~ /\A\s*\Z/; # blank line? End of paragraph, done.
  799. my $l = shift @doxygenlines;
  800. chomp($l);
  801. $l =~ s/\A\s*//;
  802. $l =~ s/\s*\Z//;
  803. $brief .= "$l ";
  804. }
  805. $brief =~ s/\A(.*?\.) /$1\n\n/; # \brief should only be one sentence, delimited by a period+space. Split if necessary.
  806. my @briefsplit = split /\n/, $brief;
  807. $brief = wikify($wikitype, shift @briefsplit) . "\n";
  808. @doxygenlines = (@briefsplit, @doxygenlines);
  809. my $remarks = '';
  810. # !!! FIXME: wordwrap and wikify might handle this, now.
  811. while (@doxygenlines) {
  812. last if $doxygenlines[0] =~ /\A\\/; # some sort of doxygen command, assume we're past the general remarks.
  813. my $l = shift @doxygenlines;
  814. if ($l =~ /\A\`\`\`/) { # syntax highlighting, don't reformat.
  815. $remarks .= "$l\n";
  816. while ((@doxygenlines) && (not $l =~ /\`\`\`\Z/)) {
  817. $l = shift @doxygenlines;
  818. $remarks .= "$l\n";
  819. }
  820. } else {
  821. $l =~ s/\A\s*//;
  822. $l =~ s/\s*\Z//;
  823. $remarks .= "$l\n";
  824. }
  825. }
  826. #print("REMARKS:\n\n $remarks\n\n");
  827. $remarks = wordwrap(wikify($wikitype, $remarks));
  828. $remarks =~ s/\A\s*//;
  829. $remarks =~ s/\s*\Z//;
  830. my $decl = $headerdecls{$fn};
  831. #$decl =~ s/\*\s+SDLCALL/ *SDLCALL/; # Try to make "void * Function" become "void *Function"
  832. #$decl =~ s/\A\s*extern\s+(SDL_DEPRECATED\s+|)DECLSPEC\s+(.*?)\s+(\*?)SDLCALL/$2$3/;
  833. my $syntax = '';
  834. if ($wikitype eq 'mediawiki') {
  835. $syntax = "<syntaxhighlight lang='c'>\n$decl</syntaxhighlight>\n";
  836. } elsif ($wikitype eq 'md') {
  837. $syntax = "```c\n$decl\n```\n";
  838. } else { die("Expected wikitype '$wikitype'\n"); }
  839. my %sections = ();
  840. $sections{'[Brief]'} = $brief; # include this section even if blank so we get a title line.
  841. $sections{'Remarks'} = "$remarks\n" if $remarks ne '';
  842. $sections{'Syntax'} = $syntax;
  843. my @params = (); # have to parse these and build up the wiki tables after, since Markdown needs to know the length of the largest string. :/
  844. while (@doxygenlines) {
  845. my $l = shift @doxygenlines;
  846. if ($l =~ /\A\\param\s+(.*?)\s+(.*)\Z/) {
  847. my $arg = $1;
  848. my $desc = $2;
  849. while (@doxygenlines) {
  850. my $subline = $doxygenlines[0];
  851. $subline =~ s/\A\s*//;
  852. last if $subline =~ /\A\\/; # some sort of doxygen command, assume we're past this thing.
  853. shift @doxygenlines; # dump this line from the array; we're using it.
  854. if ($subline eq '') { # empty line, make sure it keeps the newline char.
  855. $desc .= "\n";
  856. } else {
  857. $desc .= " $subline";
  858. }
  859. }
  860. $desc =~ s/[\s\n]+\Z//ms;
  861. # We need to know the length of the longest string to make Markdown tables, so we just store these off until everything is parsed.
  862. push @params, $arg;
  863. push @params, $desc;
  864. } elsif ($l =~ /\A\\r(eturns?)\s+(.*)\Z/) {
  865. my $retstr = "R$1"; # "Return" or "Returns"
  866. my $desc = $2;
  867. while (@doxygenlines) {
  868. my $subline = $doxygenlines[0];
  869. $subline =~ s/\A\s*//;
  870. last if $subline =~ /\A\\/; # some sort of doxygen command, assume we're past this thing.
  871. shift @doxygenlines; # dump this line from the array; we're using it.
  872. if ($subline eq '') { # empty line, make sure it keeps the newline char.
  873. $desc .= "\n";
  874. } else {
  875. $desc .= " $subline";
  876. }
  877. }
  878. $desc =~ s/[\s\n]+\Z//ms;
  879. $sections{'Return Value'} = wordwrap("$retstr " . wikify($wikitype, $desc)) . "\n";
  880. } elsif ($l =~ /\A\\deprecated\s+(.*)\Z/) {
  881. my $desc = $1;
  882. while (@doxygenlines) {
  883. my $subline = $doxygenlines[0];
  884. $subline =~ s/\A\s*//;
  885. last if $subline =~ /\A\\/; # some sort of doxygen command, assume we're past this thing.
  886. shift @doxygenlines; # dump this line from the array; we're using it.
  887. if ($subline eq '') { # empty line, make sure it keeps the newline char.
  888. $desc .= "\n";
  889. } else {
  890. $desc .= " $subline";
  891. }
  892. }
  893. $desc =~ s/[\s\n]+\Z//ms;
  894. $sections{'Deprecated'} = wordwrap(wikify($wikitype, $desc)) . "\n";
  895. } elsif ($l =~ /\A\\since\s+(.*)\Z/) {
  896. my $desc = $1;
  897. while (@doxygenlines) {
  898. my $subline = $doxygenlines[0];
  899. $subline =~ s/\A\s*//;
  900. last if $subline =~ /\A\\/; # some sort of doxygen command, assume we're past this thing.
  901. shift @doxygenlines; # dump this line from the array; we're using it.
  902. if ($subline eq '') { # empty line, make sure it keeps the newline char.
  903. $desc .= "\n";
  904. } else {
  905. $desc .= " $subline";
  906. }
  907. }
  908. $desc =~ s/[\s\n]+\Z//ms;
  909. $sections{'Version'} = wordwrap(wikify($wikitype, $desc)) . "\n";
  910. } elsif ($l =~ /\A\\threadsafety\s+(.*)\Z/) {
  911. my $desc = $1;
  912. while (@doxygenlines) {
  913. my $subline = $doxygenlines[0];
  914. $subline =~ s/\A\s*//;
  915. last if $subline =~ /\A\\/; # some sort of doxygen command, assume we're past this thing.
  916. shift @doxygenlines; # dump this line from the array; we're using it.
  917. if ($subline eq '') { # empty line, make sure it keeps the newline char.
  918. $desc .= "\n";
  919. } else {
  920. $desc .= " $subline";
  921. }
  922. }
  923. $desc =~ s/[\s\n]+\Z//ms;
  924. $sections{'Thread Safety'} = wordwrap(wikify($wikitype, $desc)) . "\n";
  925. } elsif ($l =~ /\A\\sa\s+(.*)\Z/) {
  926. my $sa = $1;
  927. $sa =~ s/\(\)\Z//; # Convert "SDL_Func()" to "SDL_Func"
  928. $sections{'Related Functions'} = '' if not defined $sections{'Related Functions'};
  929. if ($wikitype eq 'mediawiki') {
  930. $sections{'Related Functions'} .= ":[[$sa]]\n";
  931. } elsif ($wikitype eq 'md') {
  932. $sections{'Related Functions'} .= "* [$sa]($sa)\n";
  933. } else { die("Expected wikitype '$wikitype'\n"); }
  934. }
  935. }
  936. # Make sure this ends with a double-newline.
  937. $sections{'Related Functions'} .= "\n" if defined $sections{'Related Functions'};
  938. # We can build the wiki table now that we have all the data.
  939. if (scalar(@params) > 0) {
  940. my $str = '';
  941. if ($wikitype eq 'mediawiki') {
  942. while (scalar(@params) > 0) {
  943. my $arg = shift @params;
  944. my $desc = wikify($wikitype, shift @params);
  945. $str .= ($str eq '') ? "{|\n" : "|-\n";
  946. $str .= "|'''$arg'''\n";
  947. $str .= "|$desc\n";
  948. }
  949. $str .= "|}\n";
  950. } elsif ($wikitype eq 'md') {
  951. my $longest_arg = 0;
  952. my $longest_desc = 0;
  953. my $which = 0;
  954. foreach (@params) {
  955. if ($which == 0) {
  956. my $len = length($_) + 4;
  957. $longest_arg = $len if ($len > $longest_arg);
  958. $which = 1;
  959. } else {
  960. my $len = length(wikify($wikitype, $_));
  961. $longest_desc = $len if ($len > $longest_desc);
  962. $which = 0;
  963. }
  964. }
  965. # Markdown tables are sort of obnoxious.
  966. $str .= '| ' . (' ' x ($longest_arg+4)) . ' | ' . (' ' x $longest_desc) . " |\n";
  967. $str .= '| ' . ('-' x ($longest_arg+4)) . ' | ' . ('-' x $longest_desc) . " |\n";
  968. while (@params) {
  969. my $arg = shift @params;
  970. my $desc = wikify($wikitype, shift @params);
  971. $str .= "| **$arg** " . (' ' x ($longest_arg - length($arg))) . "| $desc" . (' ' x ($longest_desc - length($desc))) . " |\n";
  972. }
  973. } else {
  974. die("Unexpected wikitype!\n"); # should have checked this elsewhere.
  975. }
  976. $sections{'Function Parameters'} = $str;
  977. }
  978. my $path = "$wikipath/$_.${wikitype}.tmp";
  979. open(FH, '>', $path) or die("Can't open '$path': $!\n");
  980. my $sectionsref = $wikifuncs{$fn};
  981. foreach (@standard_wiki_sections) {
  982. # drop sections we either replaced or removed from the original wiki's contents.
  983. if (not defined $only_wiki_sections{$_}) {
  984. delete($$sectionsref{$_});
  985. }
  986. }
  987. my $wikisectionorderref = $wikisectionorder{$fn};
  988. # Make sure there's a footer in the wiki that puts this function in CategoryAPI...
  989. if (not $$sectionsref{'[footer]'}) {
  990. $$sectionsref{'[footer]'} = '';
  991. push @$wikisectionorderref, '[footer]';
  992. }
  993. # !!! FIXME: This won't be CategoryAPI if we eventually handle things other than functions.
  994. my $footer = $$sectionsref{'[footer]'};
  995. if ($wikitype eq 'mediawiki') {
  996. $footer =~ s/\[\[CategoryAPI\]\],?\s*//g;
  997. $footer = '[[CategoryAPI]]' . (($footer eq '') ? "\n" : ", $footer");
  998. } elsif ($wikitype eq 'md') {
  999. $footer =~ s/\[CategoryAPI\]\(CategoryAPI\),?\s*//g;
  1000. $footer = '[CategoryAPI](CategoryAPI)' . (($footer eq '') ? '' : ', ') . $footer;
  1001. } else { die("Unexpected wikitype '$wikitype'\n"); }
  1002. $$sectionsref{'[footer]'} = $footer;
  1003. if (defined $wikipreamble) {
  1004. my $wikified_preamble = wikify($wikitype, $wikipreamble);
  1005. if ($wikitype eq 'mediawiki') {
  1006. print FH "====== $wikified_preamble ======\n";
  1007. } elsif ($wikitype eq 'md') {
  1008. print FH "###### $wikified_preamble\n";
  1009. } else { die("Unexpected wikitype '$wikitype'\n"); }
  1010. }
  1011. my $prevsectstr = '';
  1012. my @ordered_sections = (@standard_wiki_sections, defined $wikisectionorderref ? @$wikisectionorderref : ()); # this copies the arrays into one.
  1013. foreach (@ordered_sections) {
  1014. my $sect = $_;
  1015. next if $sect eq '[start]';
  1016. next if (not defined $sections{$sect} and not defined $$sectionsref{$sect});
  1017. my $section = defined $sections{$sect} ? $sections{$sect} : $$sectionsref{$sect};
  1018. if ($sect eq '[footer]') {
  1019. # Make sure previous section ends with two newlines.
  1020. if (substr($prevsectstr, -1) ne "\n") {
  1021. print FH "\n\n";
  1022. } elsif (substr($prevsectstr, -2) ne "\n\n") {
  1023. print FH "\n";
  1024. }
  1025. print FH "----\n"; # It's the same in Markdown and MediaWiki.
  1026. } elsif ($sect eq '[Brief]') {
  1027. if ($wikitype eq 'mediawiki') {
  1028. print FH "= $fn =\n\n";
  1029. } elsif ($wikitype eq 'md') {
  1030. print FH "# $fn\n\n";
  1031. } else { die("Unexpected wikitype '$wikitype'\n"); }
  1032. } else {
  1033. if ($wikitype eq 'mediawiki') {
  1034. print FH "\n== $sect ==\n\n";
  1035. } elsif ($wikitype eq 'md') {
  1036. print FH "\n## $sect\n\n";
  1037. } else { die("Unexpected wikitype '$wikitype'\n"); }
  1038. }
  1039. my $sectstr = defined $sections{$sect} ? $sections{$sect} : $$sectionsref{$sect};
  1040. print FH $sectstr;
  1041. $prevsectstr = $sectstr;
  1042. # make sure these don't show up twice.
  1043. delete($sections{$sect});
  1044. delete($$sectionsref{$sect});
  1045. }
  1046. print FH "\n\n";
  1047. close(FH);
  1048. rename($path, "$wikipath/$_.${wikitype}") or die("Can't rename '$path' to '$wikipath/$_.${wikitype}': $!\n");
  1049. }
  1050. } elsif ($copy_direction == -2) { # --copy-to-manpages
  1051. # This only takes from the wiki data, since it has sections we omit from the headers, like code examples.
  1052. my $manpath = "$srcpath/man";
  1053. mkdir($manpath);
  1054. $manpath .= "/man3";
  1055. mkdir($manpath);
  1056. $dewikify_mode = 'manpage';
  1057. $wordwrap_mode = 'manpage';
  1058. my $introtxt = '';
  1059. if (0) {
  1060. open(FH, '<', "$srcpath/LICENSE.txt") or die("Can't open '$srcpath/LICENSE.txt': $!\n");
  1061. while (<FH>) {
  1062. chomp;
  1063. $introtxt .= ".\\\" $_\n";
  1064. }
  1065. close(FH);
  1066. }
  1067. my $gitrev = `cd "$srcpath" ; git rev-list HEAD~..`;
  1068. chomp($gitrev);
  1069. # !!! FIXME
  1070. open(FH, '<', "$srcpath/$versionfname") or die("Can't open '$srcpath/$versionfname': $!\n");
  1071. my $majorver = 0;
  1072. my $minorver = 0;
  1073. my $patchver = 0;
  1074. while (<FH>) {
  1075. chomp;
  1076. if (/$versionmajorregex/) {
  1077. $majorver = int($1);
  1078. } elsif (/$versionminorregex/) {
  1079. $minorver = int($1);
  1080. } elsif (/$versionpatchregex/) {
  1081. $patchver = int($1);
  1082. }
  1083. }
  1084. close(FH);
  1085. my $fullversion = "$majorver.$minorver.$patchver";
  1086. foreach (keys %headerfuncs) {
  1087. my $fn = $_;
  1088. next if not defined $wikifuncs{$fn}; # don't have a page for that function, skip it.
  1089. my $wikitype = $wikitypes{$fn};
  1090. my $sectionsref = $wikifuncs{$fn};
  1091. my $remarks = %$sectionsref{'Remarks'};
  1092. my $params = %$sectionsref{'Function Parameters'};
  1093. my $returns = %$sectionsref{'Return Value'};
  1094. my $version = %$sectionsref{'Version'};
  1095. my $threadsafety = %$sectionsref{'Thread Safety'};
  1096. my $related = %$sectionsref{'Related Functions'};
  1097. my $examples = %$sectionsref{'Code Examples'};
  1098. my $deprecated = %$sectionsref{'Deprecated'};
  1099. my $brief = %$sectionsref{'[Brief]'};
  1100. my $decl = $headerdecls{$fn};
  1101. my $str = '';
  1102. $brief = "$brief";
  1103. $brief =~ s/\A[\s\n]*\= .*? \=\s*?\n+//ms;
  1104. $brief =~ s/\A[\s\n]*\=\= .*? \=\=\s*?\n+//ms;
  1105. $brief =~ s/\A(.*?\.) /$1\n/; # \brief should only be one sentence, delimited by a period+space. Split if necessary.
  1106. my @briefsplit = split /\n/, $brief;
  1107. $brief = shift @briefsplit;
  1108. $brief = dewikify($wikitype, $brief);
  1109. if (defined $remarks) {
  1110. $remarks = dewikify($wikitype, join("\n", @briefsplit) . $remarks);
  1111. }
  1112. $str .= $introtxt;
  1113. $str .= ".\\\" This manpage content is licensed under Creative Commons\n";
  1114. $str .= ".\\\" Attribution 4.0 International (CC BY 4.0)\n";
  1115. $str .= ".\\\" https://creativecommons.org/licenses/by/4.0/\n";
  1116. $str .= ".\\\" This manpage was generated from ${projectshortname}'s wiki page for $fn:\n";
  1117. $str .= ".\\\" $wikiurl/$fn\n";
  1118. $str .= ".\\\" Generated with SDL/build-scripts/wikiheaders.pl\n";
  1119. $str .= ".\\\" revision $gitrev\n" if $gitrev ne '';
  1120. $str .= ".\\\" Please report issues in this manpage's content at:\n";
  1121. $str .= ".\\\" $bugreporturl\n";
  1122. $str .= ".\\\" Please report issues in the generation of this manpage from the wiki at:\n";
  1123. $str .= ".\\\" https://github.com/libsdl-org/SDL/issues/new?title=Misgenerated%20manpage%20for%20$fn\n";
  1124. $str .= ".\\\" $projectshortname can be found at $projecturl\n";
  1125. # 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.
  1126. # This wizadry is from https://web.archive.org/web/20060102165607/http://people.debian.org/~branden/talks/wtfm/wtfm.pdf
  1127. $str .= ".de URL\n";
  1128. $str .= '\\$2 \(laURL: \\$1 \(ra\\$3' . "\n";
  1129. $str .= "..\n";
  1130. $str .= '.if \n[.g] .mso www.tmac' . "\n";
  1131. $str .= ".TH $fn 3 \"$projectshortname $fullversion\" \"$projectfullname\" \"$projectshortname$majorver FUNCTIONS\"\n";
  1132. $str .= ".SH NAME\n";
  1133. $str .= "$fn";
  1134. $str .= " \\- $brief" if (defined $brief);
  1135. $str .= "\n";
  1136. $str .= ".SH SYNOPSIS\n";
  1137. $str .= ".nf\n";
  1138. $str .= ".B #include \\(dq$mainincludefname\\(dq\n";
  1139. $str .= ".PP\n";
  1140. my @decllines = split /\n/, $decl;
  1141. foreach (@decllines) {
  1142. $str .= ".BI \"$_\n";
  1143. }
  1144. $str .= ".fi\n";
  1145. if (defined $remarks) {
  1146. $str .= ".SH DESCRIPTION\n";
  1147. $str .= $remarks . "\n";
  1148. }
  1149. if (defined $deprecated) {
  1150. $str .= ".SH DEPRECATED\n";
  1151. $str .= dewikify($wikitype, $deprecated) . "\n";
  1152. }
  1153. if (defined $params) {
  1154. $str .= ".SH FUNCTION PARAMETERS\n";
  1155. my @lines = split /\n/, $params;
  1156. if ($wikitype eq 'mediawiki') {
  1157. die("Unexpected data parsing MediaWiki table") if (shift @lines ne '{|'); # Dump the '{|' start
  1158. while (scalar(@lines) >= 3) {
  1159. my $name = shift @lines;
  1160. my $desc = shift @lines;
  1161. my $terminator = shift @lines; # the '|-' or '|}' line.
  1162. last if ($terminator ne '|-') and ($terminator ne '|}'); # we seem to have run out of table.
  1163. $name =~ s/\A\|\s*//;
  1164. $name =~ s/\A\*\*(.*?)\*\*/$1/;
  1165. $name =~ s/\A\'\'\'(.*?)\'\'\'/$1/;
  1166. $desc =~ s/\A\|\s*//;
  1167. $desc = dewikify($wikitype, $desc);
  1168. #print STDERR "FN: $fn NAME: $name DESC: $desc TERM: $terminator\n";
  1169. $str .= ".TP\n";
  1170. $str .= ".I $name\n";
  1171. $str .= "$desc\n";
  1172. }
  1173. } else {
  1174. die("write me");
  1175. }
  1176. }
  1177. if (defined $returns) {
  1178. $str .= ".SH RETURN VALUE\n";
  1179. $str .= dewikify($wikitype, $returns) . "\n";
  1180. }
  1181. if (defined $examples) {
  1182. $str .= ".SH CODE EXAMPLES\n";
  1183. $dewikify_manpage_code_indent = 0;
  1184. $str .= dewikify($wikitype, $examples) . "\n";
  1185. $dewikify_manpage_code_indent = 1;
  1186. }
  1187. if (defined $threadsafety) {
  1188. $str .= ".SH THREAD SAFETY\n";
  1189. $str .= dewikify($wikitype, $threadsafety) . "\n";
  1190. }
  1191. if (defined $version) {
  1192. $str .= ".SH AVAILABILITY\n";
  1193. $str .= dewikify($wikitype, $version) . "\n";
  1194. }
  1195. if (defined $related) {
  1196. $str .= ".SH SEE ALSO\n";
  1197. # !!! FIXME: lots of code duplication in all of these.
  1198. my $v = dewikify($wikitype, $related);
  1199. my @desclines = split /\n/, $v;
  1200. my $nextstr = '';
  1201. foreach (@desclines) {
  1202. s/\A(\:|\* )//;
  1203. s/\(\)\Z//; # Convert "SDL_Func()" to "SDL_Func"
  1204. s/\[\[(.*?)\]\]/$1/; # in case some wikilinks remain.
  1205. s/\A\/*//;
  1206. s/\A\.BR\s+//; # dewikify added this, but we want to handle it.
  1207. s/\A\s+//;
  1208. s/\s+\Z//;
  1209. next if $_ eq '';
  1210. $str .= "$nextstr.BR $_ (3)";
  1211. $nextstr = ",\n";
  1212. }
  1213. $str .= "\n";
  1214. }
  1215. if (0) {
  1216. $str .= ".SH COPYRIGHT\n";
  1217. $str .= "This manpage is licensed under\n";
  1218. $str .= ".UR https://creativecommons.org/licenses/by/4.0/\n";
  1219. $str .= "Creative Commons Attribution 4.0 International (CC BY 4.0)\n";
  1220. $str .= ".UE\n";
  1221. $str .= ".PP\n";
  1222. $str .= "This manpage was generated from\n";
  1223. $str .= ".UR $wikiurl/$fn\n";
  1224. $str .= "${projectshortname}'s wiki\n";
  1225. $str .= ".UE\n";
  1226. $str .= "using SDL/build-scripts/wikiheaders.pl";
  1227. $str .= " revision $gitrev" if $gitrev ne '';
  1228. $str .= ".\n";
  1229. $str .= "Please report issues in this manpage at\n";
  1230. $str .= ".UR $bugreporturl\n";
  1231. $str .= "our bugtracker!\n";
  1232. $str .= ".UE\n";
  1233. }
  1234. my $path = "$manpath/$_.3.tmp";
  1235. open(FH, '>', $path) or die("Can't open '$path': $!\n");
  1236. print FH $str;
  1237. close(FH);
  1238. rename($path, "$manpath/$_.3") or die("Can't rename '$path' to '$manpath/$_.3': $!\n");
  1239. }
  1240. }
  1241. # end of wikiheaders.pl ...