From 2b58efe8f69b74c1c705404d57de1e83e80f4125 Mon Sep 17 00:00:00 2001 From: David Runge Date: Thu, 8 Jun 2017 19:43:13 +0200 Subject: bin/tex2pdf: Rewrite, using safe mode and functions. Redirecting output to standard out instead of log file. --- bin/tex2pdf | 137 +++++++++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 104 insertions(+), 33 deletions(-) (limited to 'bin/tex2pdf') diff --git a/bin/tex2pdf b/bin/tex2pdf index a7c367e..021682b 100755 --- a/bin/tex2pdf +++ b/bin/tex2pdf @@ -1,33 +1,104 @@ -#!/bin/bash -logdestination="$HOME/.log/tex2pdf.log" -already_open=$(ps aux | grep "$1.pdf" | grep -v "grep") -rm -rf $logdestination -touch $logdestination -echo "tex2pdf called with $1.tex">>$logdestination -echo "#####################################################">>$logdestination -echo "################## pdflatex, run #1 #################">>$logdestination -echo "#####################################################">>$logdestination -pdflatex -interaction=nonstopmode $1.tex>>$logdestination -echo "bibtex:">>$logdestination -bibtex $1.aux>>$logdestination -echo "#####################################################">>$logdestination -echo "################## pdflatex, run #2 #################">>$logdestination -echo "#####################################################">>$logdestination -pdflatex -interaction=nonstopmode $1.tex>>$logdestination -echo "####### ##############################################">>$logdestination -echo "################## pdflatex, run #3 #################">>$logdestination -echo "#####################################################">>$logdestination -pdflatex -interaction=nonstopmode $1.tex>>$logdestination -echo "#####################################################">>$logdestination -echo "Launching mupdf with $1.pdf.">>$logdestination - -if [ -n "$already_open" ]; then - kill "$(echo $already_open | awk '{print $2}')" - echo "Killing pid $(echo $already_open | awk '{print $2}'), which has $1.pdf open already." >>$logdestination -fi -mupdf $1.pdf & - -echo "#####################################################">>$logdestination -echo "Removing aux,bbl,blg,dvi,lof,log,lot,nav,out,snm,toc.">>$logdestination -echo "Current directory: $(pwd) and children of depth 1.">>$logdestination -rm -f *.{aux,bbl,bcf,blg,dvi,lof,log,lot,nav,out,run.xml,snm,toc,vrb} */*.{aux,bbl,bcf,blg,dvi,lof,log,lot,nav,out,run.xml,snm,toc,vrb} +#!/usr/bin/env bash +set -euo pipefail +IFS=$'\n\t' + +initial_pwd=$(pwd) +filename="" +dirname="" +mupdf_pid="0" +input_suffix=".tex" +probe_suffix="tex" +output_suffix=".pdf" + +function get_filename() +{ + local base=$(basename ${1}) + filename="${base%.*}" +} + +function get_dirname() +{ + dirname="$(dirname ${1})" +} + +function probe_extension() +{ + local extension="${1##*.}" + if [ ${extension} != ${probe_suffix} ]; then + echo "File extension (${extension}) is not ${probe_suffix}. Exiting..." + exit 1 + fi + return 0 +} + +function probe_tools() +{ + if [ ! -x /usr/bin/pdflatex ]; then + echo "/usr/bin/pdflatex not available. Exiting..." + exit 1 + fi + if [ ! -x /usr/bin/bibtex ]; then + echo "/usr/bin/bibtex not available. Exiting..." + exit 1 + fi + if [ ! -x /usr/bin/mupdf ]; then + echo "/usr/bin/mupdf not available. Exiting..." + exit 1 + fi + return 0 +} + +function create_pdf() +{ + cd ${dirname} + set +e + pdflatex \ + -interaction=nonstopmode \ + ${filename}${input_suffix} > /dev/null + bibtex ${filename}.aux + pdflatex \ + -interaction=nonstopmode \ + ${filename}${input_suffix} > /dev/null + pdflatex \ + -interaction=nonstopmode \ + ${filename}${input_suffix} + set -e + cd ${initial_pwd} + return 0 +} + +function check_mupdf_pid() +{ + set +e + mupdf_pid=$(ps aux | grep "mupdf ${dirname}/${filename}${output_suffix}" | grep -v "grep" | awk '{print $2}') + set -e +} + +function show_pdf() +{ + check_mupdf_pid + echo "mupdf: ${dirname}/${filename}${output_suffix}." + if [ ! -z $mupdf_pid ]; then + kill ${mupdf_pid} + fi + mupdf ${dirname}/${filename}${output_suffix} & +} + +function cleanup() +{ + rm -f ${dirname}/*.{aux,bbl,bcf,blg,dvi,idx,lof,log,lot,nav,out,run.xml,snm,toc,vrb} \ + ${dirname}/*/*.{aux,bbl,bcf,blg,dvi,idx,lof,log,lot,nav,out,run.xml,snm,toc,vrb} \ + *.{aux,bbl,bcf,blg,dvi,idx,lof,log,lot,nav,out,run.xml,snm,toc,vrb} \ + */*.{aux,bbl,bcf,blg,dvi,idx,lof,log,lot,nav,out,run.xml,snm,toc,vrb} +} + +probe_extension $1 +get_filename $1 +get_dirname $1 +echo "tex2pdf: Converting $filename$input_suffix to $filename$output_suffix." +probe_tools +create_pdf +cleanup +show_pdf + +exit 0 -- cgit v1.2.3-70-g09d2