Gist Codeblock Macro¶
Summary¶
This section describes a macro that displays Gist code in a Zensical code block.
This macro is assumed to be used with Zensical.
Usage¶
Write a macro in markdown with the following parameters to display Gist code in a code block.
Macro:gist_codeblock
| Parameters | Required | Default | Description |
|---|---|---|---|
gist_url |
required | none | Gist shared link |
indent |
optional | 0 | indent level (0: none, 1: 4 spaces, 2: 8 spaces) |
ext |
Optional | Automatic determination from URL | language extension (e.g. py, js, sh, etc.) |
Examples¶
Basic Usage¶
Specify minimal parameters
Example
#!/usr/bin/env bash
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
create_post_commit() {
cat > "$1" << EOF
#!/usr/bin/env bash
source "$SCRIPT_DIR/../.venv/bin/activate"
poetry run python "$SCRIPT_DIR/../ci/run_git_tag_base_pyproject.py"
if [ $? -ne 0 ]; then
printf "Error occurred in run_git_tag_base_pyproject.py. Exiting post-commit.\n"
exit 1
fi
git push origin main:main
git push --tags
printf ".git/hooks/post-commit end!!!\n"
EOF
if [ "$2" == "execute" ]; then
chmod +x "$1"
echo "$1 created with execution permission."
else
echo "$1 created."
fi
}
if [ -f "$SCRIPT_DIR/.git/hooks/post-commit" ]; then
# For shellcheck SC2162
read -r -p "$SCRIPT_DIR/../.git/hooks/post-commit already exists. Do you want to create $SCRIPT_DIR/.git/hooks/post-commit.second instead? (y/N): " choice
if [[ $choice == "y" || $choice == "Y" ]]; then
create_post_commit "$SCRIPT_DIR/../.git/hooks/post-commit.second"
exit 0
else
create_post_commit "$SCRIPT_DIR/../.git/hooks/post-commit" "execute"
exit 0
fi
fi
create_post_commit "$SCRIPT_DIR/../.git/hooks/post-commit" "execute"
exit 0
Specify indentation level¶
Tip
Must be specified if the code is to be displayed within a block of admonition.
Specify indentation level
{{ gist_codeblock(
gist_url="Gist shared link",
indent=1 # Indentation level (1:4 spaces, 2:8 spaces)
) }}
Example of displaying code within an admonition block
Indent example (indent=1)
#!/usr/bin/env bash
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
create_post_commit() {
cat > "$1" << EOF
#!/usr/bin/env bash
source "$SCRIPT_DIR/../.venv/bin/activate"
poetry run python "$SCRIPT_DIR/../ci/run_git_tag_base_pyproject.py"
if [ $? -ne 0 ]; then
printf "Error occurred in run_git_tag_base_pyproject.py. Exiting post-commit.\n"
exit 1
fi
git push origin main:main
git push --tags
printf ".git/hooks/post-commit end!!!\n"
EOF
if [ "$2" == "execute" ]; then
chmod +x "$1"
echo "$1 created with execution permission."
else
echo "$1 created."
fi
}
if [ -f "$SCRIPT_DIR/.git/hooks/post-commit" ]; then
# For shellcheck SC2162
read -r -p "$SCRIPT_DIR/../.git/hooks/post-commit already exists. Do you want to create $SCRIPT_DIR/.git/hooks/post-commit.second instead? (y/N): " choice
if [[ $choice == "y" || $choice == "Y" ]]; then
create_post_commit "$SCRIPT_DIR/../.git/hooks/post-commit.second"
exit 0
else
create_post_commit "$SCRIPT_DIR/../.git/hooks/post-commit" "execute"
exit 0
fi
fi
create_post_commit "$SCRIPT_DIR/../.git/hooks/post-commit" "execute"
exit 0
Indent example (indent=2)
#!/usr/bin/env bash
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
create_post_commit() {
cat > "$1" << EOF
#!/usr/bin/env bash
source "$SCRIPT_DIR/../.venv/bin/activate"
poetry run python "$SCRIPT_DIR/../ci/run_git_tag_base_pyproject.py"
if [ $? -ne 0 ]; then
printf "Error occurred in run_git_tag_base_pyproject.py. Exiting post-commit.\n"
exit 1
fi
git push origin main:main
git push --tags
printf ".git/hooks/post-commit end!!!\n"
EOF
if [ "$2" == "execute" ]; then
chmod +x "$1"
echo "$1 created with execution permission."
else
echo "$1 created."
fi
}
if [ -f "$SCRIPT_DIR/.git/hooks/post-commit" ]; then
# For shellcheck SC2162
read -r -p "$SCRIPT_DIR/../.git/hooks/post-commit already exists. Do you want to create $SCRIPT_DIR/.git/hooks/post-commit.second instead? (y/N): " choice
if [[ $choice == "y" || $choice == "Y" ]]; then
create_post_commit "$SCRIPT_DIR/../.git/hooks/post-commit.second"
exit 0
else
create_post_commit "$SCRIPT_DIR/../.git/hooks/post-commit" "execute"
exit 0
fi
fi
create_post_commit "$SCRIPT_DIR/../.git/hooks/post-commit" "execute"
exit 0
To explicitly specify a language¶
Specify language extensions
Example
#!/usr/bin/env bash
# Usage: poetry install pre-commit install
# File generated by pre-commit: https://pre-commit.com
# start templated
INSTALL_PYTHON=[Project Path]/.venv/bin/python
ARGS=(hook-impl --config=.pre-commit-config.yaml --hook-type=pre-commit)
# end templated
HERE="$(cd "$(dirname "$0")" && pwd)"
ARGS+=(--hook-dir "$HERE" -- "$@")
if [ -x "$INSTALL_PYTHON" ]; then
exec "$INSTALL_PYTHON" -mpre_commit "${ARGS[@]}"
elif command -v pre-commit > /dev/null; then
exec pre-commit "${ARGS[@]}"
else
echo '`pre-commit` not found. Did you forget to activate your virtualenv?' 1>&2
exit 1
fi