Skip to content

Commit 5b1ab10

Browse files
committed
0.3: added support for markdown and local files, fixes #4
1 parent 195a35f commit 5b1ab10

File tree

2 files changed

+101
-11
lines changed

2 files changed

+101
-11
lines changed

README.md

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ gh-md-toc
44
gh-md-toc — is for you if you **want to generate TOC** for README.md and
55
**don't want to install any additional software**.
66

7+
gh-md-toc is able to proccess:
8+
9+
* local files (markdown files in local file system)
10+
* remote files (html files on github.com)
11+
712
Table of contents
813
=================
914

@@ -23,7 +28,30 @@ $ chmod a+x gh-md-toc
2328
Usage
2429
=====
2530

26-
For a example, you have a README.md like this:
31+
32+
Local files
33+
-----------
34+
35+
Here's an example of TOC creating for a local README.md:
36+
37+
```bash
38+
➥ ./gh-md-toc ~/projects/Dockerfile.vim/README.md Вс. марта 22 22:51:46 MSK 2015
39+
40+
Table of Contents
41+
=================
42+
43+
* [Dockerfile.vim](#dockerfilevim)
44+
* [Screenshot](#screenshot)
45+
* [Installation](#installation)
46+
* [OR using Pathogen:](#or-using-pathogen)
47+
* [OR using Vundle:](#or-using-vundle)
48+
* [License](#license)
49+
```
50+
51+
Remote files
52+
------------
53+
54+
And here's an example, when you have a README.md like this:
2755
2856
* [README.md without TOC](https://github.com/ekalinin/envirius/blob/f939d3b6882bfb6ecb28ef7b6e62862f934ba945/README.md)
2957
@@ -104,10 +132,35 @@ It supports multiple files as well:
104132
* [Unique Pointers](https://github.com/aminb/rust-for-c/blob/master/unique_pointers/README.md#unique-pointers)
105133
```
106134
135+
Combo
136+
-----
137+
138+
You can easily combine both ways:
139+
140+
```bash
141+
➥ ./gh-md-toc \
142+
~/projects/Dockerfile.vim/README.md \
143+
https://github.com/ekalinin/sitemap.js/blob/master/README.md Вс. марта 22 22:53:15 MSK 2015
144+
145+
* [Dockerfile.vim](~/projects/Dockerfile.vim/README.md#dockerfilevim)
146+
* [Screenshot](~/projects/Dockerfile.vim/README.md#screenshot)
147+
* [Installation](~/projects/Dockerfile.vim/README.md#installation)
148+
* [OR using Pathogen:](~/projects/Dockerfile.vim/README.md#or-using-pathogen)
149+
* [OR using Vundle:](~/projects/Dockerfile.vim/README.md#or-using-vundle)
150+
* [License](~/projects/Dockerfile.vim/README.md#license)
151+
152+
* [sitemap.js](https://github.com/ekalinin/sitemap.js/blob/master/README.md#sitemapjs)
153+
* [Installation](https://github.com/ekalinin/sitemap.js/blob/master/README.md#installation)
154+
* [Usage](https://github.com/ekalinin/sitemap.js/blob/master/README.md#usage)
155+
* [License](https://github.com/ekalinin/sitemap.js/blob/master/README.md#license)
156+
157+
Created by [gh-md-toc](https://github.com/ekalinin/github-markdown-toc)
158+
```
159+
107160
Dependency
108161
==========
109162
110163
* curl or wget
111164
* awk
112165
113-
Tested on Ubuntu 14.04 in bash/zsh.
166+
Tested on Ubuntu 14.04/14.10 in bash/zsh.

gh-md-toc

Lines changed: 46 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
# substr($4, 7)
2020
#
2121

22-
gh_toc_version="0.2.1"
22+
gh_toc_version="0.3.0"
2323

2424
#
2525
# Download rendered into html README.md by it's url.
@@ -39,31 +39,68 @@ gh_toc_load() {
3939
fi
4040
}
4141

42+
#
43+
# Converts local md file into html by GitHub
44+
#
45+
# ➥ curl -X POST --data '{"text": "Hello world github/linguist#1 **cool**, and #1!"}' https://api.github.com/markdown
46+
# <p>Hello world github/linguist#1 <strong>cool</strong>, and #1!</p>'"
47+
gh_toc_md2html() {
48+
local gh_file_md=$1
49+
local gh_user_agent=$2
50+
curl -s --user-agent "$gh_user_agent" \
51+
--data-binary @$gh_file_md -H "Content-Type:text/plain" \
52+
https://api.github.com/markdown/raw
53+
}
54+
55+
#
56+
# Is passed string url
57+
#
58+
gh_is_url() {
59+
if [[ $1 == https* || $1 == http* ]]; then
60+
echo "yes"
61+
else
62+
echo "no"
63+
fi
64+
}
65+
4266
#
4367
# TOC generator
4468
#
4569
gh_toc(){
46-
local gh_url=$1
70+
local gh_src=$1
71+
local gh_src_copy=$1
4772
local gh_user_agent=${2:-"gh-md-toc"}
48-
local gh_count=$3
73+
local gh_ttl_docs=$3
4974

50-
if [ "$gh_url" = "" ]; then
51-
echo "Please, enter URL for a README.md"
75+
if [ "$gh_src" = "" ]; then
76+
echo "Please, enter URL or local path for a README.md"
5277
exit 1
5378
fi
5479

55-
if [ "$gh_count" = "1" ]; then
80+
81+
# Show "TOC" string only if working with one document
82+
if [ "$gh_ttl_docs" = "1" ]; then
5683

5784
echo "Table of Contents"
5885
echo "================="
5986
echo ""
87+
gh_src_copy=""
88+
89+
fi
6090

61-
gh_toc_load "$gh_url" "$gh_user_agent" | gh_toc_grab ""
91+
if [ "$(gh_is_url $gh_src)" == "yes" ]; then
92+
gh_toc_load "$gh_src" "$gh_user_agent" | gh_toc_grab "$gh_src_copy"
6293
else
63-
gh_toc_load "$gh_url" "$gh_user_agent" | gh_toc_grab "$gh_url"
94+
gh_toc_md2html "$gh_src" "$gh_user_agent" | gh_toc_grab "$gh_src_copy"
6495
fi
6596
}
6697

98+
#
99+
# Grabber of the TOC from rendered html
100+
#
101+
# $1 — a source url of document.
102+
# It's need if TOC is generated for multiple documents.
103+
#
67104
gh_toc_grab() {
68105
awk -v "gh_url=$1" '/user-content-/ {
69106
print sprintf("%*s", substr($NF, length($NF)-1, 1)*2, " ") "* [" substr($0, match($0, /a>.*<\/h/)+2, RLENGTH-5)"](" gh_url substr($4, 7, length($4)-7) ")"}'
@@ -86,7 +123,7 @@ gh_toc_app() {
86123
echo "GitHub TOC generator ($app_name): $gh_toc_version"
87124
echo ""
88125
echo "Usage:"
89-
echo " $app_name <url> Create TOC for passed url of a README file"
126+
echo " $app_name src [src] Create TOC for a README file (url or local path)"
90127
echo " $app_name --help Show help"
91128
echo " $app_name --version Show help"
92129
return

0 commit comments

Comments
 (0)