htmlilize.el
;***************************************************************************************************
;*                                                                                                 *
;* htmlilize.el: create a syntax higlighted HTML version of sources.                               *
;*                                                                                                 *
;***************************************************************************************************

;***************************************************************************************************
;*                                                                                                 *
;*    Copyright (C) 2003 Joerg Kunze                                                               *
;*                                                                                                 *
;*    This file is part of siliconBrain.                                                           *
;*                                                                                                 *
;*    siliconBrain is free software; you can redistribute it and/or modify                         *
;*    it under the terms of the GNU General Public License as published by                         *
;*    the Free Software Foundation; either version 2 of the License, or                            *
;*    (at your option) any later version.                                                          *
;*                                                                                                 *
;*    siliconBrain is distributed in the hope that it will be useful,                              *
;*    but WITHOUT ANY WARRANTY; without even the implied warranty of                               *
;*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                                *
;*    GNU General Public License for more details.                                                 *
;*                                                                                                 *
;*    You should have received a copy of the GNU General Public License                            *
;*    along with this program; if not, write to the Free Software                                  *
;*    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA                    *
;*                                                                                                 *
;***************************************************************************************************
(defvar htmlilizeRelease       "$siliconBrainRelease: 0.2.3 $")
(defvar htmlilizeRcsIdentifier "$Id: htmlilize.el,v 1.37 2004/12/14 23:31:26 joerg Exp $")
(defvar htmlilizeSaveStamp     "$siliconBrainSaveStamp: 2004/12/14 22:34:30, Joerg Kunze$")

(princ (concat
   "htmlilize.el: create HTML syntax highlighted version of the sources\n"
   htmlilizeRelease       "\n"
   htmlilizeRcsIdentifier "\n"
   htmlilizeSaveStamp     "\n"
))

;***************************************************************************************************
;*                                                                                                 *
;* fontString: translate a string into a HTML version with the <font>-tag around and some chars    *
;*             replaces by their '&' version.                                                      *
;*                                                                                                 *
;***************************************************************************************************
(defun fontString (string color)
   "put font tag around string"

   (concat
      "<font color=\"#" color "\">"
      (mapconcat
         (function (lambda (singleCharacter)
            (cond
               ( (eq singleCharacter ?& ) "&amp;"                          )
               ( (eq singleCharacter ?< ) "&lt;"                           )
               ( (eq singleCharacter ?> ) "&gt;"                           )
               ( (>= singleCharacter 128) " "                              )  ; erase non-ASCII chars
               ( t                        (char-to-string singleCharacter) )
            )
         ))
         string
         ""
      )
      "</font>"
   )
)

;***************************************************************************************************
;*                                                                                                 *
;* webifyFile: open a file and create the HTML version of it.                                      *
;*                                                                                                 *
;***************************************************************************************************
(defun webifyFile (fileName)
   "create a html version of fileName"
(let (

   ;***************************************************************************************************
   ;*                                                                                                 *
   ;* the following configuration of all faces can be created by "htmlilizeGetColors.el".             *
   ;*                                                                                                 *
   ;***************************************************************************************************
   (texinfo-heading-face         "87CEFA") ; LightSkyBlue         34695 52942 64250
   (show-paren-mismatch-face     "FFFFFF") ; white                65535 65535 65535
   (show-paren-match-face        "B0C4DE") ; LightSteelBlue       45232 50372 57054
   (makefile-space-face          "B0C4DE") ; LightSteelBlue       45232 50372 57054
   (comint-highlight-prompt      "00FFFF") ; cyan                     0 65535 65535
   (comint-highlight-input       "B0C4DE") ; LightSteelBlue       45232 50372 57054
   (change-log-acknowledgement-face "FF7F24") ; chocolate1           65535 32639  9252
   (change-log-function-face     "EEDD82") ; LightGoldenrod       61166 56797 33410
   (change-log-conditionals-face "EEDD82") ; LightGoldenrod       61166 56797 33410
   (change-log-list-face         "00FFFF") ; Cyan                     0 65535 65535
   (change-log-file-face         "87CEFA") ; LightSkyBlue         34695 52942 64250
   (change-log-email-face        "EEDD82") ; LightGoldenrod       61166 56797 33410
   (change-log-name-face         "7FFFD4") ; Aquamarine           32639 65535 54484
   (change-log-date-face         "FFA07A") ; LightSalmon          65535 41120 31354
   (sh-heredoc-face              "FFFF00") ; yellow               65535 65535     0
   (font-lock-warning-face       "FF0000") ; Red                  65535     0     0
   (font-lock-constant-face      "7FFFD4") ; Aquamarine           32639 65535 54484
   (font-lock-type-face          "98FB98") ; PaleGreen            39064 64507 39064
   (font-lock-variable-name-face "EEDD82") ; LightGoldenrod       61166 56797 33410
   (font-lock-function-name-face "87CEFA") ; LightSkyBlue         34695 52942 64250
   (font-lock-builtin-face       "B0C4DE") ; LightSteelBlue       45232 50372 57054
   (font-lock-keyword-face       "00FFFF") ; Cyan                     0 65535 65535
   (font-lock-doc-face           "FFA07A") ; LightSalmon          65535 41120 31354
   (font-lock-string-face        "FFA07A") ; LightSalmon          65535 41120 31354
   (font-lock-comment-face       "FF7F24") ; chocolate1           65535 32639  9252
   (cscope-mouse-face            "FFFFFF") ; white                65535 65535 65535
   (cscope-line-face             "00FF00") ; green                    0 65535     0
   (cscope-line-number-face      "FF0000") ; red                  65535     0     0
   (cscope-function-face         "00FFFF") ; cyan                     0 65535 65535
   (cscope-file-face             "FFFF00") ; yellow               65535 65535     0
   (tooltip                      "000000") ; black                    0     0     0
   (isearch-lazy-highlight-face  "B0C4DE") ; LightSteelBlue       45232 50372 57054
   (isearch                      "8B2323") ; brown4               35723  8995  8995
   (trailing-whitespace          "B0C4DE") ; LightSteelBlue       45232 50372 57054
   (variable-pitch               "B0C4DE") ; LightSteelBlue       45232 50372 57054
   (fixed-pitch                  "B0C4DE") ; LightSteelBlue       45232 50372 57054
   (secondary-selection          "B0C4DE") ; LightSteelBlue       45232 50372 57054
   (highlight                    "B0C4DE") ; LightSteelBlue       45232 50372 57054
   (underline                    "B0C4DE") ; LightSteelBlue       45232 50372 57054
   (bold-italic                  "B0C4DE") ; LightSteelBlue       45232 50372 57054
   (italic                       "B0C4DE") ; LightSteelBlue       45232 50372 57054
   (bold                         "B0C4DE") ; LightSteelBlue       45232 50372 57054
   (mouse                        "B0C4DE") ; LightSteelBlue       45232 50372 57054
   (cursor                       "B0C4DE") ; LightSteelBlue       45232 50372 57054
   (border                       "B0C4DE") ; LightSteelBlue       45232 50372 57054
   (menu                         "B0C4DE") ; LightSteelBlue       45232 50372 57054
   (scroll-bar                   "B0C4DE") ; LightSteelBlue       45232 50372 57054
   (fringe                       "B0C4DE") ; LightSteelBlue       45232 50372 57054
   (region                       "B0C4DE") ; LightSteelBlue       45232 50372 57054
   (tool-bar                     "000000") ; black                    0     0     0
   (header-line                  "E5E5E5") ; grey90               58853 58853 58853
   (mode-line                    "000000") ; black                    0     0     0
   (default                      "FFFFFF") ; white                65535 65535 65535

   (fileBuffer)
   (htmlFile  )
   (htmlBuffer)
   (fileNameComplete (concat sourceRoot fileName))
)
   ;--------------------;
   ; prepare input file ;
   ;--------------------;

   ;---------------------;
   ; check file type ... ;
   ;---------------------;
   (set-buffer (generate-new-buffer fileNameComplete))
   (call-process "file" nil t nil "--brief" fileNameComplete)

   ;-----------------------------------------------------------------------;
   ; if type is text, open the real file, else htmlilize the "file" output ;
   ;-----------------------------------------------------------------------;
   (goto-char 0)
   (if (re-search-forward "\\btext\\b" nil t) (progn
      (set-buffer-modified-p nil)      ; allow the following kill command without user confirmation
      (kill-buffer (current-buffer))   ; detroy buffer with the "file" command output
      (find-file fileNameComplete)     ; open source file in read only mode
   ))

   (setq buffer-read-only t)           ; not change the source file by accident
   (setq fileBuffer (current-buffer))  ; remember the buffer
   (setq font-lock-verbose nil)        ; do fontifying silently
   (font-lock-fontify-buffer)          ; decorate the source
   (goto-char 0)                       ; be sure to start at the beginning

   ;---------------------;
   ; prepare output file ;
   ;---------------------;
   (setq htmlFile (concat
      targetRoot
      fileName
      ".html"
   ))
   (find-file htmlFile) (setq htmlBuffer (current-buffer))
   (erase-buffer)

   ;--------------------------------------------------;
   ; create "copying generated file" and version info ;
   ;--------------------------------------------------;
   (insert "<!--\n" )
   (call-process
      "formatHeader" nil t nil
         " "
         htmlilizeRelease
         htmlilizeRcsIdentifier
         htmlilizeSaveStamp
         "HTMLilized version of source code"
         htmlilizeRelease
         htmlilizeRcsIdentifier
         htmlilizeSaveStamp
   )
   (insert "-->\n" )

   (insert (concat "<html>
   <header>
      <title>" fileName "</title>
   </header>
   <body style=\"background-color:#000000\">
      <code><pre><font color=\"#FFFFFF\">")
   )

   ;-----------------------;
   ; loop thru fileBuffer. ;
   ;-----------------------;
   (set-buffer fileBuffer)

   (while (not (eobp))
      (let* (

         (currentProperty (get-text-property (point) 'face))   ; get the "face" property
         (changePoint (or
            (next-single-property-change (point) 'face)        ; find end of this face
	         (point-max)
         ))
         (currentString (buffer-substring (point) changePoint)) ; get string with this face
         )

         ;------------------------------------------------------------;
         ; some faces return a list instead of a face: then unlist it ;
         ;------------------------------------------------------------;
         (when  (listp currentProperty) (setq currentProperty (car currentProperty)))

         ;-----------------------------------------------------------------------------;
         ; insert HTML version using HTML color specification, which are stored in the ;
         ; face variables.                                                             ;
         ;-----------------------------------------------------------------------------;
         (set-buffer htmlBuffer)
 	      (insert (fontString currentString (or (eval currentProperty) "FFFFFF")))

         ;------;
         ; loop ;
         ;------;
	      (set-buffer fileBuffer)
	      (goto-char changePoint)
      )
   )

   ;-------------------------------;
   ; clean up and exit this source ;
   ;-------------------------------;
   (kill-buffer fileBuffer)

   (set-buffer  htmlBuffer)
   (insert "      </font></pre></code>
   </body>
</html>")
   (save-buffer htmlBuffer)
   (princ (format "wrote \"%s\".\n" htmlFile))
   (kill-buffer htmlBuffer)
))

;***************************************************************************************************
;*                                                                                                 *
;* webifyGetCommandLineArgument: replace the standard commandline processing function by this.     *
;*                                                                                                 *
;***************************************************************************************************
(defun webifyGetCommandLineArgument ()
   "collect the filenames to be processed"
   (webifyFile argi)
)

(setq command-line-functions '(webifyGetCommandLineArgument))

; $Log: htmlilize.el,v $
; Revision 1.37  2004/12/14 23:31:26  joerg
; published for new release 0.2.3
;
; Revision 1.36  2004/12/14 23:17:05  joerg
; published for new release 0.2.2
;
; Revision 1.35  2004/12/14 22:42:23  joerg
; allFiles: all sources have a Log CVS keyword at the end now.
;