mirror of
https://git.freebsd.org/ports.git
synced 2025-05-06 10:57:37 -04:00
ChangeLog: https://github.com/ekalinin/github-markdown-toc/releases/tag/0.10.0 * Update for a new GH HTML layout PR: 276219
251 lines
9 KiB
Text
251 lines
9 KiB
Text
--- gh-md-toc.orig 2024-03-03 10:54:38 UTC
|
|
+++ gh-md-toc
|
|
@@ -1,4 +1,4 @@
|
|
-#!/usr/bin/env bash
|
|
+#!/bin/sh
|
|
|
|
#
|
|
# Steps:
|
|
@@ -34,12 +34,10 @@ gh_toc_load() {
|
|
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"
|
|
+ if type curl > /dev/null 2>&1; then
|
|
+ curl --location --fail --user-agent "$gh_user_agent" -s "$gh_url"
|
|
else
|
|
- echo "Please, install 'curl' or 'wget' and try again."
|
|
+ echo "Please, install 'curl' and try again."
|
|
exit 1
|
|
fi
|
|
}
|
|
@@ -58,7 +56,7 @@ gh_toc_md2html() {
|
|
if [ ! -z "$GH_TOC_TOKEN" ]; then
|
|
TOKEN=$GH_TOC_TOKEN
|
|
else
|
|
- TOKEN_FILE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/token.txt"
|
|
+ TOKEN_FILE="$(cd "$(dirname "$0")" && pwd)/token.txt"
|
|
if [ -f "$TOKEN_FILE" ]; then
|
|
TOKEN="$(cat $TOKEN_FILE)"
|
|
fi
|
|
@@ -109,6 +107,15 @@ gh_is_url() {
|
|
esac
|
|
}
|
|
|
|
+gh_is_invalid_url() {
|
|
+ local gh_hostname="github.com"
|
|
+
|
|
+ case $1 in
|
|
+ https://${gh_hostname}/* | http://${gh_hostname}/* ) return 0 ;;
|
|
+ *) return 1 ;;
|
|
+ esac
|
|
+}
|
|
+
|
|
#
|
|
# TOC generator
|
|
#
|
|
@@ -139,75 +146,112 @@ gh_toc(){
|
|
fi
|
|
|
|
if [ "$(gh_is_url "$gh_src")" == "yes" ]; then
|
|
- gh_toc_load "$gh_src" | gh_toc_grab "$gh_src_copy" "$indent"
|
|
- if [ "${PIPESTATUS[0]}" != "0" ]; then
|
|
+ if ! which -s "jq"; then
|
|
+ echo "Please, install 'jq' and try again."
|
|
+ exit 1
|
|
+ fi
|
|
+
|
|
+ if ! gh_is_invalid_url "${gh_src}"; then
|
|
+ echo "It looks like an invalid URL."
|
|
+ echo "Note that valid URLs are, for example, \"https://github.com/<account>/<repo>\"."
|
|
+ exit 1
|
|
+ fi
|
|
+
|
|
+ local account repo
|
|
+
|
|
+ account=$(echo -n "${gh_src}" | sed -Ee 's#https?://github\.com/([^/]+).+#\1#')
|
|
+ repo=$(echo -n "${gh_src}" | sed -Ee 's#https?://github\.com/[^/]+/([^/]+)/?.*#\1#')
|
|
+
|
|
+ local gh_content
|
|
+ gh_content=$(gh_toc_load "${gh_src}")
|
|
+ if [ $? -ne 0 ]; then
|
|
echo "Could not load remote document."
|
|
echo "Please check your url or network connectivity"
|
|
exit 1
|
|
fi
|
|
+
|
|
if [ "$need_replace" = "yes" ]; then
|
|
echo
|
|
echo "!! '$gh_src' is not a local file"
|
|
echo "!! Can't insert the TOC into it."
|
|
echo
|
|
+ exit 1
|
|
fi
|
|
- else
|
|
- local rawhtml=$(gh_toc_md2html "$gh_src" "$skip_header")
|
|
- if [ "$rawhtml" == "XXNetworkErrorXX" ]; then
|
|
- echo "Parsing local markdown file requires access to github API"
|
|
- echo "Please make sure curl is installed and check your network connectivity"
|
|
- exit 1
|
|
+
|
|
+ local json_file
|
|
+ json_file=$(mktemp -t gh-md-toc)
|
|
+ printf "%s\n" "${gh_content}" | grep defaultBranch | sed -Ee 's#<script [^>]+>(.+)</script>#\1#' > "${json_file}"
|
|
+
|
|
+ local refName path
|
|
+
|
|
+ refName=$(cat "${json_file}" | jq --raw-output '.props.initialPayload.overview.overviewFiles.[] | select(.preferredFileType == "readme") | .refName' 2> /dev/null)
|
|
+ path=$(cat "${json_file}" | jq --raw-output '.props.initialPayload.overview.overviewFiles.[] | select(.preferredFileType == "readme") | .path' 2> /dev/null)
|
|
+
|
|
+ gh_src=$(mktemp -t gh-md-toc)
|
|
+
|
|
+ gh_content=`gh_toc_load "https://raw.githubusercontent.com/${account}/${repo}/${refName}/${path}"`
|
|
+ if [ $? -ne 0 ]; then
|
|
+ echo "Could not load remote document."
|
|
+ echo "Please check your url or network connectivity"
|
|
+ exit 1
|
|
fi
|
|
- if [ "$rawhtml" == "XXRateLimitXX" ]; then
|
|
- echo "Parsing local markdown file requires access to github API"
|
|
- echo "Error: You exceeded the hourly limit. See: https://developer.github.com/v3/#rate-limiting"
|
|
- TOKEN_FILE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/token.txt"
|
|
- echo "or place GitHub auth token here: ${TOKEN_FILE}"
|
|
- exit 1
|
|
- fi
|
|
- local toc=`echo "$rawhtml" | gh_toc_grab "$gh_src_copy" "$indent"`
|
|
- echo "$toc"
|
|
- if [ "$need_replace" = "yes" ]; then
|
|
- if grep -Fxq "<!--ts-->" "$gh_src" && grep -Fxq "<!--te-->" "$gh_src"; then
|
|
- echo "Found markers"
|
|
- else
|
|
- echo "You don't have <!--ts--> or <!--te--> in your file...exiting"
|
|
- exit 1
|
|
- fi
|
|
- local ts="<\!--ts-->"
|
|
- local te="<\!--te-->"
|
|
- local dt=`date +'%F_%H%M%S'`
|
|
- local ext=".orig.${dt}"
|
|
- local toc_path="${gh_src}.toc.${dt}"
|
|
- local toc_createdby="<!-- Created by https://github.com/ekalinin/github-markdown-toc -->"
|
|
- local toc_footer="<!-- Added by: `whoami`, at: `date` -->"
|
|
- # http://fahdshariff.blogspot.ru/2012/12/sed-mutli-line-replacement-between-two.html
|
|
- # clear old TOC
|
|
- sed -i${ext} "/${ts}/,/${te}/{//!d;}" "$gh_src"
|
|
- # create toc file
|
|
- echo "${toc}" > "${toc_path}"
|
|
- if [ "${no_footer}" != "yes" ]; then
|
|
- echo -e "\n${toc_createdby}\n${toc_footer}\n" >> "$toc_path"
|
|
- fi
|
|
|
|
- # insert toc file
|
|
- if ! sed --version > /dev/null 2>&1; then
|
|
- sed -i "" "/${ts}/r ${toc_path}" "$gh_src"
|
|
- else
|
|
- sed -i "/${ts}/r ${toc_path}" "$gh_src"
|
|
- fi
|
|
- echo
|
|
- if [ "${no_backup}" = "yes" ]; then
|
|
- rm "$toc_path" "$gh_src$ext"
|
|
- fi
|
|
- echo "!! TOC was added into: '$gh_src'"
|
|
- if [ -z "${no_backup}" ]; then
|
|
- echo "!! Origin version of the file: '${gh_src}${ext}'"
|
|
- echo "!! TOC added into a separate file: '${toc_path}'"
|
|
+ printf "%s\n" "${gh_content}" > "${gh_src}"
|
|
+
|
|
+ rm -f "${json_file}"
|
|
+ fi
|
|
+
|
|
+ local rawhtml=$(gh_toc_md2html "$gh_src" "$skip_header")
|
|
+ if [ "$rawhtml" == "XXNetworkErrorXX" ]; then
|
|
+ echo "Parsing local markdown file requires access to github API"
|
|
+ echo "Please make sure curl is installed and check your network connectivity"
|
|
+ exit 1
|
|
+ fi
|
|
+ if [ "$rawhtml" == "XXRateLimitXX" ]; then
|
|
+ echo "Parsing local markdown file requires access to github API"
|
|
+ echo "Error: You exceeded the hourly limit. See: https://developer.github.com/v3/#rate-limiting"
|
|
+ TOKEN_FILE="$(cd "$(dirname "$0}")" && pwd)/token.txt"
|
|
+ echo "or place GitHub auth token here: ${TOKEN_FILE}"
|
|
+ exit 1
|
|
+ fi
|
|
+ local toc=`echo "$rawhtml" | gh_toc_grab "$gh_src_copy" "$indent"`
|
|
+ echo "$toc"
|
|
+ if [ "$need_replace" = "yes" ]; then
|
|
+ if grep -Fxq "<!--ts-->" "$gh_src" && grep -Fxq "<!--te-->" "$gh_src"; then
|
|
+ echo "Found markers"
|
|
+ else
|
|
+ echo "You don't have <!--ts--> or <!--te--> in your file...exiting"
|
|
+ exit 1
|
|
fi
|
|
- echo
|
|
+ local ts="<\!--ts-->"
|
|
+ local te="<\!--te-->"
|
|
+ local dt=`date +'%F_%H%M%S'`
|
|
+ local ext=".orig.${dt}"
|
|
+ local toc_path="${gh_src}.toc.${dt}"
|
|
+ local toc_createdby="<!-- Created by https://github.com/ekalinin/github-markdown-toc -->"
|
|
+ local toc_footer="<!-- Added by: `whoami`, at: `date` -->"
|
|
+ # http://fahdshariff.blogspot.ru/2012/12/sed-mutli-line-replacement-between-two.html
|
|
+ # clear old TOC
|
|
+ sed -i${ext} "/${ts}/,/${te}/{//!d;}" "$gh_src"
|
|
+ # create toc file
|
|
+ echo "${toc}" > "${toc_path}"
|
|
+ if [ "${no_footer}" != "yes" ]; then
|
|
+ echo -e "\n${toc_createdby}\n${toc_footer}\n" >> "$toc_path"
|
|
fi
|
|
+
|
|
+ # insert toc file
|
|
+ sed -i "" "/${ts}/r ${toc_path}" "$gh_src"
|
|
+ echo
|
|
+ if [ "${no_backup}" = "yes" ]; then
|
|
+ rm "$toc_path" "$gh_src$ext"
|
|
+ fi
|
|
+ echo "!! TOC was added into: '$gh_src'"
|
|
+ if [ -z "${no_backup}" ]; then
|
|
+ echo "!! Origin version of the file: '${gh_src}${ext}'"
|
|
+ echo "!! TOC added into a separate file: '${toc_path}'"
|
|
fi
|
|
+ echo
|
|
+ fi
|
|
}
|
|
|
|
#
|
|
@@ -218,7 +262,6 @@ gh_toc_grab() {
|
|
# $2 - number of spaces used to indent.
|
|
#
|
|
gh_toc_grab() {
|
|
-
|
|
href_regex="/href=\"[^\"]+?\"/"
|
|
common_awk_script='
|
|
modified_href = ""
|
|
@@ -298,19 +341,21 @@ show_version() {
|
|
show_version() {
|
|
echo "$gh_toc_version"
|
|
echo
|
|
- echo "os: `uname -s`"
|
|
+ echo "os: `uname -rs`"
|
|
echo "arch: `uname -m`"
|
|
- echo "kernel: `uname -r`"
|
|
- echo "shell: `$SHELL --version`"
|
|
+ echo "kernel: `uname -i`"
|
|
+ echo "shell: $SHELL"
|
|
echo
|
|
- for tool in curl wget grep awk sed; do
|
|
+ for tool in curl grep awk jq; do
|
|
printf "%-5s: " $tool
|
|
- if `type $tool &>/dev/null`; then
|
|
+ if type $tool > /dev/null 2>&1; then
|
|
echo `$tool --version | head -n 1`
|
|
else
|
|
echo "not installed"
|
|
fi
|
|
done
|
|
+ printf "%-5s: " sed
|
|
+ echo "sed (BSD sed)"
|
|
}
|
|
|
|
show_help() {
|