aboutsummaryrefslogtreecommitdiff
path: root/scripts/.local/bin/personal/compile
blob: 944891be04722b13b137eb29f6f4eb54069b1498 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#!/bin/bash

# A script to compile any (here defined) type of text file to a target format
# Depends on: bash, basename, pdflatex, pandoc, asciidoctor, make
# By David Penkowoj, 2021/08/02
#
# Usage: compile {file} [--no-make]

# Function to compile .md to .pdf files
ADOC_TO_HTML() {
  asciidoctor "$1" &&
  printf "Successfully compiled %s to %s\n" "$1" "$2.pdf" || 
  printf "[ Error ] Couldn't compile %s\n" "$1"
}

# Function to compile .tex to .pdf files without making a mess
TEX_TO_PDF() {
  printf "If the script seems to hang, try pressing <Enter>\n"

  pdflatex -shell-escape "$1" > /dev/null &&  
  pdflatex -shell-escape "$1" > /dev/null && # This is to ensure certain tex elements being drawn
  rm -rf "$2.toc" "$2.log" "$2.aux" &&
  printf "Successfully compiled %s to %s\n" "$2.tex" "$2.pdf" || 
  printf "[ Error ] Couldn't compile %s\n" "$2.tex"
}

# Function to compile .lp to .pdf files
LP_TO_PDF() {
  lilypond "$1" --pdf -s -o "$2.pdf" &&
  printf "Successfully compiled %s to %s\n" "$1" "$2.pdf" || 
  printf "[ Error ] Couldn't compile %s\n" "$1"
}

# Function to compile .md to .pdf files
MD_TO_PDF() {
  TMP="/tmp/compilemd"
  TEMPLATES="$HOME/documents/templates"

  sed '/^!.*/d' "$1" > "$TMP" && # This enables comments with "!"
  sed 's/^\\!/!/g' "$TMP" "$TMP" && # This enables escaping with "\!"

  if [[ "${1#*"hackpapier"}" != "$1" ]]; then
    pandoc "$TMP" --toc --template "$TEMPLATES/hackpapier.latex" -o "$2.pdf"
  else
    pandoc "$TMP" --template "$TEMPLATES/abgaben.latex" -o "$2.pdf"
  fi

  # printf "Successfully compiled %s to %s\n" "$1" "$2.pdf" || 
  # printf "[ Error ] Couldn't compile %s\n" "$1"
}

# Function to run the make command if possible
RUN_MAKE() {
  cd "$1" &&
  make auto &&
  printf "Successfully made project\n" ||
  printf "[ Error ] Couldn't make project\n"
}

# Check if file exists
if [[ -e "$1" ]]; then
  # Determine filename and extension
  FILENAME="$(basename -- "$1")"
  EXTENSION="${FILENAME##*.}"
  FILEPATH="$(pwd)/$1"
  DIRECTORY="${FILEPATH%/*}"

  # If make file exists, run make and exit
  if [[ -e "$DIRECTORY/makefile" ]] || [[ -e "$DIRECTORY/Makefile" ]]; then
    echo "$2"
    if [[ "$2" != "--no-make" ]]; then
      RUN_MAKE "$DIRECTORY"
      exit 0
    fi
  fi

  # Check if there is a extension at all
  if [[ "$FILENAME" == "$EXTENSION" ]]; then 
    printf "[ Error ] The specified file has no extension\n"
    exit 1
  fi

  # Do the respective compile actions
  case "$EXTENSION" in
    "adoc")
      ADOC_TO_HTML "$1" "${FILENAME%.*}"
      ;;
    "tex")
      TEX_TO_PDF "$1" "${FILENAME%.*}"
      ;;
    "pov")
      povray "$1"
      ;;
    "lp")
      MD_TO_PDF "$1" "${FILENAME%.*}"
      ;;
    "md")
      MD_TO_PDF "$1" "${FILENAME%.*}"
      ;;
    *)
      printf "[ Error ] There is no entry on how to handle '%s' files.\n" "$EXTENSION"
      exit 1
  esac
else 
  printf "[ Error ] No file specified!\n"
fi

exit 0