Daniel Gruss 2 жил өмнө
parent
commit
ea8a60dcea
2 өөрчлөгдсөн 126 нэмэгдсэн , 38 устгасан
  1. 106 35
      generator.php
  2. 20 3
      main.tex.php

+ 106 - 35
generator.php

@@ -2,6 +2,8 @@
   global $options;
   global $preamble;
   $preamble = "";
+  global $kpsefiles;
+  $kpsefiles = [];
   global $existing_commands;
   global $included_usepackages;
   $included_usepackages = [];
@@ -253,6 +255,7 @@ END;
           $bib = file_get_and_backup($file);
           $bib = preg_replace("/(^\s*@\s*[a-z]+\s*\{\s*)((?!$d)\S)/i",'${1}'.$d.':${2}',$bib);
           $bib = preg_replace("/(\n\s*@\s*[a-z]+\s*\{\s*)((?!$d)\S)/i",'${1}'.$d.':${2}',$bib);
+          $bib = preg_replace("/([\{\"].*)[^\\\\]#(.*)/i",'${1}\#${2}',$bib);
           file_put_contents($file,$bib);
           $bibresources[$d] = $file;
         }
@@ -289,6 +292,25 @@ END;
     }
     return $bibresources;
   }
+  function handle_appendix($tex)
+  {
+    $appendix = false;
+    $lines = explode("\n",$tex);
+    for ($i = 0; $i < count($lines); $i++)
+    {
+      if ($appendix)
+      {
+        $lines[$i] = str_replace(['\subsubsection','\subsection','\section'],['\paragraph','\subsubsection','\subsection'],$lines[$i]);
+      }
+      if (str_starts_with($lines[$i],'\appendices') || str_starts_with($lines[$i],'\appendix'))
+      {
+        $lines[$i] = '\section*{Appendix}';
+        $appendix = true;
+      }
+    }
+    $tex = implode("\n",$lines);
+    return $tex;
+  }
   function stripformatting($tex)
   {
     return str_ireplace(['\Large ','\bf ','\rm ','\\\\'],[' ',' ',' ',' '],$tex);
@@ -306,9 +328,9 @@ END;
   function getmathops($tex)
   {
     $mathops = [];
-    preg_match_all("/\n\s*(\\\\DeclareMathOperator([^{}]*)(?<R>{((?:[^{}]+|(?&R))*)})([^{}]*)(?<P>{((?:[^{}]+|(?&P))*)}))/i",$tex,$mathops);
+    preg_match_all("/\n\s*(\\\\DeclareMath(Operator|Alphabet)\\*?((?<R>{((?:[^{}]+|(?&R))*)})([^{}]*)|\\\\[^\{]*)(?<P>{((?:[^{}]+|(?&P))*)})(?<Q>{((?:[^{}]+|(?&Q))*)})?(?<S>{((?:[^{}]+|(?&S))*)})?(?<T>{((?:[^{}]+|(?&T))*)})?)/i",$tex,$mathops);
     if (isset($mathops[4]))
-      return [$mathops[1],$mathops[4]];
+      return [$mathops[1],$mathops[3]];
     return [];
   }
   function getnewcommands($tex)
@@ -321,12 +343,12 @@ END;
   }
   function adjust_papers($papers,$extensions = [])
   {
+    $tex = "";
+    $paper_fulltitle = "";
+    $authors = "";
+    $institute = "";
     foreach ($papers as $d => $f)
     {
-      $tex = "";
-      $paper_fulltitle = "";
-      $authors = "";
-      $institute = "";
       if (str_ends_with($f,".pdf"))
       {
         $paper_fulltitle = trim(user_prompt("Please enter the full title for the paper in folder '$d'","TODO"));
@@ -345,28 +367,30 @@ END;
 file_put_contents("$d/main.tex",$header);
       }
       $files = rscandir($d,$extensions);
+      $authors = "";
+      $institute = "";
+      $title = "";
+      $paper_fulltitle = "";
+      $paper_shorttitle = "";
       foreach ($files as $file)
       {
-        if (!str_ends_with($file,"main.tex"))
+        if (!str_ends_with($file,".tex") && !str_ends_with($file,".tikz") && !str_ends_with($file,".sty"))
           continue;
         $tex = file_get_and_backup($file,".interm_bak");
         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 ($institute == "" && preg_match("/\n\s*\\\\institute/i",$tex) == 1)
         {
-          $institute = [];
           preg_match_all("/\n\s*\\\\institute\s*(?<R>{((?:[^{}]+|(?&R))*)})/i",$tex,$institute);
           if (isset($institute[2]) && isset($institute[2][0]))
             $institute = trim(stripformatting($institute[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]));
@@ -404,12 +428,27 @@ $paper_fulltitle%
 \\end{center}
 END;
         }
+
+        [$mathopsfull,$mathops] = getmathops($tex);
+        global $existing_mathops;
+        global $preamble;
+        $addmathops = array_diff($mathops,$existing_mathops);
+        foreach ($addmathops as $k => $v)
+        {
+          $preamble .= $mathopsfull[$k] . "\n";
+          $existing_mathops[] = $v;
+        }
+
         $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("/\\\\newcommand/i",'\providecommand',$tex);
+        $tex = preg_replace("/\\\\renewcommand/i",'\providecommand',$tex);
         $tex = preg_replace("/\\\\end{abstract}/i",'',$tex);
+        $tex = preg_replace("/\\\\DeclareMathOperator/i",'%$0',$tex);
+        $tex = preg_replace("/\\\\DeclareMathAlphabet/i",'%$0',$tex);
+        $tex = preg_replace("/\\\\usepackage[cache=false]{minted}/i",'\usepackage{minted}',$tex);
         $tex = preg_replace("/\\\\thispagestyle{empty}/i",'',$tex);
         $tex = preg_replace("/\\\\PassOptionsToPackage\s*(?<R>{((?:[^{}]+|(?&R))*)})(?<Q>{((?:[^{}]+|(?&Q))*)})/i",'',$tex);
         $tex = preg_replace("/(\\\\csvautobooktabular\s*([^{}]*)\s*)(?<R>{((?:[^{}]+|(?&R))*)})/i","\\1{{$d}/\\4}",$tex);
@@ -419,27 +458,35 @@ END;
         $tex = preg_replace("/\\\\captionsetup\s*(?<R>\[((?:[^\[\]]+|(?&R))*)\])?(?<Q>{((?:[^{}]+|(?&Q))*)})/i","",$tex);
         $tex = preg_replace("/\\\\tikzexternalize\s*(?<R>\[((?:[^\[\]]+|(?&R))*)\])/i","",$tex);
         $tex = preg_replace("/\\\\floatsetup\s*(?<R>\[((?:[^\[\]]+|(?&R))*)\])(?<Q>{((?:[^{}]+|(?&Q))*)})/i","",$tex);
-        $tex = preg_replace("/\n\s*\\\\bibliography\s*(?<R>{((?:[^{}]+|(?&R))*)})/i","",$tex);
+        $tex = preg_replace("/\s*\\\\bibliography\s*(?<R>{((?:[^{}]+|(?&R))*)})/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*\\\\authorrunning\s*(?<R>{((?:[^{}]+|(?&R))*)})/i","",$tex);
         $tex = preg_replace("/\n\s*\\\\institute\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("/\\\\includegraphics\s*(?<R>\[((?:[^\[\]]+|(?&R))*)\])(?<Q>{((?:[^{}]+|(?&Q))*)})/i",'\includegraphics${1}{'.$d.'/${4}}',$tex);
         $tex = preg_replace("/\n\s*\\\\maketitle/i","",$tex);
+        $tex = preg_replace("/\\\\IEEEoverridecommandlockouts/i","",$tex);
         $tex = preg_replace("/\n\s*\\\\tikzexternalize/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)))
+          if (in_array($v,['minted']))
+          {
+            $additional_usepackages[] = "\usepackage{minted}";
+            $included_usepackages[] = $v;
+          }
+          else if (!in_array($v,['usenix','usenix,epsfig,endnotes','usenixbadges','cite','csvenhanced']) && !($v == 'floatrow' && in_array('float',$included_usepackages)))
           {
             $additional_usepackages[] = $ups[$k];
             $included_usepackages[] = $v;
           }
         }
-        $additional_usepackages = array_unique($additional_usepackages);
-        sort($additional_usepackages);
+        array_unique($additional_usepackages);
         $tex = preg_replace("/\n\s*\\\\usepackage([^{}]*)(?<R>{((?:[^{}]+|(?&R))*)})/i","",$tex);
+        $tex = preg_replace("/^\s*\\\\usepackage([^{}]*)(?<R>{((?:[^{}]+|(?&R))*)})/i","",$tex);
         
         $newcommands = getnewcommands($tex);
         global $existing_commands;
@@ -449,25 +496,12 @@ END;
           $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)
-        {
-          $preamble .= $mathopsfull[$k] . "\n";
-          $existing_mathops[] = $v;
-        }
-        $rmmathops = array_intersect($mathops,$existing_mathops);
-        foreach ($rmmathops as $rmo)
-        {
-          $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);
 
+        if (stripos($tex,"\appendices") !== false || stripos($tex,"\appendix") !== false)
+          $tex = handle_appendix($tex);
         file_put_contents($file,$tex);
       }
     }
@@ -487,7 +521,7 @@ END;
   }
   function compile_check($papers,$precopy = false)
   {
-    array_shift($papers);
+    //array_shift($papers); weird...
     if ($precopy)
       $papers = array_combine($papers,$papers);
     foreach ($papers as $p => $f)
@@ -529,6 +563,28 @@ END;
       echo "======= Paper $p -> $f  END  =======\n";
     }
   }
+  function init_kpse()
+  {
+    global $kpsefiles;
+    $kpsepath = shell_exec("kpsepath tex");
+    $kpsepath = explode(":",$kpsepath);
+    foreach ($kpsepath as $k)
+    {
+      $k = str_replace(["!!","\n","///","//"],["","","/","/"],$k);
+      if (str_starts_with($k,"."))
+          continue;
+      $found = null;
+      if (is_dir($k))
+        $found = rscandir($k,[".sty"]);
+      if ($found != null)
+      {
+        for ($i = 0; $i < count($found); $i++)
+          $found[$i] = basename($found[$i]);
+        $kpsefiles = array_unique(array_merge($kpsefiles,$found));
+      }
+    }
+    sort($kpsefiles);
+  }
   check_software();
   $options = check_options($argv);
   if (isset($options["--help"]))
@@ -566,12 +622,26 @@ END;
   [$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 5/$steps: Adjusting Papers (checking *.tex and *.tikz files) ===\n\n";
+  adjust_papers($papers,[".tex",".tikz",".sty"]);
+  init_kpse();
   foreach ($papers as $k => $v)
   {
     if (str_ends_with($papers[$k],".pdf"))
       $papers[$k] = 'main.tex';
+    else
+    {
+      $stys = rscandir($k,[".sty"]);
+      foreach ($stys as $styf)
+      {
+        $styfb = basename($styf);
+        if (in_array($styfb,$kpsefiles) === false && !file_exists("./$styfb"))
+        {
+          echo "Copying $styf to ./$styfb...\n";
+          copy($styf,"./$styfb");
+        }
+      }
+    }
   }
   echo "\n=== Step 6/$steps: Generate main.tex ===\n\n";
   $thesis_type = user_prompt("Enter Thesis Type ","PhD Thesis");
@@ -596,10 +666,10 @@ END;
   require "main.tex.php";
   $maintex_content = ob_get_contents();
   ob_end_clean();
-  //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("main.tex") || user_consent("main.tex already exists. overwrite?"))
+    file_put_contents("main.tex",$maintex_content);
 
-  if (!file_exists("mypreamble.sty"))
+  if (!file_exists("mypreamble.sty") || user_consent("mypreamble.sty already exists... overwrite?","--force-overwrite","--no-overwrite"))
     file_put_contents("mypreamble.sty","\ProvidesPackage{mypreamble}\n\n".implode("\n",$additional_usepackages)."\n\n".$preamble);
 
   @mkdir("tikz");
@@ -618,5 +688,6 @@ END;
     if (!file_exists("cv.tex"))
       file_put_contents("cv.tex","This is my CV! Thanks for checking it out!");
   }
+
   echo "=== done ===\nNext step, run this command:\nlatexmk -latexoption=\"-shell-escape\" -g -pdf main.tex\n";
 ?>

+ 20 - 3
main.tex.php

@@ -44,6 +44,17 @@ if (isset($options["--cv"]))
 
 \renewcommand{\bf}{\bfseries}
 \renewcommand{\and}{,\xspace}
+\newcommand{\CopyrightYear}{}
+\newcommand{\setcopyright}{}
+\newcommand{\conferenceinfo}{}
+\newcommand{\isbn}{}
+\newcommand{\doi}{}
+\newcommand{\printccsdesc}{}
+\newcommand{\numberofauthors}{}
+\newcommand{\alignauthor}{}
+\newcommand{\affaddr}{}
+\newcommand{\ccsdesc}[1]{}
+\newcommand{\acmPrice}{}
 \newcommand{\keywords}[1]{Keywords:\ #1.}
 
 \usepackage{graphicx}
@@ -51,7 +62,6 @@ if (isset($options["--cv"]))
 \preto\fullcite{\AtNextCite{\defcounter{maxnames}{99}}}
 
 
-\usepackage{scrhack}
 \usepackage{quotchap} %
 
 \DefineBibliographyStrings{english}{%
@@ -63,6 +73,8 @@ if (isset($options["--cv"]))
 \renewcommand\cftchapaftersnum{.}
 \setlength{\cftchapnumwidth}{2em}
 
+\usepackage{verbatim}
+
 \makeatletter
 \frenchspacing
 \widowpenalty=10000
@@ -73,6 +85,9 @@ if (isset($options["--cv"]))
                 \setlength\belowcaptionskip{10\p@}%
                 \@float{table}}
                {\end@float}
+\newenvironment{CCSXML}
+               {\comment}
+               {\endcomment}
 \makeatother
 
 \makeatletter
@@ -103,6 +118,8 @@ if (isset($options["--cv"]))
 \addtokomafont{disposition}{\rmfamily}
 
 \usepackage{mypreamble}
+\usepackage{scrhack}
+
 
 \begin{document}
 %\includepdf[pages=-]{front.pdf}
@@ -243,8 +260,8 @@ echo '
     table/search path={'.$p.'/},
   }
   \newcommand{\thesisGenIncludePath}{'.$p.'}
-  \subimport{'.$p.'/}{'.$f.'}
-  
+  \subimport*{'.$p.'/}{'.$f.'}
+  \graphicspath{}
   \end{refsection}
   \egroup