|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +# |
| 4 | +# Steps: |
| 5 | +# |
| 6 | +# 1. Download corresponding html file for some README.md: |
| 7 | +# curl -s $1 |
| 8 | +# |
| 9 | +# 2. Discard rows where no substring 'user-content-' (github's markup): |
| 10 | +# awk '/user-content-/ { ... |
| 11 | +# |
| 12 | +# 3.1 Get last number in each row like ' ... </span></a>sitemap.js</h1'. |
| 13 | +# It's a level of the current header: |
| 14 | +# substr($0, length($0), 1) |
| 15 | +# |
| 16 | +# 3.2 Get level from 3.1 and insert corresponding number of spaces before '*': |
| 17 | +# sprintf("%*s", substr($0, length($0), 1)*2, " ") |
| 18 | +# |
| 19 | +# 4. Find head's text and insert it inside "* [ ... ]": |
| 20 | +# substr($0, match($0, /a>.*<\/h/)+2, RLENGTH-5) |
| 21 | +# |
| 22 | +# 5. Find anchor and insert it inside "(...)": |
| 23 | +# substr($0, match($0, "href=\"[^\"]+?\" ")+6, RLENGTH-8) |
| 24 | +# |
| 25 | + |
| 26 | +gh_toc_version="0.4.7" |
| 27 | + |
| 28 | +gh_user_agent="gh-md-toc v$gh_toc_version" |
| 29 | + |
| 30 | +# |
| 31 | +# Download rendered into html README.md by its url. |
| 32 | +# |
| 33 | +# |
| 34 | +gh_toc_load() { |
| 35 | + local gh_url=$1 |
| 36 | + |
| 37 | + if type curl &>/dev/null; then |
| 38 | + curl --user-agent "$gh_user_agent" -s "$gh_url" |
| 39 | + elif type wget &>/dev/null; then |
| 40 | + wget --user-agent="$gh_user_agent" -qO- "$gh_url" |
| 41 | + else |
| 42 | + echo "Please, install 'curl' or 'wget' and try again." |
| 43 | + exit 1 |
| 44 | + fi |
| 45 | +} |
| 46 | + |
| 47 | +# |
| 48 | +# Converts local md file into html by GitHub |
| 49 | +# |
| 50 | +# ➥ curl -X POST --data '{"text": "Hello world github/linguist#1 **cool**, and #1!"}' https://api.github.com/markdown |
| 51 | +# <p>Hello world github/linguist#1 <strong>cool</strong>, and #1!</p>'" |
| 52 | +gh_toc_md2html() { |
| 53 | + local gh_file_md=$1 |
| 54 | + curl -s --user-agent "$gh_user_agent" \ |
| 55 | + --data-binary @"$gh_file_md" -H "Content-Type:text/plain" \ |
| 56 | + https://api.github.com/markdown/raw |
| 57 | +} |
| 58 | + |
| 59 | +# |
| 60 | +# Is passed string url |
| 61 | +# |
| 62 | +gh_is_url() { |
| 63 | + if [[ $1 == https* || $1 == http* ]]; then |
| 64 | + echo "yes" |
| 65 | + else |
| 66 | + echo "no" |
| 67 | + fi |
| 68 | +} |
| 69 | + |
| 70 | +# |
| 71 | +# TOC generator |
| 72 | +# |
| 73 | +gh_toc(){ |
| 74 | + local gh_src=$1 |
| 75 | + local gh_src_copy=$1 |
| 76 | + local gh_ttl_docs=$2 |
| 77 | + |
| 78 | + if [ "$gh_src" = "" ]; then |
| 79 | + echo "Please, enter URL or local path for a README.md" |
| 80 | + exit 1 |
| 81 | + fi |
| 82 | + |
| 83 | + |
| 84 | + # Show "TOC" string only if working with one document |
| 85 | + if [ "$gh_ttl_docs" = "1" ]; then |
| 86 | + |
| 87 | + echo "Table of Contents" |
| 88 | + echo "=================" |
| 89 | + echo "" |
| 90 | + gh_src_copy="" |
| 91 | + |
| 92 | + fi |
| 93 | + |
| 94 | + if [ "$(gh_is_url "$gh_src")" == "yes" ]; then |
| 95 | + gh_toc_load "$gh_src" | gh_toc_grab "$gh_src_copy" |
| 96 | + else |
| 97 | + gh_toc_md2html "$gh_src" | gh_toc_grab "$gh_src_copy" |
| 98 | + fi |
| 99 | +} |
| 100 | + |
| 101 | +# |
| 102 | +# Grabber of the TOC from rendered html |
| 103 | +# |
| 104 | +# $1 — a source url of document. |
| 105 | +# It's need if TOC is generated for multiple documents. |
| 106 | +# |
| 107 | +gh_toc_grab() { |
| 108 | + # if closed <h[1-6]> is on the new line, then move it on the prev line |
| 109 | + # for example: |
| 110 | + # was: The command <code>foo1</code> |
| 111 | + # </h1> |
| 112 | + # became: The command <code>foo1</code></h1> |
| 113 | + sed -e ':a' -e 'N' -e '$!ba' -e 's/\n<\/h/<\/h/g' | |
| 114 | + # find strings that corresponds to template |
| 115 | + grep -E -o '<a\s*id="user-content-[^"]*".*</h[1-6]' | |
| 116 | + # remove code tags |
| 117 | + sed 's/<code>//' | sed 's/<\/code>//' | |
| 118 | + # now all rows are like: |
| 119 | + # <a id="user-content-..." href="..."><span ...></span></a> ... </h1 |
| 120 | + # format result line |
| 121 | + # * $0 — whole string |
| 122 | + echo -e "$(awk -v "gh_url=$1" '{ |
| 123 | + print sprintf("%*s", substr($0, length($0), 1)*3, " ") "* [" substr($0, match($0, /a>.*<\/h/)+2, RLENGTH-5)"](" gh_url substr($0, match($0, "href=\"[^\"]+?\" ")+6, RLENGTH-8) ")"}' | sed 'y/+/ /; s/%/\\x/g')" |
| 124 | +} |
| 125 | + |
| 126 | +# |
| 127 | +# Returns filename only from full path or url |
| 128 | +# |
| 129 | +gh_toc_get_filename() { |
| 130 | + echo "${1##*/}" |
| 131 | +} |
| 132 | + |
| 133 | +# |
| 134 | +# Options hendlers |
| 135 | +# |
| 136 | +gh_toc_app() { |
| 137 | + local app_name="gh-md-toc" |
| 138 | + |
| 139 | + if [ "$1" = '--help' ] || [ $# -eq 0 ] ; then |
| 140 | + echo "GitHub TOC generator ($app_name): $gh_toc_version" |
| 141 | + echo "" |
| 142 | + echo "Usage:" |
| 143 | + echo " $app_name src [src] Create TOC for a README file (url or local path)" |
| 144 | + echo " $app_name - Create TOC for markdown from STDIN" |
| 145 | + echo " $app_name --help Show help" |
| 146 | + echo " $app_name --version Show version" |
| 147 | + return |
| 148 | + fi |
| 149 | + |
| 150 | + if [ "$1" = '--version' ]; then |
| 151 | + echo "$gh_toc_version" |
| 152 | + return |
| 153 | + fi |
| 154 | + |
| 155 | + if [ "$1" = "-" ]; then |
| 156 | + if [ -z "$TMPDIR" ]; then |
| 157 | + TMPDIR="/tmp" |
| 158 | + elif [ -n "$TMPDIR" -a ! -d "$TMPDIR" ]; then |
| 159 | + mkdir -p "$TMPDIR" |
| 160 | + fi |
| 161 | + local gh_tmp_md |
| 162 | + gh_tmp_md=$(mktemp $TMPDIR/tmp.XXXXXX) |
| 163 | + while read input; do |
| 164 | + echo "$input" >> "$gh_tmp_md" |
| 165 | + done |
| 166 | + gh_toc_md2html "$gh_tmp_md" | gh_toc_grab "" |
| 167 | + return |
| 168 | + fi |
| 169 | + |
| 170 | + for md in "$@" |
| 171 | + do |
| 172 | + echo "" |
| 173 | + gh_toc "$md" "$#" |
| 174 | + done |
| 175 | + |
| 176 | + echo "" |
| 177 | + echo "Created by [gh-md-toc](https://github.com/ekalinin/github-markdown-toc)" |
| 178 | +} |
| 179 | + |
| 180 | +# |
| 181 | +# Entry point |
| 182 | +# |
| 183 | +gh_toc_app "$@" |
0 commit comments