mybin

my ~/bin
git clone https://a3nm.net/git/mybin/
Log | Files | Refs | README

shorthand.pl (5590B)


      1 #!/usr/bin/perl
      2 
      3 # Convert my shorthand to beamer
      4 
      5 my $escaping = 0;
      6 my $codeescape = 0;
      7 my $in_frame = 0;
      8 my @env = qw();
      9 my @envd = qw();
     10 my $subsecneeded = 0;
     11 my $header = <<END;
     12 \\documentclass{beamer}
     13 \\usepackage{fontspec}
     14 \\usepackage{graphicx, verbatim, array, xspace, booktabs, tikz, url, etoolbox,multirow,tabularx,minibox,tikz-qtree,minted}
     15 \\usetheme{Frankfurt}
     16 \\usecolortheme{beaver}
     17 \\definecolor{beamer\@blendedblue}{rgb}{1,0,0}
     18 \\colorlet{darkgreen}{green!50!black}
     19 \\newcommand{\\imgp}[2]{
     20   \\begin{figure}
     21   \\begin{center}
     22   \\includegraphics[scale=#2]{#1.pdf}
     23   \\end{center}
     24   \\end{figure}
     25 }
     26 \\setbeamertemplate{navigation symbols}{}
     27 \\makeatletter
     28 \\newcommand{\\myitemarrow}{\$\\rightarrow\$}
     29 \\setbeamertemplate{footline}
     30 {%
     31   \\hfill \\insertframenumber/\\inserttotalframenumber \\hspace{0.2cm}
     32   \\vspace{0.2cm}
     33 }%
     34 \\makeatother
     35 \\newenvironment{changemargin}[2]{%
     36   \\begin{list}{}{%
     37     \\setlength{\\topsep}{0pt}%
     38     \\setlength{\\leftmargin}{#1}%
     39     \\setlength{\\rightmargin}{#2}%
     40     \\setlength{\\listparindent}{\\parindent}%
     41     \\setlength{\\itemindent}{\\parindent}%
     42     \\setlength{\\parsep}{\\parskip}%
     43   }%
     44   \\item[]}{\\end{list}} 
     45 END
     46 
     47 sub reformat()
     48 {
     49   my $ll = $_[0];
     50   $ll =~ s/\*([^*]+)\*/\\textcolor{red}{$1}/g;
     51   $ll =~ s/``/UNIQUEBLAH/g;
     52   $ll =~ s/\`([^`]+)\`/\\texttt{$1}/g;
     53   $ll =~ s/UNIQUEBLAH/``/g;
     54   $ll;
     55 }
     56 
     57 my $header2a = <<END;
     58 \\AtBeginSection[]
     59 {
     60 END
     61 my $header2b = <<END;
     62   \\ifnumcomp{\\value{section}}{=}{1}{}
     63     {
     64 END
     65 my $header2c = <<END;
     66   \\begin{frame}<beamer>
     67     \\frametitle{\\mytocname}
     68     \\tableofcontents[currentsection]
     69   \\end{frame}
     70 END
     71 my $header2d = <<END;
     72 }
     73 END
     74 my $header2e = <<END;
     75 }
     76 END
     77 
     78 my $header3a = <<END;
     79 \\begin{document}
     80 END
     81 
     82 my $header3b1 = <<END;
     83 \\begin{frame}
     84 END
     85 
     86 my $header3b2 = <<END;
     87 \\begin{frame}[plain,noframenumbering]
     88 END
     89 
     90 my $header3c = <<END;
     91 \\titlepage
     92 END
     93 
     94 my $header3d = <<END;
     95 \\end{frame}
     96 END
     97 
     98 print $header;
     99 
    100 my $defaultlang = "";
    101 my $toc = 1;
    102 my $nofirsttoc = 1;
    103 my $titlenum = 1;
    104 my $ontitle = "";
    105 
    106 while (<>) {
    107   last if /^$/;
    108   if (/^\\usepackage\[[^]]*,([^\],]*)\]\{babel\}/) {
    109     $defaultlang = $1;
    110   }
    111   if (/NOTOC/) {
    112     $toc = 0;
    113     next;
    114   }
    115   if (/NOTITLENUM/) {
    116     $titlenum = 0;
    117     next;
    118   }
    119   if (/ALLTOC/) {
    120     $nofirsttoc = 0;
    121     next;
    122   }
    123   if (/^ONTITLE (.*)/) {
    124     $ontitle = $1;
    125     next;
    126   }
    127   print;
    128 }
    129 
    130 if ($toc) {
    131   print $header2a;
    132   print $header2b if $nofirsttoc;
    133   print $header2c;
    134   print $header2d if $nofirsttoc;
    135   print $header2e;
    136 }
    137 print $header3a;
    138 if ($titlenum) {
    139   print $header3b1;
    140 } else {
    141   print $header3b2;
    142 }
    143 print $header3c;
    144 if (length($ontitle) > 0) {
    145   print "\\vfill ";
    146   print $ontitle;
    147 }
    148 print $header3d;
    149 
    150 while (<>) {
    151   my $l = $_;
    152   if ($escaping) {
    153     if ($l =~ /^\?>$/) {
    154       $escaping = 0;
    155     } else {
    156       print $l;
    157     }
    158     next;
    159   }
    160   if ($codeescape) {
    161     if ($l =~ /^```$/) {
    162       $codeescape = 0;
    163       print "\\end{minted}\n";
    164       print "\\end{otherlanguage}\n" if length($defaultlang) > 0;
    165     } else {
    166       print $l;
    167     }
    168     next;
    169   }
    170   if ($l =~ /^<\?$/) {
    171     $escaping = 1;
    172     next;
    173   }
    174   if ($l =~ /^FORCEEND/) {
    175     print "\\end{frame}\n\n" if ($in_frame == 1);
    176     $in_frame = 0;
    177     next;
    178   }
    179   if ($l =~ /^(!f?) (.*)/) {
    180     print "\\end{frame}\n\n" if ($in_frame == 1);
    181     print "\\subsection\{\}\n" if ($subsecneeded == 1);
    182     $subsecneeded = 0;
    183     $tit = &reformat($2);
    184     if ($1 eq "!f") {
    185       # TODO: do this automatically
    186       print "\\begin{frame}[fragile]\{$tit\}\n";
    187     } else {
    188       print "\\begin{frame}\{$tit\}\n";
    189     }
    190     $in_frame = 1;
    191     next;
    192   }
    193   if ($l =~ /^(=+) (.*) (=+)(!?)(\*?)$/) {
    194     print "\\end{frame}\n\n" if ($in_frame == 1);
    195     $in_frame = 0;
    196     my $nest = length($1);
    197     my $level;
    198     if ($nest == 2) {
    199       $level = "section";
    200       $subsecneeded = 1 if ($4 eq "");
    201     }
    202     if ($nest == 3) {
    203       $level = "subsection";
    204       $subsecneeded = 0;
    205     }
    206     $level = "$level*" if $5 eq "*";
    207     if ($2 =~ /^ *\[([^]]*)\] (.*)/) {
    208       print "\\$level\[$1\]\{$2\}\n";
    209     } else {
    210       print "\\$level\{$2\}\n";
    211     }
    212     next;
    213   }
    214   if ($l =~ /^\.\.\.$/) {
    215     print "\\pause\n";
    216     next;
    217   }
    218   if ($l =~ /^```(.*)$/) {
    219     print "\\begin{otherlanguage}{english}\n" if length($defaultlang) > 0;
    220     print "\\begin{minted}{$1}\n";
    221     $codeescape = 1;
    222     next;
    223   }
    224   if ($l =~ /^$/) {
    225     while (@env > 0) {
    226       my $e = pop @env;
    227       my $ed = pop @envd;
    228       print "\\end{$e}\n\n";
    229     }
    230     print "\n";
    231     next;
    232   }
    233   if (/^( *)(-|\[|=>)(.*)/) {
    234     my $indent = length($1);
    235     while (@env > 0 and $envd[-1] > $indent) {
    236       my $e = pop @env;
    237       my $ed = pop @envd;
    238       print "\\end{$e}\n\n";
    239     }
    240     my $key = "";
    241     if ($2 eq "[") {
    242       if ($3 =~ /([^]]*)\](.*)/) {
    243         $l = "$2";
    244         $key = $1;
    245       }
    246     } else {
    247       if ($2 eq "=>") {
    248         $key = "\\textcolor{red}{\\myitemarrow}";
    249       }
    250       $l = "$3";
    251     }
    252     if (@envd == 0 or $indent > $envd[-1]) {
    253       my $symb = $2;
    254       if ($symb eq "[") {
    255         $e = "description";
    256       } else {
    257         $e = "itemize";
    258       }
    259       push (@env, $e);
    260       push (@envd, $indent);
    261       if ($l =~ /^ *$/ and $symb eq "[") {
    262         # TODO: do this automatically
    263         print "\\begin\{$e\}\[$key\]\n";
    264         next;
    265       } else {
    266         print "\\begin\{$e\}\n";
    267       }
    268     }
    269     $key = &reformat($key);
    270     if (length($key) > 0) {
    271       print "\\item[$key] ";
    272     } else {
    273       print "\\item ";
    274     }
    275     $l = "$l\n";
    276   }
    277   $l = &reformat($l);
    278   print $l;
    279 }
    280 
    281 print "\\end{frame}\n\n" if ($in_frame == 1);
    282 print "\\end{document}\n";