diff --git a/documentation/Makefile b/documentation/Makefile --- a/documentation/Makefile +++ b/documentation/Makefile @@ -157,91 +157,19 @@ # pdf: pdf-articles pdf-books -pdf-books-target: +pdf-books: generate-books-toc .for _lang in ${BOOK_LANGS} -TMP+= ${.CURDIR}/content/${_lang}/books/*/ + ./tools/asciidoctor.sh books ${_lang} pdf .endfor -BOOKSDIR != echo ${TMP} -pdf-articles-target: +pdf-articles: .for _lang in ${ARTICLE_LANGS} -TTMP += ${.CURDIR}/content/${_lang}/articles/*/ -.endfor -ARTICLESDIR != echo ${TTMP} - -pdf-books: pdf-books-target generate-books-toc - -# Books build -# -# Notes: -# pdf-theme=default-with-fallback-font is -# used instead of pdf-theme=./themes/default-pdf-theme.yml because -# it allows ja, ru, tr, zh-* fonts to be embedded and rendering is -# better for other languages. -# -# asciidoctor-pdf-cjk and/or specific themes should be used for cjk -# fonts -.for _curpdf in ${BOOKSDIR} - @mkdir -p ${.CURDIR}/public${_curpdf:S|^${.CURDIR}/content||} -.if exists(${_curpdf}book.adoc) - ${ASCIIDOCTORPDF_CMD} \ - -r ./shared/lib/man-macro.rb \ - -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 \ - --doctype=book \ - -a skip-front-matter \ - -a pdf-theme=default-with-fallback-font \ - -o ${.CURDIR}/public${_curpdf:S|^${.CURDIR}/content||}book.pdf \ - ${_curpdf}book.adoc -.else -# some books use _index.adoc as main document - ${ASCIIDOCTORPDF_CMD} \ - -r ./shared/lib/man-macro.rb \ - -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 \ - --doctype=book \ - -a skip-front-matter \ - -a pdf-theme=default-with-fallback-font \ - -o ${.CURDIR}/public${_curpdf:S|^${.CURDIR}/content||}book.pdf \ - ${_curpdf}_index.adoc -.endif -.endfor - -pdf-articles: pdf-articles-target - -# Articles build -.for _curpdf in ${ARTICLESDIR} - @mkdir -p ${.CURDIR}/public${_curpdf:S|^${.CURDIR}/content||} - ${ASCIIDOCTORPDF_CMD} \ - -r ./shared/lib/man-macro.rb \ - -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 \ - --doctype=article \ - -a skip-front-matter \ - -a pdf-theme=default-with-fallback-font \ - -o ${.CURDIR}/public${_curpdf:S|^${.CURDIR}/content||}article.pdf \ - ${_curpdf}_index.adoc -.if exists(${.CURDIR}/static/source/articles/${_curpdf:H:T}) - cp -R ${.CURDIR}/static/source/articles/${_curpdf:H:T}/ \ - ${.CURDIR}/public${_curpdf:S|^${.CURDIR}/content||} -.endif + ./tools/asciidoctor.sh articles ${_lang} pdf .endfor pdf-clean: pdf-articles-clean pdf-books-clean -pdf-books-clean: pdf-books-target toc-clean -.for _curpdf in ${BOOKSDIR} - rm -f ${.CURDIR}/public${_curpdf:S|^${.CURDIR}/content||}book.pdf -.endfor +pdf-books-clean: toc-clean .for _lang in ${BOOK_LANGS} rm -fr ${.CURDIR}/public/${_lang}/books -rmdir ${.CURDIR}/public/${_lang} @@ -254,116 +182,14 @@ # html: html-articles html-books -html-books-target: +html-books: generate-books-toc .for _lang in ${BOOK_LANGS} -TMPH += ${.CURDIR}/content/${_lang}/books/*/ + ./tools/asciidoctor.sh books ${_lang} html .endfor -BOOKSDIR != echo ${TMPH} -html-articles-target: +html-articles: .for _lang in ${ARTICLE_LANGS} -TTMPH += ${.CURDIR}/content/${_lang}/articles/*/ -.endfor -ARTICLESDIR != echo ${TTMPH} - -html-books: html-books-target generate-books-toc - -# Books build -# -.for _curhtml in ${BOOKSDIR} - @mkdir -p ${.CURDIR}/public${_curhtml:S|^${.CURDIR}/content||} -.if exists(${_curhtml}book.adoc) - ${ASCIIDOCTOR_CMD} \ - -B ${.CURDIR}/ \ - -r ./shared/lib/man-macro.rb \ - -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 \ - --doctype=book \ - -a skip-front-matter \ - -a linkcss -a copycss=${.CURDIR}/themes/beastie/static/css/docbook.css -a stylesheet=docbook.css -a stylesdir=../../css \ - -a iconfont-remote! -a iconfont-name=font-awesome-min \ - -o ${.CURDIR}/public${_curhtml:S|^${.CURDIR}/content||}book.html \ - ${_curhtml}book.adoc -.if exists(${.CURDIR}/static/images/books/${_curhtml:H:T}) - @mkdir -p ${.CURDIR}/public${_curhtml:S|^${.CURDIR}/content||:H:H:H}/images/books/ - cp -R ${.CURDIR}/static/images/books/${_curhtml:H:T}/ \ - ${.CURDIR}/public${_curhtml:S|^${.CURDIR}/content||:H:H:H}/images/books/${_curhtml:H:T}/ - ${SED_CMD} -i '' -e "s|../../../../images|../../images|g" \ - ${.CURDIR}/public${_curhtml:S|^${.CURDIR}/content||}book.html -.endif -.else -# some books use _index.adoc as main document - ${ASCIIDOCTOR_CMD} \ - -B ${.CURDIR}/ \ - -r ./shared/lib/man-macro.rb \ - -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 \ - --doctype=book \ - -a skip-front-matter \ - -a linkcss -a copycss=${.CURDIR}/themes/beastie/static/css/docbook.css -a stylesheet=docbook.css -a stylesdir=../../css \ - -a iconfont-remote! -a iconfont-name=font-awesome-min \ - -o ${.CURDIR}/public${_curhtml:S|^${.CURDIR}/content||}book.html \ - ${_curhtml}_index.adoc -.if exists(${.CURDIR}/static/images/books/${_curhtml:H:T}) - @mkdir -p ${.CURDIR}/public${_curhtml:S|^${.CURDIR}/content||:H:H:H}/images/books/ - cp -R ${.CURDIR}/static/images/books/${_curhtml:H:T}/ \ - ${.CURDIR}/public${_curhtml:S|^${.CURDIR}/content||:H:H:H}/images/books/${_curhtml:H:T}/ - ${SED_CMD} -i '' -e "s|../../../../images|../../images|g" \ - ${.CURDIR}/public${_curhtml:S|^${.CURDIR}/content||}book.html -.endif -.endif -.if !exists(${.CURDIR}/public${_curhtml:S|^${.CURDIR}/content||:H:H:H}/fonts/) - mkdir -p ${.CURDIR}/public${_curhtml:S|^${.CURDIR}/content||:H:H:H}/fonts/ - cp -R ${.CURDIR}/themes/beastie/static/fonts/ \ - ${.CURDIR}/public${_curhtml:S|^${.CURDIR}/content||:H:H:H}/fonts/ - cp ${.CURDIR}/themes/beastie/static/css/font-awesome-min.css \ - ${.CURDIR}/public${_curhtml:S|^${.CURDIR}/content||:H:H:H}/css/ -.endif -.endfor - -html-articles: html-articles-target - -# Articles build -.for _curhtml in ${ARTICLESDIR} - @mkdir -p ${.CURDIR}/public${_curhtml:S|^${.CURDIR}/content||} - ${ASCIIDOCTOR_CMD} \ - -B ${.CURDIR}/ \ - -r ./shared/lib/man-macro.rb \ - -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 \ - --doctype=article \ - -a skip-front-matter \ - -a linkcss -a copycss=${.CURDIR}/themes/beastie/static/css/docbook.css -a stylesheet=docbook.css -a stylesdir=../../css \ - -a iconfont-remote! -a iconfont-name=font-awesome-min \ - -o ${.CURDIR}/public${_curhtml:S|^${.CURDIR}/content||}article.html \ - ${_curhtml}_index.adoc -.if exists(${.CURDIR}/static/source/articles/${_curhtml:H:T}) - cp -R ${.CURDIR}/static/source/articles/${_curhtml:H:T}/ \ - ${.CURDIR}/public${_curhtml:S|^${.CURDIR}/content||} -.endif -.if exists(${.CURDIR}/static/images/articles/${_curhtml:H:T}) - @mkdir -p ${.CURDIR}/public${_curhtml:S|^${.CURDIR}/content||:H:H:H}/images/articles/ - cp -R ${.CURDIR}/static/images/articles/${_curhtml:H:T}/ \ - ${.CURDIR}/public${_curhtml:S|^${.CURDIR}/content||:H:H:H}/images/articles/${_curhtml:H:T}/ - ${SED_CMD} -i '' -e "s|../../../images|../../images|g" \ - ${.CURDIR}/public${_curhtml:S|^${.CURDIR}/content||}article.html -.endif -.if !exists(${.CURDIR}/public${_curhtml:S|^${.CURDIR}/content||:H:H:H}/fonts/) - mkdir -p ${.CURDIR}/public${_curhtml:S|^${.CURDIR}/content||:H:H:H}/fonts/ - cp -R ${.CURDIR}/themes/beastie/static/fonts/ \ - ${.CURDIR}/public${_curhtml:S|^${.CURDIR}/content||:H:H:H}/fonts/ - cp ${.CURDIR}/themes/beastie/static/css/font-awesome-min.css \ - ${.CURDIR}/public${_curhtml:S|^${.CURDIR}/content||:H:H:H}/css/ -.endif + ./tools/asciidoctor.sh articles ${_lang} html .endfor html-clean: html-resources-clean html-articles-clean html-books-clean @@ -375,23 +201,14 @@ rm -fr ${.CURDIR}/public/${_lang}/images .endfor -html-books-clean: html-books-target toc-clean -.for _curhtml in ${BOOKSDIR} - rm -f ${.CURDIR}/public${_curhtml:S|^${.CURDIR}/content||}book.html ${_curhtml}toc*.adoc - -rmdir ${.CURDIR}/public${_curhtml:S|^${.CURDIR}/content||} -.endfor +html-books-clean: toc-clean .for _lang in ${BOOK_LANGS} rm -fr ${.CURDIR}/public/${_lang}/books - rm -fr ${.CURDIR}/public/${_lang}/images/books -rmdir ${.CURDIR}/public/${_lang} .endfor -rmdir ${.CURDIR}/public -html-articles-clean: html-articles-target -.for _curhtml in ${ARTICLESDIR} - rm -f ${.CURDIR}/public${_curhtml:S|^${.CURDIR}/content||}article.html - -rmdir ${.CURDIR}/public${_curhtml:S|^${.CURDIR}/content||} -.endfor +html-articles-clean: .for _lang in ${ARTICLE_LANGS} rm -fr ${.CURDIR}/public/${_lang}/articles rm -fr ${.CURDIR}/public/${_lang}/images/articles @@ -399,10 +216,7 @@ .endfor -rmdir ${.CURDIR}/public -pdf-articles-clean: pdf-articles-target -.for _curpdf in ${ARTICLESDIR} - rm -f ${.CURDIR}/public${_curpdf:S|^${.CURDIR}/content||}article.pdf -.endfor +pdf-articles-clean: .for _lang in ${ARTICLE_LANGS} rm -fr ${.CURDIR}/public/${_lang}/articles .if !exists(${.CURDIR}/public/${_lang}/books) diff --git a/documentation/tools/asciidoctor.sh b/documentation/tools/asciidoctor.sh new file mode 100755 --- /dev/null +++ b/documentation/tools/asciidoctor.sh @@ -0,0 +1,263 @@ +#!/bin/sh +# +# Copyright (c) 2021 Danilo G. Baio +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions are met: +# +# 1. Redistributions of source code must retain the above copyright notice, this +# list of conditions and the following disclaimer. +# +# 2. Redistributions in binary form must reproduce the above copyright notice, +# this list of conditions and the following disclaimer in the documentation +# and/or other materials provided with the distribution. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND +# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE +# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, +# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +# shellcheck disable=SC3043 + + +LOCALBASE="/usr/local" +ASCIIDOCTOR_CMD="${LOCALBASE}/bin/asciidoctor" +ASCIIDOCTORPDF_CMD="${LOCALBASE}/bin/asciidoctor-pdf" + +ARCHIVE=1 + + +temp_directory() { + if [ "$1" = "" ] || [ "$2" = "" ] || [ "$3" = "" ]; then + exit 1 + fi + + local doc_type="$1" + local doc_lang="$2" + local doc_name="$3" + + local cur_dir_source="public/.${doc_type}_${doc_lang}_${doc_name}" + local cur_dir_output="public/${doc_lang}/${doc_type}/${doc_name}" + + # Create and clean temporary directory (Output) + if [ ! -d "$cur_dir_output" ]; then + mkdir -p "$cur_dir_output" + fi + + # Create and clean temporary directory (Source) + if [ -d "$cur_dir_source" ]; then + rm -rf "$cur_dir_source" + fi + mkdir -p "$cur_dir_source" + + cp -a -R "content/$doc_lang/$doc_type/${doc_name}/" \ + "public/.${doc_type}_${doc_lang}_${doc_name}/" +} + + +build_html() { + if [ "$1" = "" ] || [ "$2" = "" ] || [ "$3" = "" ]; then + exit 1 + fi + + local doc_type="$1" + local doc_lang="$2" + local doc_name="$3" + + temp_directory "$doc_type" "$doc_lang" "$doc_name" + + local cur_dir_source="public/.${doc_type}_${doc_lang}_${doc_name}/" + local cur_dir_output="public/${doc_lang}/${doc_type}/${doc_name}/" + + 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 + + #### + # Sed's here for Hugo --> Asciidoctor adjusts: + # + # - Hugo's lang shortcode -> Asciidoctor language: + # {{% lang %}} --> {lang} + #### + + $ASCIIDOCTOR_CMD \ + -B "./" \ + -D "${cur_dir_source}output" \ + -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 \ + --doctype="$asciidoctor_type" \ + -a skip-front-matter \ + -a linkcss \ + -a copycss="themes/beastie/static/css/docbook.css" \ + -a stylesheet=docbook.css \ + -a iconfont-remote! \ + -a iconfont-name=font-awesome-min \ + -o index.html \ + "${cur_dir_source}${asciidoctor_file_name}" + + # Handle images per document + if [ -d "static/images/$doc_type/$doc_name" ]; then + if [ ! -d "public/images/$doc_type/$doc_name" ]; then + mkdir -p "public/images/$doc_type" >/dev/null 2>&1 + cp -R "static/images/$doc_type/$doc_name/" \ + "public/images/$doc_type/$doc_name/" + fi + fi + + # Fonts + if [ ! -d public/fonts ]; then + mkdir public/fonts + cp -R themes/beastie/static/fonts/ public/fonts/ + fi + sed 's|../fonts|../../../fonts|g' themes/beastie/static/css/font-awesome-min.css > \ + "$cur_dir_output/font-awesome-min.css" + + # Final + cp -R "$cur_dir_source/output/" "$cur_dir_output" + rm -rf "$cur_dir_source" + + # HTML Archive + if [ $ARCHIVE -eq 1 ]; then + cd public || exit 1 + if [ -d "images/$doc_type/$doc_name" ]; then + tar -czf "${doc_name}_${doc_lang}.tar.gz" \ + "${doc_lang}/${doc_type}/${doc_name}/" fonts/ "images/$doc_type/$doc_name/" + else + tar -czf "${doc_name}_${doc_lang}.tar.gz" \ + "${doc_lang}/${doc_type}/${doc_name}/" fonts/ + fi + mv "${doc_name}_${doc_lang}.tar.gz" "${doc_lang}/${doc_type}/${doc_name}/" + cd - || exit 1 + fi +} + + +build_pdf() { + if [ "$1" = "" ] || [ "$2" = "" ] || [ "$3" = "" ]; then + exit 1 + fi + + local doc_type="$1" + local doc_lang="$2" + local doc_name="$3" + + temp_directory "$doc_type" "$doc_lang" "$doc_name" + + local cur_dir_source="public/.${doc_type}_${doc_lang}_${doc_name}/" + local cur_dir_output="public/${doc_lang}/${doc_type}/${doc_name}/" + + 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 + + #### + # Sed's here for Hugo --> Asciidoctor adjusts: + # + # - Hugo's lang shortcode -> Asciidoctor language: + # {{% lang %}} --> {lang} + # + # - Includes. Basedir doesn't work properly with asciidoctor-pdf + #### + + find "$cur_dir_source" -name "*.adoc" \ + -exec sed -i '' \ + -e "s|^include::../../../../|include::../../|g" \ + -e 's|^:imagesdir: ../../../../|:imagesdir: ../../|g' \ + -e 's|^include::shared/|include::../../shared/|g' \ + -e 's|^include::content/|include::../../content/|g' \ + -e 's|^include::static/|include::../../static/|g' \ + -e 's|^include::{pgpkeys-path}static/|include::../../../static/|g' \ + {} \; + + $ASCIIDOCTORPDF_CMD \ + -r ./shared/lib/man-macro.rb \ + -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 \ + --doctype="$asciidoctor_type" \ + -a skip-front-matter \ + -a pdf-theme=default-with-fallback-font \ + -o "${cur_dir_output}${doc_name}_${doc_lang}.pdf" \ + "${cur_dir_source}${asciidoctor_file_name}" + + rm -rf "$cur_dir_source" +} + + +# build_epub() + + +main() { + if [ "$1" = "" ] || [ "$2" = "" ] || [ "$3" = "" ]; then + echo "Needs parameters (type, language and format)." + echo "$0 articles en html" + exit 1 + fi + + local doc_type="$1" + local doc_lang="$2" + local doc_format="$3" + + if [ ! "$doc_type" = "articles" ] && [ ! "$doc_type" = "books" ]; then + echo "First parameter needs to be 'articles' or 'books'" + exit 1 + fi + + if [ ! "$doc_format" = "html" ] && [ ! "$doc_format" = "pdf" ]; then + # Default html with archive + doc_format="html" + ARCHIVE=1 + 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 + + if [ "$doc_format" = "html" ]; then + echo "asciidoctor build_html: $doc_type $doc_lang $document $doc_format" + build_html "$doc_type" "$doc_lang" "$document" + fi + done +} + +main "$@" +