Improvements to generate_readme.sh

This commit is contained in:
labbots
2020-06-07 04:58:30 +01:00
parent 8fb2ac4974
commit a9a1970ac7

View File

@@ -192,10 +192,9 @@ _generate_toc() {
# grep: filter header candidates to be included in toc
# sed: remove the ignored headers (case: minlevel greater than one) to avoid unnecessary spacing later in level variable assignment
done <<< "$(grep -E "^#{${MINLEVEL},${MAXLEVEL}} " "${1}" | tr -d '\r' | sed "s/^#\{$((${MINLEVEL} - 1))\}//g")"
done <<< "$(grep -E "^#{${MINLEVEL},${MAXLEVEL}} " "${1}" | tr -d '\r' | sed "s/^#\{$((MINLEVEL - 1))\}//g")"
# when in toc we have two `--` quit one
output="${output//--*/-}"
echo "$output"
}
@@ -205,18 +204,16 @@ _insert_toc_to_file() {
declare source_markdown toc_text start_toc info_toc end_toc utext_ampersand utext_slash
source_markdown="${1}"
toc_text="${2}"
start_toc="<!-- START ${SCRIPT_FILE} generated TOC please keep comment here to allow auto update -->"
info_toc="<!-- DO NOT EDIT THIS SECTION, INSTEAD RE-RUN ${SCRIPT_FILE} TO UPDATE -->"
end_toc="<!-- END ${SCRIPT_FILE} generated TOC please keep comment here to allow auto update -->"
toc_block="$start_toc\n$info_toc\n## Table of Contents\n\n$toc_text\n$end_toc"
# temporary replace of '/' (confused with separator of substitutions) and '&' (confused with match regex symbol) to run the special sed command
utext_ampersand="id8234923000230gzz"
utext_slash="id9992384923423gzz"
toc_block="$(echo "${toc_block}" | sed "s,\&,${utext_ampersand},g")"
toc_block="$(echo "${toc_block}" | sed "s,\/,${utext_slash},g")"
toc_block="${toc_block//\&/${utext_ampersand}}"
toc_block="${toc_block//\//${utext_slash}}"
# search multiline toc block -> https://stackoverflow.com/questions/2686147/how-to-find-patterns-across-multiple-lines-using-grep/2686705
# grep color for debugging -> https://superuser.com/questions/914856/grep-display-all-output-but-highlight-search-matches
@@ -238,14 +235,14 @@ _insert_toc_to_file() {
}
_process_toc() {
declare toc_temp_file source_markdown
declare toc_temp_file source_markdown level
source_markdown="${1}"
toc_temp_file=$(_setup_tempfile)
sed '/```/,/```/d' "${source_markdown}" > "${toc_temp_file}"
declare level=$MINLEVEL
level=$MINLEVEL
while [[ $(grep -Ec "^#{$level} " "${toc_temp_file}") -le 1 ]]; do
level=$((level + 1))
done
@@ -263,7 +260,7 @@ main() {
_setup_arguments "${@}"
_process_sh_files "${SOURCE_MARKDOWN}" "${SOURCE_SCRIPT_DIR}"
_process_toc "${SOURCE_MARKDOWN}" "${MINLEVEL}" "${MAXLEVEL}"
_process_toc "${SOURCE_MARKDOWN}"
}
main "${@}"