From 5c4bcbdf9f98b027318fd7ac652e353c96efc05d Mon Sep 17 00:00:00 2001 From: Eugene Kalinin Date: Sun, 4 Feb 2018 19:47:44 +0300 Subject: [PATCH] Auto TOC insert/update. Fix #41 --- README.md | 89 ++++++++++++++++++++++++++++++++++++++++++------ gh-md-toc | 47 +++++++++++++++++++++---- tests/tests.bats | 43 ++++++++++++----------- 3 files changed, 142 insertions(+), 37 deletions(-) diff --git a/README.md b/README.md index 6ab22c3..3b14093 100644 --- a/README.md +++ b/README.md @@ -27,17 +27,20 @@ absolutely without dependencies. Table of contents ================= - * [gh-md-toc](#gh-md-toc) - * [Table of contents](#table-of-contents) - * [Installation](#installation) - * [Usage](#usage) - * [STDIN](#stdin) - * [Local files](#local-files) - * [Remote files](#remote-files) - * [Multiple files](#multiple-files) - * [Combo](#combo) - * [Tests](#tests) - * [Dependency](#dependency) + + * [gh-md-toc](#gh-md-toc) + * [Table of contents](#table-of-contents) + * [Installation](#installation) + * [Usage](#usage) + * [STDIN](#stdin) + * [Local files](#local-files) + * [Remote files](#remote-files) + * [Multiple files](#multiple-files) + * [Combo](#combo) + * [Auto insert and update TOC](#auto-insert-and-update-toc) + * [Tests](#tests) + * [Dependency](#dependency) + Installation @@ -210,6 +213,70 @@ You can easily combine both ways: Created by [gh-md-toc](https://github.com/ekalinin/github-markdown-toc) ``` +Auto insert and update TOC +-------------------------- + +Just put into a file these two lines: + +``` + + +``` + +And run: + +```bash +$ ./gh-md-toc --insert README.test.md + +Table of Contents +================= + + * [gh-md-toc](#gh-md-toc) + * [Installation](#installation) + * [Usage](#usage) + * [STDIN](#stdin) + * [Local files](#local-files) + * [Remote files](#remote-files) + * [Multiple files](#multiple-files) + * [Combo](#combo) + * [Tests](#tests) + * [Dependency](#dependency) + +!! TOC was added into: 'README.test.md' +!! Origin version of the file: 'README.test.md.orig.2018-02-04_192655' +!! TOC added into a separate file: 'README.test.md.toc.2018-02-04_192655' + + +Created by [gh-md-toc](https://github.com/ekalinin/github-markdown-toc) +``` + +Now check the same file: + +```bash +➜ grep -A15 "<\!\-\-ts" README.test.md + + * [gh-md-toc](#gh-md-toc) + * [Table of contents](#table-of-contents) + * [Installation](#installation) + * [Usage](#usage) + * [STDIN](#stdin) + * [Local files](#local-files) + * [Remote files](#remote-files) + * [Multiple files](#multiple-files) + * [Combo](#combo) + * [Auto insert and update TOC](#auto-insert-and-update-toc) + * [Tests](#tests) + * [Dependency](#dependency) + + + + +``` + +Next time when your file will be changed just repeat the command (`./gh-md-toc +--insert ...`) and TOC will be refreshed again. + + Tests ===== diff --git a/gh-md-toc b/gh-md-toc index f6e6aba..e8ad72c 100755 --- a/gh-md-toc +++ b/gh-md-toc @@ -80,6 +80,7 @@ gh_toc(){ local gh_src=$1 local gh_src_copy=$1 local gh_ttl_docs=$2 + local need_replace=$3 if [ "$gh_src" = "" ]; then echo "Please, enter URL or local path for a README.md" @@ -99,8 +100,36 @@ gh_toc(){ if [ "$(gh_is_url "$gh_src")" == "yes" ]; then gh_toc_load "$gh_src" | gh_toc_grab "$gh_src_copy" + if [ "$need_replace" = "yes" ]; then + echo + echo "!! '$gh_src' is not a local file" + echo "!! Can't insert the TOC into it." + echo + fi else - gh_toc_md2html "$gh_src" | gh_toc_grab "$gh_src_copy" + local toc=`gh_toc_md2html "$gh_src" | gh_toc_grab "$gh_src_copy"` + echo "$toc" + if [ "$need_replace" = "yes" ]; then + local ts="\<\!--ts--\>" + local te="\<\!--te--\>" + local dt=`date +'%F_%H%M%S'` + local ext=".orig.${dt}" + local toc_path="${gh_src}.toc.${dt}" + local toc_footer="" + # http://fahdshariff.blogspot.ru/2012/12/sed-mutli-line-replacement-between-two.html + # clear old TOC + sed -i "${ext}" -e "/${ts}/,/${te}/ {//!d;}" "$gh_src" + # create toc file + echo "${toc}" > "${toc_path}" + echo -e "\n${toc_footer}\n" >> "$toc_path" + # insert toc file + sed -i "" -e "/${ts}/r ${toc_path}" "$gh_src" + echo + echo "!! TOC was added into: '$gh_src'" + echo "!! Origin version of the file: '${gh_src}${ext}'" + echo "!! TOC added into a separate file: '${toc_path}'" + echo + fi fi } @@ -141,15 +170,16 @@ gh_toc_get_filename() { # gh_toc_app() { local app_name="gh-md-toc" + local need_replace="no" if [ "$1" = '--help' ] || [ $# -eq 0 ] ; then echo "GitHub TOC generator ($app_name): $gh_toc_version" echo "" echo "Usage:" - echo " $app_name src [src] Create TOC for a README file (url or local path)" - echo " $app_name - Create TOC for markdown from STDIN" - echo " $app_name --help Show help" - echo " $app_name --version Show version" + echo " $app_name [--insert] src [src] Create TOC for a README file (url or local path)" + echo " $app_name - Create TOC for markdown from STDIN" + echo " $app_name --help Show help" + echo " $app_name --version Show version" return fi @@ -173,10 +203,15 @@ gh_toc_app() { return fi + if [ "$1" = '--insert' ]; then + need_replace="yes" + shift + fi + for md in "$@" do echo "" - gh_toc "$md" "$#" + gh_toc "$md" "$#" "$need_replace" done echo "" diff --git a/tests/tests.bats b/tests/tests.bats index cefa496..1bf14cb 100644 --- a/tests/tests.bats +++ b/tests/tests.bats @@ -18,9 +18,10 @@ load test_helper assert_equal "${lines[8]}" " * [Remote files](#remote-files)" assert_equal "${lines[9]}" " * [Multiple files](#multiple-files)" assert_equal "${lines[10]}" " * [Combo](#combo)" - assert_equal "${lines[11]}" " * [Tests](#tests)" - assert_equal "${lines[12]}" " * [Dependency](#dependency)" - assert_equal "${lines[13]}" "Created by [gh-md-toc](https://github.com/ekalinin/github-markdown-toc)" + assert_equal "${lines[11]}" " * [Auto insert and update TOC](#auto-insert-and-update-toc)" + assert_equal "${lines[12]}" " * [Tests](#tests)" + assert_equal "${lines[13]}" " * [Dependency](#dependency)" + assert_equal "${lines[14]}" "Created by [gh-md-toc](https://github.com/ekalinin/github-markdown-toc)" } @@ -52,13 +53,14 @@ load test_helper assert_equal "${lines[6]}" " * [Remote files](README.md#remote-files)" assert_equal "${lines[7]}" " * [Multiple files](README.md#multiple-files)" assert_equal "${lines[8]}" " * [Combo](README.md#combo)" - assert_equal "${lines[9]}" " * [Tests](README.md#tests)" - assert_equal "${lines[10]}" " * [Dependency](README.md#dependency)" - assert_equal "${lines[11]}" " * [sitemap.js](https://github.com/ekalinin/sitemap.js/blob/6bc3eb12c898c1037a35a11b2eb24ababdeb3580/README.md#sitemapjs)" - assert_equal "${lines[12]}" " * [Installation](https://github.com/ekalinin/sitemap.js/blob/6bc3eb12c898c1037a35a11b2eb24ababdeb3580/README.md#installation)" - assert_equal "${lines[13]}" " * [Usage](https://github.com/ekalinin/sitemap.js/blob/6bc3eb12c898c1037a35a11b2eb24ababdeb3580/README.md#usage)" - assert_equal "${lines[14]}" " * [License](https://github.com/ekalinin/sitemap.js/blob/6bc3eb12c898c1037a35a11b2eb24ababdeb3580/README.md#license)" - assert_equal "${lines[15]}" "Created by [gh-md-toc](https://github.com/ekalinin/github-markdown-toc)" + assert_equal "${lines[9]}" " * [Auto insert and update TOC](README.md#auto-insert-and-update-toc)" + assert_equal "${lines[10]}" " * [Tests](README.md#tests)" + assert_equal "${lines[11]}" " * [Dependency](README.md#dependency)" + assert_equal "${lines[12]}" " * [sitemap.js](https://github.com/ekalinin/sitemap.js/blob/6bc3eb12c898c1037a35a11b2eb24ababdeb3580/README.md#sitemapjs)" + assert_equal "${lines[13]}" " * [Installation](https://github.com/ekalinin/sitemap.js/blob/6bc3eb12c898c1037a35a11b2eb24ababdeb3580/README.md#installation)" + assert_equal "${lines[14]}" " * [Usage](https://github.com/ekalinin/sitemap.js/blob/6bc3eb12c898c1037a35a11b2eb24ababdeb3580/README.md#usage)" + assert_equal "${lines[15]}" " * [License](https://github.com/ekalinin/sitemap.js/blob/6bc3eb12c898c1037a35a11b2eb24ababdeb3580/README.md#license)" + assert_equal "${lines[16]}" "Created by [gh-md-toc](https://github.com/ekalinin/github-markdown-toc)" } @test "TOC for markdown from stdin" { @@ -74,8 +76,9 @@ load test_helper assert_equal "${lines[6]}" " * [Remote files](#remote-files)" assert_equal "${lines[7]}" " * [Multiple files](#multiple-files)" assert_equal "${lines[8]}" " * [Combo](#combo)" - assert_equal "${lines[9]}" " * [Tests](#tests)" - assert_equal "${lines[10]}" " * [Dependency](#dependency)" + assert_equal "${lines[9]}" " * [Auto insert and update TOC](#auto-insert-and-update-toc)" + assert_equal "${lines[10]}" " * [Tests](#tests)" + assert_equal "${lines[11]}" " * [Dependency](#dependency)" } } @@ -83,20 +86,20 @@ load test_helper run $BATS_TEST_DIRNAME/../gh-md-toc --help assert_success assert_equal "${lines[1]}" "Usage:" - assert_equal "${lines[2]}" " gh-md-toc src [src] Create TOC for a README file (url or local path)" - assert_equal "${lines[3]}" " gh-md-toc - Create TOC for markdown from STDIN" - assert_equal "${lines[4]}" " gh-md-toc --help Show help" - assert_equal "${lines[5]}" " gh-md-toc --version Show version" + assert_equal "${lines[2]}" " gh-md-toc [--insert] src [src] Create TOC for a README file (url or local path)" + assert_equal "${lines[3]}" " gh-md-toc - Create TOC for markdown from STDIN" + assert_equal "${lines[4]}" " gh-md-toc --help Show help" + assert_equal "${lines[5]}" " gh-md-toc --version Show version" } @test "no arguments" { run $BATS_TEST_DIRNAME/../gh-md-toc assert_success assert_equal "${lines[1]}" "Usage:" - assert_equal "${lines[2]}" " gh-md-toc src [src] Create TOC for a README file (url or local path)" - assert_equal "${lines[3]}" " gh-md-toc - Create TOC for markdown from STDIN" - assert_equal "${lines[4]}" " gh-md-toc --help Show help" - assert_equal "${lines[5]}" " gh-md-toc --version Show version" + assert_equal "${lines[2]}" " gh-md-toc [--insert] src [src] Create TOC for a README file (url or local path)" + assert_equal "${lines[3]}" " gh-md-toc - Create TOC for markdown from STDIN" + assert_equal "${lines[4]}" " gh-md-toc --help Show help" + assert_equal "${lines[5]}" " gh-md-toc --version Show version" } @test "--version" {