(defun dot () (point)) ; Search for <!-- file: F --> comments and foreach, move the contents from ; there to the next such comment to the file named F.html. (defun separate-html-files () "Search for <!-- file: F --> comments and foreach, move the contents from there to the next such comment to the file named F.html." (interactive) (if (search-forward-regexp "<!-- file: \\(.*\\) -->" nil t) (progn (setq cur-f (match-string 1)) (beginning-of-line) ; Nuke the comment line itself. From time to time, this has been ; turned off, but it's on for now since we're trying to make the ; generated html look as simple and presentable as possible. (kill-line 1) (setq d (dot)) (next-line 1) (while (search-forward-regexp "<!-- file: \\(.*\\) -->" nil t) (setq new-f (match-string 1)) (beginning-of-line) (kill-line 1) (copy-to-html-file cur-f d) (next-line 1) ; Get past original file: comment (setq cur-f new-f) ) (end-of-buffer) (copy-to-html-file cur-f d) ; (insert-file "toc.html") ; THIS MUST HAPPEN LATER (save-buffer) ) ) ) (load "~gfisher/emacs/lib/my-save-buffer.el") (defun copy-to-html-file (cur-f d) (interactive) (setq f (concat cur-f ".html")) (copy-to-buffer f d (dot)) (delete-region d (dot)) (temp-switch-to-buffer f) (beginning-of-buffer) (insert-string "<html>\n<head>\n") (insert-html-title) (insert-string "</head>\n") (insert-string "<body bgcolor = white>\n") (insert-html-hyper-links) ; Fix the first </p><p> to <p>, if necessary. I can't think of when it ; won't be necessary, but we make it conditional just in case. (beginning-of-buffer) (if (search-forward "<p>" nil t) (progn (backward-char 3) (if (and (> (- (dot) 4) 0) (string= (buffer-substring (dot) (- (dot) 4)) "</p>")) (backward-delete-char 4) ) ) ) (end-of-buffer) (insert-string "</body>\n</html>\n") (write-file f) (switch-back-from-temp-buffer) ) (defun insert-html-title () "Search for the first html heading or title command. If the heading comes first, grab its argument and use it for the html title of the current file. If the title command comes first, then assume it was added on purpose at some point. E.g., the .Ch macro adds its own title. If neither html heading nor title is found, then insert no html title commands at all." (interactive) (if (search-forward-regexp "^<h[1-9]\\|^<title" nil t) (if (string= (substring (match-string 0) 0 2) "<h") (progn (next-line 1) (beginning-of-line) (setq d (dot)) (end-of-line) (setq title (buffer-substring d (dot))) (search-backward "<head>") (next-line 1) (insert-string (concat "<title>\n" title "\n</title>\n" ) ) ) (progn (search-forward "</title>" nil t) (next-line 1) (beginning-of-line) ) ) ) ) (defun insert-html-hyper-links () "Using info in hyper link comments, insert the actual links at the bottom of the file." (interactive) (end-of-buffer) (insert-string (concat "\n<br>\n<br>\n<br>\n<br>\n" "<hr width=80% size=3>\n" "<center>\n" "<font size=-1>\n" ) ) (insert-hyper-link "Prev") (insert-string "\n | ") (insert-hyper-link "Next") (insert-string "\n | ") (insert-hyper-link "Up") (insert-string "\n | ") (insert-hyper-link "Top") (insert-string (concat "</font>\n" "</center>\n" "<hr width=80% size=3>\n" ) ) ) (defvar html-link-name-color "black") (defun insert-hyper-link (link-type) "Insert an individual hyper link of the form: label: <a href=\"file-name.html\"> on the current end of the same line with the rest of the links, separated by a vertical bar." (interactive) (beginning-of-buffer) (search-forward-regexp (concat "<!-- " link-type ": \\(.*\\) -->") nil t) (setq link-dest (match-string 1)) (beginning-of-line) (kill-line 1) (end-of-buffer) (insert-string (concat "<font color=" html-link-name-color "> " link-type ": </font>" (if (not (string= link-dest "")) (concat "<a href=\"" link-dest ".html" "\">" link-dest "</a>" ) "[none]" ) ) ) )