#!/usr/bin/env bash # # Dings-Feature-Render-Ir-Html Use-Cases # # Run from anywhere inside the Dings-System repository: # # bash 300101006.use-cases.bash # set -euo pipefail DINGS_GPT="${DINGS_GPT:-}" Repo_Dir="$(git rev-parse --show-toplevel)" Dings_Dir="${Dings_Dir:-$(cd "${Repo_Dir}/../../.." && pwd)}" if [ -z "${DINGS_GPT}" ] && [ -x "${Dings_Dir}/Work-Dir/dings-gpt" ]; then DINGS_GPT="${Dings_Dir}/Work-Dir/dings-gpt" fi DINGS_GPT="${DINGS_GPT:-dings-gpt}" Work_Dir="${Dings_Work_Directory:-${Dings_Dir}/Work-Dir}" Output_Dir="$(mktemp -d "${TMPDIR:-/tmp}/dings-ir-html.XXXXXX")" Fixture_Md_Path="${Output_Dir}/300101006.md" Fixture_Ir_Path="${Output_Dir}/300101006.ir.json" Fixture_Html_Path="${Output_Dir}/300101006.html" cleanup() { rm -rf "${Output_Dir}" } trap cleanup EXIT export Dings_Dir export Dings_Work_Directory="${Work_Dir}" cd "${Dings_Dir}" print_case() { echo echo "== $1 ==" } run() { echo "+ $*" "$@" } assert_file() { local File_Path="$1" if [ ! -f "${File_Path}" ]; then echo "Missing file: ${File_Path}" >&2 exit 1 fi } assert_grep() { local Pattern="$1" local File_Path="$2" if ! grep -Eq "${Pattern}" "${File_Path}"; then echo "Pattern not found in ${File_Path}: ${Pattern}" >&2 exit 1 fi } assert_not_grep() { local Pattern="$1" local File_Path="$2" if grep -Eq "${Pattern}" "${File_Path}"; then echo "Unexpected pattern found in ${File_Path}: ${Pattern}" >&2 exit 1 fi } assert_command_grep() { local Pattern="$1" shift local Temp_File Temp_File="$(mktemp "${TMPDIR:-/tmp}/dings-ir-html-output.XXXXXX")" "$@" > "${Temp_File}" 2>&1 if ! grep -Eq "${Pattern}" "${Temp_File}"; then echo "Pattern not found in command output: ${Pattern}" >&2 cat "${Temp_File}" >&2 rm -f "${Temp_File}" exit 1 fi rm -f "${Temp_File}" } assert_command_fail_grep() { local Pattern="$1" shift local Temp_File Temp_File="$(mktemp "${TMPDIR:-/tmp}/dings-ir-html-output.XXXXXX")" if "$@" > "${Temp_File}" 2>&1; then echo "Command unexpectedly succeeded: $*" >&2 cat "${Temp_File}" >&2 rm -f "${Temp_File}" exit 1 fi if ! grep -Eq "${Pattern}" "${Temp_File}"; then echo "Pattern not found in failing command output: ${Pattern}" >&2 cat "${Temp_File}" >&2 rm -f "${Temp_File}" exit 1 fi rm -f "${Temp_File}" } assert_ir_app_targets() { local File_Path="$1" shift python3 - "$File_Path" "$@" <<'PY' import json import sys Path = sys.argv[1] Expected_Target_List = sys.argv[2:] with open(Path, encoding="utf-8") as File: Document = json.load(File) Target_List = [ Block.get("Target_File") for Block in Document.get("Block_List", []) if Block.get("Type") == "Dings_IR_Node_Block_App" ] Missing_Target_List = [ Target for Target in Expected_Target_List if Target not in Target_List ] if Missing_Target_List: print("Missing app targets in IR: " + ", ".join(Missing_Target_List), file=sys.stderr) raise SystemExit(1) PY } print_case "Help documents Ir-Html" assert_command_grep "Ir-Html" "${DINGS_GPT}" render --help assert_command_grep "Day/\\.html" "${DINGS_GPT}" render Ir-Html --help cat > "${Fixture_Md_Path}" <<'EOF' # Ir Html Fixture ## Body Text with [[3]](#foot), [[4]](4711), and $2^{\frac{7}{12}}$. Inline markup with _M1/M2_, *real* [Foot](#foot), **bold**, and __strong__. - First [Foot](#foot) - Second `Code` > Yes! $$ F = \sqrt[12]{128} $${#fixture-equation:7} | Method | Result | |--------|--------| | Chat-Gpt-Newton | 1.498 | ![](400000195.jpg) ![Let us go](400000196.mp3) [3] Foot [4] Local numeric anchor EOF print_case "Md-Ir parses fixture Markdown" run "${DINGS_GPT}" render Md-Ir -output-dir "${Output_Dir}" "${Fixture_Md_Path}" assert_file "${Fixture_Ir_Path}" assert_ir_app_targets "${Fixture_Ir_Path}" "400000195.jpg" "400000196.mp3" assert_grep "Dings_IR_Node_Inline_Math" "${Fixture_Ir_Path}" assert_grep "Dings_IR_Node_Inline_Emphasis" "${Fixture_Ir_Path}" assert_grep "Dings_IR_Node_Inline_Strong" "${Fixture_Ir_Path}" assert_grep "Dings_IR_Node_Block_List" "${Fixture_Ir_Path}" assert_grep "Dings_IR_Node_Block_Quote" "${Fixture_Ir_Path}" assert_grep "Dings_IR_Node_Block_Math" "${Fixture_Ir_Path}" assert_grep '"Anchor_Id": "fixture-equation"' "${Fixture_Ir_Path}" assert_grep '"Equation_Number": "7"' "${Fixture_Ir_Path}" assert_grep "Dings_IR_Node_Block_Table" "${Fixture_Ir_Path}" run "${DINGS_GPT}" render Ir-Html -output-dir "${Output_Dir}" "${Fixture_Ir_Path}" assert_file "${Fixture_Html_Path}" assert_grep "href=\"#foot\">\\[3\\]" "${Fixture_Html_Path}" assert_grep "href=\"#4711\">\\[4\\]" "${Fixture_Html_Path}" assert_grep "" "${Fixture_Html_Path}" assert_grep "M1/M2" "${Fixture_Html_Path}" assert_grep "real Foot" "${Fixture_Html_Path}" assert_grep "bold" "${Fixture_Html_Path}" assert_grep "strong" "${Fixture_Html_Path}" assert_grep "