Daniel Gruss 2 éve
szülő
commit
0458cb97ef
2 módosított fájl, 168 hozzáadás és 25 törlés
  1. 162 21
      generator.php
  2. 6 4
      main.tex.php

+ 162 - 21
generator.php

@@ -1,5 +1,12 @@
 <?php
   global $options;
+  global $preamble;
+  $preamble = "";
+  global $existing_commands;
+  global $included_usepackages;
+  $included_usepackages = [];
+  global $additional_usepackages;
+  $additional_usepackages = [];
   $steps = 10;
   function user_consent($msg,$defaultyes = null,$defaultno = null)
   {
@@ -45,6 +52,8 @@ The absolute minimum for a cumulative thesis is currently 3 papers first-authore
 --no-overwrite           do not overwrite files and folders
 --no-compile-check       do not perform a compile check
 --use-backups            restore backups before making changes
+--not-sloppy             disable use of sloppypar around places that struggle without
+                         (mainly the bibliographies)
 --cv                     add a CV
                          (unusual for PhD theses, required for habilitation theses)
 --no-stat-declaration    do not add the statutory declaration
@@ -257,42 +266,147 @@ END;
     }
     return $bibresources;
   }
+  function stripformatting($tex)
+  {
+    return str_ireplace(['\Large ','\bf ','\rm ','\\\\'],[' ',' ',' ',' '],$tex);
+  }
+  function getusepackages($tex)
+  {
+    $packages = [];
+    preg_match_all("/\n\s*(\\\\usepackage([^{}]*)(?<R>{((?:[^{}]+|(?&R))*)}))/i",$tex,$packages);
+    if (isset($packages[1]))
+    {
+      return [$packages[1],$packages[4]];
+    }
+    return [[],[]];
+  }
+  function getmathops($tex)
+  {
+    $mathops = [];
+    preg_match_all("/\n\s*(\\\\DeclareMathOperator([^{}]*)(?<R>{((?:[^{}]+|(?&R))*)})([^{}]*)(?<P>{((?:[^{}]+|(?&P))*)}))/i",$tex,$mathops);
+    if (isset($mathops[4]))
+      return [$mathops[1],$mathops[4]];
+    return [];
+  }
+  function getnewcommands($tex)
+  {
+    $newcommands = [];
+    preg_match_all("/\n\s*(\\\\newcommand([^{}]*)(?<R>{((?:[^{}]+|(?&R))*)})([^{}]*)(?<P>{((?:[^{}]+|(?&P))*)}))/i",$tex,$newcommands);
+    if (isset($newcommands[4]))
+      return $newcommands[4];
+    return [];
+  }
   function adjust_papers($papers,$extensions = [])
   {
     foreach ($papers as $d => $f)
     {
+      $paper_fulltitle = "";
+      $authors = "";
       $files = rscandir($d,$extensions);
       foreach ($files as $file)
       {
+        if (!str_ends_with($file,"main.tex"))
+          continue;
         $tex = file_get_and_backup($file,".interm_bak");
-        // labels of lstlistings etc
-        $tex = preg_replace("/([\[,]\s*label\s*=\s*)((?!$d)[^\[,]+[\[,])/i",'${1}'.$d.':${2}',$tex);
-        // normal labels
-        $tex = preg_replace("/(\\\label\s*\{\s*)((?!$d)[^}]+\})/i",'${1}'.$d.':${2}',$tex);
-        // cref ref autoref
-        $refs = [];
-        preg_match_all("/\\\(cref|ref|autoref)\s*\{\s*[^}]+\}/i",$tex,$refs);
-        foreach ($refs[0] as $ref)
+        if ($authors == "" && preg_match("/\n\s*\\\\author/i",$tex) == 1)
+        {
+          $authors = [];
+          preg_match_all("/\n\s*\\\\author\s*(?<R>{((?:[^{}]+|(?&R))*)})/i",$tex,$authors);
+          if (isset($authors[2]) && isset($authors[2][0]))
+            $authors = trim(stripformatting($authors[2][0]));
+        }
+        if ($paper_fulltitle == "" && preg_match("/\n\s*\\\\title/i",$tex) == 1)
+        {
+          $title = [];
+          preg_match_all("/\n\s*\\\\title\s*(?<R>{((?:[^{}]+|(?&R))*)})/i",$tex,$title);
+          if (isset($title[2]) && isset($title[2][0]))
+            $title = trim(stripformatting($title[2][0]));
+          else
+            $title = $d;
+          
+          $shorttitle = $title;
+          if (strpos($title,":") !== false)
+            $shorttitle = trim(explode(":",$title)[0]);
+          $paper_fulltitle = trim(user_prompt("Please enter the full title for the paper in folder '$d'","$title"));
+          $paper_shorttitle = trim(user_prompt("Please enter a short title for the paper in folder '$d'","$shorttitle"));
+        }
+        $header = "";
+        if ($paper_fulltitle != "" && $authors != "")
+        {
+          $header=<<<END
+\chapter[$paper_shorttitle]{$paper_fulltitle}\label{chapter:$d}
+\section*{Publication Data}
+\\fullcite{TODO}
+
+\section*{Contributions}
+TODO
+
+
+\\newpage
+\begin{center}
+{\Large \\textbf{
+$paper_fulltitle%
+}}\\\\\
+\\vspace{0.6cm}
+{\large $authors%
+} % TODO: check author list
+\\end{center}
+END;
+        }
+        $tex = preg_replace("/\\\documentclass.*\\n/i",'',$tex);
+        $tex = preg_replace("/\\\begin{document}/i",$header,$tex);
+        $tex = preg_replace("/\\\begin{abstract}/i",'\section*{Abstract}',$tex);
+        $tex = preg_replace("/\\\end{abstract}/i",'',$tex);
+        $tex = preg_replace("/\\\end{document}/i",'',$tex);
+        $tex = preg_replace("/\n\s*\\\\author\s*(?<R>{((?:[^{}]+|(?&R))*)})/i","",$tex);
+        $tex = preg_replace("/\n\s*\\\\title\s*(?<R>{((?:[^{}]+|(?&R))*)})/i","",$tex);
+        $tex = preg_replace("/\n\s*\\\\date\s*(?<R>{((?:[^{}]+|(?&R))*)})/i","",$tex);
+        $tex = preg_replace("/\n\s*\\\\maketitle/i","",$tex);
+        [$ups,$ps] = getusepackages($tex);
+        global $included_usepackages;
+        global $additional_usepackages;
+        foreach (array_diff($ps,$included_usepackages) as $k => $v) {
+          if ($v != "usenix,epsfig,endnotes" && $v != "cite" && !($v == 'floatrow' && in_array('float',$included_usepackages)))
+          {
+            $additional_usepackages[] = $ups[$k];
+            $included_usepackages[] = $v;
+          }
+        }
+        $additional_usepackages = array_unique($additional_usepackages);
+        sort($additional_usepackages);
+        $tex = preg_replace("/\n\s*\\\\usepackage([^{}]*)(?<R>{((?:[^{}]+|(?&R))*)})/i","",$tex);
+        
+        $newcommands = getnewcommands($tex);
+        global $existing_commands;
+        $rmcommands = array_intersect($newcommands,$existing_commands);
+        foreach ($rmcommands as $rmc)
+        {
+          $tex = preg_replace("/\n\s*\\\\newcommand([^{}]*)({\s*\\".$rmc."\s*}.*\n)/i","\n",$tex);
+        }
+
+        [$mathopsfull,$mathops] = getmathops($tex);
+        global $existing_mathops;
+        global $preamble;
+        $addmathops = array_diff($mathops,$existing_mathops);
+        foreach ($addmathops as $k => $v)
         {
-          $oldref = $ref;
-          $ref = preg_replace("/([{,])\s*((?!$d)[^,}]+[},])/i",'${1}'.$d.':${2}',$ref);
-          $ref = preg_replace("/([{,])\s*((?!$d)[^,}]+[},])/i",'${1}'.$d.':${2}',$ref);
-          $tex = str_replace($oldref,$ref,$tex);
+          $preamble .= $mathopsfull[$k] . "\n";
+          $existing_mathops[] = $v;
         }
-        // cite citeA citeauthor etc
-        $cites = [];
-        preg_match_all("/\\\([a-z]*cite[a-z]*)\s*\{\s*[^}]+\}/i",$tex,$cites);
-        foreach ($cites[0] as $cite)
+        $rmmathops = array_intersect($mathops,$existing_mathops);
+        foreach ($rmmathops as $rmo)
         {
-          $oldcite = $cite;
-          $cite = preg_replace("/([{,])\s*((?!$d)[^,}]+[},])/i",'${1}'.$d.':${2}',$cite);
-          $cite = preg_replace("/([{,])\s*((?!$d)[^,}]+[},])/i",'${1}'.$d.':${2}',$cite);
-          $tex = str_replace($oldcite,$cite,$tex);
+          $tex = preg_replace("/\n\s*\\\\DeclareMathOperator([^{}]*)({\s*\\".$rmo."\s*}.*\n)/i","\n",$tex);
         }
+
+        $matches = [];
+        $tex = preg_replace("/\n\s*\\\\pgfplotsset\s*(?<R>{((?:[^{}]+|(?&R))*)})/i","",$tex);
+        $tex = preg_replace("/\n\s*\\\\setlength{\s*\\\marginparwidth.*\n/i","\n",$tex);
+
         file_put_contents($file,$tex);
       }
     }
-    return $bibresources;
+    return;
   }
   function check_options($argv)
   {
@@ -368,6 +482,19 @@ END;
     echo "Skipping...\n";
   else
     compile_check($papers);
+  [$_t,$usepackages1] = getusepackages(file_get_contents("main.tex.php"));
+  [$_t,$usepackages2] = getusepackages(file_get_contents("tugraz_defaults.sty"));
+  global $included_usepackages;
+  $included_usepackages = array_unique(array_merge($usepackages1, $usepackages2));
+  sort($included_usepackages);
+  $cmds1 = getnewcommands(file_get_contents("main.tex.php"));
+  $cmds2 = getnewcommands(file_get_contents("tugraz_defaults.sty"));
+  global $existing_commands;
+  $existing_commands = array_unique(array_merge($cmds1, $cmds2));
+  [$del,$mathops1] = getmathops(file_get_contents("main.tex.php"));
+  [$del,$mathops2] = getmathops(file_get_contents("tugraz_defaults.sty"));
+  global $existing_mathops;
+  $existing_mathops = array_unique(array_merge($mathops1, $mathops2));
   echo "\n=== Step 5/$steps: Adjusting Papers (checking only *.tex files) ===\n\n";
   adjust_papers($papers,[".tex"]);
   echo "\n=== Step 6/$steps: Generate main.tex ===\n\n";
@@ -380,6 +507,15 @@ END;
   $thesis_assessors = user_prompt("Enter Names of your Assessors (comma-separated)","Severus Snape, Minerva McGonagall");
   $num_publications_in_thesis = count($papers);
   $num_publications = user_prompt("How many publications did you co-author during your PhD? (6 is the absolute minimum for a cumulative thesis)","6");
+
+  $sloppy_begin = "";
+  $sloppy_end = "";
+  if (!isset($options["--no-sloppy"]))
+  {
+    $sloppy_begin = '\begin{sloppypar}';
+    $sloppy_end = '\end{sloppypar}';
+  }
+
   ob_start();
   require "main.tex.php";
   $maintex_content = ob_get_contents();
@@ -387,6 +523,11 @@ END;
   //if (!file_exists("main.tex") || user_consent("main.tex already exists. overwrite?")) // TODO: enable this check
   file_put_contents("main.tex",$maintex_content);
 
+  if (!file_exists("mypreamble.sty"))
+    file_put_contents("mypreamble.sty","\ProvidesPackage{mypreamble}\n\n".implode("\n",$additional_usepackages)."\n\n".$preamble);
+
+  @mkdir("tikz");
+
   if (!file_exists("abstract.tex"))
     file_put_contents("abstract.tex","");
   if (!file_exists("intro.tex"))

+ 6 - 4
main.tex.php

@@ -98,6 +98,8 @@ if (isset($options["--cv"]))
 \usepackage{chngcntr}
 \addtokomafont{disposition}{\rmfamily}
 
+\usepackage{mypreamble}
+
 \begin{document}
 %\includepdf[pages=-]{front.pdf}
 \frontmatter
@@ -192,9 +194,9 @@ if (isset($options["--cv"]))
 
 \input{intro}
 
-\begin{sloppypar}
+<?php echo $sloppy_begin; ?>
 \printbibliography[title={References}, heading=bibintoc]
-\end{sloppypar}
+<?php echo $sloppy_end; ?>
 \end{refsection}
 \egroup
 
@@ -217,11 +219,11 @@ During my thesis, I contributed to <?php echo $num_publications ?> publications
 \newrefcontext[sorting=ydnt]
 \nocite{CV:*}
 
-\begin{sloppypar}
+<?php echo $sloppy_begin; ?>
 \printbibliography[title={Publications in this Thesis}, type=inproceedings, heading=subbibliography, keyword={mine}]
 
 \printbibliography[title={Other Contributions}, type=inproceedings, heading=subbibliography, keyword={mine_other}]
-\end{sloppypar}
+<?php echo $sloppy_end; ?>
 
 \end{refsection}
 \egroup