Files

106 lines
5.5 KiB
Bash

#!/usr/bin/env sh
#
# author: Mamadou Babaei <mamadou@senseglove.com>
#
# (The MIT License)
#
# Copyright (c) 2020 - 2026 SenseGlove
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#
if [ "$1" = "supports" ]
then
exit 0
fi
readonly JSON_INPUT=$(cat)
readonly CONTEXT=$(printf '%s' "${JSON_INPUT}" | xq '.[0]')
readonly BOOK=$(printf '%s' "${JSON_INPUT}" | xq '.[1]')
escape_string() {
printf '%s' "$1" | sed -e 's/\\/\\\\/g' -e 's/"/\\"/g'
}
SG_PROJECT_NAME_ESCAPED=$(escape_string "${SG_PROJECT_NAME}")
SG_HANDBOOK_REVISION_ESCAPED=$(escape_string "${SG_HANDBOOK_REVISION}")
SG_HANDBOOK_REVISION_URL_ESCAPED=$(escape_string "${SG_HANDBOOK_REVISION_URL}")
SG_HANDBOOK_EPUB_URL_ESCAPED=$(escape_string "${SG_HANDBOOK_EPUB_URL}")
SG_HANDBOOK_PDF_URL_ESCAPED=$(escape_string "${SG_HANDBOOK_PDF_URL}")
SG_GIT_BRANCH_ESCAPED=$(escape_string "${SG_GIT_BRANCH}")
SG_GIT_TAG_ESCAPED=$(escape_string "${SG_GIT_TAG}")
SG_GIT_COMMIT_ESCAPED=$(escape_string "${SG_GIT_COMMIT}")
SG_GIT_COMMIT_NUMS_SINCE_TAG_ESCAPED=$(escape_string "${SG_GIT_COMMIT_NUMS_SINCE_TAG}")
SG_GIT_TREE_STATE_ESCAPED=$(escape_string "${SG_GIT_TREE_STATE}")
SG_GIT_IS_SHALLOW_CLONE_ESCAPED=$(escape_string "${SG_GIT_IS_SHALLOW_CLONE}")
SG_GIT_LATEST_REMOTE_TAG_ESCAPED=$(escape_string "${SG_GIT_LATEST_REMOTE_TAG}")
SG_GIT_VERSION_ESCAPED=$(escape_string "${SG_GIT_VERSION}")
SG_GIT_VERSION_MAJOR_ESCAPED=$(escape_string "${SG_GIT_VERSION_MAJOR}")
SG_GIT_VERSION_MINOR_ESCAPED=$(escape_string "${SG_GIT_VERSION_MINOR}")
SG_GIT_VERSION_PATCH_ESCAPED=$(escape_string "${SG_GIT_VERSION_PATCH}")
SG_PLUGIN_VERSION_ESCAPED=$(escape_string "${SG_PLUGIN_VERSION}")
SG_PLUGIN_VERSION_MAJOR_ESCAPED=$(escape_string "${SG_PLUGIN_VERSION_MAJOR}")
SG_PLUGIN_VERSION_MINOR_ESCAPED=$(escape_string "${SG_PLUGIN_VERSION_MINOR}")
SG_PLUGIN_VERSION_PATCH_ESCAPED=$(escape_string "${SG_PLUGIN_VERSION_PATCH}")
SG_BUILD_HOST_ESCAPED=$(escape_string "${SG_BUILD_HOST}")
SG_BUILD_TIME_ESCAPED=$(escape_string "${SG_BUILD_TIME}")
PARSED_BOOK=$(printf '%s' "${BOOK}" | xq "
.items[] |= (
if has(\"Chapter\") then
.Chapter.content |=
walk(if type == \"string\" then
. | gsub(\"{{ #sg-project-name }}\"; \"${SG_PROJECT_NAME_ESCAPED}\") |
gsub(\"{{ #sg-handbook-revision }}\"; \"${SG_HANDBOOK_REVISION_ESCAPED}\") |
gsub(\"{{ #sg-handbook-revision-url }}\"; \"${SG_HANDBOOK_REVISION_URL_ESCAPED}\") |
gsub(\"{{ #sg-handbook-epub-url }}\"; \"${SG_HANDBOOK_EPUB_URL_ESCAPED}\") |
gsub(\"{{ #sg-handbook-pdf-url }}\"; \"${SG_HANDBOOK_PDF_URL_ESCAPED}\") |
gsub(\"{{ #sg-git-branch }}\"; \"${SG_GIT_BRANCH_ESCAPED}\") |
gsub(\"{{ #sg-git-tag }}\"; \"${SG_GIT_TAG_ESCAPED}\") |
gsub(\"{{ #sg-git-commit }}\"; \"${SG_GIT_COMMIT_ESCAPED}\") |
gsub(\"{{ #sg-git-commit-nums-since-tag }}\"; \"${SG_GIT_COMMIT_NUMS_SINCE_TAG_ESCAPED}\") |
gsub(\"{{ #sg-git-tree-state }}\"; \"${SG_GIT_TREE_STATE_ESCAPED}\") |
gsub(\"{{ #sg-git-is-shallow-clone }}\"; \"${SG_GIT_IS_SHALLOW_CLONE_ESCAPED}\") |
gsub(\"{{ #sg-git-latest-remote-tag }}\"; \"${SG_GIT_LATEST_REMOTE_TAG_ESCAPED}\") |
gsub(\"{{ #sg-git-version }}\"; \"${SG_GIT_VERSION_ESCAPED}\") |
gsub(\"{{ #sg-git-version-major }}\"; \"${SG_GIT_VERSION_MAJOR_ESCAPED}\") |
gsub(\"{{ #sg-git-version-minor }}\"; \"${SG_GIT_VERSION_MINOR_ESCAPED}\") |
gsub(\"{{ #sg-git-version-patch }}\"; \"${SG_GIT_VERSION_PATCH_ESCAPED}\") |
gsub(\"{{ #sg-plugin-version }}\"; \"${SG_PLUGIN_VERSION_ESCAPED}\") |
gsub(\"{{ #sg-plugin-version-major }}\"; \"${SG_PLUGIN_VERSION_MAJOR_ESCAPED}\") |
gsub(\"{{ #sg-plugin-version-minor }}\"; \"${SG_PLUGIN_VERSION_MINOR_ESCAPED}\") |
gsub(\"{{ #sg-plugin-version-patch }}\"; \"${SG_PLUGIN_VERSION_PATCH_ESCAPED}\") |
gsub(\"{{ #sg-build-host }}\"; \"${SG_BUILD_HOST_ESCAPED}\") |
gsub(\"{{ #sg-build-time }}\"; \"${SG_BUILD_TIME_ESCAPED}\")
else
.
end)
else
.
end
)")
# For debugging purpose only!
#echo "CONTEXT: ${CONTEXT}" >&2
#echo "BOOK: ${BOOK}" >&2
#echo "PARSED_BOOK: ${PARSED_BOOK}" >&2
printf '%s' "${PARSED_BOOK}"