wikiheaders.pl 62 KB

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