Skip to content

Commit 6ea6e9c

Browse files
committed
Support fallback markdown includes for missing files
1 parent 7137bf6 commit 6ea6e9c

File tree

2 files changed

+49
-11
lines changed

2 files changed

+49
-11
lines changed

scripts/markdown/embedMarkdownIncludes.sh

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/env bash
22

3-
# Processes template markdown (sysin) replacing placeholders like "<!-- include:intro.md -->" with the contents of the specified markdown files. The files to include needs to be in the "includes" subdirectory.
3+
# Processes template markdown (sysin) replacing placeholders like "<!-- include:intro.md -->" or "<!-- include:intro.md|fallback.md -->" with the contents of the specified markdown files. The files to include needs to be in the "includes" subdirectory.
44
# Can take an optional input for the directory that contains the markdown files to be included/embedded (defaults to "includes").
55

66
# Fail on any error ("-e" = exit on first error, "-o pipefail" exist on errors within piped commands)
@@ -34,33 +34,48 @@ echo -n "${markdown_template}" | awk -v includes_directory="${includes_directory
3434
return 1
3535
}
3636
37-
function include_file(path, fullpath, line) {
38-
fullpath = includes_directory "/" path
39-
37+
function try_include(path, fullpath, line) {
4038
if (!is_safe(path)) {
4139
print "ERROR: illegal include path: " path > "/dev/stderr"
4240
exit 1
4341
}
44-
42+
fullpath = includes_directory "/" path
4543
if ((getline test < fullpath) < 0) {
46-
print "ERROR: missing file " fullpath > "/dev/stderr"
47-
exit 1
44+
return 0 # not found
4845
}
4946
close(fullpath)
5047
5148
while ((getline line < fullpath) > 0) {
5249
print line
5350
}
5451
close(fullpath)
52+
return 1
53+
}
54+
55+
function include_file(spec, n, parts, i, success) {
56+
n = split(spec, parts, /\|/)
57+
success = 0
58+
for (i = 1; i <= n; i++) {
59+
gsub(/^[ \t]+|[ \t]+$/, "", parts[i]) # trim
60+
if (parts[i] == "") continue
61+
if (try_include(parts[i])) {
62+
success = 1
63+
break
64+
}
65+
}
66+
if (!success) {
67+
print "ERROR: missing include file(s): " spec > "/dev/stderr"
68+
exit 1
69+
}
5570
}
5671
5772
{
58-
# Look for the include marker using index+substr (portable)
59-
if ($0 ~ /<!-- include:/) {
73+
# Look for the include marker
74+
if ($0 ~ /<!--[ \t]*include:/) {
6075
start = index($0, "include:") + 8
6176
end = index($0, "-->")
6277
fname = substr($0, start, end - start)
63-
gsub(/^[ \t]+|[ \t]+$/, "", fname) # trim spaces
78+
gsub(/^[ \t]+|[ \t]+$/, "", fname)
6479
6580
include_file(fname)
6681
} else {
@@ -69,4 +84,5 @@ echo -n "${markdown_template}" | awk -v includes_directory="${includes_directory
6984
}
7085
'
7186

87+
7288
#echo "embedMarkdownIncludes: $(date +'%Y-%m-%dT%H:%M:%S%z') Successfully finished." >&2

scripts/markdown/testEmbedMarkdownIncludes.sh

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,31 @@ set -o errexit
105105
if [ ${exitCode} -eq 0 ]; then
106106
fail "2.) Test failed: Expected an error due to missing include file, but the script succeeded."
107107
fi
108-
if [[ "${errorOutput}" != *"ERROR: missing file"* ]]; then
108+
if [[ "${errorOutput}" != *"ERROR: missing include file"* ]]; then
109109
fail "2.) Test failed: Expected error message to contain 'ERROR: missing file', but got '${errorOutput}'."
110110
fi
111111

112+
# ------------------------------------------------------------
113+
# Test case --
114+
# ------------------------------------------------------------
115+
echo "testEmbedMarkdownIncludes: 4.) The fallback include is used when the main include is missing"
116+
117+
# - Setup
118+
testFallbackIncludeFileName="testFallbackInclude.md"
119+
echo "<!-- include:nonExistingInclude|${testFallbackIncludeFileName} -->" > "${testMarkdownTemplate}"
120+
121+
testFallbackIncludeFile="includes/${testFallbackIncludeFileName}"
122+
expected_test_include_content="This is the included content from the fallback include."
123+
echo "${expected_test_include_content}" > "${temporaryTestDirectory}/${testFallbackIncludeFile}"
124+
125+
# - Execute script under test
126+
embeddedContent=$(cd "${temporaryTestDirectory}"; cat "${testMarkdownTemplate}" | "${MARKDOWN_SCRIPTS_DIR}/embedMarkdownIncludes.sh")
127+
128+
# - Verify results
129+
if [ "${embeddedContent}" != "${expected_test_include_content}" ]; then
130+
fail "4.) Test failed: Expected embedded content to be '${expected_test_include_content}', but got '${embeddedContent}'."
131+
fi
132+
133+
112134
successful
113135
return 0

0 commit comments

Comments
 (0)