wikiheaders.pl 62 KB

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