Path: blob/main/documentation/tools/asciidoctor.sh
18081 views
#!/bin/sh1#2# Copyright (c) 2021 Danilo G. Baio <[email protected]>3#4# Redistribution and use in source and binary forms, with or without5# modification, are permitted provided that the following conditions are met:6#7# 1. Redistributions of source code must retain the above copyright notice, this8# list of conditions and the following disclaimer.9#10# 2. Redistributions in binary form must reproduce the above copyright notice,11# this list of conditions and the following disclaimer in the documentation12# and/or other materials provided with the distribution.13#14# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND15# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED16# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE17# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE18# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL19# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR20# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER21# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,22# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE23# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.2425# shellcheck disable=SC3043262728LOCALBASE="/usr/local"2930if [ "$USE_RUBYGEMS" = "YES" ]; then31GEMBASE="${GEM_PATH}"32else33GEMBASE="${LOCALBASE}"34fi3536ASCIIDOCTORPDF_CMD="${GEMBASE}/bin/asciidoctor-pdf"37ASCIIDOCTOREPUB3_CMD="${GEMBASE}/bin/asciidoctor-epub3"3839build_pdf() {40if [ "$1" = "" ] || [ "$2" = "" ] || [ "$3" = "" ]; then41exit 142fi4344local doc_type="$1"45local doc_lang="$2"46local doc_name="$3"4748local cur_dir_source="content/$doc_lang/$doc_type/$doc_name/"49local cur_dir_output="public/$doc_lang/$doc_type/$doc_name/"5051local theme_font=""5253if [ ! -d "$cur_dir_output" ]; then54mkdir -p "$cur_dir_output"55fi5657if [ "$doc_type" = "books" ]; then58local asciidoctor_type="book"5960if [ -f "${cur_dir_source}book.adoc" ]; then61local asciidoctor_file_name="book.adoc"62else63local asciidoctor_file_name="_index.adoc"64fi65fi6667if [ "$doc_type" = "articles" ]; then68local asciidoctor_type="article"69local asciidoctor_file_name="_index.adoc"70fi7172# Check non default fonts73case "$doc_lang" in74zh-cn)75if [ ! -f "$LOCALBASE/share/docproj-fonts-cjk/NotoSansSC-Medium.otf" ]; then76echo " font not found, skipping pdf build"77return78fi79theme_font="-a pdf-theme=./shared/zh-cn/zh-cn-theme.yml -a pdf-fontsdir=$LOCALBASE/share/docproj-fonts-cjk"80;;81zh-tw)82if [ ! -f "$LOCALBASE/share/docproj-fonts-cjk/NotoSansTC-Medium.otf" ]; then83echo " font not found, skipping pdf build"84return85fi86theme_font="-a pdf-theme=./shared/zh-tw/zh-tw-theme.yml -a pdf-fontsdir=$LOCALBASE/share/docproj-fonts-cjk/"87;;88*)89theme_font="-a pdf-theme=default-with-fallback-font"90;;91esac9293$ASCIIDOCTORPDF_CMD \94-r ./shared/lib/man-macro.rb \95-r ./shared/lib/git-macro.rb \96-r ./shared/lib/packages-macro.rb \97-r ./shared/lib/inter-document-references-macro.rb \98-r ./shared/lib/sectnumoffset-treeprocessor.rb \99-r ./shared/lib/cross-document-references-macro.rb \100--doctype="$asciidoctor_type" \101-a skip-front-matter \102-a lang="$doc_lang" \103-a isonline=1 \104-a env-beastie=1 \105${theme_font} \106-o "${cur_dir_output}${doc_name}_${doc_lang}.pdf" \107"${cur_dir_source}${asciidoctor_file_name}"108}109110111build_epub() {112if [ "$1" = "" ] || [ "$2" = "" ] || [ "$3" = "" ]; then113exit 1114fi115116local doc_type="$1"117local doc_lang="$2"118local doc_name="$3"119120local cur_dir_source="content/$doc_lang/$doc_type/$doc_name/"121local cur_dir_output="public/$doc_lang/$doc_type/$doc_name/"122123if [ ! -d "$cur_dir_output" ]; then124mkdir -p "$cur_dir_output"125fi126127if [ "$doc_type" = "books" ]; then128local asciidoctor_type="book"129130if [ -f "${cur_dir_source}book.adoc" ]; then131local asciidoctor_file_name="book.adoc"132else133local asciidoctor_file_name="_index.adoc"134fi135fi136137if [ "$doc_type" = "articles" ]; then138local asciidoctor_type="article"139local asciidoctor_file_name="_index.adoc"140fi141142$ASCIIDOCTOREPUB3_CMD \143-r ./shared/lib/man-macro.rb \144-r ./shared/lib/git-macro.rb \145-r ./shared/lib/packages-macro.rb \146-r ./shared/lib/inter-document-references-macro.rb \147-r ./shared/lib/sectnumoffset-treeprocessor.rb \148-r ./shared/lib/cross-document-references-macro.rb \149--doctype="$asciidoctor_type" \150-a skip-front-matter \151-a lang="$doc_lang" \152-a isonline=1 \153-a env-beastie=1 \154-o "${cur_dir_output}${doc_name}_${doc_lang}_POC_.epub" \155"${cur_dir_source}${asciidoctor_file_name}"156}157158159archive() {160if [ "$1" = "" ] || [ "$2" = "" ] || [ "$3" = "" ]; then161exit 1162fi163164local doc_type="$1"165local doc_lang="$2"166local doc_name="$3"167168if [ -d "public/$doc_lang" ]; then169local pub_dir="public/$doc_lang/$doc_type/$doc_name/"170elif [ -d "public/$doc_type" ]; then171# single language build172local pub_dir="public/$doc_type/$doc_name/"173fi174175if [ -f "${pub_dir}${doc_name}_${doc_lang}.tar.gz" ]; then176rm -f "${pub_dir}${doc_name}_${doc_lang}.tar.gz"177fi178179local source_doc_dir=""180if [ -d "public/source/$doc_type/$doc_name/" ]; then181source_doc_dir="public/source/$doc_type/$doc_name/"182fi183184local image_doc_dir=""185if [ -d "public/images/$doc_type/$doc_name/" ]; then186image_doc_dir="public/images/$doc_type/$doc_name/"187fi188189tar -czf "public/${doc_name}_${doc_lang}.tar.gz" \190"$pub_dir" \191public/css/ \192public/fonts/ \193public/images/FreeBSD-colors.svg \194public/images/FreeBSD-monochromatic.svg \195public/js/ \196public/styles/ \197$source_doc_dir \198$image_doc_dir199200mv -f "public/${doc_name}_${doc_lang}.tar.gz" "$pub_dir"201202}203204205main() {206if [ "$1" = "" ] || [ "$2" = "" ] || [ "$3" = "" ]; then207echo "Needs parameters (type, language and format)."208echo "$0 articles en pdf"209exit 1210fi211212local doc_type="$1"213local doc_lang="$2"214local doc_format="$3"215216if [ ! "$doc_type" = "articles" ] && [ ! "$doc_type" = "books" ]; then217echo "First parameter needs to be 'articles' or 'books'"218exit 1219fi220221case "$doc_format" in222pdf)223for document in $(find "content/$doc_lang/$doc_type/" -type d -mindepth 1 -maxdepth 1 | awk -F '/' '{ print $4 }' | sort -n); do224if [ "$document" = "pgpkeys" ]; then225continue226fi227echo "asciidoctor build_pdf: $doc_type $doc_lang $document"228build_pdf "$doc_type" "$doc_lang" "$document"229done230;;231archive)232for document in $(find "content/$doc_lang/$doc_type/" -type d -mindepth 1 -maxdepth 1 | awk -F '/' '{ print $4 }' | sort -n); do233echo "generate archive: $doc_type $doc_lang $document"234archive "$doc_type" "$doc_lang" "$document"235done236;;237epub)238for document in $(find "content/$doc_lang/$doc_type/" -type d -mindepth 1 -maxdepth 1 | awk -F '/' '{ print $4 }' | sort -n); do239if [ "$document" = "pgpkeys" ]; then240continue241fi242echo "asciidoctor epub: $doc_type $doc_lang $document"243build_epub "$doc_type" "$doc_lang" "$document"244done245;;246*)247echo "Formats available: archive, pdf, epub"248exit 1249;;250esac251}252253main "$@"254255256