-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathformat_content.sh
executable file
·96 lines (77 loc) · 2.19 KB
/
format_content.sh
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
#!/usr/bin/env bash
set -e
setup_echo_colours() {
# Exit the script on any error
set -e
# shellcheck disable=SC2034
if [ "${MONOCHROME}" = true ]; then
RED=''
GREEN=''
YELLOW=''
BLUE=''
BLUE2=''
DGREY=''
NC='' # No Colour
else
RED='\033[1;31m'
GREEN='\033[1;32m'
YELLOW='\033[1;33m'
BLUE='\033[1;34m'
BLUE2='\033[1;34m'
DGREY='\e[90m'
NC='\033[0m' # No Colour
fi
}
debug_value() {
local name="$1"; shift
local value="$1"; shift
if [ "${IS_DEBUG}" = true ]; then
echo -e "${DGREY}DEBUG ${name}: ${value}${NC}"
fi
}
debug() {
local str="$1"; shift
if [ "${IS_DEBUG}" = true ]; then
echo -e "${DGREY}DEBUG ${str}${NC}"
fi
}
main() {
IS_DEBUG=${IS_DEBUG:=false}
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null && pwd )"
CONTENT_DIR="${SCRIPT_DIR}/content"
setup_echo_colours
# find non-hugo links
# rg -t md --pcre2 '\[.*\]\([^{].*\)' ./content
# Array allows us to easily comment bits out for testing
local extra_args=()
# NOTE: This one is commented out because it risks matching lines in
# fenced blocks and there are two many edge cases.
# Add new line between sentences
# shellcheck disable=SC2016
#extra_args+=('-e' 's/(?<=\.)(?<!(?:[Ee]\.[Gg]|[Ii]\.[Ee])\.) +([A-Z])/\n$1/gm')
# Ensure two blank lines before heading
# shellcheck disable=SC2016
extra_args+=('-e' 's/(^[^#\n]*$)\n+(^##+ .*$)/$1\n\n\n$2/gm;')
# Ensure one blank line between consequtive headings
# Must be run after the ensure two lines one above
# shellcheck disable=SC2016
extra_args+=('-e' 's/(^##+ .*$)\n*?(?=^##+ .*$)/$1\n\n/gm;')
# Ensure first heading after front matter has one line between
extra_args+=('-e' 's/---\n+##/---\n\n##/gm;')
# Ensure one blank line after heading
# Ensure all fenced blocks have a language
# Ensure space between fence and language
debug_value "CONTENT_DIR" "${CONTENT_DIR}"
# The order of sed -e args is important as some may undo the work of others
# if run out of order.
find "${CONTENT_DIR}" \
-type f \
-name "*.md" \
-print0 \
| xargs -0 \
perl \
-0777 \
-pi \
"${extra_args[@]}"
}
main "$@"