#!/usr/bin/env bash## Steps:## 1. Download corresponding html file for some README.md:# curl -s 1ドル## 2. Discard rows where no substring 'user-content-' (github's markup):# awk '/user-content-/ { ...## 3.1 Get last number in each row like ' ... </span></a>sitemap.js</h1'.# It's a level of the current header:# substr(0,ドル length(0ドル), 1)## 3.2 Get level from 3.1 and insert corresponding number of spaces before '*':# sprintf("%*s", substr(0,ドル length(0ドル), 1)*3, " ")## 4. Find head's text and insert it inside "* [ ... ]":# substr(0,ドル match(0,ドル /a>.*<\/h/)+2, RLENGTH-5)## 5. Find anchor and insert it inside "(...)":# substr(0,ドル match(0,ドル "href=\"[^\"]+?\" ")+6, RLENGTH-8)#gh_toc_version="0.4.8"gh_user_agent="gh-md-toc v$gh_toc_version"## Download rendered into html README.md by its url.##gh_toc_load() {local gh_url=1ドルif type curl &>/dev/null; thencurl --user-agent "$gh_user_agent" -s "$gh_url"elif type wget &>/dev/null; thenwget --user-agent="$gh_user_agent" -qO- "$gh_url"elseecho "Please, install 'curl' or 'wget' and try again."exit 1fi}## 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ドルURL=https://api.github.com/markdown/rawTOKEN="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/token.txt"if [ -f "$TOKEN" ]; thenURL="$URL?access_token=$(cat $TOKEN)"ficurl -s --user-agent "$gh_user_agent" \--data-binary @"$gh_file_md" -H "Content-Type:text/plain" \$URL}## Is passed string url#gh_is_url() {case 1ドル inhttps* | http*)echo "yes";;*)echo "no";;esac}## TOC generator#gh_toc(){local gh_src=1ドルlocal gh_src_copy=1ドルlocal gh_ttl_docs=2ドルif [ "$gh_src" = "" ]; thenecho "Please, enter URL or local path for a README.md"exit 1fi# Show "TOC" string only if working with one documentif [ "$gh_ttl_docs" = "1" ]; thenecho "Table of Contents"echo "================="echo ""gh_src_copy=""fiif [ "$(gh_is_url "$gh_src")" == "yes" ]; thengh_toc_load "$gh_src" | gh_toc_grab "$gh_src_copy"elsegh_toc_md2html "$gh_src" | 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() {# if closed <h[1-6]> is on the new line, then move it on the prev line# for example:# was: The command <code>foo1</code># </h1># became: The command <code>foo1</code></h1>sed -e ':a' -e 'N' -e '$!ba' -e 's/\n<\/h/<\/h/g' |# find strings that corresponds to templategrep -E -o '<a\s*id="user-content-[^"]*".*</h[1-6]' |# remove code tagssed 's/<code>//' | sed 's/<\/code>//' |# now all rows are like:# <a id="user-content-..." href="..."><span ...></span></a> ... </h1# format result line# * 0ドル — whole stringecho -e "$(awk -v "gh_url=1ドル" '{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')"}## Returns filename only from full path or url#gh_toc_get_filename() {echo "${1##*/}"}## Options hendlers#gh_toc_app() {local app_name="gh-md-toc"if [ "1ドル" = '--help' ] || [ $# -eq 0 ] ; thenecho "GitHub TOC generator ($app_name): $gh_toc_version"echo ""echo "Usage:"echo " $app_name src [src] Create TOC for a README file (url or local path)"echo " $app_name - Create TOC for markdown from STDIN"echo " $app_name --help Show help"echo " $app_name --version Show version"returnfiif [ "1ドル" = '--version' ]; thenecho "$gh_toc_version"returnfiif [ "1ドル" = "-" ]; thenif [ -z "$TMPDIR" ]; thenTMPDIR="/tmp"elif [ -n "$TMPDIR" -a ! -d "$TMPDIR" ]; thenmkdir -p "$TMPDIR"filocal gh_tmp_mdgh_tmp_md=$(mktemp $TMPDIR/tmp.XXXXXX)while read input; doecho "$input" >> "$gh_tmp_md"donegh_toc_md2html "$gh_tmp_md" | gh_toc_grab ""returnfifor md in "$@"doecho ""gh_toc "$md" "$#"doneecho ""echo "Created by [gh-md-toc](https://github.com/ekalinin/github-markdown-toc)"}## Entry point#gh_toc_app "$@"
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。