wikiheaders.pl 63 KB

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