Skip to content

Commit 10c9f6f

Browse files
A Dynamic Collection of Shell Scripts with Educational Purpose
1 parent d0e7097 commit 10c9f6f

94 files changed

Lines changed: 126241 additions & 0 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

scripts/md2pdf

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
#!/usr/bin/env bash
2+
# License: GPLv3
3+
# Credits: Felipe Facundes
4+
5+
# ------------------------------------------------------------------------------
6+
# md2pdf — Convert Markdown to PDF with ABNT formatting via XeLaTeX
7+
#
8+
# Usage:
9+
# md2pdf file.md
10+
# md2pdf file.md output.pdf
11+
# md2pdf *.md (batch convert multiple files)
12+
#
13+
# Dependencies: pandoc, xelatex (texlive-core), Liberation Serif font
14+
# ------------------------------------------------------------------------------
15+
16+
set -euo pipefail
17+
18+
# --- Terminal colors ---------------------------------------------------------
19+
RED='\033[0;31m'
20+
GREEN='\033[0;32m'
21+
YELLOW='\033[1;33m'
22+
NC='\033[0m'
23+
24+
# --- Check dependencies ------------------------------------------------------
25+
for dep in pandoc xelatex; do
26+
if ! command -v "$dep" &>/dev/null; then
27+
echo -e "${RED}Error: '$dep' not found.${NC}"
28+
echo "Install with: sudo pacman -S pandoc texlive-core texlive-latexextra texlive-fontsextra texlive-fontsrecommended"
29+
exit 1
30+
fi
31+
done
32+
33+
# --- Usage -------------------------------------------------------------------
34+
if [[ $# -eq 0 ]]; then
35+
echo "Usage: md2pdf <file.md> [output.pdf]"
36+
echo " md2pdf *.md"
37+
exit 1
38+
fi
39+
40+
# --- Temporary LaTeX header file ---------------------------------------------
41+
# Created once and reused for all conversions in the session.
42+
HEADER_TEX=$(mktemp /tmp/md2pdf-XXXXXX.tex)
43+
trap "rm -f '$HEADER_TEX'" EXIT
44+
45+
cat > "$HEADER_TEX" << 'LATEX'
46+
% --- ABNT formatting packages ------------------------------------------------
47+
48+
\usepackage{xcolor}
49+
50+
% 1.5 line spacing
51+
\usepackage{setspace}
52+
\onehalfspacing
53+
54+
% Justified text
55+
\usepackage{ragged2e}
56+
\justifying
57+
58+
% Blue headings
59+
\usepackage{titlesec}
60+
\titleformat{\section}
61+
{\normalfont\Large\bfseries\color{blue}}{\thesection}{1em}{}
62+
\titleformat{\subsection}
63+
{\normalfont\large\bfseries\color{blue}}{\thesubsection}{1em}{}
64+
\titleformat{\subsubsection}
65+
{\normalfont\normalsize\bfseries\color{blue}}{\thesubsubsection}{1em}{}
66+
67+
% Page break after document title block
68+
% Ensures the title stays alone on the first page
69+
\usepackage{etoolbox}
70+
\makeatletter
71+
\preto\@maketitle{\clearpage}
72+
\appto\@maketitle{\clearpage}
73+
\makeatother
74+
LATEX
75+
76+
# --- Conversion function -----------------------------------------------------
77+
convert_file() {
78+
local input="$1"
79+
local output="$2"
80+
81+
echo -e "${YELLOW}Converting:${NC} $input$output"
82+
83+
if pandoc "$input" -o "$output" \
84+
--pdf-engine=xelatex \
85+
-V mainfont="Liberation Serif" \
86+
-V sansfont="Liberation Sans" \
87+
-V monofont="Liberation Mono" \
88+
-V fontsize=12pt \
89+
-V "geometry:top=3cm, left=3cm, bottom=2cm, right=2cm" \
90+
-V colorlinks=true \
91+
-V linkcolor=blue \
92+
--toc \
93+
--number-sections \
94+
--highlight-style=tango \
95+
-H "$HEADER_TEX"; then
96+
97+
echo -e "${GREEN}✓ Done:${NC} $output"
98+
else
99+
echo -e "${RED}✗ Failed to convert:${NC} $input"
100+
return 1
101+
fi
102+
}
103+
104+
# --- Process arguments -------------------------------------------------------
105+
106+
# Case: md2pdf input.md output.pdf (two args, second ends in .pdf)
107+
if [[ $# -eq 2 && "${2##*.}" == "pdf" ]]; then
108+
convert_file "$1" "$2"
109+
exit $?
110+
fi
111+
112+
# Case: one or more .md files
113+
for input in "$@"; do
114+
if [[ ! -f "$input" ]]; then
115+
echo -e "${RED}File not found:${NC} $input"
116+
continue
117+
fi
118+
if [[ "${input##*.}" != "md" ]]; then
119+
echo -e "${YELLOW}Warning: '$input' does not appear to be Markdown, skipping.${NC}"
120+
continue
121+
fi
122+
# Replace .md extension with .pdf
123+
output="${input%.md}.pdf"
124+
convert_file "$input" "$output"
125+
done

0 commit comments

Comments
 (0)