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