Skip to content

Commit

Permalink
0.3: added support for markdown and local files, fixes #4
Browse files Browse the repository at this point in the history
  • Loading branch information
ekalinin committed Mar 22, 2015
1 parent 195a35f commit 5b1ab10
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 11 deletions.
57 changes: 55 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ gh-md-toc
gh-md-toc — is for you if you **want to generate TOC** for README.md and
**don't want to install any additional software**.

gh-md-toc is able to proccess:

* local files (markdown files in local file system)
* remote files (html files on github.com)

Table of contents
=================

Expand All @@ -23,7 +28,30 @@ $ chmod a+x gh-md-toc
Usage
=====

For a example, you have a README.md like this:

Local files
-----------

Here's an example of TOC creating for a local README.md:

```bash
➥ ./gh-md-toc ~/projects/Dockerfile.vim/README.md Вс. марта 22 22:51:46 MSK 2015

Table of Contents
=================

* [Dockerfile.vim](#dockerfilevim)
* [Screenshot](#screenshot)
* [Installation](#installation)
* [OR using Pathogen:](#or-using-pathogen)
* [OR using Vundle:](#or-using-vundle)
* [License](#license)
```
Remote files
------------
And here's an example, when you have a README.md like this:
* [README.md without TOC](https://github.com/ekalinin/envirius/blob/f939d3b6882bfb6ecb28ef7b6e62862f934ba945/README.md)
Expand Down Expand Up @@ -104,10 +132,35 @@ It supports multiple files as well:
* [Unique Pointers](https://github.com/aminb/rust-for-c/blob/master/unique_pointers/README.md#unique-pointers)
```
Combo
-----
You can easily combine both ways:
```bash
➥ ./gh-md-toc \
~/projects/Dockerfile.vim/README.md \
https://github.com/ekalinin/sitemap.js/blob/master/README.md Вс. марта 22 22:53:15 MSK 2015

* [Dockerfile.vim](~/projects/Dockerfile.vim/README.md#dockerfilevim)
* [Screenshot](~/projects/Dockerfile.vim/README.md#screenshot)
* [Installation](~/projects/Dockerfile.vim/README.md#installation)
* [OR using Pathogen:](~/projects/Dockerfile.vim/README.md#or-using-pathogen)
* [OR using Vundle:](~/projects/Dockerfile.vim/README.md#or-using-vundle)
* [License](~/projects/Dockerfile.vim/README.md#license)

* [sitemap.js](https://github.com/ekalinin/sitemap.js/blob/master/README.md#sitemapjs)
* [Installation](https://github.com/ekalinin/sitemap.js/blob/master/README.md#installation)
* [Usage](https://github.com/ekalinin/sitemap.js/blob/master/README.md#usage)
* [License](https://github.com/ekalinin/sitemap.js/blob/master/README.md#license)

Created by [gh-md-toc](https://github.com/ekalinin/github-markdown-toc)
```
Dependency
==========
* curl or wget
* awk
Tested on Ubuntu 14.04 in bash/zsh.
Tested on Ubuntu 14.04/14.10 in bash/zsh.
55 changes: 46 additions & 9 deletions gh-md-toc
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
# substr($4, 7)
#

gh_toc_version="0.2.1"
gh_toc_version="0.3.0"

#
# Download rendered into html README.md by it's url.
Expand All @@ -39,31 +39,68 @@ gh_toc_load() {
fi
}

#
# Converts local md file into html by GitHub
#
# ➥ curl -X POST --data '{"text": "Hello world github/linguist#1 **cool**, and #1!"}' https://api.github.com/markdown
# <p>Hello world github/linguist#1 <strong>cool</strong>, and #1!</p>'"
gh_toc_md2html() {
local gh_file_md=$1
local gh_user_agent=$2
curl -s --user-agent "$gh_user_agent" \
--data-binary @$gh_file_md -H "Content-Type:text/plain" \
https://api.github.com/markdown/raw
}

#
# Is passed string url
#
gh_is_url() {
if [[ $1 == https* || $1 == http* ]]; then
echo "yes"
else
echo "no"
fi
}

#
# TOC generator
#
gh_toc(){
local gh_url=$1
local gh_src=$1
local gh_src_copy=$1
local gh_user_agent=${2:-"gh-md-toc"}
local gh_count=$3
local gh_ttl_docs=$3

if [ "$gh_url" = "" ]; then
echo "Please, enter URL for a README.md"
if [ "$gh_src" = "" ]; then
echo "Please, enter URL or local path for a README.md"
exit 1
fi

if [ "$gh_count" = "1" ]; then

# Show "TOC" string only if working with one document
if [ "$gh_ttl_docs" = "1" ]; then

echo "Table of Contents"
echo "================="
echo ""
gh_src_copy=""

fi

gh_toc_load "$gh_url" "$gh_user_agent" | gh_toc_grab ""
if [ "$(gh_is_url $gh_src)" == "yes" ]; then
gh_toc_load "$gh_src" "$gh_user_agent" | gh_toc_grab "$gh_src_copy"
else
gh_toc_load "$gh_url" "$gh_user_agent" | gh_toc_grab "$gh_url"
gh_toc_md2html "$gh_src" "$gh_user_agent" | gh_toc_grab "$gh_src_copy"
fi
}

#
# Grabber of the TOC from rendered html
#
# $1 — a source url of document.
# It's need if TOC is generated for multiple documents.
#
gh_toc_grab() {
awk -v "gh_url=$1" '/user-content-/ {
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) ")"}'
Expand All @@ -86,7 +123,7 @@ gh_toc_app() {
echo "GitHub TOC generator ($app_name): $gh_toc_version"
echo ""
echo "Usage:"
echo " $app_name <url> Create TOC for passed url of a README file"
echo " $app_name src [src] Create TOC for a README file (url or local path)"
echo " $app_name --help Show help"
echo " $app_name --version Show help"
return
Expand Down

0 comments on commit 5b1ab10

Please sign in to comment.