Skip to content

Commit 495dfb3

Browse files
committed
Fix for issue#100 ("+"s in titles)
See: #100 « Plus signs in the toc's link texts (e.g: in "C++") are overzealously converted to whitespaces #100 »
1 parent 2e3c450 commit 495dfb3

File tree

3 files changed

+37
-5
lines changed

3 files changed

+37
-5
lines changed

gh-md-toc

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -197,24 +197,43 @@ gh_toc(){
197197
# It's need if TOC is generated for multiple documents.
198198
#
199199
gh_toc_grab() {
200+
common_awk_script='
201+
modified_href = ""
202+
split(href, chars, "")
203+
for (i=1;i <= length(href); i++) {
204+
c = chars[i]
205+
res = ""
206+
if (c == "+") {
207+
res = " "
208+
} else {
209+
if (c == "%") {
210+
res = "\\\\x"
211+
} else {
212+
res = c ""
213+
}
214+
}
215+
modified_href = modified_href res
216+
}
217+
print sprintf("%*s", level*3, " ") "* [" text "](" gh_url modified_href ")"
218+
'
200219
if [ `uname -s` == "OS/390" ]; then
201220
grepcmd="pcregrep -o"
202221
echoargs=""
203222
awkscript='{
204223
level = substr($0, length($0), 1)
205224
text = substr($0, match($0, /a>.*<\/h/)+2, RLENGTH-5)
206225
href = substr($0, match($0, "href=\"([^\"]+)?\"")+6, RLENGTH-7)
207-
print sprintf("%*s", level*3, " ") "* [" text "](" gh_url href ")"
208-
}'
226+
'"$common_awk_script"'
227+
}'
209228
else
210229
grepcmd="grep -Eo"
211230
echoargs="-e"
212231
awkscript='{
213232
level = substr($0, length($0), 1)
214233
text = substr($0, match($0, /a>.*<\/h/)+2, RLENGTH-5)
215234
href = substr($0, match($0, "href=\"[^\"]+?\"")+6, RLENGTH-7)
216-
print sprintf("%*s", level*3, " ") "* [" text "](" gh_url href ")"
217-
}'
235+
'"$common_awk_script"'
236+
}'
218237
fi
219238
href_regex='href=\"[^\"]+?\"'
220239

@@ -239,9 +258,11 @@ gh_toc_grab() {
239258
# format result line
240259
# * $0 - whole string
241260
# * last element of each row: "</hN" where N in (1,2,3,...)
242-
echo $echoargs "$(awk -v "gh_url=$1" "$awkscript" | sed 'y/+/ /; s/%/\\x/g')"
261+
echo $echoargs "$(awk -v "gh_url=$1" "$awkscript")"
243262
}
244263

264+
# perl -lpE 's/(\[[^\]]*\]\()(.*?)(\))/my ($pre, $in, $post)=($1, $2, $3) ; $in =~ s{\+}{ }g; $in =~ s{%}{\\x}g; $pre.$in.$post/ems')"
265+
245266
#
246267
# Returns filename only from full path or url
247268
#

tests/test_plussign.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# C vs C++
2+
3+
Blabla...
4+

tests/tests.bats

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,3 +160,10 @@ load test_helper
160160
assert_equal "${lines[5]}" " * [The command bar2 is better](#the-command-bar2-is-better)"
161161
assert_equal "${lines[6]}" " * [The command bar3 is the best](#the-command-bar3-is-the-best)"
162162
}
163+
164+
@test "TOC for text with plus signs, #100" {
165+
run $BATS_TEST_DIRNAME/../gh-md-toc tests/test_plussign.md
166+
assert_success
167+
168+
assert_equal "${lines[2]}" " * [C vs C++](#c-vs-c)"
169+
}

0 commit comments

Comments
 (0)