noxz-sites

A collection of a builder and various scripts creating the noxz.tech sites
git clone https://noxz.tech/git/noxz-sites.git
noxz-sites

commit: 486385f711eaa6f8205082f6b001ccae7e0c5345
parent: 3ec298a498527f7da9e5b4bed5575d50f3c29a9c
author: Chris Noxz <chris@noxz.tech>
date:   Sun, 26 Jun 2022 17:23:39 +0200
add feature for file preview in build script
Mbuild169++++++++++++++++----
Mgit.noxz.tech/pub/style.css16++
2 files changed, 154 insertions(+), 31 deletions(-)
diff --git a/build b/build
@@ -417,30 +417,68 @@ render_main() {
 	print_done
 }
 
+render_git_file() {
+	printf '\t<span class="git-file">%s</span>\n\t<hr />\n' "${2}"
+	if git -C "${1}" diff 4b825dc642cb6eb9a060e54bf8d69288fbee4904          \
+		--numstat HEAD -- "${2}" | grep '^-' >/dev/null 2>&1; then
+		printf '\t%s\n' "binary file."
+	else
+		git -C "${1}" cat-file "HEAD:${2}" -p                               \
+		| html_encode                                                       \
+		| awk '
+			BEGIN {
+				print "<pre>"
+			}
+
+			{
+				print "<span>" NR "</span>" $0
+			}
+
+			END {
+				print "</pre>"
+			}
+		'
+	fi
+}
+
 render_git_repo() {
 	repo_path="${SITE_GIT_REPO_ROOT}/$2"
-	repo_path_head="$1/${2}/head.html.tmp"
-	repo_path_log="$1/${2}/log.html.tmp"
-	repo_path_tags="$1/${2}/tags.html.tmp"
-	repo_path_readme="$1/${2}/readme.html.tmp"
-	repo_path_license="$1/${2}/license.html.tmp"
-	repo_path_archive="$1/${2}/archive"
+	repo_path_head="${1}/${2}/head.html.tmp"
+	repo_path_log="${1}/${2}/log.html.tmp"
+	repo_path_files="${1}/${2}/files.html.tmp"
+	repo_path_file="${1}/${2}/file"
+	repo_path_tags="${1}/${2}/tags.html.tmp"
+	repo_path_readme="${1}/${2}/readme.html.tmp"
+	repo_path_license="${1}/${2}/license.html.tmp"
+	repo_path_archive="${1}/${2}/archive"
 	repo_name="${2%.*}"
 	repo_description=""
 
-	rsync -a --delete-before "${repo_path}/" "$1/${2##*/}"
-	git -C "$1/${2##*/}" --bare update-server-info
-	ln -s "refs" "$1/${2##*/}/info/refs?service=git-upload-pack"
+	rsync -a --delete-before "${repo_path}/" "${1}/${2##*/}"
+	git -C "${1}/${2##*/}" --bare update-server-info
+	ln -s "refs" "${1}/${2##*/}/info/refs?service=git-upload-pack"
 
 	repo_description="$(< "${1}/${2}"/description html_encode)"
 
-	# create archive files
+	# create archive files (for each tag)
 	mkdir "${repo_path_archive}"
-	git -C "$1/${2}" tag                                                    \
-		| xargs -I{} git -C "$1/${2}" archive                               \
+	git -C "${1}/${2}" tag                                                  \
+		| xargs -I{} git -C "${1}/${2}" archive                             \
 			--format=tar.gz --prefix="${repo_name}-{}/"                     \
 			-o "archive/${repo_name}-{}.tar.gz" "{}"
 
+	# create preview files
+	mkdir "${repo_path_file}"
+	git -C "${1}/${2}" ls-tree                                              \
+		-r --format="%(objectmode)%x0a%(path)" HEAD                         \
+		| grep '^100[0-9][0-9][0-9]$' -A1                                   \
+		| grep -v '^--$\|^100[0-9][0-9][0-9]$'                              \
+		| while read -r file; do
+			file_path="${repo_path_file}/${file}"
+			mkdir -p "${file_path%/*}"
+			render_git_file "${1}/${2}" "${file}" > "${file_path}.html.tmp"
+		done
+
 	# render common html head
 	awk                                                                     \
 		-v var_site="$(awk_safe "${1}")"                                    \
@@ -449,15 +487,14 @@ render_git_repo() {
 		-v var_name="$(awk_safe "${repo_name}")"                            \
 		-v var_description="$(awk_safe "${repo_description}")"              \
 		-v var_menu="$(
-			git -C "$1/${2}" show HEAD:README >/dev/null 2>&1 ||            \
-			git -C "$1/${2}" show HEAD:LICENSE >/dev/null 2>&1 &&           \
-			printf '<a href="log.html">Log</a>'
-			git -C "$1/${2}" describe --tags >/dev/null 2>&1 &&
-			printf ' | <a href="tags.html">Tags</a>'
-			git -C "$1/${2}" show HEAD:README >/dev/null 2>&1 &&            \
-			printf ' | <a href="readme.html">README</a>'
-			git -C "$1/${2}" show HEAD:LICENSE >/dev/null 2>&1 &&           \
-			printf ' | <a href="license.html">LICENSE</a>'
+			printf '<a href="//%s/log.html">Log</a>' "${1}/${2}"
+			printf ' | <a href="//%s/files.html">Files</a>' "${1}/${2}"
+			git -C "${1}/${2}" describe --tags >/dev/null 2>&1 &&
+			printf ' | <a href="//%s/tags.html">Tags</a>' "${1}/${2}"
+			git -C "${1}/${2}" show HEAD:README >/dev/null 2>&1 &&          \
+			printf ' | <a href="//%s/readme.html">README</a>' "${1}/${2}"
+			git -C "${1}/${2}" show HEAD:LICENSE >/dev/null 2>&1 &&         \
+			printf ' | <a href="//%s/license.html">LICENSE</a>' "${1}/${2}"
 		)"                                                                  \
 	'
 		{
@@ -472,7 +509,7 @@ render_git_repo() {
 	' "${CFG_GIT_HEAD_HTML}" > "${repo_path_head}"
 
 	# render log
-	git -C "$1/${2}" log                                                    \
+	git -C "${1}/${2}" log                                                  \
 		--pretty="format:%ai%n%s%n%an%n%h"                                  \
 	| html_encode                                                           \
 	| awk '
@@ -504,8 +541,78 @@ render_git_repo() {
 		}
 	' > "${repo_path_log}"
 
+	# render files
+	git -C "${1}/${2}" ls-tree                                              \
+		-r --format="%(objectmode)%x0a%(path)%x0a%(objectsize)" HEAD        \
+	| html_encode                                                           \
+	| awk '
+		function oct2sym(data)
+		{
+			type = substr(data, 0, 3)
+
+			if (type == "100") {
+				type = "-"
+			} else if (type == "060") {
+				type = "b"
+			} else if (type == "020") {
+				type = "c"
+			} else if (type == "040") {
+				type = "d"
+			} else if (type == "010") {
+				type = "p"
+			} else if (type == "120") {
+				type = "l"
+			} else if (type == "140") {
+				type = "s"
+			} else {
+				type = "?"
+			}
+
+			sym = substr(data, 4, 3)
+			gsub("0", "---", sym)
+			gsub("1", "--x", sym)
+			gsub("2", "-w-", sym)
+			gsub("3", "-wx", sym)
+			gsub("4", "r--", sym)
+			gsub("5", "r-x", sym)
+			gsub("6", "rw-", sym)
+			gsub("7", "rwx", sym)
+			return type sym
+		}
+
+		BEGIN {
+			printf "\t<table id=\"files\">\n"
+			printf "\t\t<tr class=\"nohi\">"
+			printf "<td>Mode</td>"
+			printf "<td>Name</td>"
+			printf "<td>Size</td>"
+			printf "</tr>\n"
+			printf "\t\t<tbody>\n"
+		}
+
+		{
+			entry[(NR-1) % 3] = $0
+			if (NR % 3 == 0) {
+				printf "\t\t\t<tr>"
+				for (i = 0; i < 3; i++) {
+					if (i == 1 && substr(entry[0], 0, 3) == "100") {
+						printf "<td><a href=\"./file/%s.html\">%s</a></td>",entry[i],entry[i]
+					} else {
+						printf "<td>%s</td>", i == 0 ? oct2sym(entry[i]) : entry[i]
+					}
+				}
+				printf "</tr>\n"
+			}
+		}
+
+		END {
+			printf "\t\t</tbody>\n"
+			printf "\t</table>\n"
+		}
+	' > "${repo_path_files}"
+
 	# render tags
-	git -C "$1/${2}" log                                                    \
+	git -C "${1}/${2}" log                                                  \
 		--tags --pretty="format:%S%n%ai%n%an%n%h"                           \
 	| html_encode                                                           \
 	| awk '
@@ -542,8 +649,8 @@ render_git_repo() {
 	' | sed "s/__REPONAME__/${repo_name}/g" > "${repo_path_tags}"
 
 	# render readme if existing
-	git -C "$1/${2}" show HEAD:README >/dev/null 2>&1 &&                    \
-	git -C "$1/${2}" cat-file HEAD:README -p                                \
+	git -C "${1}/${2}" show HEAD:README >/dev/null 2>&1 &&                  \
+	git -C "${1}/${2}" cat-file HEAD:README -p                              \
 	| html_encode                                                           \
 	| awk '
 		BEGIN {
@@ -557,12 +664,12 @@ render_git_repo() {
 		END {
 			print "</pre>"
 		}
-	' > "${repo_path_readme}" &&                                                \
-	ln -s "readme.html" "$1/${2}/index.html"
+	' > "${repo_path_readme}" &&                                            \
+	ln -s "readme.html" "${1}/${2}/index.html"
 
 	# render license if existing
-	git -C "$1/${2}" show HEAD:LICENSE >/dev/null 2>&1 &&                   \
-	git -C "$1/${2}" cat-file HEAD:LICENSE -p                               \
+	git -C "${1}/${2}" show HEAD:LICENSE >/dev/null 2>&1 &&                 \
+	git -C "${1}/${2}" cat-file HEAD:LICENSE -p                             \
 	| html_encode                                                           \
 	| awk '
 		BEGIN {
@@ -578,7 +685,7 @@ render_git_repo() {
 		}
 	' > "${repo_path_license}"
 
-	[ ! -h "$1/${2}/index.html" ] && ln -s "log.html" "$1/${2}/index.html"
+	[ ! -h "${1}/${2}/index.html" ] && ln -s "log.html" "${1}/${2}/index.html"
 }
 
 render_git() {
@@ -650,7 +757,7 @@ render_git() {
 		[ "${file##*/}" = "head.html.tmp" ] && continue
 		{
 			awk '1;/<body>/{exit}' "${git_index}"
-			cat "${file%/*}/head.html.tmp"
+			cat "${file%%.git/*}.git/head.html.tmp"
 			cat "${file}"
 			awk '/<\/body>/,0' "${git_index}"
 		} > "${file%.*}"
diff --git a/git.noxz.tech/pub/style.css b/git.noxz.tech/pub/style.css
@@ -35,15 +35,18 @@ table td {
 }
 
 #index,
+#files,
 #log,
 #tags,
 #index td:nth-child(2),
+#files td:nth-child(2),
 #log td:nth-child(2),
 #tags td:nth-child(2) {
 	width                           : 100%;
 }
 
 #index td,
+#files td,
 #log td,
 #tags td {
 	padding                         : 0.2em 0.4em;
@@ -52,6 +55,7 @@ table td {
 }
 
 #index tr:hover td,
+#files tr:hover td,
 #log tr:hover td,
 #tags tr:hover td {
 	background-color                : #f3f1f9;
@@ -63,11 +67,23 @@ tr.nohi:hover td {
 }
 
 #index tr td:nth-child(2),
+#files tr td:nth-child(2),
 #log tr td:nth-child(2),
 #tags tr td:nth-child(2) {
 	white-space                     : normal;
 }
 
+#files tr td:nth-child(3) {
+	text-align                      : right;
+}
+
+#files tr td:nth-child(3):after {
+	content                         : " bytes"
+}
+#files tr.nohi td:nth-child(3):after {
+	content                         : ""
+}
+
 .desc {
 	color                           : #777;
 }