|
| 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)*3, " ") |
| 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.8" |
| 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 | +iftype curl&>/dev/null;then |
| 38 | + curl --user-agent"$gh_user_agent" -s"$gh_url" |
| 39 | +eliftype 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 | + URL=https://api.github.com/markdown/raw |
| 55 | + TOKEN="$(cd"$(dirname"${BASH_SOURCE[0]}")"&& pwd)/token.txt" |
| 56 | +if [-f"$TOKEN" ];then |
| 57 | + URL="$URL?access_token=$(cat$TOKEN)" |
| 58 | +fi |
| 59 | + curl -s --user-agent"$gh_user_agent" \ |
| 60 | + --data-binary @"$gh_file_md" -H"Content-Type:text/plain" \ |
| 61 | +$URL |
| 62 | +} |
| 63 | + |
| 64 | +# |
| 65 | +# Is passed string url |
| 66 | +# |
| 67 | +gh_is_url() { |
| 68 | +case$1in |
| 69 | + https* | http*) |
| 70 | +echo"yes";; |
| 71 | +*) |
| 72 | +echo"no";; |
| 73 | +esac |
| 74 | +} |
| 75 | + |
| 76 | +# |
| 77 | +# TOC generator |
| 78 | +# |
| 79 | +gh_toc(){ |
| 80 | +local gh_src=$1 |
| 81 | +local gh_src_copy=$1 |
| 82 | +local gh_ttl_docs=$2 |
| 83 | + |
| 84 | +if ["$gh_src"="" ];then |
| 85 | +echo"Please, enter URL or local path for a README.md" |
| 86 | +exit 1 |
| 87 | +fi |
| 88 | + |
| 89 | + |
| 90 | +# Show "TOC" string only if working with one document |
| 91 | +if ["$gh_ttl_docs"="1" ];then |
| 92 | + |
| 93 | +echo"Table of Contents" |
| 94 | +echo"=================" |
| 95 | +echo"" |
| 96 | + gh_src_copy="" |
| 97 | + |
| 98 | +fi |
| 99 | + |
| 100 | +if ["$(gh_is_url"$gh_src")"=="yes" ];then |
| 101 | + gh_toc_load"$gh_src"| gh_toc_grab"$gh_src_copy" |
| 102 | +else |
| 103 | + gh_toc_md2html"$gh_src"| gh_toc_grab"$gh_src_copy" |
| 104 | +fi |
| 105 | +} |
| 106 | + |
| 107 | +# |
| 108 | +# Grabber of the TOC from rendered html |
| 109 | +# |
| 110 | +# $1 — a source url of document. |
| 111 | +# It's need if TOC is generated for multiple documents. |
| 112 | +# |
| 113 | +gh_toc_grab() { |
| 114 | +# if closed <h[1-6]> is on the new line, then move it on the prev line |
| 115 | +# for example: |
| 116 | +# was: The command <code>foo1</code> |
| 117 | +# </h1> |
| 118 | +# became: The command <code>foo1</code></h1> |
| 119 | + sed -e':a' -e'N' -e'$!ba' -e's/\n<\/h/<\/h/g'| |
| 120 | +# find strings that corresponds to template |
| 121 | + grep -E -o'<a\s*id="user-content-[^"]*".*</h[1-6]'| |
| 122 | +# remove code tags |
| 123 | + sed's/<code>//'| sed's/<\/code>//'| |
| 124 | +# now all rows are like: |
| 125 | +# <a id="user-content-..." href="..."><span ...></span></a> ... </h1 |
| 126 | +# format result line |
| 127 | +# * $0 — whole string |
| 128 | +echo -e"$(awk -v"gh_url=$1"'{ |
| 129 | + 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')" |
| 130 | +} |
| 131 | + |
| 132 | +# |
| 133 | +# Returns filename only from full path or url |
| 134 | +# |
| 135 | +gh_toc_get_filename() { |
| 136 | +echo"${1##*/}" |
| 137 | +} |
| 138 | + |
| 139 | +# |
| 140 | +# Options hendlers |
| 141 | +# |
| 142 | +gh_toc_app() { |
| 143 | +local app_name="gh-md-toc" |
| 144 | + |
| 145 | +if ["$1"='--help' ]|| [$#-eq 0 ];then |
| 146 | +echo"GitHub TOC generator ($app_name):$gh_toc_version" |
| 147 | +echo"" |
| 148 | +echo"Usage:" |
| 149 | +echo"$app_name src [src] Create TOC for a README file (url or local path)" |
| 150 | +echo"$app_name - Create TOC for markdown from STDIN" |
| 151 | +echo"$app_name --help Show help" |
| 152 | +echo"$app_name --version Show version" |
| 153 | +return |
| 154 | +fi |
| 155 | + |
| 156 | +if ["$1"='--version' ];then |
| 157 | +echo"$gh_toc_version" |
| 158 | +return |
| 159 | +fi |
| 160 | + |
| 161 | +if ["$1"="-" ];then |
| 162 | +if [-z"$TMPDIR" ];then |
| 163 | + TMPDIR="/tmp" |
| 164 | +elif [-n"$TMPDIR"-a!-d"$TMPDIR" ];then |
| 165 | + mkdir -p"$TMPDIR" |
| 166 | +fi |
| 167 | +local gh_tmp_md |
| 168 | + gh_tmp_md=$(mktemp$TMPDIR/tmp.XXXXXX) |
| 169 | +whileread input;do |
| 170 | +echo"$input">>"$gh_tmp_md" |
| 171 | +done |
| 172 | + gh_toc_md2html"$gh_tmp_md"| gh_toc_grab"" |
| 173 | +return |
| 174 | +fi |
| 175 | + |
| 176 | +formdin"$@" |
| 177 | +do |
| 178 | +echo"" |
| 179 | + gh_toc"$md""$#" |
| 180 | +done |
| 181 | + |
| 182 | +echo"" |
| 183 | +echo"Created by [gh-md-toc](https://github.com/ekalinin/github-markdown-toc)" |
| 184 | +} |
| 185 | + |
| 186 | +# |
| 187 | +# Entry point |
| 188 | +# |
| 189 | +gh_toc_app"$@" |