Skip to content

Commit

Permalink
Refactor in makefile
Browse files Browse the repository at this point in the history
  • Loading branch information
cfgnunes committed Aug 8, 2023
1 parent 4f1193c commit a5f2339
Showing 1 changed file with 48 additions and 31 deletions.
79 changes: 48 additions & 31 deletions makefile
Original file line number Diff line number Diff line change
@@ -1,60 +1,77 @@
SRC = meu-trabalho
SRC_COMP = meu-trabalho-compactado
OUTPUT_DIR = latex.out
SOURCE := meu-trabalho
COMPRESSED_SUFFIX := compactado
OUTPUT_DIR := latex.out

TEX_FILES = $(shell find . -type f -iname "*.tex" -or -iname "*.cls" -or -iname "*.bib")
IMG_FILES = $(shell find . -type f -iname "*.png" -or -iname "*.jpg" -or -iname "*.eps")
PDF_FILES = $(shell find . -type f -iname "*.pdf" ! -iname "$(SRC).pdf" ! -iname "$(SRC_COMP).pdf")
SVG_FILES = $(shell find . -type f -iname "*.svg")
INPUT_TEX := $(SOURCE).tex
OUTPUT_PDF := $(SOURCE).pdf
OUTPUT_PDF_150_DPI := $(SOURCE)-$(COMPRESSED_SUFFIX)-150dpi.pdf
OUTPUT_PDF_300_DPI := $(SOURCE)-$(COMPRESSED_SUFFIX)-300dpi.pdf

.PHONY: default help compile compress clean
TEX_FILES := $(shell find . -type f -iname "*.tex" -or -iname "*.cls" -or -iname "*.bib" ! -path "*/$(OUTPUT_DIR)/*")
IMG_FILES := $(shell find . -type f -iname "*.png" -or -iname "*.jpg" -or -iname "*.eps" ! -path "*/$(OUTPUT_DIR)/*")
PDF_FILES := $(shell find . -type f -iname "*.pdf" ! -path "*/$(OUTPUT_DIR)/*")
SVG_FILES := $(shell find . -type f -iname "*.svg" ! -path "*/$(OUTPUT_DIR)/*")
SVG_TO_PDF_FILES := $(SVG_FILES:.svg=.pdf)
SOURCE_FILES := $(TEX_FILES) $(IMG_FILES) $(PDF_FILES) $(SVG_TO_PDF_FILES)

default: compile
# Define phony targets (not associated with file names)
.PHONY: default help compile compress compress-max clean

# Default target: compress PDF to 150 DPI
default: compress-max

# Display help information
help:
@echo "Usage:"
@echo " make : Create the PDF document."
@echo " make compress : Create the compressed PDF document."
@echo " make clean : Remove generated files."
@echo " make : Create the PDF document."
@echo " make compress-max : Create the compressed PDF document (150 DPI)."
@echo " make compress : Create the compressed PDF document (300 DPI)."
@echo " make clean : Remove generated files."
@echo ""

compile: $(OUTPUT_DIR)/$(SRC).pdf
# Compile the project
compile: $(OUTPUT_DIR)/$(OUTPUT_PDF)

$(OUTPUT_DIR)/$(SRC).pdf: $(TEX_FILES) $(IMG_FILES) $(PDF_FILES) $(SVG_FILES:.svg=.pdf)
$(OUTPUT_DIR)/$(OUTPUT_PDF): $(SOURCE_FILES)
@echo "Building the project..."
@mkdir -p $(OUTPUT_DIR)
@latexmk \
-pdf \
-synctex=1 \
-output-directory=$(OUTPUT_DIR) $(SRC).tex
@touch $(OUTPUT_DIR)/$(SRC).pdf
@latexmk -pdf -synctex=1 -output-directory=$(OUTPUT_DIR) $(INPUT_TEX)
@echo "Done!"
@echo

# Convert SVG files to PDF
%.pdf: %.svg
@echo "Converting SVG to PDF..."
@echo " > File: '$<'..."
@inkscape --export-area-drawing --export-margin=1 --export-filename=$@ $<
@echo "Done!"
@echo

compress: $(OUTPUT_DIR)/$(SRC_COMP).pdf
# Compress the PDF at 300 DPI
compress: $(OUTPUT_DIR)/$(OUTPUT_PDF_300_DPI)

$(OUTPUT_DIR)/$(OUTPUT_PDF_300_DPI): $(OUTPUT_DIR)/$(OUTPUT_PDF)
@echo "Compressing the document (300 DPI)..."
@gs -q -dNOPAUSE -dBATCH -dFastWebView -sDEVICE=pdfwrite \
-dPDFSETTINGS=/printer \
-sOutputFile=$(OUTPUT_DIR)/$(OUTPUT_PDF_300_DPI) \
$(OUTPUT_DIR)/$(OUTPUT_PDF)
@echo "Done!"
@echo

# Compress the PDF at 150 DPI
compress-max: $(OUTPUT_DIR)/$(OUTPUT_PDF_150_DPI)

$(OUTPUT_DIR)/$(SRC_COMP).pdf: $(OUTPUT_DIR)/$(SRC).pdf
@echo "Compressing the document..."
@gs \
-q \
-dNOPAUSE \
-dBATCH \
-dFastWebView \
-sDEVICE=pdfwrite \
$(OUTPUT_DIR)/$(OUTPUT_PDF_150_DPI): $(OUTPUT_DIR)/$(OUTPUT_PDF)
@echo "Compressing the document (150 DPI)..."
@gs -q -dNOPAUSE -dBATCH -dFastWebView -sDEVICE=pdfwrite \
-dPDFSETTINGS=/ebook \
-sOutputFile=$(OUTPUT_DIR)/$(SRC_COMP).pdf \
$(OUTPUT_DIR)/$(SRC).pdf
@touch $(OUTPUT_DIR)/$(SRC_COMP).pdf
-sOutputFile=$(OUTPUT_DIR)/$(OUTPUT_PDF_150_DPI) \
$(OUTPUT_DIR)/$(OUTPUT_PDF)
@echo "Done!"
@echo

# Clean up all generated files
clean:
@echo "Cleaning up generated files..."
@rm -rf $(OUTPUT_DIR)
Expand Down

0 comments on commit a5f2339

Please sign in to comment.