#!/www/bin/mhc -z
;;; mkapi: -*- Meta-HTML -*- Build documentation API from sources.
;;;
;;;  Copyright (c) 1996 Brian J. Fox
;;;  Author: Brian J. Fox (bfox@ai.mit.edu) Wed May 27 19:56:11 1998
<set-var mhc::explicit-output-only=true>
<set-var mhtml::gather-documentation = true>
<set-var mhtml::include-prefix[] = <cgi-exec pwd>>

<defun mkapi::print item>
  <with-open-stream print *standard-output* mode=write>
    <stream-put print <get-var item>>
  </with-open-stream>
</defun>

;;; Parse command line arguments.
;;; They are simply the name of the output file, and the names of files
;;; of Meta-HTML code to include, then write out documentation for.
<set-var filenames[]="" output-file="" this-arg-is-output="">

<foreach arg program-arguments start=1>
  <if <get-var this-arg-is-output>
      <set-var output-file=<get-var arg> this-arg-is-output="">
    <if <or <string-eq <get-var arg> -o>
	    <string-eq <get-var arg> --output>>
	<set-var this-arg-is-output=true>
      <array-append <get-var arg> filenames>>>
</foreach>

<copy-var *meta-html*::include *meta-html*::mhc-include>

<set-var mkapi::inclev = 0>

<defsubst include f &optional verbatim &key &unevalled alt whitespace=delete>
  <mkapi::print
   "<pad \"\" <mul 2 mkapi::inclev> align=right> Including <get-var f>\n">
  <increment mkapi::inclev>
  <mhc-include <get-var f> <get-var verbatim>
	       alt=<or <get-var-once alt>
		       <mkapi::print " Failed!!\n">>>
  <decrement mkapi::inclev>
</defsubst>

<foreach fname filenames>
  <if <string-eq <substring <get-var fname> 0 1> />
      <set-var mhtml::include-prefix = />>
  <include <get-var fname>>
</foreach>

<undef include>

;;; Get the list of functions that we have just read in.
<set-var all-funcs[]=<package-vars *user-functions* strip=true>>
<sort all-funcs>

;;; Okay, it is safe to bootstrap now.
<%%bootstrap-metahtml>

;;; Write the documentation out to a file.
<set-var api-file = <dir::basename <get-var-once output-file>>>
<set-var mhtml-file = <concat
		       <dir::filename-sans-extension <get-var-once api-file>>
		       .mhtml>>

<set-var curr-prefix="">
<with-open-stream s <or <get-var output-file> *standard-output*>
  mode=write-create>
  <stream-put
   s
   "<get-var api-file>: An API in -*- Text -*- mode.
This file contains the Application Programmers Interface created by `mkapi'
for the tagset file <get-var mhtml-file> on <date>.
Do NOT edit this file, as it is automatically created.\n\n">
  <foreach fun all-funcs>
    <when <%%function-documentation <get-var fun>>>
      <set-var args[]= <%%function-arguments <get-var fun>>>
      <set-var type[]=
	<subst-in-string <symbol-info *user-functions*::<get-var fun>>
			 "USER " "">>
      <set-var doc = <%%function-documentation <get-var fun>>>
      <set-var prefix =
	<subst-in-string <get-var fun> "(^[^:]+)(::)(.*$)" "\\1">>

      ;;; See if this starts a new documentation section.
      <if <not <string-eq <get-var curr-prefix> <get-var prefix>>>
	  ;;; Yes.  Say the right thing.
	  <concat
	   <if <get-var-once prefix>
	       <concat
		<stream-put p "Functions in class <get-var prefix>:\n">
		<stream-put p <pad "" 60 align=center pad-char="-">>
		<stream-put p "\n">>>
	   <set-var curr-prefix = <get-var prefix>>>>

      <var-case
	type=DEFUN	<set-var class=DEFUN>
	type=SUBST	<set-var class=DEFUN>
	type=MACRO	<set-var class=DEFMACRO>>

      ;;; Put the opening line in the output file.
      <unset-var line>
      <set-var line = <downcase <get-var-once fun>>>
      ;;; Put the args.
      <foreach arg args>
	<set-var line = <concat <get-var-once line> " <get-var arg>">>
      </foreach>
      <set-var ws = <sub 72 <string-length <get-var line>>>>
      <set-var ws = <sub ws <string-length class>>>
      <when <lt ws 0>>
	<set-var line[]=<word-wrap <get-var-once line> width=60>>
	<set-var line[0]=<subst-in-string <get-var-once line[0]> "^ +" "">>
	<set-var ws = <sub 72 <string-length <get-var line>>>>
	<set-var ws = <sub ws <string-length class>>>
      </when>
      <if <lt ws 0> <set-var ws=1>>
      <stream-put s <get-var-once line>>
      <stream-put s <pad "" <get-var ws>>>
      <stream-put s <get-var-once class>>
      <stream-put s "\n">
      <foreach l line start=1>
	<set-var l=<word-wrap <get-var-once l> width=60 indent=8>>
	<stream-put s <get-var-once l>>
	<stream-put s "\n">
      </foreach>

      ;;; Put the documentation.
      <stream-put s <word-wrap <get-var-once doc> width=60 indent=4>>
      <stream-put s "\n\n">
    </when>
  </foreach>
</with-open-stream>

<mkapi::print "<debugging-output retrieve>">
<mkapi::print "<system-error-output retrieve>">
