From 5b1ab101e200f7b22352fa70ac54b80477733a60 Mon Sep 17 00:00:00 2001 From: Eugene Kalinin Date: Sun, 22 Mar 2015 23:06:39 +0300 Subject: [PATCH] 0.3: added support for markdown and local files, fixes #4 --- README.md | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++-- gh-md-toc | 55 ++++++++++++++++++++++++++++++++++++++++++++--------- 2 files changed, 101 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index e5f8033..48197e8 100644 --- a/README.md +++ b/README.md @@ -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 ================= @@ -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) @@ -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. diff --git a/gh-md-toc b/gh-md-toc index c6a777f..805c0c9 100755 --- a/gh-md-toc +++ b/gh-md-toc @@ -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. @@ -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 +#

Hello world github/linguist#1 cool, and #1!

'" +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) ")"}' @@ -86,7 +123,7 @@ gh_toc_app() { echo "GitHub TOC generator ($app_name): $gh_toc_version" echo "" echo "Usage:" - echo " $app_name 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