;;; source.mhtml: -*- Meta-HTML -*-  Macros which aid in source viewing.
;;;
;;;  Copyright (c) 1996 Brian J. Fox
;;;  Author: Brian J. Fox (bfox@ai.mit.edu) Fri Jul  5 09:13:05 1996.

<defun source-link text>
  ;;; DOC_SECTION (HTML-HELPERS)
  ;;; You write `Click <source-link here> for source.'
  <set-var path=<subst-in-string <get-var mhtml::location> "\\?.+$" "">>
  <a href="/helpers/view-file.mhtml<get-var path>">
    <get-var text>
  </a>
</defun>

<defun source-view-link link current-path>
  <set-var disp=<get-var link>>
  <when <not <string-eq <substring <get-var link> 0 1> />>>
    <set-var dir=<subst-in-string <get-var current-path> "/[^/]+$" "">>
    <subst-in-var dir "^<get-var mhtml::include-prefix>" "">
    <if <not <match <get-var dir> "/">> <set-var dir="">>
    <set-var link=<get-var dir>/<get-var link>>
  </when>
  <a href="<concat <get-var mhtml::url-to-dir>/
    <get-var mhtml::current-doc>/
    <get-var link>>">
    <get-var disp>
  </a>
</defun>

<defun manpage-link funname>
  <a href="<concat <get-var mhtml::http-prefix>
    /documentation/manual/man-page.mhtml
    ?KEY=<get-var funname>>">
    <get-var funname>
  </a>
</defun>

<defun source-of web-path highlight-keywords-p>
  ;;; DOC_SECTION (HTML-HELPERS)
  ;;; Display the contents of <var web-path> in <code>HTML</code>.
  ;;; Use sparingly.
  ;;; If <var highlight-keywords-p> is non-empty, then make each
  ;;; <Meta-HTML> function in the displayed text be a link which will
  ;;; display the contents of the the manual page for that keyword.
  <if <string-eq <substring <get-var web-path> 0 1> />
      <set-var path = <get-var mhtml::include-prefix web-path>>
    <set-var path =
      <concat <get-var mhtml::include-prefix mhtml::relative-prefix>/
	      <get-var web-path>>>>

  <with-open-stream stream <get-var path> mode=read>
    <stream-get-contents stream contents>
  </with-open-stream>
  <coerce-var contents type=string>

  <subst-in-var contents "&" "&amp;" "<" "&lt;" ">" "&gt;">
  <subst-in-var contents "(^|[ \t\n]+)(\\;\\;\\;)([^\n]+)"
		"\\1<i>&#059;&#059;&#059;\\3</i>">
  <subst-in-var contents
		"(&lt;include[ \t\n]+)([^&]+)(&gt;)"
		"&lt;include <verbatim><source-view-link \\2 <get-var path>></verbatim>&gt;">

  ;;; Now the ridiculous part.
  <when <get-var highlight-keywords-p>>
    <set-var i=0>
    <set-var keywords[]=<downcase <package-vars *meta-html* strip=true>>>
    <while <get-var keywords[i]>>
      <subst-in-var contents
		    "(&lt;<get-var keywords[i]>)([ \t\n])"
		    "&lt;<manpage-link <get-var keywords[i]>>\\2">
      <increment i>
    </while>
  </when>
  <get-var contents>
</defun>
