wikiheaders.pl 66 KB

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