gendocs.sh 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  1. #!/bin/sh -e
  2. # gendocs.sh -- generate a GNU manual in many formats. This script is
  3. # mentioned in maintain.texi. See the help message below for usage details.
  4. scriptversion=2013-02-03.15
  5. # Copyright 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013
  6. # Free Software Foundation, Inc.
  7. #
  8. # This program is free software: you can redistribute it and/or modify
  9. # it under the terms of the GNU General Public License as published by
  10. # the Free Software Foundation; either version 3 of the License, or
  11. # (at your option) any later version.
  12. #
  13. # This program is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. # GNU General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU General Public License
  19. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. #
  21. # Original author: Mohit Agarwal.
  22. # Send bug reports and any other correspondence to bug-texinfo@gnu.org.
  23. #
  24. # The latest version of this script, and the companion template, is
  25. # available from Texinfo CVS:
  26. # http://savannah.gnu.org/cgi-bin/viewcvs/texinfo/texinfo/util/gendocs.sh
  27. # http://savannah.gnu.org/cgi-bin/viewcvs/texinfo/texinfo/util/gendocs_template
  28. #
  29. # An up-to-date copy is also maintained in Gnulib (gnu.org/software/gnulib).
  30. # TODO:
  31. # - image importation was only implemented for HTML generated by
  32. # makeinfo. But it should be simple enough to adjust.
  33. # - images are not imported in the source tarball. All the needed
  34. # formats (PDF, PNG, etc.) should be included.
  35. prog=`basename "$0"`
  36. srcdir=`pwd`
  37. scripturl="http://savannah.gnu.org/cgi-bin/viewcvs/~checkout~/texinfo/texinfo/util/gendocs.sh"
  38. templateurl="http://savannah.gnu.org/cgi-bin/viewcvs/~checkout~/texinfo/texinfo/util/gendocs_template"
  39. : ${SETLANG="env LANG= LC_MESSAGES= LC_ALL= LANGUAGE="}
  40. : ${MAKEINFO="makeinfo"}
  41. : ${TEXI2DVI="texi2dvi -t @finalout"}
  42. : ${DOCBOOK2HTML="docbook2html"}
  43. : ${DOCBOOK2PDF="docbook2pdf"}
  44. : ${DOCBOOK2TXT="docbook2txt"}
  45. : ${GENDOCS_TEMPLATE_DIR="."}
  46. : ${PERL='perl'}
  47. : ${TEXI2HTML="texi2html"}
  48. unset CDPATH
  49. unset use_texi2html
  50. version="gendocs.sh $scriptversion
  51. Copyright 2013 Free Software Foundation, Inc.
  52. There is NO warranty. You may redistribute this software
  53. under the terms of the GNU General Public License.
  54. For more information about these matters, see the files named COPYING."
  55. usage="Usage: $prog [OPTION]... PACKAGE MANUAL-TITLE
  56. Generate output in various formats from PACKAGE.texinfo (or .texi or
  57. .txi) source. See the GNU Maintainers document for a more extensive
  58. discussion:
  59. http://www.gnu.org/prep/maintain_toc.html
  60. Options:
  61. --email ADR use ADR as contact in generated web pages; always give this.
  62. -s SRCFILE read Texinfo from SRCFILE, instead of PACKAGE.{texinfo|texi|txi}
  63. -o OUTDIR write files into OUTDIR, instead of manual/.
  64. -I DIR append DIR to the Texinfo search path.
  65. --common ARG pass ARG in all invocations.
  66. --html ARG pass ARG to makeinfo or texi2html for HTML targets.
  67. --info ARG pass ARG to makeinfo for Info, instead of --no-split.
  68. --no-ascii skip generating the plain text output.
  69. --source ARG include ARG in tar archive of sources.
  70. --split HOW make split HTML by node, section, chapter; default node.
  71. --texi2html use texi2html to make HTML target, with all split versions.
  72. --docbook convert through DocBook too (xml, txt, html, pdf).
  73. --help display this help and exit successfully.
  74. --version display version information and exit successfully.
  75. Simple example: $prog --email bug-gnu-emacs@gnu.org emacs \"GNU Emacs Manual\"
  76. Typical sequence:
  77. cd PACKAGESOURCE/doc
  78. wget \"$scripturl\"
  79. wget \"$templateurl\"
  80. $prog --email BUGLIST MANUAL \"GNU MANUAL - One-line description\"
  81. Output will be in a new subdirectory \"manual\" (by default;
  82. use -o OUTDIR to override). Move all the new files into your web CVS
  83. tree, as explained in the Web Pages node of maintain.texi.
  84. Please use the --email ADDRESS option so your own bug-reporting
  85. address will be used in the generated HTML pages.
  86. MANUAL-TITLE is included as part of the HTML <title> of the overall
  87. manual/index.html file. It should include the name of the package being
  88. documented. manual/index.html is created by substitution from the file
  89. $GENDOCS_TEMPLATE_DIR/gendocs_template. (Feel free to modify the
  90. generic template for your own purposes.)
  91. If you have several manuals, you'll need to run this script several
  92. times with different MANUAL values, specifying a different output
  93. directory with -o each time. Then write (by hand) an overall index.html
  94. with links to them all.
  95. If a manual's Texinfo sources are spread across several directories,
  96. first copy or symlink all Texinfo sources into a single directory.
  97. (Part of the script's work is to make a tar.gz of the sources.)
  98. As implied above, by default monolithic Info files are generated.
  99. If you want split Info, or other Info options, use --info to override.
  100. You can set the environment variables MAKEINFO, TEXI2DVI, TEXI2HTML,
  101. and PERL to control the programs that get executed, and
  102. GENDOCS_TEMPLATE_DIR to control where the gendocs_template file is
  103. looked for. With --docbook, the environment variables DOCBOOK2HTML,
  104. DOCBOOK2PDF, and DOCBOOK2TXT are also consulted.
  105. By default, makeinfo and texi2dvi are run in the default (English)
  106. locale, since that's the language of most Texinfo manuals. If you
  107. happen to have a non-English manual and non-English web site, see the
  108. SETLANG setting in the source.
  109. Email bug reports or enhancement requests to bug-texinfo@gnu.org.
  110. "
  111. MANUAL_TITLE=
  112. PACKAGE=
  113. EMAIL=webmasters@gnu.org # please override with --email
  114. commonarg= # passed to all makeinfo/texi2html invcations.
  115. dirargs= # passed to all tools (-I dir).
  116. dirs= # -I's directories.
  117. htmlarg=
  118. infoarg=--no-split
  119. generate_ascii=true
  120. outdir=manual
  121. source_extra=
  122. split=node
  123. srcfile=
  124. while test $# -gt 0; do
  125. case $1 in
  126. -s) shift; srcfile=$1;;
  127. -o) shift; outdir=$1;;
  128. -I) shift; dirargs="$dirargs -I '$1'"; dirs="$dirs $1";;
  129. --common) shift; commonarg=$1;;
  130. --docbook) docbook=yes;;
  131. --email) shift; EMAIL=$1;;
  132. --html) shift; htmlarg=$1;;
  133. --info) shift; infoarg=$1;;
  134. --no-ascii) generate_ascii=false;;
  135. --source) shift; source_extra=$1;;
  136. --split) shift; split=$1;;
  137. --texi2html) use_texi2html=1;;
  138. --help) echo "$usage"; exit 0;;
  139. --version) echo "$version"; exit 0;;
  140. -*)
  141. echo "$0: Unknown option \`$1'." >&2
  142. echo "$0: Try \`--help' for more information." >&2
  143. exit 1;;
  144. *)
  145. if test -z "$PACKAGE"; then
  146. PACKAGE=$1
  147. elif test -z "$MANUAL_TITLE"; then
  148. MANUAL_TITLE=$1
  149. else
  150. echo "$0: extra non-option argument \`$1'." >&2
  151. exit 1
  152. fi;;
  153. esac
  154. shift
  155. done
  156. # makeinfo uses the dirargs, but texi2dvi doesn't.
  157. commonarg=" $dirargs $commonarg"
  158. # For most of the following, the base name is just $PACKAGE
  159. base=$PACKAGE
  160. if test -n "$srcfile"; then
  161. # but here, we use the basename of $srcfile
  162. base=`basename "$srcfile"`
  163. case $base in
  164. *.txi|*.texi|*.texinfo) base=`echo "$base"|sed 's/\.[texinfo]*$//'`;;
  165. esac
  166. PACKAGE=$base
  167. elif test -s "$srcdir/$PACKAGE.texinfo"; then
  168. srcfile=$srcdir/$PACKAGE.texinfo
  169. elif test -s "$srcdir/$PACKAGE.texi"; then
  170. srcfile=$srcdir/$PACKAGE.texi
  171. elif test -s "$srcdir/$PACKAGE.txi"; then
  172. srcfile=$srcdir/$PACKAGE.txi
  173. else
  174. echo "$0: cannot find .texinfo or .texi or .txi for $PACKAGE in $srcdir." >&2
  175. exit 1
  176. fi
  177. if test ! -r $GENDOCS_TEMPLATE_DIR/gendocs_template; then
  178. echo "$0: cannot read $GENDOCS_TEMPLATE_DIR/gendocs_template." >&2
  179. echo "$0: it is available from $templateurl." >&2
  180. exit 1
  181. fi
  182. # Function to return size of $1 in something resembling kilobytes.
  183. calcsize()
  184. {
  185. size=`ls -ksl $1 | awk '{print $1}'`
  186. echo $size
  187. }
  188. # copy_images OUTDIR HTML-FILE...
  189. # -------------------------------
  190. # Copy all the images needed by the HTML-FILEs into OUTDIR. Look
  191. # for them in the -I directories.
  192. copy_images()
  193. {
  194. local odir
  195. odir=$1
  196. shift
  197. $PERL -n -e "
  198. BEGIN {
  199. \$me = '$prog';
  200. \$odir = '$odir';
  201. @dirs = qw($dirs);
  202. }
  203. " -e '
  204. /<img src="(.*?)"/g && ++$need{$1};
  205. END {
  206. #print "$me: @{[keys %need]}\n"; # for debugging, show images found.
  207. FILE: for my $f (keys %need) {
  208. for my $d (@dirs) {
  209. if (-f "$d/$f") {
  210. use File::Basename;
  211. my $dest = dirname ("$odir/$f");
  212. #
  213. use File::Path;
  214. -d $dest || mkpath ($dest)
  215. || die "$me: cannot mkdir $dest: $!\n";
  216. #
  217. use File::Copy;
  218. copy ("$d/$f", $dest)
  219. || die "$me: cannot copy $d/$f to $dest: $!\n";
  220. next FILE;
  221. }
  222. }
  223. die "$me: $ARGV: cannot find image $f\n";
  224. }
  225. }
  226. ' -- "$@" || exit 1
  227. }
  228. case $outdir in
  229. /*) abs_outdir=$outdir;;
  230. *) abs_outdir=$srcdir/$outdir;;
  231. esac
  232. echo "Making output for $srcfile"
  233. echo " in `pwd`"
  234. mkdir -p "$outdir/"
  235. cmd="$SETLANG $MAKEINFO -o $PACKAGE.info $commonarg $infoarg \"$srcfile\""
  236. echo "Generating info... ($cmd)"
  237. eval "$cmd"
  238. tar czf "$outdir/$PACKAGE.info.tar.gz" $PACKAGE.info*
  239. ls -l "$outdir/$PACKAGE.info.tar.gz"
  240. info_tgz_size=`calcsize "$outdir/$PACKAGE.info.tar.gz"`
  241. # do not mv the info files, there's no point in having them available
  242. # separately on the web.
  243. cmd="$SETLANG $TEXI2DVI $dirargs \"$srcfile\""
  244. printf "\nGenerating dvi... ($cmd)\n"
  245. eval "$cmd"
  246. # compress/finish dvi:
  247. gzip -f -9 $PACKAGE.dvi
  248. dvi_gz_size=`calcsize $PACKAGE.dvi.gz`
  249. mv $PACKAGE.dvi.gz "$outdir/"
  250. ls -l "$outdir/$PACKAGE.dvi.gz"
  251. cmd="$SETLANG $TEXI2DVI --pdf $dirargs \"$srcfile\""
  252. printf "\nGenerating pdf... ($cmd)\n"
  253. eval "$cmd"
  254. pdf_size=`calcsize $PACKAGE.pdf`
  255. mv $PACKAGE.pdf "$outdir/"
  256. ls -l "$outdir/$PACKAGE.pdf"
  257. if $generate_ascii; then
  258. opt="-o $PACKAGE.txt --no-split --no-headers $commonarg"
  259. cmd="$SETLANG $MAKEINFO $opt \"$srcfile\""
  260. printf "\nGenerating ascii... ($cmd)\n"
  261. eval "$cmd"
  262. ascii_size=`calcsize $PACKAGE.txt`
  263. gzip -f -9 -c $PACKAGE.txt >"$outdir/$PACKAGE.txt.gz"
  264. ascii_gz_size=`calcsize "$outdir/$PACKAGE.txt.gz"`
  265. mv $PACKAGE.txt "$outdir/"
  266. ls -l "$outdir/$PACKAGE.txt" "$outdir/$PACKAGE.txt.gz"
  267. fi
  268. html_split()
  269. {
  270. opt="--split=$1 --node-files $commonarg $htmlarg"
  271. cmd="$SETLANG $TEXI2HTML --output $PACKAGE.html $opt \"$srcfile\""
  272. printf "\nGenerating html by $1... ($cmd)\n"
  273. eval "$cmd"
  274. split_html_dir=$PACKAGE.html
  275. (
  276. cd ${split_html_dir} || exit 1
  277. ln -sf ${PACKAGE}.html index.html
  278. tar -czf "$abs_outdir/${PACKAGE}.html_$1.tar.gz" -- *.html
  279. )
  280. eval html_$1_tgz_size=`calcsize "$outdir/${PACKAGE}.html_$1.tar.gz"`
  281. rm -f "$outdir"/html_$1/*.html
  282. mkdir -p "$outdir/html_$1/"
  283. mv ${split_html_dir}/*.html "$outdir/html_$1/"
  284. rmdir ${split_html_dir}
  285. }
  286. if test -z "$use_texi2html"; then
  287. opt="--no-split --html -o $PACKAGE.html $commonarg $htmlarg"
  288. cmd="$SETLANG $MAKEINFO $opt \"$srcfile\""
  289. printf "\nGenerating monolithic html... ($cmd)\n"
  290. rm -rf $PACKAGE.html # in case a directory is left over
  291. eval "$cmd"
  292. html_mono_size=`calcsize $PACKAGE.html`
  293. gzip -f -9 -c $PACKAGE.html >"$outdir/$PACKAGE.html.gz"
  294. html_mono_gz_size=`calcsize "$outdir/$PACKAGE.html.gz"`
  295. copy_images "$outdir/" $PACKAGE.html
  296. mv $PACKAGE.html "$outdir/"
  297. ls -l "$outdir/$PACKAGE.html" "$outdir/$PACKAGE.html.gz"
  298. opt="--html -o $PACKAGE.html --split=$split $commonarg $htmlarg"
  299. cmd="$SETLANG $MAKEINFO $opt \"$srcfile\""
  300. printf "\nGenerating html by $split... ($cmd)\n"
  301. eval "$cmd"
  302. split_html_dir=$PACKAGE.html
  303. copy_images $split_html_dir/ $split_html_dir/*.html
  304. (
  305. cd $split_html_dir || exit 1
  306. tar -czf "$abs_outdir/$PACKAGE.html_$split.tar.gz" -- *
  307. )
  308. eval \
  309. html_${split}_tgz_size=`calcsize "$outdir/$PACKAGE.html_$split.tar.gz"`
  310. rm -rf "$outdir/html_$split/"
  311. mv $split_html_dir "$outdir/html_$split/"
  312. du -s "$outdir/html_$split/"
  313. ls -l "$outdir/$PACKAGE.html_$split.tar.gz"
  314. else # use texi2html:
  315. opt="--output $PACKAGE.html $commonarg $htmlarg"
  316. cmd="$SETLANG $TEXI2HTML $opt \"$srcfile\""
  317. printf "\nGenerating monolithic html with texi2html... ($cmd)\n"
  318. rm -rf $PACKAGE.html # in case a directory is left over
  319. eval "$cmd"
  320. html_mono_size=`calcsize $PACKAGE.html`
  321. gzip -f -9 -c $PACKAGE.html >"$outdir/$PACKAGE.html.gz"
  322. html_mono_gz_size=`calcsize "$outdir/$PACKAGE.html.gz"`
  323. mv $PACKAGE.html "$outdir/"
  324. html_split node
  325. html_split chapter
  326. html_split section
  327. fi
  328. printf "\nMaking .tar.gz for sources...\n"
  329. d=`dirname $srcfile`
  330. (
  331. cd "$d"
  332. srcfiles=`ls -d *.texinfo *.texi *.txi *.eps $source_extra 2>/dev/null` || true
  333. tar czfh "$abs_outdir/$PACKAGE.texi.tar.gz" $srcfiles
  334. ls -l "$abs_outdir/$PACKAGE.texi.tar.gz"
  335. )
  336. texi_tgz_size=`calcsize "$outdir/$PACKAGE.texi.tar.gz"`
  337. if test -n "$docbook"; then
  338. opt="-o - --docbook $commonarg"
  339. cmd="$SETLANG $MAKEINFO $opt \"$srcfile\" >${srcdir}/$PACKAGE-db.xml"
  340. printf "\nGenerating docbook XML... ($cmd)\n"
  341. eval "$cmd"
  342. docbook_xml_size=`calcsize $PACKAGE-db.xml`
  343. gzip -f -9 -c $PACKAGE-db.xml >"$outdir/$PACKAGE-db.xml.gz"
  344. docbook_xml_gz_size=`calcsize "$outdir/$PACKAGE-db.xml.gz"`
  345. mv $PACKAGE-db.xml "$outdir/"
  346. split_html_db_dir=html_node_db
  347. opt="$commonarg -o $split_html_db_dir"
  348. cmd="$DOCBOOK2HTML $opt \"${outdir}/$PACKAGE-db.xml\""
  349. printf "\nGenerating docbook HTML... ($cmd)\n"
  350. eval "$cmd"
  351. (
  352. cd ${split_html_db_dir} || exit 1
  353. tar -czf "$abs_outdir/${PACKAGE}.html_node_db.tar.gz" -- *.html
  354. )
  355. html_node_db_tgz_size=`calcsize "$outdir/${PACKAGE}.html_node_db.tar.gz"`
  356. rm -f "$outdir"/html_node_db/*.html
  357. mkdir -p "$outdir/html_node_db"
  358. mv ${split_html_db_dir}/*.html "$outdir/html_node_db/"
  359. rmdir ${split_html_db_dir}
  360. cmd="$DOCBOOK2TXT \"${outdir}/$PACKAGE-db.xml\""
  361. printf "\nGenerating docbook ASCII... ($cmd)\n"
  362. eval "$cmd"
  363. docbook_ascii_size=`calcsize $PACKAGE-db.txt`
  364. mv $PACKAGE-db.txt "$outdir/"
  365. cmd="$DOCBOOK2PDF \"${outdir}/$PACKAGE-db.xml\""
  366. printf "\nGenerating docbook PDF... ($cmd)\n"
  367. eval "$cmd"
  368. docbook_pdf_size=`calcsize $PACKAGE-db.pdf`
  369. mv $PACKAGE-db.pdf "$outdir/"
  370. fi
  371. printf "\nMaking index file...\n"
  372. if test -z "$use_texi2html"; then
  373. CONDS="/%%IF *HTML_SECTION%%/,/%%ENDIF *HTML_SECTION%%/d;\
  374. /%%IF *HTML_CHAPTER%%/,/%%ENDIF *HTML_CHAPTER%%/d"
  375. else
  376. # should take account of --split here.
  377. CONDS="/%%ENDIF.*%%/d;/%%IF *HTML_SECTION%%/d;/%%IF *HTML_CHAPTER%%/d"
  378. fi
  379. curdate=`$SETLANG date '+%B %d, %Y'`
  380. sed \
  381. -e "s!%%TITLE%%!$MANUAL_TITLE!g" \
  382. -e "s!%%EMAIL%%!$EMAIL!g" \
  383. -e "s!%%PACKAGE%%!$PACKAGE!g" \
  384. -e "s!%%DATE%%!$curdate!g" \
  385. -e "s!%%HTML_MONO_SIZE%%!$html_mono_size!g" \
  386. -e "s!%%HTML_MONO_GZ_SIZE%%!$html_mono_gz_size!g" \
  387. -e "s!%%HTML_NODE_TGZ_SIZE%%!$html_node_tgz_size!g" \
  388. -e "s!%%HTML_SECTION_TGZ_SIZE%%!$html_section_tgz_size!g" \
  389. -e "s!%%HTML_CHAPTER_TGZ_SIZE%%!$html_chapter_tgz_size!g" \
  390. -e "s!%%INFO_TGZ_SIZE%%!$info_tgz_size!g" \
  391. -e "s!%%DVI_GZ_SIZE%%!$dvi_gz_size!g" \
  392. -e "s!%%PDF_SIZE%%!$pdf_size!g" \
  393. -e "s!%%ASCII_SIZE%%!$ascii_size!g" \
  394. -e "s!%%ASCII_GZ_SIZE%%!$ascii_gz_size!g" \
  395. -e "s!%%TEXI_TGZ_SIZE%%!$texi_tgz_size!g" \
  396. -e "s!%%DOCBOOK_HTML_NODE_TGZ_SIZE%%!$html_node_db_tgz_size!g" \
  397. -e "s!%%DOCBOOK_ASCII_SIZE%%!$docbook_ascii_size!g" \
  398. -e "s!%%DOCBOOK_PDF_SIZE%%!$docbook_pdf_size!g" \
  399. -e "s!%%DOCBOOK_XML_SIZE%%!$docbook_xml_size!g" \
  400. -e "s!%%DOCBOOK_XML_GZ_SIZE%%!$docbook_xml_gz_size!g" \
  401. -e "s,%%SCRIPTURL%%,$scripturl,g" \
  402. -e "s!%%SCRIPTNAME%%!$prog!g" \
  403. -e "$CONDS" \
  404. $GENDOCS_TEMPLATE_DIR/gendocs_template >"$outdir/index.html"
  405. echo "Done, see $outdir/ subdirectory for new files."
  406. # Local variables:
  407. # eval: (add-hook 'write-file-hooks 'time-stamp)
  408. # time-stamp-start: "scriptversion="
  409. # time-stamp-format: "%:y-%02m-%02d.%02H"
  410. # time-stamp-end: "$"
  411. # End: