git: 038be2aa83 - main - documentation: Add an option to archive/compress HTML offline files

From: Danilo G. Baio <dbaio_at_FreeBSD.org>
Date: Sat, 06 Nov 2021 11:10:04 UTC
The branch main has been updated by dbaio:

URL: https://cgit.FreeBSD.org/doc/commit/?id=038be2aa83b06064a952308730b6c7449d14e799

commit 038be2aa83b06064a952308730b6c7449d14e799
Author:     Danilo G. Baio <dbaio@FreeBSD.org>
AuthorDate: 2021-11-04 23:07:26 +0000
Commit:     Danilo G. Baio <dbaio@FreeBSD.org>
CommitDate: 2021-11-06 11:08:24 +0000

    documentation: Add an option to archive/compress HTML offline files
    
    Reviewed by:    carlavilla
    Differential Revision:  https://reviews.freebsd.org/D32867
---
 documentation/Makefile             | 23 ++++++++++-
 documentation/tools/asciidoctor.sh | 82 ++++++++++++++++++++++++++++++--------
 2 files changed, 86 insertions(+), 19 deletions(-)

diff --git a/documentation/Makefile b/documentation/Makefile
index 3e63ccef69..3b1a29d9bc 100644
--- a/documentation/Makefile
+++ b/documentation/Makefile
@@ -10,7 +10,10 @@
 # run		-	serves the built documentation site for local browsing
 # pdf		-	build PDF versions of the articles and books.
 # html		-	build HTML versions of the articles and books for
-# offline use
+#			offline use.
+#			If variable DOC_HTML_ARCHIVE is set, all documents will be
+#			archived/compressed, and only these files will be kept in the public
+#			directory.
 #
 # The run target uses hugo's built-in webserver to make the documentation site
 # available for local browsing.  The documentation should have been built prior
@@ -205,7 +208,7 @@ pdf-articles-clean:
 #
 # HTML targets
 #
-html: generate-books-toc-offline build-offline html-clean-global html-clean-articles html-clean-books
+html: generate-books-toc-offline build-offline html-clean-global html-clean-articles html-clean-books html-archive html-archive-clean-files
 
 html-clean: hugo-clean
 
@@ -223,3 +226,19 @@ html-clean-books:
 .for _lang in ${BOOK_LANGS}
 	rm -fr ${.CURDIR}/public/${_lang}/books/index.html
 .endfor
+
+html-archive:
+.if defined(DOC_HTML_ARCHIVE)
+.for _lang in ${ARTICLE_LANGS}
+	./tools/asciidoctor.sh articles ${_lang} archive
+.endfor
+.for _lang in ${BOOK_LANGS}
+	./tools/asciidoctor.sh books ${_lang} archive
+.endfor
+.endif
+
+html-archive-clean-files:
+.if defined(DOC_HTML_ARCHIVE)
+	find ${.CURDIR}/public/ ! -name '*.pdf' ! -name '*.tar.gz' -type f -delete
+	find ${.CURDIR}/public/ -type d -empty -delete
+.endif
diff --git a/documentation/tools/asciidoctor.sh b/documentation/tools/asciidoctor.sh
index 6f33181e25..28924d0b03 100755
--- a/documentation/tools/asciidoctor.sh
+++ b/documentation/tools/asciidoctor.sh
@@ -69,7 +69,7 @@ build_pdf() {
 		-r ./shared/lib/cross-document-references-macro.rb \
 		--doctype="$asciidoctor_type" \
 		-a skip-front-matter \
-		-a lang=${doc_lang} \
+		-a lang="$doc_lang" \
 		-a isonline=1 \
 		-a env-beastie=1 \
 		-a pdf-theme=default-with-fallback-font \
@@ -81,6 +81,49 @@ build_pdf() {
 # build_epub()
 
 
+archive() {
+	if [ "$1" = "" ] || [ "$2" = "" ] || [ "$3" = "" ]; then
+		exit 1
+	fi
+
+	local doc_type="$1"
+	local doc_lang="$2"
+	local doc_name="$3"
+
+	if [ -d "public/$doc_lang" ]; then
+		local pub_dir="public/$doc_lang/$doc_type/$doc_name/"
+	elif [ -d "public/$doc_type" ]; then
+		# single language build
+		local pub_dir="public/$doc_type/$doc_name/"
+	fi
+
+	if [ -f "${pub_dir}${doc_name}_${doc_lang}.tar.gz" ]; then
+		rm -f "${pub_dir}${doc_name}_${doc_lang}.tar.gz"
+	fi
+
+	local source_doc_dir=""
+	if [ -d "public/source/$doc_type/$doc_name/" ]; then
+		source_doc_dir="public/source/$doc_type/$doc_name/"
+	fi
+
+	local image_doc_dir=""
+	if [ -d "public/images/$doc_type/$doc_name/" ]; then
+		image_doc_dir="public/images/$doc_type/$doc_name/"
+	fi
+
+	tar -czf "public/${doc_name}_${doc_lang}.tar.gz" \
+		"$pub_dir" \
+		public/css/ \
+		public/fonts/ \
+		public/js/ \
+		$source_doc_dir \
+		$image_doc_dir
+
+	mv -f "public/${doc_name}_${doc_lang}.tar.gz" "$pub_dir"
+
+}
+
+
 main() {
 	if [ "$1" = "" ] || [ "$2" = "" ] || [ "$3" = "" ]; then
 		echo "Needs parameters (type, language and format)."
@@ -97,22 +140,27 @@ main() {
 		exit 1
 	fi
 
-	if [ ! "$doc_format" = "pdf" ]; then
-		# Default pdf
-		doc_format="pdf"
-	fi
-
-	for document in $(find "content/$doc_lang/$doc_type/" -type d -mindepth 1 -maxdepth 1 | awk -F '/' '{ print $4 }' | sort -n); do
-		if [ "$doc_format" = "pdf" ] && [ "$document" = "pgpkeys" ]; then
-			continue
-		fi
-
-		if [ "$doc_format" = "pdf" ]; then
-			echo "asciidoctor build_pdf: $doc_type $doc_lang $document $doc_format"
-			build_pdf "$doc_type" "$doc_lang" "$document"
-		fi
-
-	done
+	case "$doc_format" in
+		pdf)
+			for document in $(find "content/$doc_lang/$doc_type/" -type d -mindepth 1 -maxdepth 1 | awk -F '/' '{ print $4 }' | sort -n); do
+				if [ "$document" = "pgpkeys" ]; then
+					continue
+				fi
+				echo "asciidoctor build_pdf: $doc_type $doc_lang $document"
+				build_pdf "$doc_type" "$doc_lang" "$document"
+			done
+			;;
+		archive)
+			for document in $(find "content/$doc_lang/$doc_type/" -type d -mindepth 1 -maxdepth 1 | awk -F '/' '{ print $4 }' | sort -n); do
+				echo "generate archive: $doc_type $doc_lang $document"
+				archive "$doc_type" "$doc_lang" "$document"
+			done
+			;;
+		*)
+			echo "Formats available: archive, pdf"
+			exit 1
+			;;
+	esac
 }
 
 main "$@"