Skip to content

Commit

Permalink
sysutils/gh-md-toc: Update to 0.10.0
Browse files Browse the repository at this point in the history
ChangeLog: https://github.com/ekalinin/github-markdown-toc/releases/tag/0.10.0

* Update for a new GH HTML layout

PR:		276219
  • Loading branch information
DtxdF authored and clausecker committed Mar 19, 2024
1 parent 7873631 commit 849771c
Show file tree
Hide file tree
Showing 3 changed files with 211 additions and 53 deletions.
7 changes: 4 additions & 3 deletions sysutils/gh-md-toc/Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
PORTNAME= gh-md-toc
DISTVERSION= 0.8.0
DISTVERSION= 0.10.0
CATEGORIES= sysutils

MAINTAINER= DtxdF@disroot.org
Expand All @@ -9,7 +9,8 @@ WWW= https://github.com/ekalinin/github-markdown-toc
LICENSE= MIT
LICENSE_FILE= ${WRKSRC}/LICENSE

RUN_DEPENDS= curl>0:ftp/curl
RUN_DEPENDS= curl>0:ftp/curl \
jq>0:textproc/jq

USE_GITHUB= yes
GH_ACCOUNT= ekalinin
Expand All @@ -21,7 +22,7 @@ PLIST_FILES= bin/gh-md-toc

PORTDOCS= README.md

OPTIONS_DEFINE= DOCS
OPTIONS_DEFINE= DOCS

do-install:
${INSTALL_SCRIPT} ${WRKSRC}/gh-md-toc ${STAGEDIR}${PREFIX}/bin
Expand Down
6 changes: 3 additions & 3 deletions sysutils/gh-md-toc/distinfo
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
TIMESTAMP = 1687043066
SHA256 (ekalinin-github-markdown-toc-0.8.0_GH0.tar.gz) = 7f6079026a32c03d6e7254e5921b94f6802c2d52c9d74d85a1f8015d864b58d0
SIZE (ekalinin-github-markdown-toc-0.8.0_GH0.tar.gz) = 10927
TIMESTAMP = 1710461706
SHA256 (ekalinin-github-markdown-toc-0.10.0_GH0.tar.gz) = d123d360b76e0ba97974cb32fb386905612d6b0ebb0a018161056062d9b44023
SIZE (ekalinin-github-markdown-toc-0.10.0_GH0.tar.gz) = 24032
251 changes: 204 additions & 47 deletions sysutils/gh-md-toc/files/patch-gh-md-toc
Original file line number Diff line number Diff line change
@@ -1,27 +1,28 @@
--- gh-md-toc.orig 2023-06-30 18:59:15 UTC
--- 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_user_agent="gh-md-toc v$gh_toc_version"
@@ -34,12 +34,10 @@ gh_toc_load() {
gh_toc_load() {
local gh_url=$1

- if type curl &>/dev/null; then
+ if type curl > /dev/null 2>&1; then
curl --user-agent "$gh_user_agent" -s "$gh_url"
- 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
}
@@ -56,7 +54,7 @@ gh_toc_md2html() {
@@ -58,7 +56,7 @@ gh_toc_md2html() {
if [ ! -z "$GH_TOC_TOKEN" ]; then
TOKEN=$GH_TOC_TOKEN
else
Expand All @@ -30,65 +31,221 @@
if [ -f "$TOKEN_FILE" ]; then
TOKEN="$(cat $TOKEN_FILE)"
fi
@@ -124,12 +122,14 @@ gh_toc(){
@@ -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"
- gh_toc_load "$gh_src" | gh_toc_grab "$gh_src_copy" "$indent"
- if [ "${PIPESTATUS[0]}" != "0" ]; then
+ local gh_content
+ gh_content=`gh_toc_load "$gh_src"`
+ 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
+ printf "%s\n" "$gh_content" | gh_toc_grab "$gh_src_copy"
+
if [ "$need_replace" = "yes" ]; then
echo
echo "!! '$gh_src' is not a local file"
@@ -146,7 +146,7 @@ gh_toc(){
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"
+ TOKEN_FILE="$(cd "$(dirname "$0")" && pwd)/token.txt"
echo "or place GitHub auth token here: ${TOKEN_FILE}"
exit 1
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
@@ -175,11 +175,7 @@ gh_toc(){
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 [[ "`uname`" == "Darwin" ]]; then
- # 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
+ sed -i "" "/${ts}/r ${toc_path}" "$gh_src"
echo
if [ "${no_backup}" = "yes" ]; then
rm ${toc_path} ${gh_src}${ext}
@@ -296,14 +292,18 @@ gh_toc_app() {
if [ "$1" = '--version' ]; then
echo "$gh_toc_version"
echo
- echo "os: `lsb_release -d | cut -f 2`"
- echo "kernel: `cat /proc/version`"
- echo "shell: `$SHELL --version`"
+ echo "os: `uname -rs`"
+ echo "kernel: `uname -K`"
+ echo "shell: $SHELL"
echo
- for tool in curl wget grep awk sed; do
+ for tool in grep awk; do
printf "%-5s: " $tool
echo `$tool --version | head -n 1`
done
+ printf "%-5s: " sed
+ echo "sed (BSD sed)"
+ printf "%-5s: " fetch
+ echo "fetch (BSD fetch)"
return
- 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() {

0 comments on commit 849771c

Please sign in to comment.