; This is a postprocessing file to add html links to the results of grefer.
; Its use is exemplified in the me2html converter, where the results of grefer
; are piped into here, and these results are then sent on to groff.

(defun dot () (point))

(defun convert-refer-results ()
"Convert [n] to [<a href=#citen>n</a>], hoping that the reg-expr is strong
enough not to confuse other uses of square brackets.  After the References
section, convert \nn. to <a name=bibrefn>n. </a>"
    (interactive)
    (beginning-of-buffer)
    (convert-numeric-refer-citations)
    (beginning-of-buffer)
    (convert-refer-bib)
    (save-buffer)
)


(defun convert-numeric-refer-citations ()
"Convert [n] to [<a href=\"#bibrefn\">n</a>], hoping that the reg-expr is
strong enough not to confuse other uses of square brackets.  Convert multi-
number cites, of the form [n1,...,nn] appropriately.

Also convert cites of the form [author DD] to their href'd form."

    (interactive)
    (convert-1-numeric-refer-citation)
    (beginning-of-buffer)
    (convert-2-numeric-refer-citations)
    (beginning-of-buffer)
    (convert-3-numeric-refer-citations)
    (beginning-of-buffer)
    (convert-4-numeric-refer-citations)

    (beginning-of-buffer)
    (convert-1-auth-date-refer-citation)

)

(defvar numeric-cite "\\[\\([0-9]*\\)\\]")
(defvar auth-date-cite "\\[\\([A-Za-z\-]* [0-9][0-9]\\)\\]")

(defun convert-1-numeric-refer-citation ()
    (interactive)
    (while (search-forward-regexp numeric-cite nil t)
      (backward-char 1)
      (insert-string "</a>")
      (backward-word 1)
      (backward-char 2)
      (setq d (dot))
      (backward-word 1)
      (setq citenum (buffer-substring (dot) d))
      (insert-string (concat "<a href=\"#cite" citenum "\">"))
    )
)

(defun convert-2-numeric-refer-citations ()
    (interactive)
    (while (search-forward-regexp "\\[\\([0-9]*\\),[ \n]\\([0-9]*\\)\\]" nil t)
      (setq citenum1 (match-string 1))
      (setq citenum2 (match-string 2))
      (backwards-fixup-refer-citenum citenum2)
      (backwards-fixup-refer-citenum citenum1)
    )
)
(defun convert-3-numeric-refer-citations ()
    (interactive)
    (while (search-forward-regexp
	    "\\[\\([0-9]*\\),[ \n]\\([0-9]*\\),[ \n]\\([0-9]*\\)\\]" nil t)
      (setq citenum1 (match-string 1))
      (setq citenum2 (match-string 2))
      (setq citenum3 (match-string 3))
      (backwards-fixup-refer-citenum citenum3)
      (backwards-fixup-refer-citenum citenum2)
      (backwards-fixup-refer-citenum citenum1)
    )
)
(defun convert-4-numeric-refer-citations ()
    (interactive)
    (while (search-forward-regexp
     "\\[\\([0-9]*\\),[ \n]\\([0-9]*\\),[ \n]\\([0-9]*\\),[ \n]\\([0-9]*\\)\\]"
          nil t)
      (setq citenum1 (match-string 1))
      (setq citenum2 (match-string 2))
      (setq citenum3 (match-string 3))
      (setq citenum3 (match-string 4))
      (backwards-fixup-refer-citenum citenum4)
      (backwards-fixup-refer-citenum citenum3)
      (backwards-fixup-refer-citenum citenum2)
      (backwards-fixup-refer-citenum citenum1)
    )
)

(defun convert-1-auth-date-refer-citation ()
    (interactive)
    (setq auth-date-cite "\\[\\([A-Za-z\-]*\\) \\([0-9][0-9]\\)\\]")
    (while (search-forward-regexp auth-date-cite nil t)
      (setq auth (match-string 1))
      (setq date (match-string 2))
      (backward-char 1)
      (insert-string "</a>")
      (search-backward "[")
      (insert-string (concat "<a href=\"#cite" auth "\\ " date "\">"))
    )
)

(defun backwards-fixup-refer-citenum (citenumstr)
  (interactive)
  (search-backward citenumstr)
  (kill-word 1)
  (insert-string (concat "<a href=\"#cite" citenumstr "\">" citenumstr "</a>"))
)

(defun convert-refer-bib ()
"After the ``.uh References'' or ``.uh Collected References''section, convert
.][ x to <a name=citen>n. </a>, for 1 <= n <= number of refs, followed by the
original .][ x"
    (interactive)
    (search-forward-regexp "\.uh \"References\"\\|\.uh \"Collected References\"")
    (setq n 0)
    (while (search-forward ".ds [F " nil t)
      (setq d (dot))
      (end-of-line)
      (setq refstr (buffer-substring d (dot)))
      (search-forward ".][")
      (beginning-of-line)
      (insert-string (concat ".lp\n<a name=\"cite"
			     refstr
;			     (int-to-string (setq n (+ n 1)))
			     "\"></a>\n.br\n"))
;     (forward-line 1)
    )
)

(defun convert-refer-bib-after-groff ()
"After the References section, convert \nn. to <a name=citen>n. </a>.
This function is not currently used, since it's designed to work after groff
is done processing the refer-made bib.  In the current scheme, the function
conver-refer-bib is used, which is designed to run after refer but before
groff.  This will allow groff to generate reasonably clean html for the
bib refs.  In general, its probably not a good idea to do any postprocessing
after groff is done, since this could mess up the raw format of the html.
The raw html format is not critical, but would be nice to maintain in as
readable a form as possible."
    (interactive)
    (search-forward ".uh References")
   (while (search-forward-regexp "\n[0-9]+\." nil t)
      (backward-char 1)
      (setq d (dot))
      (backward-word 1)
      (setq refnum (buffer-substring (dot) d))
      (insert-string (concat "<a name=cite" (int-to-string refnum) ">\n"))
      (forward-word 1)
      (insert-string "</a>")
    )
)

; If there is a separate refs.html file, then the cites created by refer2thml
; need to be fixed from #citen to refs.html#citen
(defun fix-separated-cite-links ()
"Fix cite links when refs have been moved to separate refs.html file."
    (interactive)
    (replace-regexp "\"#cite\\([A-Za-z\-]* [0-9][0-9]\\)"
		    "\"refs.html#cite\\1")
    (save-buffer)
)