Skip to content

Commit

Permalink
Auto TOC insert/update. Fix #41
Browse files Browse the repository at this point in the history
  • Loading branch information
ekalinin committed Feb 4, 2018
1 parent f2c48b8 commit 5c4bcbd
Show file tree
Hide file tree
Showing 3 changed files with 142 additions and 37 deletions.
89 changes: 78 additions & 11 deletions README.md
Expand Up @@ -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)
<!--ts-->
* [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)
<!--te-->


Installation
Expand Down Expand Up @@ -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:
```
<!--ts-->
<!--te-->
```
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
<!--ts-->
* [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)
<!-- Added by: <your-user>, at: 2018-02-04T19:38+03:00 -->
<!--te-->
```
Next time when your file will be changed just repeat the command (`./gh-md-toc
--insert ...`) and TOC will be refreshed again.
Tests
=====
Expand Down
47 changes: 41 additions & 6 deletions gh-md-toc
Expand Up @@ -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"
Expand All @@ -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="<!-- Added by: `whoami`, at: `date --iso-8601='minutes'` -->"
# 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
}

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

Expand All @@ -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 ""
Expand Down
43 changes: 23 additions & 20 deletions tests/tests.bats
Expand Up @@ -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)"

}

Expand Down Expand Up @@ -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" {
Expand All @@ -74,29 +76,30 @@ 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)"
}
}

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

0 comments on commit 5c4bcbd

Please sign in to comment.