12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769 |
- (require 'cl)
- (eval-when-compile
- (if (string-match "XEmacs" emacs-version)
- (byte-compiler-options
- (warnings (- unresolved))))
- (defvar font-lock-auto-fontify)
- (defvar font-lock-support-mode)
- (defvar global-font-lock-mode)
- (when (and (eq emacs-major-version 19)
- (not (string-match "XEmacs" emacs-version)))
-
-
- (load "cl-extra")))
- (defconst htmlize-version "1.36")
- (eval-and-compile
- (condition-case ()
- (require 'custom)
- (error nil))
- (if (and (featurep 'custom) (fboundp 'custom-declare-variable))
- nil
-
-
-
- (defmacro defgroup (&rest ignored) nil)
- (defmacro defcustom (var value doc &rest ignored)
- `(defvar ,var ,value ,doc))
- (defmacro defface (face value doc &rest stuff)
- `(make-face ,face))))
- (defgroup htmlize nil
- "Convert buffer text and faces to HTML."
- :group 'hypermedia)
- (defcustom htmlize-head-tags ""
- "*Additional tags to insert within HEAD of the generated document."
- :type 'string
- :group 'htmlize)
- (defcustom htmlize-output-type 'css
- "*Output type of generated HTML, one of `css', `inline-css', or `font'.
- When set to `css' (the default), htmlize will generate a style sheet
- with description of faces, and use it in the HTML document, specifying
- the faces in the actual text with <span class=\"FACE\">.
- When set to `inline-css', the style will be generated as above, but
- placed directly in the STYLE attribute of the span ELEMENT: <span
- style=\"STYLE\">. This makes it easier to paste the resulting HTML to
- other documents.
- When set to `font', the properties will be set using layout tags
- <font>, <b>, <i>, <u>, and <strike>.
- `css' output is normally preferred, but `font' is still useful for
- supporting old, pre-CSS browsers, and both `inline-css' and `font' for
- easier embedding of colorized text in foreign HTML documents (no style
- sheet to carry around)."
- :type '(choice (const css) (const inline-css) (const font))
- :group 'htmlize)
- (defcustom htmlize-generate-hyperlinks t
- "*Non-nil means generate the hyperlinks for URLs and mail addresses.
- This is on by default; set it to nil if you don't want htmlize to
- insert hyperlinks in the resulting HTML. (In which case you can still
- do your own hyperlinkification from htmlize-after-hook.)"
- :type 'boolean
- :group 'htmlize)
- (defcustom htmlize-hyperlink-style "
- a {
- color: inherit;
- background-color: inherit;
- font: inherit;
- text-decoration: inherit;
- }
- a:hover {
- text-decoration: underline;
- }
- "
- "*The CSS style used for hyperlinks when in CSS mode."
- :type 'string
- :group 'htmlize)
- (defcustom htmlize-replace-form-feeds t
- "*Non-nil means replace form feeds in source code with HTML separators.
- Form feeds are the ^L characters at line beginnings that are sometimes
- used to separate sections of source code. If this variable is set to
- `t', form feed characters are replaced with the <hr> separator. If this
- is a string, it specifies the replacement to use. Note that <pre> is
- temporarily closed before the separator is inserted, so the default
- replacement is effectively \"</pre><hr /><pre>\". If you specify
- another replacement, don't forget to close and reopen the <pre> if you
- want the output to remain valid HTML.
- If you need more elaborate processing, set this to nil and use
- htmlize-after-hook."
- :type 'boolean
- :group 'htmlize)
- (defcustom htmlize-html-charset nil
- "*The charset declared by the resulting HTML documents.
- When non-nil, causes htmlize to insert the following in the HEAD section
- of the generated HTML:
- <meta http-equiv=\"Content-Type\" content=\"text/html; charset=CHARSET\">
- where CHARSET is the value you've set for htmlize-html-charset. Valid
- charsets are defined by MIME and include strings like \"iso-8859-1\",
- \"iso-8859-15\", \"utf-8\", etc.
- If you are using non-Latin-1 charsets, you might need to set this for
- your documents to render correctly. Also, the W3C validator requires
- submitted HTML documents to declare a charset. So if you care about
- validation, you can use this to prevent the validator from bitching.
- Needless to say, if you set this, you should actually make sure that
- the buffer is in the encoding you're claiming it is in. (Under Mule
- that is done by ensuring the correct \"file coding system\" for the
- buffer.) If you don't understand what that means, this option is
- probably not for you."
- :type '(choice (const :tag "Unset" nil)
- string)
- :group 'htmlize)
- (defcustom htmlize-convert-nonascii-to-entities (featurep 'mule)
- "*Whether non-ASCII characters should be converted to HTML entities.
- When this is non-nil, characters with codes in the 128-255 range will be
- considered Latin 1 and rewritten as \"&#CODE;\". Characters with codes
- above 255 will be converted to \"&#UCS;\", where UCS denotes the Unicode
- code point of the character. If the code point cannot be determined,
- the character will be copied unchanged, as would be the case if the
- option were nil.
- When the option is nil, the non-ASCII characters are copied to HTML
- without modification. In that case, the web server and/or the browser
- must be set to understand the encoding that was used when saving the
- buffer. (You might also want to specify it by setting
- `htmlize-html-charset'.)
- Note that in an HTML entity \"&#CODE;\", CODE is always a UCS code point,
- which has nothing to do with the charset the page is in. For example,
- \"©\" *always* refers to the copyright symbol, regardless of charset
- specified by the META tag or the charset sent by the HTTP server. In
- other words, \"©\" is exactly equivalent to \"©\".
- By default, entity conversion is turned on for Mule-enabled Emacsen and
- turned off otherwise. This is because Mule knows the charset of
- non-ASCII characters in the buffer. A non-Mule Emacs cannot tell
- whether a character with code 0xA9 represents Latin 1 copyright symbol,
- Latin 2 \"S with caron\", or something else altogether. Setting this to
- t without Mule means asserting that 128-255 characters always mean Latin
- 1.
- For most people htmlize will work fine with this option left at the
- default setting; don't change it unless you know what you're doing."
- :type 'sexp
- :group 'htmlize)
- (defcustom htmlize-ignore-face-size 'absolute
- "*Whether face size should be ignored when generating HTML.
- If this is nil, face sizes are used. If set to t, sizes are ignored
- If set to `absolute', only absolute size specifications are ignored.
- Please note that font sizes only work with CSS-based output types."
- :type '(choice (const :tag "Don't ignore" nil)
- (const :tag "Ignore all" t)
- (const :tag "Ignore absolute" absolute))
- :group 'htmlize)
- (defcustom htmlize-css-name-prefix ""
- "*The prefix used for CSS names.
- The CSS names that htmlize generates from face names are often too
- generic for CSS files; for example, `font-lock-type-face' is transformed
- to `type'. Use this variable to add a prefix to the generated names.
- The string \"htmlize-\" is an example of a reasonable prefix."
- :type 'string
- :group 'htmlize)
- (defcustom htmlize-use-rgb-txt t
- "*Whether `rgb.txt' should be used to convert color names to RGB.
- This conversion means determining, for instance, that the color
- \"IndianRed\" corresponds to the (205, 92, 92) RGB triple. `rgb.txt'
- is the X color database that maps hundreds of color names to such RGB
- triples. When this variable is non-nil, `htmlize' uses `rgb.txt' to
- look up color names.
- If this variable is nil, htmlize queries Emacs for RGB components of
- colors using `color-instance-rgb-components' and `x-color-values'.
- This can yield incorrect results on non-true-color displays.
- If the `rgb.txt' file is not found (which will be the case if you're
- running Emacs on non-X11 systems), this option is ignored."
- :type 'boolean
- :group 'htmlize)
- (defcustom htmlize-html-major-mode nil
- "The mode the newly created HTML buffer will be put in.
- Set this to nil if you prefer the default (fundamental) mode."
- :type '(radio (const :tag "No mode (fundamental)" nil)
- (function-item html-mode)
- (function :tag "User-defined major mode"))
- :group 'htmlize)
- (defvar htmlize-before-hook nil
- "Hook run before htmlizing a buffer.
- The hook functions are run in the source buffer (not the resulting HTML
- buffer).")
- (defvar htmlize-after-hook nil
- "Hook run after htmlizing a buffer.
- Unlike `htmlize-before-hook', these functions are run in the generated
- HTML buffer. You may use them to modify the outlook of the final HTML
- output.")
- (defvar htmlize-file-hook nil
- "Hook run by `htmlize-file' after htmlizing a file, but before saving it.")
- (defvar htmlize-buffer-places)
- (defconst htmlize-running-xemacs (string-match "XEmacs" emacs-version))
- (eval-and-compile
-
-
-
-
-
-
-
-
-
- (unless (fboundp 'save-current-buffer)
- (defmacro save-current-buffer (&rest forms)
- `(let ((__scb_current (current-buffer)))
- (unwind-protect
- (progn ,@forms)
- (set-buffer __scb_current)))))
- (unless (fboundp 'with-current-buffer)
- (defmacro with-current-buffer (buffer &rest forms)
- `(save-current-buffer (set-buffer ,buffer) ,@forms)))
- (unless (fboundp 'with-temp-buffer)
- (defmacro with-temp-buffer (&rest forms)
- (let ((temp-buffer (gensym "tb-")))
- `(let ((,temp-buffer
- (get-buffer-create (generate-new-buffer-name " *temp*"))))
- (unwind-protect
- (with-current-buffer ,temp-buffer
- ,@forms)
- (and (buffer-live-p ,temp-buffer)
- (kill-buffer ,temp-buffer))))))))
- (cond
- (htmlize-running-xemacs
-
- (defun htmlize-next-change (pos prop &optional limit)
- (next-single-property-change pos prop nil (or limit (point-max)))))
- ((fboundp 'next-single-char-property-change)
-
- (defun htmlize-next-change (pos prop &optional limit)
- (next-single-char-property-change pos prop nil limit)))
- ((fboundp 'next-char-property-change)
-
- (defun htmlize-next-change (pos prop &optional limit)
- (let ((done nil)
- (current-value (get-char-property pos prop))
- newpos next-value)
-
-
- (while (not done)
- (setq newpos (next-char-property-change pos limit)
- next-value (get-char-property newpos prop))
- (cond ((eq newpos pos)
-
- (setq done t))
- ((eq next-value current-value)
-
- )
- (t
- (setq done t)))
- (setq pos newpos))
- pos)))
- (t
-
- (defun htmlize-next-change (pos prop &optional limit)
- (unless limit
- (setq limit (point-max)))
- (let ((res (next-single-property-change pos prop)))
- (if (or (null res)
- (> res limit))
- limit
- res)))))
- (defvar htmlize-basic-character-table
-
-
- (let ((table (make-vector 128 ?\0)))
-
-
- (dotimes (i 128)
- (setf (aref table i) (if (and (>= i 32) (<= i 126))
- (char-to-string i)
- (format "&#%d;" i))))
-
- (setf
-
- (aref table ?\n) "\n"
- (aref table ?\r) "\r"
- (aref table ?\t) "\t"
-
- (aref table ?&) "&"
- (aref table ?<) "<"
- (aref table ?>) ">"
-
-
-
-
- )
- table))
- (defvar htmlize-extended-character-cache (make-hash-table :test 'eq))
- (defun htmlize-protect-string (string)
- "HTML-protect string, escaping HTML metacharacters and I18N chars."
-
-
- (if (not (string-match "[^\r\n\t -%'-;=?-~]" string))
- string
- (mapconcat (lambda (char)
- (cond
- ((< char 128)
-
- (aref htmlize-basic-character-table char))
- ((gethash char htmlize-extended-character-cache)
-
-
- )
- ((not htmlize-convert-nonascii-to-entities)
-
-
- (setf (gethash char htmlize-extended-character-cache)
- (char-to-string char)))
- ((< char 256)
-
- (setf (gethash char htmlize-extended-character-cache)
- (format "&#%d;" char)))
- ((and (fboundp 'encode-char)
-
-
- (encode-char char 'ucs))
- (setf (gethash char htmlize-extended-character-cache)
- (format "&#%d;" (encode-char char 'ucs))))
- (t
-
-
- (setf (gethash char htmlize-extended-character-cache)
- (char-to-string char)))))
- string "")))
- (defconst htmlize-ellipsis "...")
- (put-text-property 0 (length htmlize-ellipsis) 'htmlize-ellipsis t htmlize-ellipsis)
- (defun htmlize-buffer-substring-no-invisible (beg end)
-
-
-
- (let ((pos beg)
- visible-list invisible show next-change)
-
-
-
- (while (< pos end)
- (setq invisible (get-char-property pos 'invisible)
- next-change (htmlize-next-change pos 'invisible end))
- (if (not (listp buffer-invisibility-spec))
-
-
- (setq show (not invisible))
-
-
-
-
-
-
-
-
- (let (match)
- (if (symbolp invisible)
- (setq match (member* invisible buffer-invisibility-spec
- :key (lambda (i)
- (if (symbolp i) i (car i)))))
- (setq match (block nil
- (dolist (elem invisible)
- (let ((m (member*
- elem buffer-invisibility-spec
- :key (lambda (i)
- (if (symbolp i) i (car i))))))
- (when m (return m))))
- nil)))
- (setq show (cond ((null match) t)
- ((and (cdr-safe (car match))
-
- (not (eq show htmlize-ellipsis)))
- htmlize-ellipsis)
- (t nil)))))
- (cond ((eq show t)
- (push (buffer-substring-no-properties pos next-change) visible-list))
- ((stringp show)
- (push show visible-list)))
- (setq pos next-change))
- (if (= (length visible-list) 1)
-
-
-
- (car visible-list)
- (apply #'concat (nreverse visible-list)))))
- (defun htmlize-trim-ellipsis (text)
-
-
-
-
- (if (get-text-property 0 'htmlize-ellipsis text)
- (substring text (length htmlize-ellipsis))
- text))
- (defconst htmlize-tab-spaces
-
-
- (let ((v (make-vector 32 nil)))
- (dotimes (i (length v))
- (setf (aref v i) (make-string i ?\ )))
- v))
- (defun htmlize-untabify (text start-column)
- "Untabify TEXT, assuming it starts at START-COLUMN."
- (let ((column start-column)
- (last-match 0)
- (chunk-start 0)
- chunks match-pos tab-size)
- (while (string-match "[\t\n]" text last-match)
- (setq match-pos (match-beginning 0))
- (cond ((eq (aref text match-pos) ?\t)
-
-
- (push (substring text chunk-start match-pos) chunks)
-
-
-
- (incf column (- match-pos last-match))
-
- (setq tab-size (- tab-width (% column tab-width)))
-
- (push (aref htmlize-tab-spaces tab-size) chunks)
- (incf column tab-size)
- (setq chunk-start (1+ match-pos)))
- (t
-
- (setq column 0)))
- (setq last-match (1+ match-pos)))
-
-
- (if (null chunks)
- text
- (when (< chunk-start (length text))
-
- (push (substring text chunk-start) chunks))
-
- (apply #'concat (nreverse chunks)))))
- (defun htmlize-despam-address (string)
- "Replace every occurrence of '@' in STRING with @.
- `htmlize-make-hyperlinks' uses this to spam-protect mailto links
- without modifying their meaning."
-
- (while (string-match "@" string)
- (setq string (replace-match "@" nil t string)))
- string)
- (defun htmlize-make-hyperlinks ()
- "Make hyperlinks in HTML."
-
-
- (goto-char (point-min))
- (while (re-search-forward
- "<\\(\\(mailto:\\)?\\([-=+_.a-zA-Z0-9]+@[-_.a-zA-Z0-9]+\\)\\)>"
- nil t)
- (let ((address (match-string 3))
- (link-text (match-string 1)))
- (delete-region (match-beginning 0) (match-end 0))
- (insert "<<a href=\"mailto:"
- (htmlize-despam-address address)
- "\">"
- (htmlize-despam-address link-text)
- "</a>>")))
- (goto-char (point-min))
- (while (re-search-forward "<\\(\\(URL:\\)?\\([a-zA-Z]+://[^;]+\\)\\)>"
- nil t)
- (let ((url (match-string 3))
- (link-text (match-string 1)))
- (delete-region (match-beginning 0) (match-end 0))
- (insert "<<a href=\"" url "\">" link-text "</a>>"))))
- (defun htmlize-defang-local-variables ()
-
-
-
-
- (goto-char (point-min))
- (while (search-forward "Local Variables:" nil t)
- (replace-match "Local Variables:" nil t)))
-
- (if (fboundp 'locate-file)
- (defalias 'htmlize-locate-file 'locate-file)
- (defun htmlize-locate-file (file path)
- (dolist (dir path nil)
- (when (file-exists-p (expand-file-name file dir))
- (return (expand-file-name file dir))))))
- (defvar htmlize-x-library-search-path
- '("/usr/X11R6/lib/X11/"
- "/usr/X11R5/lib/X11/"
- "/usr/lib/X11R6/X11/"
- "/usr/lib/X11R5/X11/"
- "/usr/local/X11R6/lib/X11/"
- "/usr/local/X11R5/lib/X11/"
- "/usr/local/lib/X11R6/X11/"
- "/usr/local/lib/X11R5/X11/"
- "/usr/X11/lib/X11/"
- "/usr/lib/X11/"
- "/usr/local/lib/X11/"
- "/usr/X386/lib/X11/"
- "/usr/x386/lib/X11/"
- "/usr/XFree86/lib/X11/"
- "/usr/unsupported/lib/X11/"
- "/usr/athena/lib/X11/"
- "/usr/local/x11r5/lib/X11/"
- "/usr/lpp/Xamples/lib/X11/"
- "/usr/openwin/lib/X11/"
- "/usr/openwin/share/lib/X11/"))
- (defun htmlize-get-color-rgb-hash (&optional rgb-file)
- "Return a hash table mapping X color names to RGB values.
- The keys in the hash table are X11 color names, and the values are the
- #rrggbb RGB specifications, extracted from `rgb.txt'.
- If RGB-FILE is nil, the function will try hard to find a suitable file
- in the system directories.
- If no rgb.txt file is found, return nil."
- (let ((rgb-file (or rgb-file (htmlize-locate-file
- "rgb.txt"
- htmlize-x-library-search-path)))
- (hash nil))
- (when rgb-file
- (with-temp-buffer
- (insert-file-contents rgb-file)
- (setq hash (make-hash-table :test 'equal))
- (while (not (eobp))
- (cond ((looking-at "^\\s-*\\([!#]\\|$\\)")
-
- )
- ((looking-at
- "[ \t]*\\([0-9]+\\)[ \t]+\\([0-9]+\\)[ \t]+\\([0-9]+\\)[ \t]+\\(.*\\)")
- (setf (gethash (downcase (match-string 4)) hash)
- (format "#%02x%02x%02x"
- (string-to-number (match-string 1))
- (string-to-number (match-string 2))
- (string-to-number (match-string 3)))))
- (t
- (error
- "Unrecognized line in %s: %s"
- rgb-file
- (buffer-substring (point) (progn (end-of-line) (point))))))
- (forward-line 1))))
- hash))
- (defvar htmlize-color-rgb-hash (htmlize-get-color-rgb-hash))
- (defun htmlize-face-specifies-property (face prop)
-
-
-
-
-
-
-
- (or (eq face 'default)
- (assq 'global (specifier-spec-list (face-property face prop)))))
- (defun htmlize-face-color-internal (face fg)
-
-
-
-
- (let* ((function (if fg #'face-foreground #'face-background))
- color)
- (if (>= emacs-major-version 22)
-
- (setq color (funcall function face nil t))
- (setq color (funcall function face))
-
-
- (when (and (null color)
- (fboundp 'face-attribute)
- (face-attribute face :inherit)
- (not (eq (face-attribute face :inherit) 'unspecified)))
- (setq color (htmlize-face-color-internal
- (face-attribute face :inherit) fg))))
- (when (and (eq face 'default) (null color))
- (setq color (cdr (assq (if fg 'foreground-color 'background-color)
- (frame-parameters)))))
- (when (or (eq color 'unspecified)
- (equal color "unspecified-fg")
- (equal color "unspecified-bg"))
- (setq color nil))
- (when (and (eq face 'default)
- (null color))
-
-
- (setq color (if fg "black" "white")))
- color))
- (defun htmlize-face-foreground (face)
-
-
- (cond (htmlize-running-xemacs
-
- (and (htmlize-face-specifies-property face 'foreground)
- (color-instance-name (face-foreground-instance face))))
- (t
-
- (htmlize-face-color-internal face t))))
- (defun htmlize-face-background (face)
-
-
- (cond (htmlize-running-xemacs
-
- (and (htmlize-face-specifies-property face 'background)
- (color-instance-name (face-background-instance face))))
- (t
-
- (htmlize-face-color-internal face nil))))
- (defun htmlize-color-to-rgb (color)
- (let ((rgb-string nil))
- (cond ((null color)
-
-
-
- )
- ((string-match "\\`#" color)
-
- (setq rgb-string color))
- ((and htmlize-use-rgb-txt
- htmlize-color-rgb-hash)
-
-
- (setq rgb-string (gethash (downcase color) htmlize-color-rgb-hash)))
- (t
-
- (let ((rgb
-
-
-
-
- (if htmlize-running-xemacs
- (mapcar (lambda (arg)
- (/ arg 256))
- (color-instance-rgb-components
- (make-color-instance color)))
- (mapcar (lambda (arg)
- (/ arg 256))
- (x-color-values color)))))
- (when rgb
- (setq rgb-string (apply #'format "#%02x%02x%02x" rgb))))))
-
-
-
- (or rgb-string color)))
- (defstruct htmlize-fstruct
- foreground
- background
- size
- boldp
- italicp
- underlinep
- overlinep
- strikep
- css-name
- )
- (defun htmlize-face-emacs21-attr (fstruct attr value)
-
- (case attr
- (:foreground
- (setf (htmlize-fstruct-foreground fstruct) (htmlize-color-to-rgb value)))
- (:background
- (setf (htmlize-fstruct-background fstruct) (htmlize-color-to-rgb value)))
- (:height
- (setf (htmlize-fstruct-size fstruct) value))
- (:weight
- (when (string-match (symbol-name value) "bold")
- (setf (htmlize-fstruct-boldp fstruct) t)))
- (:slant
- (setf (htmlize-fstruct-italicp fstruct) (or (eq value 'italic)
- (eq value 'oblique))))
- (:bold
- (setf (htmlize-fstruct-boldp fstruct) value))
- (:italic
- (setf (htmlize-fstruct-italicp fstruct) value))
- (:underline
- (setf (htmlize-fstruct-underlinep fstruct) value))
- (:overline
- (setf (htmlize-fstruct-overlinep fstruct) value))
- (:strike-through
- (setf (htmlize-fstruct-strikep fstruct) value))))
- (defun htmlize-face-size (face)
-
-
- (let ((size-list
- (loop
- for f = face then (ignore-errors (face-attribute f :inherit))
- until (or (not f) (eq f 'unspecified))
- for h = (ignore-errors (face-attribute f :height))
- collect (if (eq h 'unspecified) nil h))))
- (reduce 'htmlize-merge-size (cons nil size-list))))
- (defun htmlize-face-to-fstruct (face)
- "Convert Emacs face FACE to fstruct."
- (let ((fstruct (make-htmlize-fstruct
- :foreground (htmlize-color-to-rgb
- (htmlize-face-foreground face))
- :background (htmlize-color-to-rgb
- (htmlize-face-background face)))))
- (cond (htmlize-running-xemacs
-
-
-
-
- (let* ((font-instance (face-font-instance face))
- (props (font-instance-properties font-instance)))
- (when (equalp (cdr (assq 'WEIGHT_NAME props)) "bold")
- (setf (htmlize-fstruct-boldp fstruct) t))
- (when (or (equalp (cdr (assq 'SLANT props)) "i")
- (equalp (cdr (assq 'SLANT props)) "o"))
- (setf (htmlize-fstruct-italicp fstruct) t))
- (setf (htmlize-fstruct-strikep fstruct)
- (face-strikethru-p face))
- (setf (htmlize-fstruct-underlinep fstruct)
- (face-underline-p face))))
- ((fboundp 'face-attribute)
-
- (dolist (attr '(:weight :slant :underline :overline :strike-through))
- (let ((value (if (>= emacs-major-version 22)
-
- (face-attribute face attr nil t)
-
- (let ((face face))
- (while (and (eq (face-attribute face attr)
- 'unspecified)
- (not (eq (face-attribute face :inherit)
- 'unspecified)))
- (setq face (face-attribute face :inherit)))
- (face-attribute face attr)))))
- (when (and value (not (eq value 'unspecified)))
- (htmlize-face-emacs21-attr fstruct attr value))))
- (let ((size (htmlize-face-size face)))
- (unless (eql size 1.0)
- (setf (htmlize-fstruct-size fstruct) size))))
- (t
-
-
- (when (fboundp 'face-bold-p)
- (setf (htmlize-fstruct-boldp fstruct) (face-bold-p face)))
- (when (fboundp 'face-italic-p)
- (setf (htmlize-fstruct-italicp fstruct) (face-italic-p face)))
- (setf (htmlize-fstruct-underlinep fstruct)
- (face-underline-p face))))
-
-
-
-
- (setf (htmlize-fstruct-css-name fstruct)
- (let ((name (downcase (symbol-name face))))
- (when (string-match "\\`font-lock-" name)
-
- (setq name (replace-match "" t t name)))
- (when (string-match "-face\\'" name)
-
- (setq name (replace-match "" t t name)))
- (while (string-match "[^-a-zA-Z0-9]" name)
-
- (setq name (replace-match "X" t t name)))
- (when (string-match "\\`[-0-9]" name)
-
- (setq name (concat "X" name)))
-
-
- (when (equal name "")
- (setq name "face"))
-
- (setq name (concat htmlize-css-name-prefix name))
- name))
- fstruct))
- (defmacro htmlize-copy-attr-if-set (attr-list dest source)
-
-
-
-
- (cons 'progn
- (loop for attr in attr-list
- for attr-sym = (intern (format "htmlize-fstruct-%s" attr))
- collect `(and (,attr-sym ,source)
- (setf (,attr-sym ,dest) (,attr-sym ,source))))))
- (defun htmlize-merge-size (merged next)
-
- (cond ((null merged) next)
- ((integerp next) next)
- ((null next) merged)
- ((floatp merged) (* merged next))
- ((integerp merged) (round (* merged next)))))
- (defun htmlize-merge-two-faces (merged next)
- (htmlize-copy-attr-if-set
- (foreground background boldp italicp underlinep overlinep strikep)
- merged next)
- (setf (htmlize-fstruct-size merged)
- (htmlize-merge-size (htmlize-fstruct-size merged)
- (htmlize-fstruct-size next)))
- merged)
- (defun htmlize-merge-faces (fstruct-list)
- (cond ((null fstruct-list)
-
- (make-htmlize-fstruct))
- ((null (cdr fstruct-list))
-
-
- (car fstruct-list))
- (t
- (reduce #'htmlize-merge-two-faces
- (cons (make-htmlize-fstruct) fstruct-list)))))
- (defun htmlize-attrlist-to-fstruct (attrlist)
-
- (let ((fstruct (make-htmlize-fstruct)))
- (cond ((eq (car attrlist) 'foreground-color)
-
- (setf (htmlize-fstruct-foreground fstruct)
- (htmlize-color-to-rgb (cdr attrlist))))
- ((eq (car attrlist) 'background-color)
-
- (setf (htmlize-fstruct-background fstruct)
- (htmlize-color-to-rgb (cdr attrlist))))
- (t
-
- (while attrlist
- (let ((attr (pop attrlist))
- (value (pop attrlist)))
- (when (and value (not (eq value 'unspecified)))
- (htmlize-face-emacs21-attr fstruct attr value))))))
- (setf (htmlize-fstruct-css-name fstruct) "ATTRLIST")
- fstruct))
- (defun htmlize-face-list-p (face-prop)
- "Return non-nil if FACE-PROP is a list of faces, nil otherwise."
-
-
- (cond
- ((eq face-prop nil)
-
- t)
- ((symbolp face-prop)
-
-
- nil)
- ((not (consp face-prop))
-
- nil)
- (t
-
-
- (let* ((car (car face-prop))
- (attrlist-p (and (symbolp car)
- (or (eq car 'foreground-color)
- (eq car 'background-color)
- (eq (aref (symbol-name car) 0) ?:)))))
-
-
- (not attrlist-p)))))
- (defun htmlize-make-face-map (faces)
-
-
-
- (let ((face-map (make-hash-table :test 'equal))
- css-names)
- (dolist (face faces)
- (unless (gethash face face-map)
-
-
- (let ((fstruct (if (symbolp face)
- (htmlize-face-to-fstruct face)
- (htmlize-attrlist-to-fstruct face))))
- (setf (gethash face face-map) fstruct)
- (let* ((css-name (htmlize-fstruct-css-name fstruct))
- (new-name css-name)
- (i 0))
-
-
- (while (member new-name css-names)
- (setq new-name (format "%s-%s" css-name (incf i))))
- (unless (equal new-name css-name)
- (setf (htmlize-fstruct-css-name fstruct) new-name))
- (push new-name css-names)))))
- face-map))
- (defun htmlize-unstringify-face (face)
- "If FACE is a string, return it interned, otherwise return it unchanged."
- (if (stringp face)
- (intern face)
- face))
- (defun htmlize-faces-in-buffer ()
- "Return a list of faces used in the current buffer.
- Under XEmacs, this returns the set of faces specified by the extents
- with the `face' property. (This covers text properties as well.) Under
- GNU Emacs, it returns the set of faces specified by the `face' text
- property and by buffer overlays that specify `face'."
- (let (faces)
-
-
- (if htmlize-running-xemacs
- (let (face-prop)
- (map-extents (lambda (extent ignored)
- (setq face-prop (extent-face extent)
-
-
- faces (if (listp face-prop)
- (union face-prop faces)
- (adjoin face-prop faces)))
- nil)
- nil
-
-
- (point-min) (point-max) nil nil 'face))
-
-
- (let ((pos (point-min)) face-prop next)
- (while (< pos (point-max))
- (setq face-prop (get-text-property pos 'face)
- next (or (next-single-property-change pos 'face) (point-max)))
-
- (setq faces (if (htmlize-face-list-p face-prop)
- (nunion (mapcar #'htmlize-unstringify-face face-prop)
- faces :test 'equal)
- (adjoin (htmlize-unstringify-face face-prop)
- faces :test 'equal)))
- (setq pos next)))
-
- (dolist (overlay (overlays-in (point-min) (point-max)))
- (let ((face-prop (overlay-get overlay 'face)))
-
- (setq faces (if (htmlize-face-list-p face-prop)
- (nunion (mapcar #'htmlize-unstringify-face face-prop)
- faces :test 'equal)
- (adjoin (htmlize-unstringify-face face-prop)
- faces :test 'equal))))))
- faces))
- (cond (htmlize-running-xemacs
- (defun htmlize-faces-at-point ()
- (let (extent extent-list face-list face-prop)
- (while (setq extent (extent-at (point) nil 'face extent))
- (push extent extent-list))
-
-
-
- (setq extent-list (stable-sort extent-list #'<
- :key #'extent-priority))
- (dolist (extent extent-list)
- (setq face-prop (extent-face extent))
-
-
- (setq face-list (if (listp face-prop)
- (append face-prop face-list)
- (cons face-prop face-list))))
- (nreverse face-list))))
- (t
- (defun htmlize-faces-at-point ()
- (let (all-faces)
-
- (let ((face-prop (get-text-property (point) 'face)))
- (setq all-faces (if (htmlize-face-list-p face-prop)
- (nreverse (mapcar #'htmlize-unstringify-face
- face-prop))
- (list (htmlize-unstringify-face face-prop)))))
-
- (let ((overlays
-
- (delete-if-not (lambda (o)
- (overlay-get o 'face))
- (overlays-at (point))))
- list face-prop)
-
-
-
-
- (setq overlays (sort* overlays
-
- #'<
-
- :key (lambda (o)
- (- (overlay-end o)
- (overlay-start o)))))
-
-
-
- (setq overlays (stable-sort
- overlays
-
- #'<
-
- :key (lambda (o)
- (or (overlay-get o 'priority) 0))))
- (dolist (overlay overlays)
- (setq face-prop (overlay-get overlay 'face))
- (setq list (if (htmlize-face-list-p face-prop)
- (nconc (nreverse (mapcar
- #'htmlize-unstringify-face
- face-prop))
- list)
- (cons (htmlize-unstringify-face face-prop) list))))
-
-
-
- (setq all-faces (nconc all-faces list)))
- all-faces))))
- (defmacro htmlize-method (method &rest args)
-
-
- `(funcall (htmlize-method-function ',method) ,@args))
- (defun htmlize-method-function (method)
-
-
- (let ((sym (intern (format "htmlize-%s-%s" htmlize-output-type method))))
- (indirect-function (if (fboundp sym)
- sym
- (let ((default (intern (concat "htmlize-default-"
- (symbol-name method)))))
- (if (fboundp default)
- default
- 'ignore))))))
- (defvar htmlize-memoization-table (make-hash-table :test 'equal))
- (defmacro htmlize-memoize (key generator)
- "Return the value of GENERATOR, memoized as KEY.
- That means that GENERATOR will be evaluated and returned the first time
- it's called with the same value of KEY. All other times, the cached
- \(memoized) value will be returned."
- (let ((value (gensym)))
- `(let ((,value (gethash ,key htmlize-memoization-table)))
- (unless ,value
- (setq ,value ,generator)
- (setf (gethash ,key htmlize-memoization-table) ,value))
- ,value)))
- (defun htmlize-default-doctype ()
- nil
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01//EN\">"
-
-
- )
- (defun htmlize-default-body-tag (face-map)
- nil
- "<body>")
- (defun htmlize-css-specs (fstruct)
- (let (result)
- (when (htmlize-fstruct-foreground fstruct)
- (push (format "color: %s;" (htmlize-fstruct-foreground fstruct))
- result))
- (when (htmlize-fstruct-background fstruct)
- (push (format "background-color: %s;"
- (htmlize-fstruct-background fstruct))
- result))
- (let ((size (htmlize-fstruct-size fstruct)))
- (when (and size (not (eq htmlize-ignore-face-size t)))
- (cond ((floatp size)
- (push (format "font-size: %d%%;" (* 100 size)) result))
- ((not (eq htmlize-ignore-face-size 'absolute))
- (push (format "font-size: %spt;" (/ size 10.0)) result)))))
- (when (htmlize-fstruct-boldp fstruct)
- (push "font-weight: bold;" result))
- (when (htmlize-fstruct-italicp fstruct)
- (push "font-style: italic;" result))
- (when (htmlize-fstruct-underlinep fstruct)
- (push "text-decoration: underline;" result))
- (when (htmlize-fstruct-overlinep fstruct)
- (push "text-decoration: overline;" result))
- (when (htmlize-fstruct-strikep fstruct)
- (push "text-decoration: line-through;" result))
- (nreverse result)))
- (defun htmlize-css-insert-head (buffer-faces face-map)
- (insert " <style type=\"text/css\">\n <!--\n")
- (insert " body {\n "
- (mapconcat #'identity
- (htmlize-css-specs (gethash 'default face-map))
- "\n ")
- "\n }\n")
- (dolist (face (sort* (copy-list buffer-faces) #'string-lessp
- :key (lambda (f)
- (htmlize-fstruct-css-name (gethash f face-map)))))
- (let* ((fstruct (gethash face face-map))
- (cleaned-up-face-name
- (let ((s
-
-
-
- (prin1-to-string face)))
-
- (while (string-match "--" s)
- (setq s (replace-match "-" t t s)))
- (while (string-match "\\*/" s)
- (setq s (replace-match "XX" t t s)))
- s))
- (specs (htmlize-css-specs fstruct)))
- (insert " ." (htmlize-fstruct-css-name fstruct))
- (if (null specs)
- (insert " {")
- (insert " {\n /* " cleaned-up-face-name " */\n "
- (mapconcat #'identity specs "\n ")))
- (insert "\n }\n")))
- (insert htmlize-hyperlink-style
- " -->\n </style>\n"))
- (defun htmlize-css-insert-text (text fstruct-list buffer)
-
-
-
- (dolist (fstruct fstruct-list)
- (princ "<span class=\"" buffer)
- (princ (htmlize-fstruct-css-name fstruct) buffer)
- (princ "\">" buffer))
- (princ text buffer)
- (dolist (fstruct fstruct-list)
- (ignore fstruct)
- (princ "</span>" buffer)))
- (defun htmlize-inline-css-body-tag (face-map)
- (format "<body style=\"%s\">"
- (mapconcat #'identity (htmlize-css-specs (gethash 'default face-map))
- " ")))
- (defun htmlize-inline-css-insert-text (text fstruct-list buffer)
- (let* ((merged (htmlize-merge-faces fstruct-list))
- (style (htmlize-memoize
- merged
- (let ((specs (htmlize-css-specs merged)))
- (and specs
- (mapconcat #'identity (htmlize-css-specs merged) " "))))))
- (when style
- (princ "<span style=\"" buffer)
- (princ style buffer)
- (princ "\">" buffer))
- (princ text buffer)
- (when style
- (princ "</span>" buffer))))
- (defun htmlize-font-body-tag (face-map)
- (let ((fstruct (gethash 'default face-map)))
- (format "<body text=\"%s\" bgcolor=\"%s\">"
- (htmlize-fstruct-foreground fstruct)
- (htmlize-fstruct-background fstruct))))
-
- (defun htmlize-font-insert-text (text fstruct-list buffer)
-
-
-
- (let* ((merged (htmlize-merge-faces fstruct-list))
- (markup (htmlize-memoize
- merged
- (cons (concat
- (and (htmlize-fstruct-foreground merged)
- (format "<font color=\"%s\">" (htmlize-fstruct-foreground merged)))
- (and (htmlize-fstruct-boldp merged) "<b>")
- (and (htmlize-fstruct-italicp merged) "<i>")
- (and (htmlize-fstruct-underlinep merged) "<u>")
- (and (htmlize-fstruct-strikep merged) "<strike>"))
- (concat
- (and (htmlize-fstruct-strikep merged) "</strike>")
- (and (htmlize-fstruct-underlinep merged) "</u>")
- (and (htmlize-fstruct-italicp merged) "</i>")
- (and (htmlize-fstruct-boldp merged) "</b>")
- (and (htmlize-fstruct-foreground merged) "</font>"))))))
- (princ (car markup) buffer)
- (princ text buffer)
- (princ (cdr markup) buffer)))
- (defun htmlize-buffer-1 ()
-
-
-
-
- (save-excursion
-
- (save-excursion
- (run-hooks 'htmlize-before-hook))
-
-
- (htmlize-ensure-fontified)
- (clrhash htmlize-extended-character-cache)
- (clrhash htmlize-memoization-table)
- (let* ((buffer-faces (htmlize-faces-in-buffer))
- (face-map (htmlize-make-face-map (adjoin 'default buffer-faces)))
-
-
- (htmlbuf (generate-new-buffer (if (buffer-file-name)
- (htmlize-make-file-name
- (file-name-nondirectory
- (buffer-file-name)))
- "*html*")))
-
-
- (places '(nil nil))
- (title (if (buffer-file-name)
- (file-name-nondirectory (buffer-file-name))
- (buffer-name))))
-
- (with-current-buffer htmlbuf
- (buffer-disable-undo)
- (insert (htmlize-method doctype) ?\n
- (format "<!-- Created by htmlize-%s in %s mode. -->\n"
- htmlize-version htmlize-output-type)
- "<html>\n ")
- (plist-put places 'head-start (point-marker))
- (insert "<head>\n"
- " <title>" (htmlize-protect-string title) "</title>\n"
- (if htmlize-html-charset
- (format (concat " <meta http-equiv=\"Content-Type\" "
- "content=\"text/html; charset=%s\">\n")
- htmlize-html-charset)
- "")
- htmlize-head-tags)
- (htmlize-method insert-head buffer-faces face-map)
- (insert " </head>")
- (plist-put places 'head-end (point-marker))
- (insert "\n ")
- (plist-put places 'body-start (point-marker))
- (insert (htmlize-method body-tag face-map)
- "\n ")
- (plist-put places 'content-start (point-marker))
- (insert "<pre>\n"))
- (let ((insert-text-method
-
-
-
- (htmlize-method-function 'insert-text))
-
-
-
- next-change text face-list fstruct-list trailing-ellipsis)
-
-
-
-
-
-
- (goto-char (point-min))
- (while (not (eobp))
- (setq next-change (htmlize-next-change (point) 'face))
-
-
- (setq face-list (htmlize-faces-at-point)
- fstruct-list (delq nil (mapcar (lambda (f)
- (gethash f face-map))
- face-list)))
-
-
- (setq text (htmlize-buffer-substring-no-invisible
- (point) next-change))
- (when trailing-ellipsis
- (setq text (htmlize-trim-ellipsis text)))
-
- (when (> (length text) 0)
- (setq trailing-ellipsis
- (get-text-property (1- (length text))
- 'htmlize-ellipsis text)))
- (setq text (htmlize-untabify text (current-column)))
- (setq text (htmlize-protect-string text))
-
-
- (when (> (length text) 0)
-
-
- (funcall insert-text-method text fstruct-list htmlbuf))
- (goto-char next-change)))
-
- (with-current-buffer htmlbuf
- (insert "</pre>")
- (plist-put places 'content-end (point-marker))
- (insert "\n </body>")
- (plist-put places 'body-end (point-marker))
- (insert "\n</html>\n")
- (when htmlize-generate-hyperlinks
- (htmlize-make-hyperlinks))
- (htmlize-defang-local-variables)
- (when htmlize-replace-form-feeds
-
- (goto-char (point-min))
- (let ((source
-
- (htmlize-protect-string "\n\^L"))
- (replacement
- (if (stringp htmlize-replace-form-feeds)
- htmlize-replace-form-feeds
- "</pre><hr /><pre>")))
- (while (search-forward source nil t)
- (replace-match replacement t t))))
- (goto-char (point-min))
- (when htmlize-html-major-mode
-
-
- (funcall htmlize-html-major-mode))
- (set (make-local-variable 'htmlize-buffer-places) places)
- (run-hooks 'htmlize-after-hook)
- (buffer-enable-undo))
- htmlbuf)))
- (defmacro htmlize-with-fontify-message (&rest body)
-
-
-
- `(progn
- (if (> (buffer-size) 65536)
- (message "Forcing fontification of %s..."
- (buffer-name (current-buffer))))
- ,@body
- (if (> (buffer-size) 65536)
- (message "Forcing fontification of %s...done"
- (buffer-name (current-buffer))))))
- (defun htmlize-ensure-fontified ()
-
-
-
-
- (when (and (boundp 'font-lock-mode)
- font-lock-mode)
-
- (cond
- ((and (boundp 'jit-lock-mode)
- (symbol-value 'jit-lock-mode))
- (htmlize-with-fontify-message
- (jit-lock-fontify-now (point-min) (point-max))))
- ((and (boundp 'lazy-lock-mode)
- (symbol-value 'lazy-lock-mode))
- (htmlize-with-fontify-message
- (lazy-lock-fontify-region (point-min) (point-max))))
- ((and (boundp 'lazy-shot-mode)
- (symbol-value 'lazy-shot-mode))
- (htmlize-with-fontify-message
-
-
- (lazy-shot-fontify-region (point-min) (point-max))))
-
-
-
- )))
- (defun htmlize-buffer (&optional buffer)
- "Convert BUFFER to HTML, preserving colors and decorations.
- The generated HTML is available in a new buffer, which is returned.
- When invoked interactively, the new buffer is selected in the current
- window. The title of the generated document will be set to the buffer's
- file name or, if that's not available, to the buffer's name.
- Note that htmlize doesn't fontify your buffers, it only uses the
- decorations that are already present. If you don't set up font-lock or
- something else to fontify your buffers, the resulting HTML will be
- plain. Likewise, if you don't like the choice of colors, fix the mode
- that created them, or simply alter the faces it uses."
- (interactive)
- (let ((htmlbuf (with-current-buffer (or buffer (current-buffer))
- (htmlize-buffer-1))))
- (when (interactive-p)
- (switch-to-buffer htmlbuf))
- htmlbuf))
- (defun htmlize-region (beg end)
- "Convert the region to HTML, preserving colors and decorations.
- See `htmlize-buffer' for details."
- (interactive "r")
-
- (when (fboundp 'zmacs-deactivate-region)
- (zmacs-deactivate-region))
- (let ((htmlbuf (save-restriction
- (narrow-to-region beg end)
- (htmlize-buffer-1))))
- (when (interactive-p)
- (switch-to-buffer htmlbuf))
- htmlbuf))
- (defun htmlize-region-for-paste (beg end)
- "Htmlize the region and return just the HTML as a string.
- This forces the `inline-css' style and only returns the HTML body,
- but without the BODY tag. This should make it useful for inserting
- the text to another HTML buffer."
- (let* ((htmlize-output-type 'inline-css)
- (htmlbuf (htmlize-region beg end)))
- (unwind-protect
- (with-current-buffer htmlbuf
- (buffer-substring (plist-get htmlize-buffer-places 'content-start)
- (plist-get htmlize-buffer-places 'content-end)))
- (kill-buffer htmlbuf))))
- (defun htmlize-make-file-name (file)
- "Make an HTML file name from FILE.
- In its default implementation, this simply appends `.html' to FILE.
- This function is called by htmlize to create the buffer file name, and
- by `htmlize-file' to create the target file name.
- More elaborate transformations are conceivable, such as changing FILE's
- extension to `.html' (\"file.c\" -> \"file.html\"). If you want them,
- overload this function to do it and htmlize will comply."
- (concat file ".html"))
- (defun htmlize-file (file &optional target)
- "Load FILE, fontify it, convert it to HTML, and save the result.
- Contents of FILE are inserted into a temporary buffer, whose major mode
- is set with `normal-mode' as appropriate for the file type. The buffer
- is subsequently fontified with `font-lock' and converted to HTML. Note
- that, unlike `htmlize-buffer', this function explicitly turns on
- font-lock. If a form of highlighting other than font-lock is desired,
- please use `htmlize-buffer' directly on buffers so highlighted.
- Buffers currently visiting FILE are unaffected by this function. The
- function does not change current buffer or move the point.
- If TARGET is specified and names a directory, the resulting file will be
- saved there instead of to FILE's directory. If TARGET is specified and
- does not name a directory, it will be used as output file name."
- (interactive (list (read-file-name
- "HTML-ize file: "
- nil nil nil (and (buffer-file-name)
- (file-name-nondirectory
- (buffer-file-name))))))
- (let ((output-file (if (and target (not (file-directory-p target)))
- target
- (expand-file-name
- (htmlize-make-file-name (file-name-nondirectory file))
- (or target (file-name-directory file)))))
-
-
- (font-lock-mode nil)
- (font-lock-auto-fontify nil)
- (global-font-lock-mode nil)
-
- (font-lock-maximum-size nil)
-
-
-
- (font-lock-support-mode nil))
- (with-temp-buffer
-
- (insert-file-contents file)
-
-
-
- (let ((buffer-file-name file))
-
- (normal-mode)
- (font-lock-mode 1)
- (unless font-lock-mode
-
-
- (font-lock-fontify-buffer))
-
- (with-current-buffer (htmlize-buffer-1)
- (unwind-protect
- (progn
- (run-hooks 'htmlize-file-hook)
- (write-region (point-min) (point-max) output-file))
- (kill-buffer (current-buffer)))))))
-
-
- nil)
- (defun htmlize-many-files (files &optional target-directory)
- "Convert FILES to HTML and save the corresponding HTML versions.
- FILES should be a list of file names to convert. This function calls
- `htmlize-file' on each file; see that function for details. When
- invoked interactively, you are prompted for a list of files to convert,
- terminated with RET.
- If TARGET-DIRECTORY is specified, the HTML files will be saved to that
- directory. Normally, each HTML file is saved to the directory of the
- corresponding source file."
- (interactive
- (list
- (let (list file)
-
-
- (while (not (equal (setq file (read-file-name
- "HTML-ize file (RET to finish): "
- (and list (file-name-directory
- (car list)))
- "" t))
- ""))
- (push file list))
- (nreverse list))))
-
-
-
- (and target-directory
- (not (file-directory-p target-directory))
- (error "target-directory must name a directory: %s" target-directory))
- (dolist (file files)
- (htmlize-file file target-directory)))
- (defun htmlize-many-files-dired (arg &optional target-directory)
- "HTMLize dired-marked files."
- (interactive "P")
- (htmlize-many-files (dired-get-marked-files nil arg) target-directory))
- (provide 'htmlize)
|