git: 82c271aa23 - main - documentation: Add experimental support for EPUB output

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

URL: https://cgit.FreeBSD.org/doc/commit/?id=82c271aa234060749b3fd104f5e5e028fb8d18df

commit 82c271aa234060749b3fd104f5e5e028fb8d18df
Author:     Danilo G. Baio <dbaio@FreeBSD.org>
AuthorDate: 2021-11-06 13:53:31 +0000
Commit:     Danilo G. Baio <dbaio@FreeBSD.org>
CommitDate: 2021-11-06 13:56:04 +0000

    documentation: Add experimental support for EPUB output
---
 documentation/Makefile             | 57 ++++++++++++++++++++++++++++++++++++
 documentation/tools/asciidoctor.sh | 59 ++++++++++++++++++++++++++++++++++++--
 2 files changed, 114 insertions(+), 2 deletions(-)

diff --git a/documentation/Makefile b/documentation/Makefile
index 3b1a29d9bc..96d7ffafef 100644
--- a/documentation/Makefile
+++ b/documentation/Makefile
@@ -14,6 +14,7 @@
 #			If variable DOC_HTML_ARCHIVE is set, all documents will be
 #			archived/compressed, and only these files will be kept in the public
 #			directory.
+# epub		-	build EPUB versions of the articles and books (Experimental).
 #
 # 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
@@ -116,6 +117,11 @@ requirements-pdf:
 	@(echo ${LOCALBASE}/bin/asciidoctor-pdf not found, please run 'pkg install rubygem-asciidoctor-pdf'; exit 1)
 .endif
 
+requirements-epub:
+.if !exists(${LOCALBASE}/bin/asciidoctor-epub3)
+	@(echo ${LOCALBASE}/bin/asciidoctor-epub3 not found, please run 'pkg install rubygem-asciidoctor-epub3'; exit 1)
+.endif
+
 starting-message: .PHONY
 	@echo ---------------------------------------------------------------
 	@echo                   Building the documentation
@@ -242,3 +248,54 @@ html-archive-clean-files:
 	find ${.CURDIR}/public/ ! -name '*.pdf' ! -name '*.tar.gz' -type f -delete
 	find ${.CURDIR}/public/ -type d -empty -delete
 .endif
+
+#
+# EPUB targets
+# Use DOC_LANG to choose the language, e.g., make DOC_LANG="en fr" pdf-books
+#
+epub: epub-articles epub-books
+
+epub-books: requirements-epub generate-books-toc
+	@echo ---------------------------------------------------------------
+	@echo !!! EPUB output is experimental !!!
+	@echo
+	@echo Asciidoctor EPUB3 is currently alpha software. Use accordingly. Although the
+	@echo bulk of AsciiDoc content is converted, there’s still work needed to fill in
+	@echo gaps where conversion is incomplete or unstyled.
+	@echo https://docs.asciidoctor.org/epub3-converter/latest/#project-status
+	@echo ---------------------------------------------------------------
+.for _lang in ${BOOK_LANGS}
+	./tools/asciidoctor.sh books ${_lang} epub
+.endfor
+
+epub-articles: requirements-epub
+	@echo ---------------------------------------------------------------
+	@echo !!! EPUB output is experimental !!!
+	@echo
+	@echo Asciidoctor EPUB3 is currently alpha software. Use accordingly. Although the
+	@echo bulk of AsciiDoc content is converted, there’s still work needed to fill in
+	@echo gaps where conversion is incomplete or unstyled.
+	@echo https://docs.asciidoctor.org/epub3-converter/latest/#project-status
+	@echo ---------------------------------------------------------------
+.for _lang in ${ARTICLE_LANGS}
+	./tools/asciidoctor.sh articles ${_lang} epub
+.endfor
+
+epub-clean: epub-articles-clean epub-books-clean
+
+epub-books-clean:
+.for _lang in ${BOOK_LANGS}
+	rm -fr ${.CURDIR}/public/${_lang}/books
+	-rmdir ${.CURDIR}/public/${_lang}
+.endfor
+	-rmdir ${.CURDIR}/public/
+
+epub-articles-clean:
+.for _lang in ${ARTICLE_LANGS}
+	rm -fr ${.CURDIR}/public/${_lang}/articles
+.if !exists(${.CURDIR}/public/${_lang}/books)
+	rm -fr ${.CURDIR}/public/${_lang}
+.endif
+.endfor
+	-rmdir ${.CURDIR}/public
+
diff --git a/documentation/tools/asciidoctor.sh b/documentation/tools/asciidoctor.sh
index 28924d0b03..a834b2e532 100755
--- a/documentation/tools/asciidoctor.sh
+++ b/documentation/tools/asciidoctor.sh
@@ -28,6 +28,7 @@
 
 LOCALBASE="/usr/local"
 ASCIIDOCTORPDF_CMD="${LOCALBASE}/bin/asciidoctor-pdf"
+ASCIIDOCTOREPUB_CMD="${LOCALBASE}/bin/asciidoctor-epub3"
 
 build_pdf() {
 	if [ "$1" = "" ] || [ "$2" = "" ] || [ "$3" = "" ]; then
@@ -78,7 +79,52 @@ build_pdf() {
 }
 
 
-# build_epub()
+build_epub() {
+	if [ "$1" = "" ] || [ "$2" = "" ] || [ "$3" = "" ]; then
+		exit 1
+	fi
+
+	local doc_type="$1"
+	local doc_lang="$2"
+	local doc_name="$3"
+
+	local cur_dir_source="content/$doc_lang/$doc_type/$doc_name/"
+	local cur_dir_output="public/$doc_lang/$doc_type/$doc_name/"
+
+	if [ ! -d "$cur_dir_output" ]; then
+		mkdir -p "$cur_dir_output"
+	fi
+
+	if [ "$doc_type" = "books" ]; then
+		local asciidoctor_type="book"
+
+		if [ -f "${cur_dir_source}book.adoc" ]; then
+			local asciidoctor_file_name="book.adoc"
+		else
+			local asciidoctor_file_name="_index.adoc"
+		fi
+	fi
+
+	if [ "$doc_type" = "articles" ]; then
+		local asciidoctor_type="article"
+		local asciidoctor_file_name="_index.adoc"
+	fi
+
+	$ASCIIDOCTOREPUB_CMD \
+		-r ./shared/lib/man-macro.rb \
+		-r ./shared/lib/git-macro.rb \
+		-r ./shared/lib/packages-macro.rb \
+		-r ./shared/lib/inter-document-references-macro.rb \
+		-r ./shared/lib/sectnumoffset-treeprocessor.rb \
+		-r ./shared/lib/cross-document-references-macro.rb \
+		--doctype="$asciidoctor_type" \
+		-a skip-front-matter \
+		-a lang="$doc_lang" \
+		-a isonline=1 \
+		-a env-beastie=1 \
+		-o "${cur_dir_output}${doc_name}_${doc_lang}_POC_.epub" \
+		"${cur_dir_source}${asciidoctor_file_name}"
+}
 
 
 archive() {
@@ -156,8 +202,17 @@ main() {
 				archive "$doc_type" "$doc_lang" "$document"
 			done
 			;;
+		epub)
+			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 epub: $doc_type $doc_lang $document"
+				build_epub "$doc_type" "$doc_lang" "$document"
+			done
+			;;
 		*)
-			echo "Formats available: archive, pdf"
+			echo "Formats available: archive, pdf, epub"
 			exit 1
 			;;
 	esac