Skip to content

Commit b4f3ed5

Browse files
committed
Make markdown includes directory configurable and use sysin for template
1 parent a54dad8 commit b4f3ed5

File tree

3 files changed

+45
-13
lines changed

3 files changed

+45
-13
lines changed

scripts/executeQuery.sh

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ set -o errexit -o pipefail
2626
SCRIPTS_DIR=${SCRIPTS_DIR:-$( CDPATH=. cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd -P )} # Repository directory containing the shell scripts
2727
#echo "executeQuery: SCRIPTS_DIR=$SCRIPTS_DIR" >&2
2828

29+
MARKDOWN_SCRIPTS_DIR=${MARKDOWN_SCRIPTS_DIR:-"${SCRIPTS_DIR}/markdown"}
30+
#echo "executeQuery: MARKDOWN_SCRIPTS_DIR=${MARKDOWN_SCRIPTS_DIR}" >&2
31+
2932
# Overrideable Defaults
3033
NEO4J_HTTP_PORT=${NEO4J_HTTP_PORT:-"7474"} # Neo4j HTTP API port for executing queries
3134
NEO4J_HTTP_TRANSACTION_ENDPOINT=${NEO4J_HTTP_TRANSACTION_ENDPOINT:-"db/neo4j/tx/commit"} # Since Neo4j v5: "db/<name>/tx/commit", Neo4j v4: "db/data/transaction/commit"
@@ -149,8 +152,8 @@ if [[ -n "${error_message}" ]]; then
149152
fi
150153

151154
if [ "${output_markdown_table}" = "true" ] ; then
152-
echo "executeQuery: Will output in Markdown Table Format" >&2
153-
echo -n "${cypher_query_result}" | "${SCRIPTS_DIR}/markdown/formatQueryResultAsMarkdownTable.sh"
155+
#echo "executeQuery: Will output in Markdown Table Format" >&2
156+
echo -n "${cypher_query_result}" | "${MARKDOWN_SCRIPTS_DIR}/formatQueryResultAsMarkdownTable.sh"
154157
else
155158
# Output results in CSV format
156159
if [ "${no_source_reference}" = true ] ; then

scripts/markdown/embedMarkdownIncludes.sh

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

3-
# Processes a template_markdown_file markdown file, 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 -->" with the contents of the specified markdown files. The files to include needs to be in the "includes" subdirectory.
4+
# Can take an optional input for the directory that contains the markdown files to be included/embedded (defaults to "includes").
45

56
# Fail on any error ("-e" = exit on first error, "-o pipefail" exist on errors within piped commands)
67
set -o errexit -o pipefail
@@ -12,10 +13,20 @@ set -o errexit -o pipefail
1213
MARKDOWN_SCRIPTS_DIR=${MARKDOWN_SCRIPTS_DIR:-$( CDPATH=. cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd -P )} # Repository directory containing the shell scripts for markdown
1314
#echo "embedMarkdownIncludes: MARKDOWN_SCRIPTS_DIR=${MARKDOWN_SCRIPTS_DIR}" >&2
1415

15-
template_markdown_file="$1"
16-
include_directory="includes"
16+
# Read all input (including multiline) into markdown_template
17+
markdown_template=$(cat)
1718

18-
awk -v include_directory="${include_directory}" '
19+
includes_directory="$1"
20+
if [ -z "${includes_directory}" ] ; then
21+
includes_directory="./includes"
22+
echo "embedMarkdownIncludes: Using default include directory ${includes_directory}." >&2
23+
fi
24+
if [ ! -d "${includes_directory}" ] ; then
25+
echo "embedMarkdownIncludes: Couldn't find include directory ${includes_directory}." >&2
26+
exit 2
27+
fi
28+
29+
echo -n "${markdown_template}" | awk -v includes_directory="${includes_directory}" '
1930
# Check if the filename is safe
2031
function is_safe(path) {
2132
if (substr(path, 1, 1) == "/") return 0
@@ -24,7 +35,7 @@ awk -v include_directory="${include_directory}" '
2435
}
2536
2637
function include_file(path, fullpath, line) {
27-
fullpath = include_directory "/" path
38+
fullpath = includes_directory "/" path
2839
2940
if (!is_safe(path)) {
3041
print "ERROR: illegal include path: " path > "/dev/stderr"
@@ -56,6 +67,6 @@ awk -v include_directory="${include_directory}" '
5667
print
5768
}
5869
}
59-
' "${template_markdown_file}"
70+
'
6071

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

scripts/markdown/testEmbedMarkdownIncludes.sh

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ expected_test_include_content="This is the included content for the test."
6161
echo "${expected_test_include_content}" > "${temporaryTestDirectory}/${testIncludeFile}"
6262

6363
# - Execute script under test
64-
embeddedContent=$(cd "${temporaryTestDirectory}"; "${MARKDOWN_SCRIPTS_DIR}/embedMarkdownIncludes.sh" "${testMarkdownTemplate}" )
64+
embeddedContent=$(cat "${testMarkdownTemplate}" | "${MARKDOWN_SCRIPTS_DIR}/embedMarkdownIncludes.sh" "${temporaryTestDirectory}/includes")
6565

6666
# - Verify results
6767
if [ "${embeddedContent}" != "${expected_test_include_content}" ]; then
@@ -71,15 +71,33 @@ fi
7171
# ------------------------------------------------------------
7272
# Test case --
7373
# ------------------------------------------------------------
74-
echo "testEmbedMarkdownIncludes: 2.) A missing include file results in an error."
74+
echo "testEmbedMarkdownIncludes: 2.) An existing include file in the DEFAULT directory is correctly embedded."
7575

7676
# - Setup
77-
testMarkdownTemplateMissingInclude="testMarkdownTemplateMissingInclude.md"
78-
echo "<!-- include:nonExistentFile.md -->" > "${temporaryTestDirectory}/${testMarkdownTemplateMissingInclude}"
77+
testIncludeFile="includes/testInclude.md"
78+
expected_test_include_content="This is the included content for the test."
79+
echo "${expected_test_include_content}" > "${temporaryTestDirectory}/${testIncludeFile}"
80+
81+
# - Execute script under test
82+
embeddedContent=$(cd "${temporaryTestDirectory}"; cat "${testMarkdownTemplate}" | "${MARKDOWN_SCRIPTS_DIR}/embedMarkdownIncludes.sh")
83+
84+
# - Verify results
85+
if [ "${embeddedContent}" != "${expected_test_include_content}" ]; then
86+
fail "2.) Test failed: Expected embedded content to be '${expected_test_include_content}', but got '${embeddedContent}'."
87+
fi
88+
89+
# ------------------------------------------------------------
90+
# Test case --
91+
# ------------------------------------------------------------
92+
echo "testEmbedMarkdownIncludes: 3.) A missing include file results in an error."
93+
94+
# - Setup
95+
testMarkdownTemplateMissingInclude="${temporaryTestDirectory}/testMarkdownTemplateMissingInclude.md"
96+
echo "<!-- include:nonExistentFile.md -->" > "${testMarkdownTemplateMissingInclude}"
7997

8098
# - Execute script under test
8199
set +o errexit
82-
errorOutput=$(cd "${temporaryTestDirectory}"; { "${MARKDOWN_SCRIPTS_DIR}/embedMarkdownIncludes.sh" "${testMarkdownTemplateMissingInclude}" 2>&1 1>/dev/null; } )
100+
errorOutput=$( { cat "${testMarkdownTemplateMissingInclude}" | "${MARKDOWN_SCRIPTS_DIR}/embedMarkdownIncludes.sh" "${temporaryTestDirectory}/includes" 2>&1 1>/dev/null; } )
83101
exitCode=$?
84102
set -o errexit
85103

0 commit comments

Comments
 (0)