git3html

git clone https://orangeshoelaces.net/git/git3html.git

388ecc420a3c3362a0f57524c69945302ccaf3f7

Author: Vasilii Kolobkov on 01/11/2019

Committer: Vasilii Kolobkov on 01/11/2019

Write file tree skeleton

Stats

git3html | 51 ++++++--
1 file changed, 42 insertions(+), 9 deletions(-)

Patch

diff --git a/git3html b/git3html
index ff56a81..e580830 100755
--- a/git3html
+++ b/git3html
@@ -23,6 +23,14 @@ escape() {
 	sed 's,&,\&amp;,g; s,<,\&lt;,g; s,>,\&gt;,g'
 }
 
+parent() {
+	if [ "${1%/}" = '.' ]; then
+		echo ".."
+	else
+		echo "${1%/}/.."
+	fi
+}
+
 g() {
 	git --git-dir="$repodir" "$@"
 }
@@ -33,7 +41,7 @@ tmrc() {
 }
 
 defaultname() {
-	if [ "$(basename "$repodir")" = . ]; then
+	if [ "$(basename "$repodir")" = '.' ]; then
 		basename $(pwd)
 	else
 		basename "$repodir"
@@ -49,13 +57,15 @@ header() {
 	<body>
 	<header>
 	END
-	echo "<h1>$(echo "$name" | escape)</h1>"
+	echo "<h1>$(echo "$projname" | escape)</h1>"
 	cat <<-END
 	<nav>
 	<ul>
-	<li><a href="readme.html">Readme</a></li>
-	<li><a href="log.html">Log</a></li>
-	<li><a href="files.html">Files</a></li>
+	END
+	echo "<li><a href=\"${1%/}/readme.html\">Readme</a></li>"
+	echo "<li><a href=\"${1%/}/log.html\">Log</a></li>"
+	echo "<li><a href=\"${1%/}/files.html\">Files</a></li>"
+	cat <<-END
 	</ul>
 	</nav>
 	</header>
@@ -70,7 +80,7 @@ footer() {
 }
 
 readme() (
-	header
+	header '.'
 	root=$(tmrc)
 	echo '<pre>'
 	g ls-tree --name-only "$root" | grep '^README*' | while read -r readme; do
@@ -80,6 +90,29 @@ readme() (
 	footer
 )
 
+files() (
+	root="$1"
+	outdir="$2"
+	rootreldir="$3"
+
+	header "$rootreldir"
+	g ls-tree "$root" | awk '$2 == "tree"' | while read d; do
+		droot="$(echo "$d" | awk '{ print $3 }')"
+		dname="$(echo "$d" | awk '{ print $4 }')"
+		doutdir="${outdir%/}/$dname"
+		if [ ! -d "$doutdir" ]; then
+			mkdir "$doutdir"
+		fi
+		files "$droot" "$doutdir" "$(parent "$rootreldir")" > "${doutdir%/}/files.html"
+		echo "<li><a href=\"$dname/files.html\">$(echo "$dname" | escape)</a></li>"
+	done
+
+	g ls-tree "$root" | awk '$2 == "blob"' | while read f; do
+		fname="$(echo "$f" | awk '{ print $4 }')"
+		echo "<li><a href=\"$fname.html\">$(echo "$fname" | escape)</a></li>"
+	done
+	footer
+)
 
 # ........ driver ........
 
@@ -91,7 +124,7 @@ usage() {
 while getopts 'p:u:d:h' opt; do
 	case "$opt" in
 	p)
-		name="$OPTARG"
+		projname="$OPTARG"
 		;;
 	u)
 		cloneurl="$OPTARG"
@@ -112,7 +145,7 @@ fi
 
 repodir="$1"
 htmldir="$2"
-name="${name:-$(defaultname)}"
+projname="${projname:-$(defaultname)}"
 depth="${depth:-0}"
 
 if [ ! -d "$htmldir" ]; then
@@ -121,4 +154,4 @@ fi
 
 readme > "${htmldir%/}/readme.html"
 #log
-#files
+files "$(tmrc)" "$htmldir" '.' > "${htmldir%/}/files.html"