Skip to content

Commit 9242567

Browse files
authored
Merge branch 'master' into issue-125-skip-headers
2 parents 22079e3 + 5659f2a commit 9242567

File tree

5 files changed

+63
-34
lines changed

5 files changed

+63
-34
lines changed

.github/workflows/ci.yml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
name: CI
2+
3+
on:
4+
push:
5+
branches: [ master ]
6+
pull_request:
7+
branches: [ master ]
8+
9+
# Allows you to run this workflow manually from the Actions tab
10+
workflow_dispatch:
11+
12+
jobs:
13+
build:
14+
runs-on: ubuntu-latest
15+
steps:
16+
- name: Setup bats
17+
uses: mig4/setup-bats@v1
18+
with:
19+
bats-version: 1.6.0
20+
21+
- name: Check out code
22+
uses: actions/checkout@v2
23+
24+
- name: Run tests
25+
run: make test

.travis.yml

Lines changed: 0 additions & 7 deletions
This file was deleted.

README.md

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
gh-md-toc
22
=========
33

4+
[![CI](https://github.com/ekalinin/github-markdown-toc/actions/workflows/ci.yml/badge.svg?branch=master)](https://github.com/ekalinin/github-markdown-toc/actions/workflows/ci.yml)
5+
![GitHub release (latest by date)](https://img.shields.io/github/v/release/ekalinin/github-markdown-toc)
6+
47
gh-md-toc — is for you if you **want to generate TOC** (Table Of Content) for a README.md or
58
a GitHub wiki page **without installing additional software**.
69

@@ -22,8 +25,6 @@ better to use a golang based implementation:
2225
It's more solid, reliable and with ability of a parallel processing. And
2326
absolutely without dependencies.
2427

25-
[![Build Status](https://travis-ci.org/ekalinin/github-markdown-toc.svg?branch=master)](https://travis-ci.org/ekalinin/github-markdown-toc)
26-
2728
Table of contents
2829
=================
2930

@@ -229,7 +230,7 @@ You can easily combine both ways:
229230
* [Usage](https://github.com/ekalinin/sitemap.js/blob/master/README.md#usage)
230231
* [License](https://github.com/ekalinin/sitemap.js/blob/master/README.md#license)
231232
232-
Created by [gh-md-toc](https://github.com/ekalinin/github-markdown-toc)
233+
<!-- Created by https://github.com/ekalinin/github-markdown-toc -->
233234
```
234235
235236
Auto insert and update TOC
@@ -266,7 +267,7 @@ Table of Contents
266267
!! TOC added into a separate file: 'README.test.md.toc.2018-02-04_192655'
267268
268269
269-
Created by [gh-md-toc](https://github.com/ekalinin/github-markdown-toc)
270+
<!-- Created by https://github.com/ekalinin/github-markdown-toc -->
270271
```
271272
272273
Now check the same file:
@@ -359,9 +360,10 @@ jobs:
359360
steps:
360361
- uses: actions/checkout@v2
361362
- run: |
362-
curl https://raw.githubusercontent.com/ekalinin/github-markdown-toc/master/gh-md-toc -o gh-md-toc
363+
curl https://raw.githubusercontent.com/ekalinin/github-markdown-toc/0.8.0/gh-md-toc -o gh-md-toc
363364
chmod a+x gh-md-toc
364-
./gh-md-toc --insert --no-backup foo.md
365+
./gh-md-toc --insert --no-backup --hide-footer foo.md
366+
rm gh-md-toc
365367
- uses: stefanzweifel/git-auto-commit-action@v4
366368
with:
367369
commit_message: Auto update markdown TOC
@@ -370,7 +372,7 @@ jobs:
370372
Tests
371373
=====
372374
373-
Done with [bats](https://github.com/sstephenson/bats).
375+
Done with [bats](https://github.com/bats-core/bats-core).
374376
Useful articles:
375377
376378
* https://blog.engineyard.com/2014/bats-test-command-line-tools

gh-md-toc

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
# substr($0, length($0), 1)
1515
#
1616
# 3.2 Get level from 3.1 and insert corresponding number of spaces before '*':
17-
# sprintf("%*s", substr($0, length($0), 1)*3, " ")
17+
# sprintf("%*s", (level-1)*'"$nb_spaces"', "")
1818
#
1919
# 4. Find head's text and insert it inside "* [ ... ]":
2020
# substr($0, match($0, /a>.*<\/h/)+2, RLENGTH-5)
@@ -119,7 +119,8 @@ gh_toc(){
119119
local need_replace=$3
120120
local no_backup=$4
121121
local no_footer=$5
122-
local skip_header=$6
122+
local indent=$6
123+
local skip_header=$7
123124

124125
if [ "$gh_src" = "" ]; then
125126
echo "Please, enter URL or local path for a README.md"
@@ -138,7 +139,7 @@ gh_toc(){
138139
fi
139140

140141
if [ "$(gh_is_url "$gh_src")" == "yes" ]; then
141-
gh_toc_load "$gh_src" | gh_toc_grab "$gh_src_copy"
142+
gh_toc_load "$gh_src" | gh_toc_grab "$gh_src_copy" "$indent"
142143
if [ "${PIPESTATUS[0]}" != "0" ]; then
143144
echo "Could not load remote document."
144145
echo "Please check your url or network connectivity"
@@ -164,7 +165,7 @@ gh_toc(){
164165
echo "or place GitHub auth token here: ${TOKEN_FILE}"
165166
exit 1
166167
fi
167-
local toc=`echo "$rawhtml" | gh_toc_grab "$gh_src_copy"`
168+
local toc=`echo "$rawhtml" | gh_toc_grab "$gh_src_copy" "$indent"`
168169
echo "$toc"
169170
if [ "$need_replace" = "yes" ]; then
170171
if grep -Fxq "<!--ts-->" $gh_src && grep -Fxq "<!--te-->" $gh_src; then
@@ -178,18 +179,19 @@ gh_toc(){
178179
local dt=`date +'%F_%H%M%S'`
179180
local ext=".orig.${dt}"
180181
local toc_path="${gh_src}.toc.${dt}"
182+
local toc_createdby="<!-- Created by https://github.com/ekalinin/github-markdown-toc -->"
181183
local toc_footer="<!-- Added by: `whoami`, at: `date` -->"
182184
# http://fahdshariff.blogspot.ru/2012/12/sed-mutli-line-replacement-between-two.html
183185
# clear old TOC
184186
sed -i${ext} "/${ts}/,/${te}/{//!d;}" "$gh_src"
185187
# create toc file
186188
echo "${toc}" > "${toc_path}"
187189
if [ "${no_footer}" != "yes" ]; then
188-
echo -e "\n${toc_footer}\n" >> "$toc_path"
190+
echo -e "\n${toc_createdby}\n${toc_footer}\n" >> "$toc_path"
189191
fi
190192

191193
# insert toc file
192-
if [[ "`uname`" == "Darwin" ]]; then
194+
if ! sed --version > /dev/null 2>&1; then
193195
sed -i "" "/${ts}/r ${toc_path}" "$gh_src"
194196
else
195197
sed -i "/${ts}/r ${toc_path}" "$gh_src"
@@ -212,7 +214,8 @@ gh_toc(){
212214
# Grabber of the TOC from rendered html
213215
#
214216
# $1 - a source url of document.
215-
# It's need if TOC is generated for multiple documents.
217+
# It's need if TOC is generated for multiple documents.
218+
# $2 - number of spaces used to indent.
216219
#
217220
gh_toc_grab() {
218221
common_awk_script='
@@ -232,7 +235,7 @@ gh_toc_grab() {
232235
}
233236
modified_href = modified_href res
234237
}
235-
print sprintf("%*s", (level-1)*3, "") "* [" text "](" gh_url modified_href ")"
238+
print sprintf("%*s", (level-1)*'"$2"', "") "* [" text "](" gh_url modified_href ")"
236239
'
237240
if [ `uname -s` == "OS/390" ]; then
238241
grepcmd="pcregrep -o"
@@ -293,14 +296,15 @@ gh_toc_get_filename() {
293296
#
294297
gh_toc_app() {
295298
local need_replace="no"
299+
local indent=3
296300

297301
if [ "$1" = '--help' ] || [ $# -eq 0 ] ; then
298302
local app_name=$(basename "$0")
299303
echo "GitHub TOC generator ($app_name): $gh_toc_version"
300304
echo ""
301305
echo "Usage:"
302-
echo " $app_name [--insert] [--hide-footer] [--skip-header] src [src] Create TOC for a README file (url or local path)"
303-
echo " $app_name [--no-backup] [--hide-footer] [--skip-header] src [src] Create TOC without backup, requires <!--ts--> / <!--te--> placeholders"
306+
echo " $app_name [--insert] [--hide-footer] [--skip-header] [--indent #spaces] src [src] Create TOC for a README file (url or local path)"
307+
echo " $app_name [--no-backup] [--hide-footer] [--skip-header] [--indent #spaces] src [src] Create TOC without backup, requires <!--ts--> / <!--te--> placeholders"
304308
echo " $app_name - Create TOC for markdown from STDIN"
305309
echo " $app_name --help Show help"
306310
echo " $app_name --version Show version"
@@ -321,6 +325,11 @@ gh_toc_app() {
321325
return
322326
fi
323327

328+
if [ "$1" = '--indent' ]; then
329+
indent="$2"
330+
shift
331+
fi
332+
324333
if [ "$1" = "-" ]; then
325334
if [ -z "$TMPDIR" ]; then
326335
TMPDIR="/tmp"
@@ -337,7 +346,7 @@ gh_toc_app() {
337346
while read input; do
338347
echo "$input" >> "$gh_tmp_md"
339348
done
340-
gh_toc_md2html "$gh_tmp_md" | gh_toc_grab ""
349+
gh_toc_md2html "$gh_tmp_md" | gh_toc_grab "" "$indent"
341350
return
342351
fi
343352

@@ -367,11 +376,11 @@ gh_toc_app() {
367376
for md in "$@"
368377
do
369378
echo ""
370-
gh_toc "$md" "$#" "$need_replace" "$no_backup" "$no_footer" "$skip_header"
379+
gh_toc "$md" "$#" "$need_replace" "$no_backup" "$no_footer" "$indent" "$skip_header"
371380
done
372381

373382
echo ""
374-
echo "Created by [gh-md-toc](https://github.com/ekalinin/github-markdown-toc)"
383+
echo "<!-- Created by https://github.com/ekalinin/github-markdown-toc -->"
375384
}
376385

377386
#

tests/tests.bats

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ load test_helper
2626
assert_equal "${lines[16]}" "* [Docker](#docker)"
2727
assert_equal "${lines[17]}" " * [Local](#local)"
2828
assert_equal "${lines[18]}" " * [Public](#public)"
29-
assert_equal "${lines[19]}" "Created by [gh-md-toc](https://github.com/ekalinin/github-markdown-toc)"
29+
assert_equal "${lines[19]}" "<!-- Created by https://github.com/ekalinin/github-markdown-toc -->"
3030

3131
}
3232

@@ -64,7 +64,7 @@ load test_helper
6464
assert_equal "${lines[3]}" " * [Installation](#installation)"
6565
assert_equal "${lines[4]}" " * [Usage](#usage)"
6666
assert_equal "${lines[5]}" " * [License](#license)"
67-
assert_equal "${lines[6]}" "Created by [gh-md-toc](https://github.com/ekalinin/github-markdown-toc)"
67+
assert_equal "${lines[6]}" "<!-- Created by https://github.com/ekalinin/github-markdown-toc -->"
6868
}
6969

7070
@test "TOC for mixed README.md (remote/local)" {
@@ -94,7 +94,7 @@ load test_helper
9494
assert_equal "${lines[18]}" " * [Installation](https://github.com/ekalinin/sitemap.js/blob/6bc3eb12c898c1037a35a11b2eb24ababdeb3580/README.md#installation)"
9595
assert_equal "${lines[19]}" " * [Usage](https://github.com/ekalinin/sitemap.js/blob/6bc3eb12c898c1037a35a11b2eb24ababdeb3580/README.md#usage)"
9696
assert_equal "${lines[20]}" " * [License](https://github.com/ekalinin/sitemap.js/blob/6bc3eb12c898c1037a35a11b2eb24ababdeb3580/README.md#license)"
97-
assert_equal "${lines[21]}" "Created by [gh-md-toc](https://github.com/ekalinin/github-markdown-toc)"
97+
assert_equal "${lines[21]}" "<!-- Created by https://github.com/ekalinin/github-markdown-toc -->"
9898
}
9999

100100
@test "TOC for markdown from stdin" {
@@ -122,8 +122,8 @@ load test_helper
122122
run $BATS_TEST_DIRNAME/../gh-md-toc --help
123123
assert_success
124124
assert_equal "${lines[1]}" "Usage:"
125-
assert_equal "${lines[2]}" " gh-md-toc [--insert] [--hide-footer] [--skip-header] src [src] Create TOC for a README file (url or local path)"
126-
assert_equal "${lines[3]}" " gh-md-toc [--no-backup] [--hide-footer] [--skip-header] src [src] Create TOC without backup, requires <!--ts--> / <!--te--> placeholders"
125+
assert_equal "${lines[2]}" " gh-md-toc [--insert] [--hide-footer] [--skip-header] [--indent #spaces] src [src] Create TOC for a README file (url or local path)"
126+
assert_equal "${lines[3]}" " gh-md-toc [--no-backup] [--hide-footer] [--skip-header] [--indent #spaces] src [src] Create TOC without backup, requires <!--ts--> / <!--te--> placeholders"
127127
assert_equal "${lines[4]}" " gh-md-toc - Create TOC for markdown from STDIN"
128128
assert_equal "${lines[5]}" " gh-md-toc --help Show help"
129129
assert_equal "${lines[6]}" " gh-md-toc --version Show version"
@@ -133,8 +133,8 @@ load test_helper
133133
run $BATS_TEST_DIRNAME/../gh-md-toc
134134
assert_success
135135
assert_equal "${lines[1]}" "Usage:"
136-
assert_equal "${lines[2]}" " gh-md-toc [--insert] [--hide-footer] [--skip-header] src [src] Create TOC for a README file (url or local path)"
137-
assert_equal "${lines[3]}" " gh-md-toc [--no-backup] [--hide-footer] [--skip-header] src [src] Create TOC without backup, requires <!--ts--> / <!--te--> placeholders"
136+
assert_equal "${lines[2]}" " gh-md-toc [--insert] [--hide-footer] [--skip-header] [--indent #spaces] src [src] Create TOC for a README file (url or local path)"
137+
assert_equal "${lines[3]}" " gh-md-toc [--no-backup] [--hide-footer] [--skip-header] [--indent #spaces] src [src] Create TOC without backup, requires <!--ts--> / <!--te--> placeholders"
138138
assert_equal "${lines[4]}" " gh-md-toc - Create TOC for markdown from STDIN"
139139
assert_equal "${lines[5]}" " gh-md-toc --help Show help"
140140
assert_equal "${lines[6]}" " gh-md-toc --version Show version"

0 commit comments

Comments
 (0)