#!/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 ' ... sitemap.js.*<\/h/)+2, RLENGTH-5) # # 5. Find anchor and insert it inside "(...)": # substr($0, match($0, "href=\"[^\"]+?\" ")+6, RLENGTH-8) # gh_toc_version="0.4.9" 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; then curl --user-agent "$gh_user_agent" -s "$gh_url" elif type wget &>/dev/null; then wget --user-agent="$gh_user_agent" -qO- "$gh_url" else echo "Please, install 'curl' or 'wget' and try again." exit 1 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 URL=https://api.github.com/markdown/raw TOKEN="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/token.txt" if [ -f "$TOKEN" ]; then URL="$URL?access_token=$(cat $TOKEN)" fi curl -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 in https* | 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" = "" ]; then echo "Please, enter URL or local path for a README.md" exit 1 fi # 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 if [ "$(gh_is_url "$gh_src")" == "yes" ]; then gh_toc_load "$gh_src" | gh_toc_grab "$gh_src_copy" else gh_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 closedfoo1
#
# became: The command foo1
sed -e ':a' -e 'N' -e '$!ba' -e 's/\n<\/h/<\/h/g' |
# find strings that corresponds to template
grep -E -o '