From 495dfb3ebaf6db4c9c7fbea2e5266f43a785b40c Mon Sep 17 00:00:00 2001 From: Shlomi Fish Date: Sat, 24 Oct 2020 15:43:46 +0300 Subject: [PATCH] Fix for issue#100 ("+"s in titles) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit See: https://github.com/ekalinin/github-markdown-toc/issues/100 « Plus signs in the toc's link texts (e.g: in "C++") are overzealously converted to whitespaces #100 » --- gh-md-toc | 31 ++++++++++++++++++++++++++----- tests/test_plussign.md | 4 ++++ tests/tests.bats | 7 +++++++ 3 files changed, 37 insertions(+), 5 deletions(-) create mode 100644 tests/test_plussign.md diff --git a/gh-md-toc b/gh-md-toc index a3e7948..2be2e5f 100755 --- a/gh-md-toc +++ b/gh-md-toc @@ -197,6 +197,25 @@ gh_toc(){ # It's need if TOC is generated for multiple documents. # gh_toc_grab() { + common_awk_script=' + modified_href = "" + split(href, chars, "") + for (i=1;i <= length(href); i++) { + c = chars[i] + res = "" + if (c == "+") { + res = " " + } else { + if (c == "%") { + res = "\\\\x" + } else { + res = c "" + } + } + modified_href = modified_href res + } + print sprintf("%*s", level*3, " ") "* [" text "](" gh_url modified_href ")" + ' if [ `uname -s` == "OS/390" ]; then grepcmd="pcregrep -o" echoargs="" @@ -204,8 +223,8 @@ gh_toc_grab() { level = substr($0, length($0), 1) text = substr($0, match($0, /a>.*<\/h/)+2, RLENGTH-5) href = substr($0, match($0, "href=\"([^\"]+)?\"")+6, RLENGTH-7) - print sprintf("%*s", level*3, " ") "* [" text "](" gh_url href ")" - }' + '"$common_awk_script"' + }' else grepcmd="grep -Eo" echoargs="-e" @@ -213,8 +232,8 @@ gh_toc_grab() { level = substr($0, length($0), 1) text = substr($0, match($0, /a>.*<\/h/)+2, RLENGTH-5) href = substr($0, match($0, "href=\"[^\"]+?\"")+6, RLENGTH-7) - print sprintf("%*s", level*3, " ") "* [" text "](" gh_url href ")" - }' + '"$common_awk_script"' + }' fi href_regex='href=\"[^\"]+?\"' @@ -239,9 +258,11 @@ gh_toc_grab() { # format result line # * $0 - whole string # * last element of each row: "