Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for issue#100 ("+"s in titles) #104

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 26 additions & 5 deletions gh-md-toc
Original file line number Diff line number Diff line change
Expand Up @@ -197,24 +197,43 @@ 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=""
awkscript='{
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"
awkscript='{
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=\"[^\"]+?\"'

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

# perl -lpE 's/(\[[^\]]*\]\()(.*?)(\))/my ($pre, $in, $post)=($1, $2, $3) ; $in =~ s{\+}{ }g; $in =~ s{%}{\\x}g; $pre.$in.$post/ems')"

#
# Returns filename only from full path or url
#
Expand Down
4 changes: 4 additions & 0 deletions tests/test_plussign.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# C vs C++

Blabla...

7 changes: 7 additions & 0 deletions tests/tests.bats
Original file line number Diff line number Diff line change
Expand Up @@ -160,3 +160,10 @@ load test_helper
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_plussign.md
assert_success

assert_equal "${lines[2]}" " * [C vs C++](#c-vs-c)"
}