wikiheaders.pl 53 KB

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