wikiheaders.pl 58 KB

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