#!/bin/sh # -*- indent-tabs-mode: nil -*- # # yelp-build # Copyright (C) 2010-2015 Shaun McCance # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, but # WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU # General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. xsl_mal_cache='@XSL_MAL_CACHE@' xsl_db2html='@XSL_DB2HTML@' xsl_db2xhtml='@XSL_DB2XHTML@' xsl_mal2html='@XSL_MAL2HTML@' xsl_mal2xhtml='@XSL_MAL2XHTML@' yelp_icon_dir='@YELP_ICON_DIR@' yelp_js_dir='@YELP_JS_DIR@' xsl_mal_epub='@DATADIR@/xslt/mal-epub.xsl' xsl_mal_opf='@DATADIR@/xslt/mal-opf.xsl' xsl_mal_ncx='@DATADIR@/xslt/mal-ncx.xsl' xsl_mal_files='@DATADIR@/xslt/mal-files.xsl' mkdir_p () { if [ ! -d "$1" ]; then mkdir_p `dirname "$1"` mkdir "$1" fi } urlencode () { LC_ALL=C awk ' BEGIN { for (i = 1; i <= 255; i++) chars[sprintf("%c", i)] = i; } { ret = ""; for (i = 1; i <= length($0); i++) { c = substr($0, i, 1); if (c ~ /[\/a-zA-Z0-9._-]/) ret = ret c; else ret = ret sprintf("%%%X%X", int(chars[c] / 16), chars[c] % 16); } print ret; }' } urldecode () { LC_ALL=C awk ' BEGIN { for(i = 0; i < 10; i++) hex[i] = i; hex["A"] = hex["a"] = 10; hex["B"] = hex["b"] = 11; hex["C"] = hex["c"] = 12; hex["D"] = hex["d"] = 13; hex["E"] = hex["e"] = 14; hex["F"] = hex["f"] = 15; } { ret = ""; for (i = 1; i <= length($0); i++) { c = substr($0, i, 1); if (c == "+") { ret = ret " "; } else if (c == "%") { c = sprintf("%c", hex[substr($0, i + 1, 1)] * 16 + hex[substr($0, i + 2, 1)]); ret = ret c; i += 2; } else { ret = ret c; } } print ret; }' } yelp_usage () { ( echo "Usage: yelp-build [OPTIONS] [FILES]" echo "" echo "Commands:" echo " cache Create a Mallard cache file" echo " epub Create an EPUB file for Mallard" echo " html Convert input files to HTML" echo " xhtml Convert input files to XHTML" ) 1>&2 } yelp_usage_cache () { ( echo "Usage: yelp-build cache " echo "" echo " Create a Mallard cache file from the page files FILES." echo " If FILES contains directories, all .page files in those" echo " directories will be used." echo "" echo "Options:" echo " -o OUT Output cache to the file OUT" echo " -s Treat pages as belonging to a Mallard site" ) 1>&2 } yelp_usage_epub () { ( echo "Usage: yelp-build epub [OPTIONS] " echo "" echo " Create and EPUB file from the Mallard page files FILES." echo "" echo "Options:" echo " -c CACHE Use the existing Mallard cache CACHE" echo " -o OUT Output en EPUB file named OUT" echo " -x CUSTOM Import the custom XSLT file CUSTOM" echo " -p PATHS Extra paths to search for files" echo " -i Ignore missing media files" ) 1>&2 } yelp_usage_html () { ( echo "Usage: yelp-build [OPTIONS] " echo "" echo " Create HTML or XHTML from the input files FILES." echo " FILES can be DocBook files, Mallard page files," echo " or directories containing Mallard page files." echo "" echo "Options:" echo " -c CACHE Use the existing Mallard cache CACHE" echo " -o OUT Output files in the directory OUT" echo " -x CUSTOM Import the custom XSLT file CUSTOM" echo " -p PATHS Extra paths to search for files" echo " -i Ignore missing media files" ) 1>&2 } if [ $# = 0 ]; then yelp_usage exit 1 fi yelp_paths_normalize () { for path in $(echo "$1" | sed -e 's/[: ]/\n/g'); do (cd "$path" && pwd) done | tr '\n' ' ' } yelp_cache_in_page () { fbase=$(basename "$1") fdir=$( (cd $(dirname "$1") && pwd) ) sdir=${fdir##${cache_site_root}}/ url=file://$(echo "$fdir/$fbase" | urlencode) if [ "x$cache_site" = "x1" ]; then siteattr=' site:dir="'"$sdir"'"' fi echo '' } yelp_cache_in_site () { for dir in "$1"/*; do if [ -d "$dir" ]; then if [ $(basename "$dir") != "__pintail__" ]; then yelp_cache_in_site "$dir" fi fi done for page in "$1"/*.page; do if [ -e "$page" ]; then yelp_cache_in_page "$page" fi done } yelp_cache_in () { echo '' for page in "$@"; do if [ -d "$page" ]; then if [ "x$cache_site" = "x1" ]; then yelp_cache_in_site "$page" else for sub in "$page"/*.page; do yelp_cache_in_page "$sub" done fi else yelp_cache_in_page "$page" fi done echo '' } yelp_cache () { cache_out="index.cache" while [ "$#" != "0" ]; do case "$1" in "-o") shift cache_out="$1" shift ;; "-s") cache_site="1" cache_site_root=$(pwd) shift ;; *) break ;; esac done if [ "$#" = "0" -o "x$1" = "x--help" -o "x$1" = "x-h" ]; then yelp_usage_cache exit 1 fi yelp_cache_in "$@" | xsltproc --path "$html_paths" \ --xinclude -o "$cache_out" \ "$xsl_mal_cache" - } yelp_html_xsl_common () { if [ "x$html_internal_datadir" != "x" ]; then echo '' echo '' echo '' else echo '' fi if [ "x$html_internal_xsl" != "x" ]; then echo '' fi if [ "x$html_custom" != "x" ]; then echo '' fi echo '' echo ' ' echo ' ' echo ' ' echo ' ' echo ' ' echo ' ' echo ' ' echo ' ' echo ' ' echo ' ' echo ' C' echo ' ' echo ' ' echo ' ' echo ' ' echo ' ' echo ' ' echo ' ' echo ' ' echo ' ' echo ' ' echo ' ' echo ' ' echo ' ' echo ' ' echo '' echo '' echo ' ' echo ' ' echo ' ' echo ' ' echo ' ' echo ' ' echo ' ' echo ' ' echo ' ' echo ' ' echo ' ' echo ' ' echo '