From 37b2cb72a07004ed9f667a69e8f343bc191890e2 Mon Sep 17 00:00:00 2001 From: jofas Date: Sun, 4 Aug 2024 16:11:50 +0200 Subject: [PATCH 1/2] --no-code-tag-removal flag --- gh-md-toc | 41 +++++++++++++++++++++++++++-------------- tests/tests.bats | 26 ++++++++++++++++++++------ 2 files changed, 47 insertions(+), 20 deletions(-) diff --git a/gh-md-toc b/gh-md-toc index 35239bf..3cfc958 100755 --- a/gh-md-toc +++ b/gh-md-toc @@ -121,6 +121,7 @@ gh_toc(){ local no_footer=$5 local indent=$6 local skip_header=$7 + local no_code_tag_removal=$8 if [ "$gh_src" = "" ]; then echo "Please, enter URL or local path for a README.md" @@ -139,7 +140,7 @@ gh_toc(){ fi if [ "$(gh_is_url "$gh_src")" == "yes" ]; then - gh_toc_load "$gh_src" | gh_toc_grab "$gh_src_copy" "$indent" + gh_toc_load "$gh_src" | gh_toc_grab "$gh_src_copy" "$indent" "$no_code_tag_removal" if [ "${PIPESTATUS[0]}" != "0" ]; then echo "Could not load remote document." echo "Please check your url or network connectivity" @@ -167,7 +168,7 @@ gh_toc(){ exit 1 fi local toc - toc=`echo "$rawhtml" | gh_toc_grab "$gh_src_copy" "$indent"` + toc=`echo "$rawhtml" | gh_toc_grab "$gh_src_copy" "$indent" "$no_code_tag_removal"` echo "$toc" if [ "$need_replace" = "yes" ]; then if grep -Fxq "" "$gh_src" && grep -Fxq "" "$gh_src"; then @@ -220,8 +221,10 @@ gh_toc(){ # $1 - a source url of document. # It's need if TOC is generated for multiple documents. # $2 - number of spaces used to indent. +# $3 - whether and tags should be removed from the TOC entries. # gh_toc_grab() { + local no_code_tag_removal=$3 href_regex="/href=\"[^\"]+?\"/" common_awk_script=' @@ -276,12 +279,16 @@ gh_toc_grab() { # find strings that corresponds to template $grepcmd '//g' | sed 's/<\/code>//g' | - # remove g-emoji sed 's/]*[^<]*<\/g-emoji> //g' | + # remove code tags or convert them to backticks if no code tag removal is specified + if [ "$no_code_tag_removal" = "no" ]; then + sed 's///g' | sed 's/<\/code>//g' + else + sed 's//`/g' | sed 's/<\/code>/`/g' + fi | + # now all rows are like: #

title

.. # format result line @@ -329,13 +336,14 @@ show_help() { echo " $app_name --version Show version" echo "" echo "Options:" - echo " --indent Set indent size. Default: 3." - echo " --insert Insert new TOC into original file. For local files only. Default: false." - echo " See https://github.com/ekalinin/github-markdown-toc/issues/41 for details." - echo " --no-backup Remove backup file. Set --insert as well. Default: false." - echo " --hide-footer Do not write date & author of the last TOC update. Set --insert as well. Default: false." - echo " --skip-header Hide entry of the topmost headlines. Default: false." - echo " See https://github.com/ekalinin/github-markdown-toc/issues/125 for details." + echo " --indent Set indent size. Default: 3." + echo " --insert Insert new TOC into original file. For local files only. Default: false." + echo " See https://github.com/ekalinin/github-markdown-toc/issues/41 for details." + echo " --no-backup Remove backup file. Set --insert as well. Default: false." + echo " --hide-footer Do not write date & author of the last TOC update. Set --insert as well. Default: false." + echo " --skip-header Hide entry of the topmost headlines. Default: false." + echo " See https://github.com/ekalinin/github-markdown-toc/issues/125 for details." + echo " --no-code-tag-removal Do not remove and tags from the TOC entries. Default: false." echo "" } @@ -345,6 +353,7 @@ show_help() { gh_toc_app() { local need_replace="no" local indent=3 + local no_code_tag_removal="no" if [ "$1" = '--help' ] || [ $# -eq 0 ] ; then show_help @@ -378,7 +387,7 @@ gh_toc_app() { while read -r input; do echo "$input" >> "$gh_tmp_md" done - gh_toc_md2html "$gh_tmp_md" | gh_toc_grab "" "$indent" + gh_toc_md2html "$gh_tmp_md" | gh_toc_grab "" "$indent" "$no_code_tag_removal" return fi @@ -404,11 +413,15 @@ gh_toc_app() { shift fi + if [ "$1" = '--no-code-tag-removal' ]; then + no_code_tag_removal="yes" + shift + fi for md in "$@" do echo "" - gh_toc "$md" "$#" "$need_replace" "$no_backup" "$no_footer" "$indent" "$skip_header" + gh_toc "$md" "$#" "$need_replace" "$no_backup" "$no_footer" "$indent" "$skip_header" "$no_code_tag_removal" done echo "" diff --git a/tests/tests.bats b/tests/tests.bats index c7b3b75..403d110 100755 --- a/tests/tests.bats +++ b/tests/tests.bats @@ -54,6 +54,7 @@ load test_helper assert_equal "${lines[17]}" "" } + # @test "TOC for remote README.md" { # run $BATS_TEST_DIRNAME/../gh-md-toc https://github.com/ekalinin/sitemap.js/blob/6bc3eb12c898c1037a35a11b2eb24ababdeb3580/README.md # assert_success @@ -125,12 +126,14 @@ test_help() { assert_equal "${lines[4]}" " gh-md-toc --help Show help" assert_equal "${lines[5]}" " gh-md-toc --version Show version" assert_equal "${lines[6]}" "Options:" - assert_equal "${lines[7]}" " --indent Set indent size. Default: 3." - assert_equal "${lines[8]}" " --insert Insert new TOC into original file. For local files only. Default: false." - assert_equal "${lines[10]}" " --no-backup Remove backup file. Set --insert as well. Default: false." - assert_equal "${lines[11]}" " --hide-footer Do not write date & author of the last TOC update. Set --insert as well. Default: false." - assert_equal "${lines[12]}" " --skip-header Hide entry of the topmost headlines. Default: false." - assert_equal "${#lines[@]}" "14" + assert_equal "${lines[7]}" " --indent Set indent size. Default: 3." + assert_equal "${lines[8]}" " --insert Insert new TOC into original file. For local files only. Default: false." + assert_equal "${lines[10]}" " --no-backup Remove backup file. Set --insert as well. Default: false." + assert_equal "${lines[11]}" " --hide-footer Do not write date & author of the last TOC update. Set --insert as well. Default: false." + assert_equal "${lines[12]}" " --skip-header Hide entry of the topmost headlines. Default: false." + assert_equal "${lines[13]}" " See https://github.com/ekalinin/github-markdown-toc/issues/125 for details." + assert_equal "${lines[14]}" " --no-code-tag-removal Do not remove and tags from the TOC entries. Default: false." + assert_equal "${#lines[@]}" "15" } @test "--help" { @@ -205,6 +208,17 @@ test_help() { assert_equal "${lines[6]}" " * [The command bar3 is the best](#the-command-bar3-is-the-best)" } +@test "TOC for text with backquote with `--no-code-tag-removal`" { + run $BATS_TEST_DIRNAME/../gh-md-toc --no-code-tag-removal tests/test\ directory/test_backquote.md + assert_success + + assert_equal "${lines[2]}" '* [The command `foo1`](#the-command-foo1)' + assert_equal "${lines[3]}" ' * [The command `foo2` is better](#the-command-foo2-is-better)' + assert_equal "${lines[4]}" '* [The command `bar1`](#the-command-bar1)' + assert_equal "${lines[5]}" ' * [The command `bar2` is better](#the-command-bar2-is-better)' + assert_equal "${lines[6]}" ' * [The command `bar3` is the best](#the-command-bar3-is-the-best)' +} + @test "TOC for text with plus signs, #100" { run $BATS_TEST_DIRNAME/../gh-md-toc tests/test\ directory/test_plussign.md assert_success From b20bf8f3bcd59311c0b1cfd5d067ce6b23462052 Mon Sep 17 00:00:00 2001 From: jofas Date: Sun, 4 Aug 2024 16:50:52 +0200 Subject: [PATCH 2/2] enhanced help message for --no-code-tag-removal flag --- gh-md-toc | 4 ++-- tests/tests.bats | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/gh-md-toc b/gh-md-toc index 3cfc958..6e280f4 100755 --- a/gh-md-toc +++ b/gh-md-toc @@ -221,7 +221,7 @@ gh_toc(){ # $1 - a source url of document. # It's need if TOC is generated for multiple documents. # $2 - number of spaces used to indent. -# $3 - whether and tags should be removed from the TOC entries. +# $3 - whether backticks should be removed from the TOC entries. # gh_toc_grab() { local no_code_tag_removal=$3 @@ -343,7 +343,7 @@ show_help() { echo " --hide-footer Do not write date & author of the last TOC update. Set --insert as well. Default: false." echo " --skip-header Hide entry of the topmost headlines. Default: false." echo " See https://github.com/ekalinin/github-markdown-toc/issues/125 for details." - echo " --no-code-tag-removal Do not remove and tags from the TOC entries. Default: false." + echo " --no-code-tag-removal Do not remove backticks (\`) from the TOC entries. Default: false." echo "" } diff --git a/tests/tests.bats b/tests/tests.bats index 403d110..5d4e84c 100755 --- a/tests/tests.bats +++ b/tests/tests.bats @@ -132,7 +132,7 @@ test_help() { assert_equal "${lines[11]}" " --hide-footer Do not write date & author of the last TOC update. Set --insert as well. Default: false." assert_equal "${lines[12]}" " --skip-header Hide entry of the topmost headlines. Default: false." assert_equal "${lines[13]}" " See https://github.com/ekalinin/github-markdown-toc/issues/125 for details." - assert_equal "${lines[14]}" " --no-code-tag-removal Do not remove and tags from the TOC entries. Default: false." + assert_equal "${lines[14]}" " --no-code-tag-removal Do not remove backticks (\`) from the TOC entries. Default: false." assert_equal "${#lines[@]}" "15" } @@ -208,7 +208,7 @@ test_help() { assert_equal "${lines[6]}" " * [The command bar3 is the best](#the-command-bar3-is-the-best)" } -@test "TOC for text with backquote with `--no-code-tag-removal`" { +@test "TOC for text with backquote with --no-code-tag-removal" { run $BATS_TEST_DIRNAME/../gh-md-toc --no-code-tag-removal tests/test\ directory/test_backquote.md assert_success