-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpandoc-format
More file actions
executable file
·104 lines (92 loc) · 2.31 KB
/
pandoc-format
File metadata and controls
executable file
·104 lines (92 loc) · 2.31 KB
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
#!/usr/bin/env bash
#
# Wrapper script for pandoc to format markdown files
# Used as a pre-commit hook
set -euo pipefail
# Check if pandoc is installed
if ! command -v pandoc &> /dev/null; then
echo "Error: pandoc is not installed. Please install pandoc before using this hook."
echo "Visit https://pandoc.org/installing.html for installation instructions."
exit 1
fi
# Default values
COLUMNS=80
USE_REFERENCE_LINKS=true
FROM_FORMAT="gfm"
TO_FORMAT="gfm"
# Parse arguments
FILES=()
while [[ $# -gt 0 ]]; do
case "$1" in
--columns)
if [[ $# -lt 2 ]]; then
echo "Error: --columns requires an argument"
exit 1
fi
COLUMNS="$2"
shift 2
;;
--no-reference-links)
USE_REFERENCE_LINKS=false
shift
;;
--from)
if [[ $# -lt 2 ]]; then
echo "Error: --from requires an argument"
exit 1
fi
FROM_FORMAT="$2"
shift 2
;;
--to)
if [[ $# -lt 2 ]]; then
echo "Error: --to requires an argument"
exit 1
fi
TO_FORMAT="$2"
shift 2
;;
*)
FILES+=("$1")
shift
;;
esac
done
# Check if any files were provided
if [ ${#FILES[@]} -eq 0 ]; then
echo "Error: No files provided"
exit 1
fi
# Build pandoc arguments
PANDOC_ARGS=(
"--columns=$COLUMNS"
"-s"
"-f" "$FROM_FORMAT"
"-t" "$TO_FORMAT"
)
if [ "$USE_REFERENCE_LINKS" = true ]; then
PANDOC_ARGS+=("--reference-links")
fi
# Process each file
exit_code=0
for file in "${FILES[@]}"; do
# Create a temporary file for output
tmpfile=$(mktemp)
# Run pandoc with the specified flags
if pandoc "${PANDOC_ARGS[@]}" --output "$tmpfile" "$file" 2>/dev/null; then
# Check if the file changed
if ! cmp -s "$file" "$tmpfile"; then
# Replace the original file with the formatted version
mv "$tmpfile" "$file"
echo "Formatted: $file"
exit_code=1
else
rm "$tmpfile"
fi
else
echo "Error: Failed to format $file"
rm -f "$tmpfile"
exit_code=1
fi
done
exit $exit_code