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

Implementation for --no-code-tag-removal flag #154

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
41 changes: 27 additions & 14 deletions gh-md-toc
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand Down Expand Up @@ -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 "<!--ts-->" "$gh_src" && grep -Fxq "<!--te-->" "$gh_src"; then
Expand Down Expand Up @@ -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 backticks should be removed from the TOC entries.
#
gh_toc_grab() {
local no_code_tag_removal=$3

href_regex="/href=\"[^\"]+?\"/"
common_awk_script='
Expand Down Expand Up @@ -276,12 +279,16 @@ gh_toc_grab() {
# find strings that corresponds to template
$grepcmd '<h.*class="heading-element".*</a' |

# remove code tags
sed 's/<code>//g' | sed 's/<\/code>//g' |

# remove g-emoji
sed 's/<g-emoji[^>]*[^<]*<\/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/<code>//g' | sed 's/<\/code>//g'
else
sed 's/<code>/`/g' | sed 's/<\/code>/`/g'
fi |

# now all rows are like:
# <h1 class="heading-element">title</h1><a href="..."><span>..</span></a>
# format result line
Expand Down Expand Up @@ -329,13 +336,14 @@ show_help() {
echo " $app_name --version Show version"
echo ""
echo "Options:"
echo " --indent <NUM> 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 <NUM> 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 backticks (\`) from the TOC entries. Default: false."
echo ""
}

Expand All @@ -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
Expand Down Expand Up @@ -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

Expand All @@ -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 ""
Expand Down
26 changes: 20 additions & 6 deletions tests/tests.bats
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ load test_helper
assert_equal "${lines[17]}" "<!-- Created by https://github.com/ekalinin/github-markdown-toc -->"

}

# @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
Expand Down Expand Up @@ -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 <NUM> 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 <NUM> 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 backticks (\`) from the TOC entries. Default: false."
assert_equal "${#lines[@]}" "15"
}

@test "--help" {
Expand Down Expand Up @@ -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
Expand Down
Loading