; The complement of ./me2html.el, q.v.
;
; Here's an oldish approximation of the translation:
;
;       HTML				ME
;	============================================================
;	<title> ... </title>		.(TI ... .)TI
;	<br>				.br
;	<p>				.pp
;	<hn> ... </hn>			.sh n "..."
;	<blockquote> ... </blockquote>	.(i ... .)i
;	<font size=+1> ... </font>	.(S ... .)S
;	<strong> ... </strong>		\fB ... \fP
;	<em> ... </em>			\fI ... \fP
;	<tt> ... </tt>			\fC ... \fP
;	<a ...> ... </a>		.(x ... .)x
;	<pre> ... </pre>		.(t 0 ... .)t 0
;	<ol>				.(i\n.(L 1
;	<li>				.le
;	</ol>				.)L 1\n.)i
;	<p align=center> ... </p>	.(C ... .)C

(defun dot () (point))

(defun html2me ()
    (interactive)
    ; <title> ... </title> to .(TI ... .)TI
    (beginning-of-buffer)
    (replace-string "<title>" ".(TI")
    (beginning-of-buffer)
    (replace-string "</title>" ".)TI")

    ; <br> .br
    (beginning-of-buffer)
    (replace-string "<br>" ".br")

    ; <p> to .pp
    (beginning-of-buffer)
    (replace-string "<p>" ".pp")

    ; <hn> ... </hn> to .sh n "..."
    (beginning-of-buffer)
    (replace-sections)

    ; <blockquote> ... </blockquote> to.(i ... .)i
    (beginning-of-buffer)
    (replace-string "<blockquote>" ".(i")
    (beginning-of-buffer)
    (replace-string "</blockquote>" ".)i")

    ; <font size=+1> ... </font> to .(S ... .)S
    (beginning-of-buffer)
    (replace-string "<font size=+1>" "(.S +1")
    (beginning-of-buffer)
    (replace-string "</font>" ").S")

    ; <strong> ... </strong> to \fB ... \fP
    (beginning-of-buffer)
    (replace-string "<strong>" "\\fB")
    (beginning-of-buffer)
    (replace-string "</strong>" "\\fP")

    ; <em> ... </em> to \fI ... \fP
    (beginning-of-buffer)
    (replace-string "<em>" "\\fI")
    (beginning-of-buffer)
    (replace-string "</em>" "\\fP")

    ; <tt> ... </tt> to \fC ... \fP
    (beginning-of-buffer)
    (replace-string "<tt>" "\\fC")
    (beginning-of-buffer)
    (replace-string "</tt>" "\\fP")

    ; <pre> ... </pre> to .sp or .(t 0 ... .)t 0
    (replace-string "<pre>\n</pre>" ".sp 1")
    (beginning-of-buffer)
    (replace-string "<pre>\n\n</pre>" ".sp 2")
    (beginning-of-buffer)
    (beginning-of-buffer)
    (replace-string "<pre>" ".(t 0")
    (beginning-of-buffer)
    (replace-string "</pre>" ".)t 0")

    ; <ol> to .(L 1 .4v 4n
    (beginning-of-buffer)
    (replace-string "<ol>" ".(L 1 .4v 4n")

    ; <ol type=$1> to (L $1 .4v -4n
    (beginning-of-buffer)
    (replace-regexp "<ol type.*=.*\\(.*\\)>" "(L \\1")

    ; <ul> ... <li> ... </ul> to .(E .4v 4n ... .ee ... .)E
    (replace-enum-lists)

    ; <li> to .le (for <li>'s not converted from .ee's just above)
    (beginning-of-buffer)
    (replace-string "<li>" ".le")

    ; </ol> to .)L 1\n.)i
    (beginning-of-buffer)
    (replace-string "</ol>" ".)i\n.)L 1")

    ; <p align=center> ... </p> to .(C ... .)C
    (beginning-of-buffer)
    (replace-centered-paragraphs)
)

(defun replace-enum-lists ()
    (interactive)
    (while (search-forward-regexp "<ul.*>" nil t)
        (beginning-of-line)
	(kill-line 1)
	(insert-string ".(E\n")
	(setq done nil)
	(while (or (not done)
		   (search-forward-regexp "\\(</ul>\\|<li>\\)" nil t))
	    (setq ms (match-string 1))
	    (if (string= ms "<li>")
		(progn
		    (beginning-of-line)
		    (kill-line 1)
		    (insert-string ".ee\n")
		)
	        (progn
		    (beginning-of-line)
		    (kill-line 1)
		    (insert-string ".)E\n")
		    (setq done t)
		)
	    )
	)
    )
)

(defun replace-centered-paragraphs ()
    (interactive)
    (while (search-forward-regexp "<p align.*=.*center>" nil t)
       (replace-centered-paragraph)
    )
)

(defun replace-centered-paragraph ()
    (search-backward "<p")
    (kill-line 1)
    (insert-string ".(C\n")
    (search-forward "</p>" nil t)
    (kill-line 1)
    (insert-string ".)C\n")
)

;
; Currently unused (and unfinished) function that attempts generality in terms 
; of capitalization and position of html commands (in this case <br>).  If
; we're going to go this far, we probably might as well just use Lex and/or
; Yacc.
;
(defun replace-br ()
    (while (search-forward-regexp "<br>\|<BR>" nil t)
      (progn
	(if (not (= (following-char) 10))
	    (open-line 1)
	)
	(backward-char 4)
	(if (not (= (preceding-char) 10))
	    (open-line 1)
	)
      )
    )
)

(defun replace-sections ()
    (interactive)
    (while (search-forward-regexp "<h\\([1-9]\\)>" nil t)
      (setq m (match-string 1))
      (setq d (dot))
      (search-backward "<h")
      (setq b (dot))
      (goto-char d)
      (move-over-whitespace)
      (setq d (dot))
      (search-forward " ")
      (backward-char 1)
      (if (= (preceding-char) ?.)
	  (progn
	    (move-over-whitespace)
	    (setq d (dot))
	  )
      )
      (search-forward-regexp (concat "</h" m ">"))
      (setq e (dot))
      (search-backward "</h")
      (backward-word 1)
      (forward-word 1)
      (setq title (buffer-substring d (dot)))
      (goto-char b)
      (kill-region b e)
      (if (not (and (bolp) (eolp)))
	  (newline)
      )
      (insert-string (concat ".sh "
			     (int-to-string (- (string-to-int m) 1))
			     " \"" title "\""))
    )
)