Skip to content

Commit

Permalink
fixed ekalinin#13
Browse files Browse the repository at this point in the history
  • Loading branch information
ekalinin committed Jul 15, 2015
1 parent 21e1cf1 commit 1056956
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions gh-md-toc
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,21 @@ gh_toc(){
# It's need if TOC is generated for multiple documents.
#
gh_toc_grab() {
echo -e "$(awk -v "gh_url=$1" '/user-content-/ {
print sprintf("%*s", substr($NF, length($NF)-1, 1)*2, " ") "* [" substr($0, match($0, /a>.*<\/h/)+2, RLENGTH-5)"](" gh_url substr($0, match($0, "href=\".*\" ")+6, RLENGTH-8) ")"}' | sed 'y/+/ /; s/%/\\x/g')"
}
# find strings that corresponds to template
grep -E --null-data --text -o \
'<h[1-6]>\s*<a\s*id="user-content-[^"]*"\s*class="anchor"\s*href="[^"]*"[^>]*>\s*<span[^<*]*</span>\s*</a>.*?\s*</h' | \
# replace all new lines except last one
sed 's/h$/h\nendd/g' | tr '\n' ' ' | sed 's/h endd/\h\n/g' | \
# trim head spaces
sed 's/^\s*//' |
# remove code tags
sed 's/<code>//' | sed 's/<\/code>//' |
# now all rows are like:
# <h1> <a id="user-content-..." href="..."><span ...></span></a> ... </h
# format result line
echo -e "$(awk -v "gh_url=$1" '{
print sprintf("%*s", substr($1, 3, 1)*2, " ") "* [" substr($0, match($0, /a>.*<\/h/)+2, RLENGTH-5)"](" gh_url substr($0, match($0, "href=\".*\" ")+6, RLENGTH-8) ")"}' | sed 'y/+/ /; s/%/\\x/g')"
}

#
# Returns filename only from full path or url
Expand Down

0 comments on commit 1056956

Please sign in to comment.