My Scripts.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
bin/pdf

273 lines
5.6 KiB

#!/bin/bash
# Author: Skiqqy
# Basically solves any problem that has to do with pdfs
# Usage: pdf -h
args=("$@")
SCRIPT_PATH=$(dirname "$0")
com="mupdf -C FFDED7 -I"
# shellcheck source=./import/alert.sh
. "$SCRIPT_PATH/import/alert.sh" > /dev/null 2>&1
. "$SCRIPT_PATH/import/stdlib.sh" > /dev/null 2>&1
[ ! "$(command -v error)" ] && echo "[WARNING] Missing 'error.sh' import."
[ ! "$(command -v mupdf)" ] && warning "Missing 'mupdf' dependency." 1
# Keep track of what needs to be done.
TODO ()
{
cat << EOF
pdf ~ TODO Tracker
------------------
TODO: Fix argument passing to only set vars, not run functions, as this is bad style.
- Reason: Program can 'break' if arguments are passed in a wierd order.
EOF
}
usage ()
{
cat << EOF
pdf ~ An easy way to deal with pdfs
-----------------------------------
Usage: fpdf [options] file
Options
-------
-h Shows this message.
-o Specify output file when compiling a pdf.
-p Use dmenu to enter a password for the pdf.
-v Specify which pdf viewer you want to use.
-e Use gimp to edit a pdf.
-d Use mutool (from mupdf) to decompile a pdf.
-c Compile a pdf and then open it, Supported file type(s) include:
- .md
- .tex
- .1 (man pages)
-t Generate a .tex skeleton document.
--TODO Lists current TODO's.
--output=<FILENAME> Specify outputfile when compiling a pdf.
Example(s)
----------
\$ pdf https://skiqqy.xyz/assets/CurriculumVitae.pdf # Download and open pdf
\$ pdf some.pdf # open pdf
\$ pdf document.tex # Compile a tex file to 'document.pdf'
\$ pdf document.md # Compile a md file to 'document.pdf'
\$ pdf -c document.md # Compile a md file to 'document.pdf' and then open it
Author: Skiqqy
EOF
}
fetch_pdf ()
{
index=$((${#args[@]} - 1))
url="${args[$index]}"
# Check to see if we need to download the pdf
if [[ "$url" =~ ^http:// ]] || [[ "$url" =~ ^https:// ]]
then
file=$(basename "$url")
else
file="$url"
url=
fi
extension=$(echo "$file" | cut -d "." -f 2)
if [ "$extension" == "pdf" ]
then
if [ -z $url ]
then
$com "$file"
else
curl -s "$url" > /tmp/"$file"
$com /tmp/"$file"
fi
else
error "Invalid file type."
fi
}
# Usage: build_tex "file_name"
# Note: will, if we call with "file_name", then it will also use a "file_name.bib" when building, if it exists
build_tex ()
{
[ ! "$(command -v pdflatex)" ] && error "Missing 'pdflatex', cannot build from .tex"
pdflatex "$1".tex && biber "$1"; pdflatex "$1".tex && pdflatex "$1".tex
[ ! -f "$1.bib" ] && warning "\'$1.bib\' not found, skipped."
rm -f "$1."{'aux','bbl','bcf','blg','log','xml','nav','out','snm','toc'} && success "Cleaned build files."
if [ -n "$out" ]
then
[ "$(echo "$out" | cut -d\. -f 2)" = pdf ] && out="$(echo "$out" | cut -d\. -f 1)"
mv "$1.pdf" "$out.pdf"
fi
}
# Usage: build_md "file_name"
# Compiles file_name.md to file_name.pdf
build_md ()
{
[ ! "$(command -v pandoc)" ] && error "Missing 'pandoc', cannot build from .md"
if [ -z "$out" ]
then
out="$1"
else
[ "$(echo "$out" | cut -d\. -f 2)" = pdf ] && out="$(echo "$out" | cut -d\. -f 1)"
fi
# Compile
pandoc "$1.md" -o "$out.pdf" && success "Compiled \'$1.md\'."
}
# Usage: build_man_page "file_name"
# Compiles file_name.1 to file_name.pdf
build_man_page ()
{
[ ! "$(command -v ps2pdf)" ] && error "Missing 'ps2pdf', cannot build from .1"
if [ -z "$out" ]
then
out="$1"
else
[ "$(echo "$out" | cut -d\. -f 2)" = pdf ] && out="$(echo "$out" | cut -d\. -f 1)"
fi
# Compile
man -t <(cat "$1.1") | ps2pdf - "$out.pdf"
}
parse_build ()
{
[ -z "$file" ] && [ -z "$ftype" ] && warning "Missing file." && usage && exit 1
[ ! -f "$file.$ftype" ] && error "\'$file.$ftype\' not found."
case $ftype in
tex)
build_tex "$file"
;;
md)
build_md "$file"
;;
1)
build_man_page "$file"
;;
*)
error "Unknown file extension."
;;
esac
}
gen_tex_skelly ()
{
cat << EOF
\documentclass{article}
\\title{TITLE}
\\author{$USER}
\date{\\today}
\\begin{document}
\maketitle
\\tableofcontents
\\section{Introduction}
\\end{document}
EOF
}
main ()
{
if [ "${#args[@]}" -lt 1 ]
then
warning "Missing file."
usage
exit 1
fi
index=$((${#args[@]} - 1))
file="${args[$index]}"
base=$(basename "$file" 2>/dev/null)
ftype=$(echo "$base" | cut -d\. -f 2)
file=$(echo "$base" | cut -d\. -f 1)
out=
while getopts "hpcv:o:edt-:" opt
do
case "$opt" in
-)
case "$OPTARG" in
TODO)
TODO
return 0
;;
output=*)
out=${OPTARG#*=}
;;
*)
error "Invalid arg, use -h for help." 1
;;
esac
;;
h)
usage
exit 0
;;
p)
[ ! "$(command -v dmenu)" ] && error "Missing 'dmenu' dependency." 1
pass=$(prompt "PDF Password:")
com="$com -p $pass"
;;
c)
parse_build
index=$((${#args[@]} - 1))
url="${args[$index]}"
if [ -z "$out" ]
then
args["$index"]="$(echo "$url" | cut -d\. -f 1).pdf"
else
[ "$(echo "$out" | cut -d\. -f 2)" = pdf ] && out="$(echo "$out" | cut -d\. -f 1)"
args["$index"]="$out.pdf"
fi
fetch_pdf
return 0
;;
v)
com="$OPTARG"
;;
e)
[ ! "$ftype" = "pdf" ] && error "Not a pdf!"
gimp "${args[$index]}"
return 0
;;
d)
[ ! "$ftype" = "pdf" ] && error "Not a pdf!"
file="${args[$index]}"
mutool clean -d "$file" "decompiled-$file"
return 0
;;
t)
gen_tex_skelly
return 0
;;
o)
out="$OPTARG"
;;
*)
error "Invalid arg, use -h for help." 1
;;
esac
done
# Detect what we must do, based on file extension.
case $ftype in
pdf)
fetch_pdf # We open if the extension is .pdf
;;
*)
parse_build # If the extionsion is not .pdf, we attempt to build it
;;
esac
}
main "$@"