Skip to content

Commit 5c4bcbd

Browse files
committed
Auto TOC insert/update. Fix #41
1 parent f2c48b8 commit 5c4bcbd

File tree

3 files changed

+142
-37
lines changed

3 files changed

+142
-37
lines changed

README.md

Lines changed: 78 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,20 @@ absolutely without dependencies.
2727
Table of contents
2828
=================
2929

30-
* [gh-md-toc](#gh-md-toc)
31-
* [Table of contents](#table-of-contents)
32-
* [Installation](#installation)
33-
* [Usage](#usage)
34-
* [STDIN](#stdin)
35-
* [Local files](#local-files)
36-
* [Remote files](#remote-files)
37-
* [Multiple files](#multiple-files)
38-
* [Combo](#combo)
39-
* [Tests](#tests)
40-
* [Dependency](#dependency)
30+
<!--ts-->
31+
* [gh-md-toc](#gh-md-toc)
32+
* [Table of contents](#table-of-contents)
33+
* [Installation](#installation)
34+
* [Usage](#usage)
35+
* [STDIN](#stdin)
36+
* [Local files](#local-files)
37+
* [Remote files](#remote-files)
38+
* [Multiple files](#multiple-files)
39+
* [Combo](#combo)
40+
* [Auto insert and update TOC](#auto-insert-and-update-toc)
41+
* [Tests](#tests)
42+
* [Dependency](#dependency)
43+
<!--te-->
4144

4245

4346
Installation
@@ -210,6 +213,70 @@ You can easily combine both ways:
210213
Created by [gh-md-toc](https://github.com/ekalinin/github-markdown-toc)
211214
```
212215
216+
Auto insert and update TOC
217+
--------------------------
218+
219+
Just put into a file these two lines:
220+
221+
```
222+
<!--ts-->
223+
<!--te-->
224+
```
225+
226+
And run:
227+
228+
```bash
229+
$ ./gh-md-toc --insert README.test.md
230+
231+
Table of Contents
232+
=================
233+
234+
* [gh-md-toc](#gh-md-toc)
235+
* [Installation](#installation)
236+
* [Usage](#usage)
237+
* [STDIN](#stdin)
238+
* [Local files](#local-files)
239+
* [Remote files](#remote-files)
240+
* [Multiple files](#multiple-files)
241+
* [Combo](#combo)
242+
* [Tests](#tests)
243+
* [Dependency](#dependency)
244+
245+
!! TOC was added into: 'README.test.md'
246+
!! Origin version of the file: 'README.test.md.orig.2018-02-04_192655'
247+
!! TOC added into a separate file: 'README.test.md.toc.2018-02-04_192655'
248+
249+
250+
Created by [gh-md-toc](https://github.com/ekalinin/github-markdown-toc)
251+
```
252+
253+
Now check the same file:
254+
255+
```bash
256+
➜ grep -A15 "<\!\-\-ts" README.test.md
257+
<!--ts-->
258+
* [gh-md-toc](#gh-md-toc)
259+
* [Table of contents](#table-of-contents)
260+
* [Installation](#installation)
261+
* [Usage](#usage)
262+
* [STDIN](#stdin)
263+
* [Local files](#local-files)
264+
* [Remote files](#remote-files)
265+
* [Multiple files](#multiple-files)
266+
* [Combo](#combo)
267+
* [Auto insert and update TOC](#auto-insert-and-update-toc)
268+
* [Tests](#tests)
269+
* [Dependency](#dependency)
270+
271+
<!-- Added by: <your-user>, at: 2018-02-04T19:38+03:00 -->
272+
273+
<!--te-->
274+
```
275+
276+
Next time when your file will be changed just repeat the command (`./gh-md-toc
277+
--insert ...`) and TOC will be refreshed again.
278+
279+
213280
Tests
214281
=====
215282

gh-md-toc

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ gh_toc(){
8080
local gh_src=$1
8181
local gh_src_copy=$1
8282
local gh_ttl_docs=$2
83+
local need_replace=$3
8384

8485
if [ "$gh_src" = "" ]; then
8586
echo "Please, enter URL or local path for a README.md"
@@ -99,8 +100,36 @@ gh_toc(){
99100

100101
if [ "$(gh_is_url "$gh_src")" == "yes" ]; then
101102
gh_toc_load "$gh_src" | gh_toc_grab "$gh_src_copy"
103+
if [ "$need_replace" = "yes" ]; then
104+
echo
105+
echo "!! '$gh_src' is not a local file"
106+
echo "!! Can't insert the TOC into it."
107+
echo
108+
fi
102109
else
103-
gh_toc_md2html "$gh_src" | gh_toc_grab "$gh_src_copy"
110+
local toc=`gh_toc_md2html "$gh_src" | gh_toc_grab "$gh_src_copy"`
111+
echo "$toc"
112+
if [ "$need_replace" = "yes" ]; then
113+
local ts="\<\!--ts--\>"
114+
local te="\<\!--te--\>"
115+
local dt=`date +'%F_%H%M%S'`
116+
local ext=".orig.${dt}"
117+
local toc_path="${gh_src}.toc.${dt}"
118+
local toc_footer="<!-- Added by: `whoami`, at: `date --iso-8601='minutes'` -->"
119+
# http://fahdshariff.blogspot.ru/2012/12/sed-mutli-line-replacement-between-two.html
120+
# clear old TOC
121+
sed -i "${ext}" -e "/${ts}/,/${te}/ {//!d;}" "$gh_src"
122+
# create toc file
123+
echo "${toc}" > "${toc_path}"
124+
echo -e "\n${toc_footer}\n" >> "$toc_path"
125+
# insert toc file
126+
sed -i "" -e "/${ts}/r ${toc_path}" "$gh_src"
127+
echo
128+
echo "!! TOC was added into: '$gh_src'"
129+
echo "!! Origin version of the file: '${gh_src}${ext}'"
130+
echo "!! TOC added into a separate file: '${toc_path}'"
131+
echo
132+
fi
104133
fi
105134
}
106135

@@ -141,15 +170,16 @@ gh_toc_get_filename() {
141170
#
142171
gh_toc_app() {
143172
local app_name="gh-md-toc"
173+
local need_replace="no"
144174

145175
if [ "$1" = '--help' ] || [ $# -eq 0 ] ; then
146176
echo "GitHub TOC generator ($app_name): $gh_toc_version"
147177
echo ""
148178
echo "Usage:"
149-
echo " $app_name src [src] Create TOC for a README file (url or local path)"
150-
echo " $app_name - Create TOC for markdown from STDIN"
151-
echo " $app_name --help Show help"
152-
echo " $app_name --version Show version"
179+
echo " $app_name [--insert] src [src] Create TOC for a README file (url or local path)"
180+
echo " $app_name - Create TOC for markdown from STDIN"
181+
echo " $app_name --help Show help"
182+
echo " $app_name --version Show version"
153183
return
154184
fi
155185

@@ -173,10 +203,15 @@ gh_toc_app() {
173203
return
174204
fi
175205

206+
if [ "$1" = '--insert' ]; then
207+
need_replace="yes"
208+
shift
209+
fi
210+
176211
for md in "$@"
177212
do
178213
echo ""
179-
gh_toc "$md" "$#"
214+
gh_toc "$md" "$#" "$need_replace"
180215
done
181216

182217
echo ""

tests/tests.bats

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,10 @@ load test_helper
1818
assert_equal "${lines[8]}" " * [Remote files](#remote-files)"
1919
assert_equal "${lines[9]}" " * [Multiple files](#multiple-files)"
2020
assert_equal "${lines[10]}" " * [Combo](#combo)"
21-
assert_equal "${lines[11]}" " * [Tests](#tests)"
22-
assert_equal "${lines[12]}" " * [Dependency](#dependency)"
23-
assert_equal "${lines[13]}" "Created by [gh-md-toc](https://github.com/ekalinin/github-markdown-toc)"
21+
assert_equal "${lines[11]}" " * [Auto insert and update TOC](#auto-insert-and-update-toc)"
22+
assert_equal "${lines[12]}" " * [Tests](#tests)"
23+
assert_equal "${lines[13]}" " * [Dependency](#dependency)"
24+
assert_equal "${lines[14]}" "Created by [gh-md-toc](https://github.com/ekalinin/github-markdown-toc)"
2425

2526
}
2627

@@ -52,13 +53,14 @@ load test_helper
5253
assert_equal "${lines[6]}" " * [Remote files](README.md#remote-files)"
5354
assert_equal "${lines[7]}" " * [Multiple files](README.md#multiple-files)"
5455
assert_equal "${lines[8]}" " * [Combo](README.md#combo)"
55-
assert_equal "${lines[9]}" " * [Tests](README.md#tests)"
56-
assert_equal "${lines[10]}" " * [Dependency](README.md#dependency)"
57-
assert_equal "${lines[11]}" " * [sitemap.js](https://github.com/ekalinin/sitemap.js/blob/6bc3eb12c898c1037a35a11b2eb24ababdeb3580/README.md#sitemapjs)"
58-
assert_equal "${lines[12]}" " * [Installation](https://github.com/ekalinin/sitemap.js/blob/6bc3eb12c898c1037a35a11b2eb24ababdeb3580/README.md#installation)"
59-
assert_equal "${lines[13]}" " * [Usage](https://github.com/ekalinin/sitemap.js/blob/6bc3eb12c898c1037a35a11b2eb24ababdeb3580/README.md#usage)"
60-
assert_equal "${lines[14]}" " * [License](https://github.com/ekalinin/sitemap.js/blob/6bc3eb12c898c1037a35a11b2eb24ababdeb3580/README.md#license)"
61-
assert_equal "${lines[15]}" "Created by [gh-md-toc](https://github.com/ekalinin/github-markdown-toc)"
56+
assert_equal "${lines[9]}" " * [Auto insert and update TOC](README.md#auto-insert-and-update-toc)"
57+
assert_equal "${lines[10]}" " * [Tests](README.md#tests)"
58+
assert_equal "${lines[11]}" " * [Dependency](README.md#dependency)"
59+
assert_equal "${lines[12]}" " * [sitemap.js](https://github.com/ekalinin/sitemap.js/blob/6bc3eb12c898c1037a35a11b2eb24ababdeb3580/README.md#sitemapjs)"
60+
assert_equal "${lines[13]}" " * [Installation](https://github.com/ekalinin/sitemap.js/blob/6bc3eb12c898c1037a35a11b2eb24ababdeb3580/README.md#installation)"
61+
assert_equal "${lines[14]}" " * [Usage](https://github.com/ekalinin/sitemap.js/blob/6bc3eb12c898c1037a35a11b2eb24ababdeb3580/README.md#usage)"
62+
assert_equal "${lines[15]}" " * [License](https://github.com/ekalinin/sitemap.js/blob/6bc3eb12c898c1037a35a11b2eb24ababdeb3580/README.md#license)"
63+
assert_equal "${lines[16]}" "Created by [gh-md-toc](https://github.com/ekalinin/github-markdown-toc)"
6264
}
6365

6466
@test "TOC for markdown from stdin" {
@@ -74,29 +76,30 @@ load test_helper
7476
assert_equal "${lines[6]}" " * [Remote files](#remote-files)"
7577
assert_equal "${lines[7]}" " * [Multiple files](#multiple-files)"
7678
assert_equal "${lines[8]}" " * [Combo](#combo)"
77-
assert_equal "${lines[9]}" " * [Tests](#tests)"
78-
assert_equal "${lines[10]}" " * [Dependency](#dependency)"
79+
assert_equal "${lines[9]}" " * [Auto insert and update TOC](#auto-insert-and-update-toc)"
80+
assert_equal "${lines[10]}" " * [Tests](#tests)"
81+
assert_equal "${lines[11]}" " * [Dependency](#dependency)"
7982
}
8083
}
8184

8285
@test "--help" {
8386
run $BATS_TEST_DIRNAME/../gh-md-toc --help
8487
assert_success
8588
assert_equal "${lines[1]}" "Usage:"
86-
assert_equal "${lines[2]}" " gh-md-toc src [src] Create TOC for a README file (url or local path)"
87-
assert_equal "${lines[3]}" " gh-md-toc - Create TOC for markdown from STDIN"
88-
assert_equal "${lines[4]}" " gh-md-toc --help Show help"
89-
assert_equal "${lines[5]}" " gh-md-toc --version Show version"
89+
assert_equal "${lines[2]}" " gh-md-toc [--insert] src [src] Create TOC for a README file (url or local path)"
90+
assert_equal "${lines[3]}" " gh-md-toc - Create TOC for markdown from STDIN"
91+
assert_equal "${lines[4]}" " gh-md-toc --help Show help"
92+
assert_equal "${lines[5]}" " gh-md-toc --version Show version"
9093
}
9194

9295
@test "no arguments" {
9396
run $BATS_TEST_DIRNAME/../gh-md-toc
9497
assert_success
9598
assert_equal "${lines[1]}" "Usage:"
96-
assert_equal "${lines[2]}" " gh-md-toc src [src] Create TOC for a README file (url or local path)"
97-
assert_equal "${lines[3]}" " gh-md-toc - Create TOC for markdown from STDIN"
98-
assert_equal "${lines[4]}" " gh-md-toc --help Show help"
99-
assert_equal "${lines[5]}" " gh-md-toc --version Show version"
99+
assert_equal "${lines[2]}" " gh-md-toc [--insert] src [src] Create TOC for a README file (url or local path)"
100+
assert_equal "${lines[3]}" " gh-md-toc - Create TOC for markdown from STDIN"
101+
assert_equal "${lines[4]}" " gh-md-toc --help Show help"
102+
assert_equal "${lines[5]}" " gh-md-toc --version Show version"
100103
}
101104

102105
@test "--version" {

0 commit comments

Comments
 (0)