wikiheaders.pl 56 KB

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