view paper/md2tex.pl @ 16:586757336bdd

...
author anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
date Sat, 02 May 2020 14:39:45 +0900
parents 875bf4bc5059
children 099e7864ee79
line wrap: on
line source

#!/usr/bin/env perl
use strict;
use warnings;
use utf8;

my $source_md = shift;
my $target_tex = shift;

open my $texFH, '>', $target_tex;

{
  open my $fh, '<', 'md2tex/first.tex';
  while (my $line = <$fh> ) {
    print $texFH $line;
  }
  close $fh;
}

open my $fh, '<', $source_md;

my $in_codeblock = 0;

while (my $line = <$fh>) {
  if ($line =~/^#/) {
    $line =~ s/# (.*)/\\section{$1}/;
  }

  if ($line =~ /`([\w_\-\\]+)`/) {
    my $codeBlock = $1;
    $codeBlock =~ s/_/\\_/g;
    $line =~ s/`([\w_\-\\]+)`/\\texttt{$codeBlock}/;
  }

  if ($line =~ /```/) {
    $in_codeblock = !$in_codeblock;
    if (!$in_codeblock) {
      $line = '\end{lstlisting}' ."\n";
    }
    if ($line =~ /``` (.*),\s*(.*)/) {
      $line = '\b'."egin{lstlisting}[frame=lrbt,label=$1,caption={$2}]\n";
    }
  }

  if ($line =~ /^\[(.*),\s*(.*)\]\((.*)\)/) {
      $line = '\l' ."stinputlisting[label=$1, caption={$2}]{$3}\n";
  }
  print $texFH $line;
}
close $fh;

print $texFH  <<'EOF';

\nocite{*}
\bibliographystyle{ipsjunsrt}
\bibliography{anatofuz-bib}


\end{document}
EOF


close $texFH;