Skip to content

Instantly share code, notes, and snippets.

@neumantm
Created November 12, 2023 17:40
Show Gist options
  • Save neumantm/37ef76fe3f542103314aee3aec4612d4 to your computer and use it in GitHub Desktop.
Save neumantm/37ef76fe3f542103314aee3aec4612d4 to your computer and use it in GitHub Desktop.
ReqDoc Make Full Document
#!/usr/bin/env bash
buildDir="build/toHtml"
artifactPath="build/artifacts/path-to/requirements.zip"
# Unpack markdown
if ! test -f "$artifactPath"; then
echo "No artifact found at $artifactPath"
exit 1
fi
echo "Unpacking markdown from $artifactPath to $buildDir/unpacking"
mkdir --parents "$buildDir"
rm -rf "$buildDir/unpacking"
unzip -q "$artifactPath" -d "$buildDir/unpacking"
requirementsSrcJar="$buildDir/unpacking/requirements-src.jar"
if ! test -f "$requirementsSrcJar"; then
echo "No requirements-src.jar found at $requirementsSrcJar"
exit 1
fi
echo "Unpacking requirements-src.jar to $buildDir/unpacking"
unzip -q "$requirementsSrcJar" -d "$buildDir/unpacking"
unset requirementsSrcJar
markdownFolder="$buildDir/unpacking/md"
if ! test -d "$markdownFolder"; then
echo "Markdown folder not found at $markdownFolder"
exit 1
fi
echo "Copying markdown from $markdownFolder to $buildDir/md"
rm -rf "$buildDir/md"
cp -r $markdownFolder "$buildDir/md"
unset markdownFolder
# Postprocess markdown
mdFolder="$buildDir/md"
reqsFolder="$mdFolder/reqs"
reqsIndex="$reqsFolder/index.md"
echo "Generating requirements index."
echo "# Requirements" > $reqsIndex
echo "" >> $reqsIndex
echo "Here is a list of all current requirements:" >> $reqsIndex
echo "" >> $reqsIndex
for f in "$reqsFolder"/*.md; do
if [ "$f" != "$reqsIndex" ]; then
echo "- [$(head -n 1 "$f" | sed -e 's/^#* //')]($(basename "$f"))" >> $reqsIndex
fi
done
echo "Generating top level index."
mdIndex="$mdFolder/index.md"
echo "# Title" > $mdIndex
echo "" >> $mdIndex
echo "This is the automatically generated webpage of the requirements collection for project." >> $mdIndex
echo "" >> $mdIndex
echo "## Table of Contents" >> $mdIndex
echo "" >> $mdIndex
echo "- [List of all Requirements](reqs/index.md)" >> $mdIndex
echo "- [List of Origins](origins.md)" >> $mdIndex
echo "- [Complete Requirements Document](docs/document.md)" >> $mdIndex
echo "Modifying document."
cat "$mdFolder/docs/document.md" | sed -e 's/^########*/######/' > "$mdFolder/docs/document.md.tmp"
mv "$mdFolder/docs/document.md.tmp" "$mdFolder/docs/document.md"
head -n 2 "$mdFolder/docs/document.md" > "$mdFolder/docs/document.md.tmp"
cat "./.scripts/static/docs/document.md/preContent.md" >> "$mdFolder/docs/document.md.tmp"
tail -n +4 "$mdFolder/docs/document.md" >> "$mdFolder/docs/document.md.tmp"
mv "$mdFolder/docs/document.md.tmp" "$mdFolder/docs/document.md"
echo "Modifying priority Questionnaire."
cat "$mdFolder/docs/forPrioQuestionnaire.md" | sed -e 's/^########*/######/' > "$mdFolder/docs/forPrioQuestionnaire.md.tmp"
mv "$mdFolder/docs/forPrioQuestionnaire.md.tmp" "$mdFolder/docs/forPrioQuestionnaire.md"
head -n 2 "$mdFolder/docs/forPrioQuestionnaire.md" > "$mdFolder/docs/forPrioQuestionnaire.md.tmp"
cat "./.scripts/static/docs/priorityQuestionnaire.md/preContent.md" >> "$mdFolder/docs/forPrioQuestionnaire.md.tmp"
tail -n +4 "$mdFolder/docs/forPrioQuestionnaire.md" >> "$mdFolder/docs/forPrioQuestionnaire.md.tmp"
mv "$mdFolder/docs/forPrioQuestionnaire.md.tmp" "$mdFolder/docs/forPrioQuestionnaire.md"
newElementsLength=150
lineLength=$(wc -L < "$mdFolder/docs/forPrioQuestionnaire.md")
lineLengthWithPadding=$((lineLength+2))
tableLineSep="+"$(printf -- '-%.0s' $(seq 1 $lineLengthWithPadding))"+"$(printf -- '-%.0s' $(seq 1 $newElementsLength))"+"
spacesInLine=$(printf -- ' %.0s' $(seq 1 $lineLength))
spacesForNewElements=$(printf -- ' %.0s' $(seq 1 $newElementsLength))
echo -n "" > "$mdFolder/docs/forPrioQuestionnaire.md.tmp"
tabIndex=1
inReq=false
while IFS= read -r line; do
pattern='^###* [A-Z0-9][A-Z0-9][A-Z0-9][A-Z0-9]:'
thisLineLength=$(wc -L <<< "$line")
thisLineExtraPaddingLength=$((lineLength-thisLineLength))
thisLineExtraPadding=$(printf -- ' %.0s' $(seq 1 $thisLineExtraPaddingLength))
if [[ "$line" =~ $pattern ]] ;then
inReq=true
r=$(echo "$line" | sed -e 's/^###* \([A-Z0-9][A-Z0-9][A-Z0-9][A-Z0-9]\):.*/\1/')
echo "$tableLineSep" >> "$mdFolder/docs/forPrioQuestionnaire.md.tmp"
echo "| $spacesInLine | <label for=\"prio-$r\">Priority (0-9):</label> <input class=\"data-prio\" type=\"number\" id=\"prio-$r\" name=\"prio-$r\" min=\"0\" max=\"9\" oninput=\"updateInput(this)\" tabindex=\"$tabIndex\" > \ |" >> "$mdFolder/docs/forPrioQuestionnaire.md.tmp"
tabIndex=$((tabIndex+1))
echo "| $spacesInLine | <label for=\"sat_f-$r\">Satisfaction if fulfilled(--, -, 0, +, ++):</label> <input class=\"data-sat_f\" type=\"text\" id=\"sat_f-$r\" name=\"sat_f-$r\" size=\"2\" pattern=\"--|-|0|\+|\+\+\" oninput=\"updateInput(this)\" tabindex=\"$tabIndex\" > \ |" >> "$mdFolder/docs/forPrioQuestionnaire.md.tmp"
tabIndex=$((tabIndex+1))
echo "| $spacesInLine | <label for=\"sat_n-$r\">Satisfaction if not fulfilled(--, -, 0, +, ++):</label> <input class=\"data-sat_n\" type=\"text\" id=\"sat_n-$r\" name=\"sat_n-$r\" size=\"2\" pattern=\"--|-|0|\+|\+\+\"oninput=\"updateInput(this)\" tabindex=\"$tabIndex\" > \ |" >> "$mdFolder/docs/forPrioQuestionnaire.md.tmp"
tabIndex=$((tabIndex+1))
echo "| $spacesInLine |$spacesForNewElements|" >> "$mdFolder/docs/forPrioQuestionnaire.md.tmp"
echo "| $line$thisLineExtraPadding |$spacesForNewElements|" >> "$mdFolder/docs/forPrioQuestionnaire.md.tmp"
elif $inReq ;then
if [[ "$line" =~ ^#.* ]] ;then
inReq=false
echo "$tableLineSep" >> "$mdFolder/docs/forPrioQuestionnaire.md.tmp"
echo "" >> "$mdFolder/docs/forPrioQuestionnaire.md.tmp"
echo "$line" >> "$mdFolder/docs/forPrioQuestionnaire.md.tmp"
else
echo "| $line$thisLineExtraPadding |$spacesForNewElements|" >> "$mdFolder/docs/forPrioQuestionnaire.md.tmp"
fi
else
echo "$line" >> "$mdFolder/docs/forPrioQuestionnaire.md.tmp"
fi
done < "$mdFolder/docs/forPrioQuestionnaire.md"
rm "$mdFolder/docs/forPrioQuestionnaire.md"
mv "$mdFolder/docs/forPrioQuestionnaire.md.tmp" "$mdFolder/docs/priorityQuestionnaire.md"
# Build HTML
echo "Building html."
htmlFolder="$buildDir/html"
snippetsFolder="$buildDir/snippets"
rm -rf "$htmlFolder"
rm -rf "$snippetsFolder"
mkdir --parents "$snippetsFolder"
IFS=$'\n'; set -f
for f in $(find "$mdFolder" -name '*.md'); do
unset IFS; set +f
title="$(head -n 1 "$f" | sed -e 's/^##* //')"
tail -n +2 "$f" > "$f.tmp"
mv "$f.tmp" "$f"
fInMdFolder="${f#$mdFolder/}"
fInMdFolderWoExt="${fInMdFolder%.md}"
fInMdFolderWoExtBasename="$(basename "$fInMdFolderWoExt")"
htmlFile="$htmlFolder/$fInMdFolderWoExt.html"
mkdir --parents "$(dirname "$htmlFile")"
mkdir --parents "$(dirname "$snippetsFolder/$fInMdFolder")"
additionalArgs=""
if [[ "$fInMdFolder" = "docs/"* ]] ;then
additionalArgs="$additionalArgs --lua-filter=./.scripts/open-link-in-new-tab.lua"
fi
if [ -f "./.scripts/static/$fInMdFolder/style.css" ] ;then
cp "./.scripts/static/$fInMdFolder/style.css" "$htmlFolder/${fInMdFolderWoExt}_style.css"
additionalArgs="$additionalArgs --css=${fInMdFolderWoExtBasename}_style.css"
fi
if [ -f "./.scripts/static/$fInMdFolder/script.js" ] ;then
cp "./.scripts/static/$fInMdFolder/script.js" "$htmlFolder/${fInMdFolderWoExt}_script.js"
echo "<script src=\"${fInMdFolderWoExtBasename}_script.js\"></script>" > "$snippetsFolder/${fInMdFolderWoExt}_script_include.md"
additionalArgs="$additionalArgs --include-after-body=$snippetsFolder/${fInMdFolderWoExt}_script_include.md"
fi
pandoc -s --metadata "title=$title" -f markdown+lists_without_preceding_blankline -t html5 -i "$f" -o "$htmlFile" --lua-filter=./.scripts/links-to-html.lua $additionalArgs
IFS=$'\n'; set -f
done
unset IFS; set +f
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment