From 0e6a848c8cd1e9442e3794c7dcd2f535ea9797c6 Mon Sep 17 00:00:00 2001
From: Akim Demaille <akim@lrde.epita.fr>
Date: Thu, 25 Oct 2012 15:44:22 +0200
Subject: [PATCH] gendocs: copy images need by HTML output

* build-aux/gendocs.sh (copy_images): New.
Use it.
---
 build-aux/gendocs.sh | 73 +++++++++++++++++++++++++++++++++++++++++++++-------
 1 file changed, 64 insertions(+), 9 deletions(-)

diff --git a/build-aux/gendocs.sh b/build-aux/gendocs.sh
index b4c328a..f992ec0 100755
--- a/build-aux/gendocs.sh
+++ b/build-aux/gendocs.sh
@@ -2,7 +2,7 @@
 # gendocs.sh -- generate a GNU manual in many formats.  This script is
 #   mentioned in maintain.texi.  See the help message below for usage details.
 
-scriptversion=2012-10-23.16
+scriptversion=2012-10-26.09
 
 # Copyright 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
 # Free Software Foundation, Inc.
@@ -30,6 +30,13 @@ scriptversion=2012-10-23.16
 #
 # An up-to-date copy is also maintained in Gnulib (gnu.org/software/gnulib).
 
+# TODO:
+# - image importation was only implemented for HTML generated by
+#   makeinfo.  But it should be simple enough to adjust.
+# - images are not imported in the source tarball.  All the needed
+#   formats (PDF, PNG, etc.) should be included.
+# - remove PS support (including in the doc templates)
+
 prog=`basename "$0"`
 srcdir=`pwd`
 
@@ -45,6 +52,7 @@ templateurl="http://savannah.gnu.org/cgi-bin/viewcvs/~checkout~/texinfo/texinfo/
 : ${DOCBOOK2PS="docbook2ps"}
 : ${DOCBOOK2TXT="docbook2txt"}
 : ${GENDOCS_TEMPLATE_DIR="."}
+: ${PERL='perl'}
 : ${TEXI2HTML="texi2html"}
 unset CDPATH
 unset use_texi2html
@@ -128,10 +136,57 @@ calcsize()
   echo $size
 }
 
+# copy_images OUTDIR HTML-FILE...
+# -------------------------------
+# Copy all the images needed by the HTML-FILEs into OUTDIR.  Look
+# for them in the -I directories.
+copy_images()
+{
+  local odir
+  odir=$1
+  shift
+  $PERL -n -e "
+BEGIN
+{
+  \$me = '$prog';
+  \$odir = '$odir';
+  @dirs = qw($dirs);
+}
+" -e '
+/<img src="(.*?)"/g && ++$need{$1};
+
+END
+{
+  print "$me: @{[keys %need]}\n";
+  FILE: foreach my $f (keys %need)
+  {
+    foreach $d (@dirs)
+    {
+      if (-f "$d/$f")
+      {
+        use File::Basename;
+        my $dest = dirname "$odir/$f";
+        use File::Path;
+        -d $dest
+          or mkpath ($dest)
+          or die "$me: cannot mkdir $dest: $!\n";
+        use File::Copy;
+        copy "$d/$f", $dest
+          or die "$me: cannot copy $d/$f to $dest: $!\n";
+        next FILE;
+      }
+    }
+    die "$me: $ARGV: cannot find image $f\n";
+  }
+}
+' -- "$@" || exit 1
+}
+
 MANUAL_TITLE=
 PACKAGE=
 EMAIL=webmasters@gnu.org  # please override with --email
 commonarg= # Options passed to all the tools (-I dir).
+dirs=      # -I's directories.
 htmlarg=
 infoarg=--no-split
 outdir=manual
@@ -144,7 +199,7 @@ while test $# -gt 0; do
     --version) echo "$version"; exit 0;;
     -s) shift; srcfile=$1;;
     -o) shift; outdir=$1;;
-    -I) shift; commonarg="$commonarg -I '$1'";;
+    -I) shift; commonarg="$commonarg -I '$1'"; dirs="$dirs $1";;
     --docbook) docbook=yes;;
     --html) shift; htmlarg=$1;;
     --info) shift; infoarg=$1;;
@@ -268,6 +323,7 @@ if test -z "$use_texi2html"; then
   html_mono_size=`calcsize $PACKAGE.html`
   gzip -f -9 -c $PACKAGE.html >"$outdir/$PACKAGE.html.gz"
   html_mono_gz_size=`calcsize "$outdir/$PACKAGE.html.gz"`
+  copy_images "$outdir/" $PACKAGE.html
   mv $PACKAGE.html "$outdir/"
 
   opt="--html -o $PACKAGE.html $commonarg $htmlarg"
@@ -275,15 +331,14 @@ if test -z "$use_texi2html"; then
   echo "Generating html by node... ($cmd)"
   eval "$cmd"
   split_html_dir=$PACKAGE.html
+  copy_images $split_html_dir/ $split_html_dir/*.html
   (
-   cd ${split_html_dir} || exit 1
-   tar -czf "$abs_outdir/${PACKAGE}.html_node.tar.gz" -- *.html
+    cd $split_html_dir || exit 1
+    tar -czf "$abs_outdir/$PACKAGE.html_node.tar.gz" -- *
   )
-  html_node_tgz_size=`calcsize "$outdir/${PACKAGE}.html_node.tar.gz"`
-  rm -f "$outdir"/html_node/*.html
-  mkdir -p "$outdir/html_node/"
-  mv ${split_html_dir}/*.html "$outdir/html_node/"
-  rmdir ${split_html_dir}
+  html_node_tgz_size=`calcsize "$outdir/$PACKAGE.html_node.tar.gz"`
+  rm -rf "$outdir/html_node/"
+  mv $split_html_dir "$outdir/html_node/"
 else
   opt="--output $PACKAGE.html $commonarg $htmlarg"
   cmd="$SETLANG $TEXI2HTML $opt \"$srcfile\""
-- 
1.7.12.2

