wikiheaders.pl 50 KB

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