wikiheaders.pl 59 KB

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