generator.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622
  1. <?php
  2. global $options;
  3. global $preamble;
  4. $preamble = "";
  5. global $existing_commands;
  6. global $included_usepackages;
  7. $included_usepackages = [];
  8. global $additional_usepackages;
  9. $additional_usepackages = [];
  10. $steps = 10;
  11. function user_consent($msg,$defaultyes = null,$defaultno = null)
  12. {
  13. global $options;
  14. if (isset($options["-n"]))
  15. return false;
  16. if ($defaultyes !== null && isset($options[$defaultyes]))
  17. return true;
  18. if ($defaultno !== null && isset($options[$defaultno]))
  19. return false;
  20. $response = false;
  21. do
  22. {
  23. echo "$msg (y/n)\n";
  24. $response = rtrim(fgets(STDIN));
  25. if ($response === "n" || $response == "N")
  26. return false;
  27. } while ($response !== "y" && $response !== "Y" && $response !== "");
  28. return true;
  29. }
  30. function user_prompt($msg,$default)
  31. {
  32. global $options;
  33. echo "$msg [Default: $default]\n";
  34. $response = "";
  35. if (!isset($options["-n"]))
  36. $response = rtrim(fgets(STDIN));
  37. if ($response == "")
  38. $response = $default;
  39. echo "You entered: '$response'\n";
  40. return $response;
  41. }
  42. function print_help()
  43. {
  44. $name = basename(__FILE__);
  45. echo<<<END
  46. Usage: php $name /path/folder1/main.tex /path/folder2/article.tex ... /path/folderN/paper.tex\n
  47. Generate a thesis template from the provided paper tex files and folders.
  48. The absolute minimum for a cumulative thesis is currently 3 papers first-authored and 6 in total.
  49. --force-overwrite overwrite files and folders
  50. --no-overwrite do not overwrite files and folders
  51. --no-compile-check do not perform a compile check
  52. --use-backups restore backups before making changes
  53. --not-sloppy disable use of sloppypar around places that struggle without
  54. (mainly the bibliographies)
  55. --cv add a CV
  56. (unusual for PhD theses, required for habilitation theses)
  57. --no-stat-declaration do not add the statutory declaration
  58. (required for PhD thesis, unusual for habilitation theses)
  59. -n skip all prompts (implies --no-overwrite)
  60. --help display this help and exit
  61. END;
  62. exit(0);
  63. }
  64. function error_exit($msg,$usage = false)
  65. {
  66. echo "ERROR: $msg\n";
  67. if ($usage)
  68. echo "Usage: php ".basename(__FILE__)." /path/folder1/main.tex /path/folder2/article.tex ... /path/folderN/paper.tex\n";
  69. exit(-1);
  70. }
  71. function check_software()
  72. {
  73. if (strpos(shell_exec("latexmk --version"),"Latexmk,") === false)
  74. error_exit("latexmk not installed");
  75. if (!str_starts_with(shell_exec("pdftotext --help 2>&1"),"pdftotext version"))
  76. error_exit("pdftotext not installed");
  77. }
  78. function rrmdir($dir) {
  79. if (is_dir($dir)) {
  80. $files = scandir($dir);
  81. foreach ($files as $file)
  82. if ($file != "." && $file != "..") rrmdir("$dir/$file");
  83. rmdir($dir);
  84. }
  85. else if (file_exists($dir)) unlink($dir);
  86. }
  87. function rcopy($src, $dst) {
  88. if (file_exists($dst)) rrmdir($dst);
  89. if (is_dir($src)) {
  90. mkdir($dst);
  91. $files = scandir($src);
  92. foreach ($files as $file)
  93. if ($file != "." && $file != "..") rcopy("$src/$file", "$dst/$file");
  94. }
  95. else if (file_exists($src)) copy($src, $dst);
  96. }
  97. function rscandir($dir,$extensions = []) {
  98. $result = [];
  99. if (!str_ends_with($dir,"/"))
  100. $dir .= "/";
  101. if (str_ends_with($dir,"latex.out/"))
  102. return $result;
  103. $array = scandir($dir);
  104. foreach ($array as $item) {
  105. if (!str_starts_with($item,"."))
  106. {
  107. if (!is_dir($dir.$item))
  108. {
  109. $extension_match = true;
  110. if ($extensions != [])
  111. {
  112. $extension_match = false;
  113. foreach ($extensions as $e)
  114. {
  115. if (str_ends_with($item,"$e"))
  116. {
  117. $extension_match = true;
  118. }
  119. }
  120. }
  121. if ($extension_match)
  122. $result[] = $dir.$item;
  123. }
  124. else
  125. $result = array_merge($result, rscandir($dir.$item,$extensions));
  126. }
  127. }
  128. return $result;
  129. }
  130. function unique_targets($targets)
  131. {
  132. $parts_required = 1;
  133. $unique_targets = [];
  134. while (count($unique_targets) != count($targets))
  135. {
  136. foreach ($targets as $t)
  137. {
  138. $tparts = explode("/",$t);
  139. $tpcount = count($tparts);
  140. $tparts = array_splice($tparts,$tpcount-$parts_required,$parts_required);
  141. $t = implode("_",$tparts);
  142. if (isset($unique_targets[$t]))
  143. {
  144. $parts_required++;
  145. $unique_targets = [];
  146. break;
  147. }
  148. $unique_targets[$t] = 1;
  149. }
  150. }
  151. return array_keys($unique_targets);
  152. }
  153. function check_folders($argv)
  154. {
  155. $args = array_slice($argv,1);
  156. $sourcedirs = [];
  157. foreach ($args as $a)
  158. {
  159. if (!str_starts_with($a,"-"))
  160. $sourcedirs[] = $a;
  161. }
  162. if (count($sourcedirs) < 1)
  163. {
  164. error_exit("Too few papers. Need at least one paper to run this.",true);
  165. }
  166. if (count($sourcedirs) < 3)
  167. {
  168. echo "Warning: Too few papers. The statutes require as an absolute minimum for a cumulative thesis at least 3 first-author papers and at least 6 in total.";
  169. }
  170. if (count($sourcedirs) < 5)
  171. {
  172. echo "Warning: Few papers. Check your institute guidelines to see how many papers are recommended. Also the statutes require as an absolute minimum for a cumulative thesis at least 6 papers in total.";
  173. }
  174. $texfiles = [];
  175. for ($i = 0; $i < count($sourcedirs); $i++)
  176. {
  177. $texfiles[$i] = basename($sourcedirs[$i]);
  178. $sourcedirs[$i] = dirname($sourcedirs[$i]);
  179. }
  180. $targets = unique_targets($sourcedirs);
  181. return [$targets,$texfiles,$sourcedirs];
  182. }
  183. function check_and_copy_folders($argv)
  184. {
  185. [$targets,$texfiles,$sourcedirs] = check_folders($argv);
  186. //echo "Source -> Target Directory Mapping:\n";
  187. $n = count($sourcedirs);
  188. //print_r($mapping);
  189. for ($i = 0; $i < $n; $i++)
  190. {
  191. $s = $sourcedirs[$i];
  192. $t = $targets[$i];
  193. $f = $texfiles[$i];
  194. if (str_ends_with($f,".pdf"))
  195. {
  196. if (!file_exists("$s/$f"))
  197. error_exit("$s/$f not found");
  198. if (file_exists("$t/$f.pdf") && !user_consent("$t/$f.pdf already exists... overwrite?","--force-overwrite","--no-overwrite"))
  199. {
  200. echo "Skipping $s/$f, $t/$f.pdf already exists...\n";
  201. }
  202. else
  203. {
  204. echo "Copying $s/$f to $t/$f...\n";
  205. if (!file_exists($t)) mkdir($t);
  206. rcopy("$s/$f","$t/$f");
  207. }
  208. }
  209. else
  210. {
  211. if (!file_exists($s))
  212. error_exit("$s not found");
  213. if (file_exists($t) && !user_consent("$t already exists... overwrite?","--force-overwrite","--no-overwrite"))
  214. {
  215. echo "Skipping ".$s.", ".$t." already exists...\n";
  216. }
  217. else
  218. {
  219. echo "Copying ".$s." to ".$t."...\n";
  220. rcopy($s,$t);
  221. }
  222. }
  223. }
  224. return array_combine($targets,$texfiles);
  225. }
  226. function file_get_and_backup($f,$bak = ".orig_bak")
  227. {
  228. global $options;
  229. if (isset($options["--use-backups"]))
  230. {
  231. if (file_exists("$f$bak"))
  232. copy("$f$bak", $f);
  233. }
  234. $content = file_get_contents("$f");
  235. if (!file_exists("$f$bak"))
  236. file_put_contents("$f$bak",$content);
  237. return $content;
  238. }
  239. function adjust_references($papers,$extensions = [])
  240. {
  241. $bibresources = [];
  242. foreach ($papers as $d => $f)
  243. {
  244. if (str_ends_with($f,".pdf"))
  245. continue;
  246. $files = rscandir($d,$extensions);
  247. foreach ($files as $file)
  248. {
  249. if (str_ends_with($file,".bib"))
  250. {
  251. $bib = file_get_and_backup($file);
  252. $bib = preg_replace("/(^\s*@\s*[a-z]+\s*\{\s*)((?!$d)\S)/i",'${1}'.$d.':${2}',$bib);
  253. $bib = preg_replace("/(\n\s*@\s*[a-z]+\s*\{\s*)((?!$d)\S)/i",'${1}'.$d.':${2}',$bib);
  254. file_put_contents($file,$bib);
  255. $bibresources[$d] = $file;
  256. }
  257. else
  258. {
  259. $tex = file_get_and_backup($file);
  260. // labels of lstlistings etc
  261. $tex = preg_replace("/([\[,]\s*label\s*=\s*)((?!$d)[^\[,]+[\[,])/i",'${1}'.$d.':${2}',$tex);
  262. // normal labels
  263. $tex = preg_replace("/(\\\label\s*\{\s*)((?!$d)[^}]+\})/i",'${1}'.$d.':${2}',$tex);
  264. // cref ref autoref
  265. $refs = [];
  266. preg_match_all("/\\\(cref|ref|autoref)\s*\{\s*[^}]+\}/i",$tex,$refs);
  267. foreach ($refs[0] as $ref)
  268. {
  269. $oldref = $ref;
  270. $ref = preg_replace("/([{,])\s*((?!$d)[^,}]+[},])/i",'${1}'.$d.':${2}',$ref);
  271. $ref = preg_replace("/([{,])\s*((?!$d)[^,}]+[},])/i",'${1}'.$d.':${2}',$ref);
  272. $tex = str_replace($oldref,$ref,$tex);
  273. }
  274. // cite citeA citeauthor etc
  275. $cites = [];
  276. preg_match_all("/\\\([a-z]*cite[a-z]*)\s*\{\s*[^}]+\}/i",$tex,$cites);
  277. foreach ($cites[0] as $cite)
  278. {
  279. $oldcite = $cite;
  280. $cite = preg_replace("/([{,])\s*((?!$d)[^,}]+[},])/i",'${1}'.$d.':${2}',$cite);
  281. $cite = preg_replace("/([{,])\s*((?!$d)[^,}]+[},])/i",'${1}'.$d.':${2}',$cite);
  282. $tex = str_replace($oldcite,$cite,$tex);
  283. }
  284. file_put_contents($file,$tex);
  285. }
  286. }
  287. }
  288. return $bibresources;
  289. }
  290. function stripformatting($tex)
  291. {
  292. return str_ireplace(['\Large ','\bf ','\rm ','\\\\'],[' ',' ',' ',' '],$tex);
  293. }
  294. function getusepackages($tex)
  295. {
  296. $packages = [];
  297. preg_match_all("/\n\s*(\\\\usepackage([^{}]*)(?<R>{((?:[^{}]+|(?&R))*)}))/i",$tex,$packages);
  298. if (isset($packages[1]))
  299. {
  300. return [$packages[1],$packages[4]];
  301. }
  302. return [[],[]];
  303. }
  304. function getmathops($tex)
  305. {
  306. $mathops = [];
  307. preg_match_all("/\n\s*(\\\\DeclareMathOperator([^{}]*)(?<R>{((?:[^{}]+|(?&R))*)})([^{}]*)(?<P>{((?:[^{}]+|(?&P))*)}))/i",$tex,$mathops);
  308. if (isset($mathops[4]))
  309. return [$mathops[1],$mathops[4]];
  310. return [];
  311. }
  312. function getnewcommands($tex)
  313. {
  314. $newcommands = [];
  315. preg_match_all("/\n\s*(\\\\newcommand([^{}]*)(?<R>{((?:[^{}]+|(?&R))*)})([^{}]*)(?<P>{((?:[^{}]+|(?&P))*)}))/i",$tex,$newcommands);
  316. if (isset($newcommands[4]))
  317. return $newcommands[4];
  318. return [];
  319. }
  320. function adjust_papers($papers,$extensions = [])
  321. {
  322. foreach ($papers as $d => $f)
  323. {
  324. $tex = "";
  325. $paper_fulltitle = "";
  326. $authors = "";
  327. $institute = "";
  328. if (str_ends_with($f,".pdf"))
  329. {
  330. $paper_fulltitle = trim(user_prompt("Please enter the full title for the paper in folder '$d'","TODO"));
  331. $paper_shorttitle = trim(user_prompt("Please enter a short title for the paper in folder '$d'","TODO"));
  332. $authors = trim(user_prompt("Please enter the author list for paper '$d'","TODO"));
  333. $header=<<<END
  334. \chapter[$paper_shorttitle]{{$paper_fulltitle}}\label{chapter:$d}
  335. \section*{Publication Data}
  336. \\fullcite{TODO}
  337. \section*{Contributions}
  338. TODO
  339. \includepdf[pages=-]{{$d}/{$f}}
  340. END;
  341. file_put_contents("$d/main.tex",$header);
  342. }
  343. $files = rscandir($d,$extensions);
  344. foreach ($files as $file)
  345. {
  346. if (!str_ends_with($file,"main.tex"))
  347. continue;
  348. $tex = file_get_and_backup($file,".interm_bak");
  349. if ($authors == "" && preg_match("/\n\s*\\\\author/i",$tex) == 1)
  350. {
  351. $authors = [];
  352. preg_match_all("/\n\s*\\\\author\s*(?<R>{((?:[^{}]+|(?&R))*)})/i",$tex,$authors);
  353. if (isset($authors[2]) && isset($authors[2][0]))
  354. $authors = trim(stripformatting($authors[2][0]));
  355. }
  356. if ($institute == "" && preg_match("/\n\s*\\\\institute/i",$tex) == 1)
  357. {
  358. $institute = [];
  359. preg_match_all("/\n\s*\\\\institute\s*(?<R>{((?:[^{}]+|(?&R))*)})/i",$tex,$institute);
  360. if (isset($institute[2]) && isset($institute[2][0]))
  361. $institute = trim(stripformatting($institute[2][0]));
  362. }
  363. if ($paper_fulltitle == "" && preg_match("/\n\s*\\\\title/i",$tex) == 1)
  364. {
  365. $title = [];
  366. preg_match_all("/\n\s*\\\\title\s*(?<R>{((?:[^{}]+|(?&R))*)})/i",$tex,$title);
  367. if (isset($title[2]) && isset($title[2][0]))
  368. $title = trim(stripformatting($title[2][0]));
  369. else
  370. $title = $d;
  371. $shorttitle = $title;
  372. if (strpos($title,":") !== false)
  373. $shorttitle = trim(explode(":",$title)[0]);
  374. $paper_fulltitle = trim(user_prompt("Please enter the full title for the paper in folder '$d'","$title"));
  375. $paper_shorttitle = trim(user_prompt("Please enter a short title for the paper in folder '$d'","$shorttitle"));
  376. }
  377. $header = "";
  378. if ($paper_fulltitle != "" && $authors != "")
  379. {
  380. $header=<<<END
  381. \chapter[$paper_shorttitle]{$paper_fulltitle}\label{chapter:$d}
  382. \section*{Publication Data}
  383. \\fullcite{TODO}
  384. \section*{Contributions}
  385. TODO
  386. \\newpage
  387. \begin{center}
  388. {\Large \\textbf{
  389. $paper_fulltitle%
  390. }}\\\\\
  391. \\vspace{0.6cm}
  392. {\large $authors%
  393. } % TODO: check author list
  394. {\large $institute%
  395. } % TODO: check author list
  396. \\end{center}
  397. END;
  398. }
  399. $tex = preg_replace("/\\\documentclass.*\\n/i","
  400. ",$tex);
  401. $tex = preg_replace("/\\\begin{document}/i",$header,$tex);
  402. $tex = preg_replace("/\\\begin{abstract}/i",'\section*{Abstract}',$tex);
  403. $tex = preg_replace("/\\\\newcommand/i",'\providecommand',$tex);
  404. $tex = preg_replace("/\\\\end{abstract}/i",'',$tex);
  405. $tex = preg_replace("/\\\\thispagestyle{empty}/i",'',$tex);
  406. $tex = preg_replace("/\\\\PassOptionsToPackage\s*(?<R>{((?:[^{}]+|(?&R))*)})(?<Q>{((?:[^{}]+|(?&Q))*)})/i",'',$tex);
  407. $tex = preg_replace("/(\\\\csvautobooktabular\s*([^{}]*)\s*)(?<R>{((?:[^{}]+|(?&R))*)})/i","\\1{{$d}/\\4}",$tex);
  408. $tex = preg_replace("/\\\\bibliographystyle\s*(?<R>{((?:[^{}]+|(?&R))*)})/i","",$tex);
  409. $tex = preg_replace("/\\\\usetikzlibrary\s*(?<R>{((?:[^{}]+|(?&R))*)})/i","",$tex);
  410. $tex = preg_replace("/\\\\DeclareFloatFont\s*(?<R>{((?:[^{}]+|(?&R))*)})(?<Q>{((?:[^{}]+|(?&Q))*)})/i","",$tex);
  411. $tex = preg_replace("/\\\\captionsetup\s*(?<R>\[((?:[^\[\]]+|(?&R))*)\])?(?<Q>{((?:[^{}]+|(?&Q))*)})/i","",$tex);
  412. $tex = preg_replace("/\\\\tikzexternalize\s*(?<R>\[((?:[^\[\]]+|(?&R))*)\])/i","",$tex);
  413. $tex = preg_replace("/\\\\floatsetup\s*(?<R>\[((?:[^\[\]]+|(?&R))*)\])(?<Q>{((?:[^{}]+|(?&Q))*)})/i","",$tex);
  414. $tex = preg_replace("/\n\s*\\\\bibliography\s*(?<R>{((?:[^{}]+|(?&R))*)})/i","",$tex);
  415. $tex = preg_replace("/\\\\end{document}/i",'',$tex);
  416. $tex = preg_replace("/\n\s*\\\\author\s*(?<R>{((?:[^{}]+|(?&R))*)})/i","",$tex);
  417. $tex = preg_replace("/\n\s*\\\\institute\s*(?<R>{((?:[^{}]+|(?&R))*)})/i","",$tex);
  418. $tex = preg_replace("/\n\s*\\\\title\s*(?<R>{((?:[^{}]+|(?&R))*)})/i","",$tex);
  419. $tex = preg_replace("/\n\s*\\\\date\s*(?<R>{((?:[^{}]+|(?&R))*)})/i","",$tex);
  420. $tex = preg_replace("/\n\s*\\\\maketitle/i","",$tex);
  421. $tex = preg_replace("/\n\s*\\\\tikzexternalize/i","",$tex);
  422. [$ups,$ps] = getusepackages($tex);
  423. global $included_usepackages;
  424. global $additional_usepackages;
  425. foreach (array_diff($ps,$included_usepackages) as $k => $v) {
  426. if ($v != "usenix,epsfig,endnotes" && $v != "cite" && !($v == 'floatrow' && in_array('float',$included_usepackages)))
  427. {
  428. $additional_usepackages[] = $ups[$k];
  429. $included_usepackages[] = $v;
  430. }
  431. }
  432. $additional_usepackages = array_unique($additional_usepackages);
  433. sort($additional_usepackages);
  434. $tex = preg_replace("/\n\s*\\\\usepackage([^{}]*)(?<R>{((?:[^{}]+|(?&R))*)})/i","",$tex);
  435. $newcommands = getnewcommands($tex);
  436. global $existing_commands;
  437. $rmcommands = array_intersect($newcommands,$existing_commands);
  438. foreach ($rmcommands as $rmc)
  439. {
  440. $tex = preg_replace("/\n\s*\\\\newcommand([^{}]*)({\s*\\".$rmc."\s*}.*\n)/i","\n",$tex);
  441. }
  442. [$mathopsfull,$mathops] = getmathops($tex);
  443. global $existing_mathops;
  444. global $preamble;
  445. $addmathops = array_diff($mathops,$existing_mathops);
  446. foreach ($addmathops as $k => $v)
  447. {
  448. $preamble .= $mathopsfull[$k] . "\n";
  449. $existing_mathops[] = $v;
  450. }
  451. $rmmathops = array_intersect($mathops,$existing_mathops);
  452. foreach ($rmmathops as $rmo)
  453. {
  454. $tex = preg_replace("/\n\s*\\\\DeclareMathOperator([^{}]*)({\s*\\".$rmo."\s*}.*\n)/i","\n",$tex);
  455. }
  456. $matches = [];
  457. $tex = preg_replace("/\n\s*\\\\pgfplotsset\s*(?<R>{((?:[^{}]+|(?&R))*)})/i","",$tex);
  458. $tex = preg_replace("/\n\s*\\\\setlength{\s*\\\marginparwidth.*\n/i","\n",$tex);
  459. file_put_contents($file,$tex);
  460. }
  461. }
  462. return;
  463. }
  464. function check_options($argv)
  465. {
  466. $options = [];
  467. foreach ($argv as $a)
  468. {
  469. if (str_starts_with($a,"-"))
  470. $options[$a] = 1;
  471. if ($a == "-n")
  472. $options["--no-overwrite"] = 1;
  473. }
  474. return $options;
  475. }
  476. function compile_check($papers,$precopy = false)
  477. {
  478. array_shift($papers);
  479. if ($precopy)
  480. $papers = array_combine($papers,$papers);
  481. foreach ($papers as $p => $f)
  482. {
  483. if (str_ends_with($f,".pdf"))
  484. {
  485. echo "======= Skipping PDF: $f =======\n";
  486. continue;
  487. }
  488. echo "======= Paper $p -> $f START =======\n";
  489. if ($precopy)
  490. {
  491. echo "$p\n";
  492. if (strpos($p,"/") === false || str_starts_with($p,".") || !is_dir(dirname($p)))
  493. continue;
  494. $f = basename($p);
  495. $p = dirname($p);
  496. }
  497. else
  498. {
  499. shell_exec("cd $p; latexmk -c 2>/dev/null; latexmk -C 2>/dev/null; rm -f *.bbl");
  500. }
  501. $str = shell_exec("cd $p; latexmk -latexoption=\"-shell-escape\" -g -pdf $f 2>&1");
  502. $lines = explode("\n",$str);
  503. $skip = true;
  504. $failed = false;
  505. foreach($lines as $l)
  506. {
  507. if (str_starts_with($l,"Latexmk: ====List of undefined refs and citations:"))
  508. $skip = false;
  509. if ($skip)
  510. continue;
  511. if (stripos($l,"fail") !== false)
  512. $failed = true;
  513. echo "$l\n";
  514. }
  515. if ($failed || user_consent("Please check the PDF file. Are there any problems with invalid references to figures, tables or with the bibliography?","--","--no-overwrite"))
  516. error_exit("The paper did not compile properly, please fix the errors next before continuing.");
  517. echo "======= Paper $p -> $f END =======\n";
  518. }
  519. }
  520. check_software();
  521. $options = check_options($argv);
  522. if (isset($options["--help"]))
  523. print_help();
  524. echo "\n=== Step 1/$steps: Check Folders ===\n\n";
  525. [$targets,$texfiles,$sourcedirs] = check_folders($argv);
  526. echo "\n=== Step 2/$steps: Pre-Copy Compile Check ===\n\n";
  527. $targets_exist = true;
  528. foreach ($targets as $t)
  529. if (!is_dir($t))
  530. $targets_exist = false;
  531. if (isset($options["--no-compile-check"]) || $targets_exist)
  532. echo "Skipping...\n";
  533. else
  534. compile_check($argv,true);
  535. echo "\n=== Step 3/$steps: Copying Files ===\n\n";
  536. $papers = check_and_copy_folders($argv);
  537. echo "\n=== Step 4/$steps: Adjusting References (in *.bib *.tex *.tikz) ===\n\n";
  538. $bibresources = adjust_references($papers,[".bib",".tex",".tikz"]);
  539. echo "\n=== Step 5/$steps: Compile Check ===\n\n";
  540. if (isset($options["--no-compile-check"]))
  541. echo "Skipping...\n";
  542. else
  543. compile_check($papers);
  544. [$_t,$usepackages1] = getusepackages(file_get_contents("main.tex.php"));
  545. [$_t,$usepackages2] = getusepackages(file_get_contents("tugraz_defaults.sty"));
  546. global $included_usepackages;
  547. $included_usepackages = array_unique(array_merge($usepackages1, $usepackages2));
  548. sort($included_usepackages);
  549. $cmds1 = getnewcommands(file_get_contents("main.tex.php"));
  550. $cmds2 = getnewcommands(file_get_contents("tugraz_defaults.sty"));
  551. global $existing_commands;
  552. $existing_commands = array_unique(array_merge($cmds1, $cmds2));
  553. [$del,$mathops1] = getmathops(file_get_contents("main.tex.php"));
  554. [$del,$mathops2] = getmathops(file_get_contents("tugraz_defaults.sty"));
  555. global $existing_mathops;
  556. $existing_mathops = array_unique(array_merge($mathops1, $mathops2));
  557. echo "\n=== Step 5/$steps: Adjusting Papers (checking only *.tex files) ===\n\n";
  558. adjust_papers($papers,[".tex"]);
  559. foreach ($papers as $k => $v)
  560. {
  561. if (str_ends_with($papers[$k],".pdf"))
  562. $papers[$k] = 'main.tex';
  563. }
  564. echo "\n=== Step 6/$steps: Generate main.tex ===\n\n";
  565. $thesis_type = user_prompt("Enter Thesis Type ","PhD Thesis");
  566. $thesis_title = user_prompt("Enter Thesis Title ","Security of TODO");
  567. $thesis_part1_title = user_prompt("Enter Introductory Part Title ","Introduction to the Security of TODO");
  568. $thesis_author = user_prompt("Enter Your Name ","Harry Potter");
  569. $thesis_date = user_prompt("Enter Prospective Defense Month", "July 1998");
  570. $thesis_institute = user_prompt("Enter Your Institute","Institute for Applied Information Processing and Communications");
  571. $thesis_assessors = user_prompt("Enter Names of your Assessors (comma-separated)","Severus Snape, Minerva McGonagall");
  572. $num_publications_in_thesis = count($papers);
  573. $num_publications = user_prompt("How many publications did you co-author during your PhD? (6 is the absolute minimum for a cumulative thesis)","6");
  574. $sloppy_begin = "";
  575. $sloppy_end = "";
  576. if (!isset($options["--no-sloppy"]))
  577. {
  578. $sloppy_begin = '\begin{sloppypar}';
  579. $sloppy_end = '\end{sloppypar}';
  580. }
  581. ob_start();
  582. require "main.tex.php";
  583. $maintex_content = ob_get_contents();
  584. ob_end_clean();
  585. //if (!file_exists("main.tex") || user_consent("main.tex already exists. overwrite?")) // TODO: enable this check
  586. file_put_contents("main.tex",$maintex_content);
  587. if (!file_exists("mypreamble.sty"))
  588. file_put_contents("mypreamble.sty","\ProvidesPackage{mypreamble}\n\n".implode("\n",$additional_usepackages)."\n\n".$preamble);
  589. @mkdir("tikz");
  590. if (!file_exists("abstract.tex"))
  591. file_put_contents("abstract.tex","");
  592. if (!file_exists("intro.tex"))
  593. file_put_contents("intro.tex","");
  594. if (!file_exists("main.bib"))
  595. file_put_contents("main.bib","");
  596. if (isset($options["--cv"]))
  597. {
  598. if (!file_exists("cv.bib"))
  599. file_put_contents("cv.bib","");
  600. if (!file_exists("cv.tex"))
  601. file_put_contents("cv.tex","This is my CV! Thanks for checking it out!");
  602. }
  603. echo "=== done ===\nNext step, run this command:\nlatexmk -latexoption=\"-shell-escape\" -g -pdf main.tex\n";
  604. ?>