From a9a1970ac72594c3e2fb03f3743b6a2be9a34287 Mon Sep 17 00:00:00 2001 From: labbots Date: Sun, 7 Jun 2020 04:58:30 +0100 Subject: [PATCH] Improvements to generate_readme.sh --- bin/generate_readme.sh | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/bin/generate_readme.sh b/bin/generate_readme.sh index 641a702..95ca04f 100755 --- a/bin/generate_readme.sh +++ b/bin/generate_readme.sh @@ -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="" info_toc="" end_toc="" 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 "${@}"