wikiheaders.pl 58 KB

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