1 ;;; htmlfontify.el --- htmlise a buffer/source tree with optional hyperlinks
3 ;; Copyright (C) 2002, 2003, 2009, 2010 Free Software Foundation, Inc.
5 ;; Emacs Lisp Archive Entry
6 ;; Package: htmlfontify
7 ;; Filename: htmlfontify.el
9 ;; Keywords: html, hypermedia, markup, etags
10 ;; Author: Vivek Dasmohapatra <vivek@etla.org>
11 ;; Maintainer: Vivek Dasmohapatra <vivek@etla.org>
12 ;; Created: 2002-01-05
13 ;; Description: htmlise a buffer/source tree with optional hyperlinks
14 ;; URL: http://rtfm.etla.org/emacs/htmlfontify/
15 ;; Compatibility: Emacs23, Emacs22
16 ;; Incompatibility: Emacs19, Emacs20, Emacs21
17 ;; Last Updated: Thu 2009-11-19 01:31:21 +0000
19 ;; This file is part of GNU Emacs.
21 ;; GNU Emacs is free software: you can redistribute it and/or modify
22 ;; it under the terms of the GNU General Public License as published by
23 ;; the Free Software Foundation, either version 3 of the License, or
24 ;; (at your option) any later version.
26 ;; GNU Emacs is distributed in the hope that it will be useful,
27 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
28 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29 ;; GNU General Public License for more details.
31 ;; You should have received a copy of the GNU General Public License
32 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
35 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
36 ;; I have made some changes to make it work for Emacs 22. A lot of
37 ;; small bug fixes related to the format of text and overlay
38 ;; properties (which might have changed since the beginning of 2003
39 ;; when this file was originally written).
41 ;; The function `hfy-face-at' currently carries much of the burden of
42 ;; my lacking understanding of the formats mentioned above and should
43 ;; need some knowledgeable help.
45 ;; Another thing that maybe could be fixed is that overlay background
46 ;; colors which are now only seen where there is text (in the XHTML
47 ;; output). A bit of CSS tweaking is necessary there.
49 ;; The face 'default has a value :background "SystemWindow" for the
50 ;; background color. There is no explicit notion that this should be
51 ;; considered transparent, but I have assumed that it could be handled
52 ;; like if it was here. (I am unsure that background and foreground
53 ;; priorities are handled ok, but it looks ok in my tests now.)
55 ;; 2007-12-27 Lennart Borgman
56 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
58 ;; Here's some elisp code to html-pretty-print an Emacs buffer, preserving
59 ;; the Emacs syntax/whatever highlighting. It also knows how to drive etags
60 ;; (exuberant-ctags or Emacs etags) and hyperlink the code according
61 ;; to its (etags') output.
63 ;; NOTE: Currently the hyperlinking code only knows how to drive GNU find
64 ;; and the exuberant and GNU variants of etags : I do not know of any other
65 ;; etags variants, but mechanisms have been provided to allow htmlfontify
66 ;; to be taught how to drive them. As long as your version of find has
67 ;; the -path test and is reasonably sane, you should be fine.
69 ;; A sample of the htmlfontified / hyperlinked output of this module can be
70 ;; found at http://rtfm.etla.org/sql/dbishell/src/ - it's not perfect, but
71 ;; it's a hell of a lot faster and more thorough than I could hope to be
72 ;; doing this by hand.
74 ;; some user / horrified onlooker comments:
75 ;; What? No! There's something deeply wrong here... (R. Shufflebotham)
76 ;; You're a freak. (D. Silverstone)
77 ;; Aren't we giving you enough to do? (J. Busuttil)
78 ;; You're almost as messed up as Lexx is! (N. Graves-Morris)
81 ;; Changes: moved to changelog (CHANGES) file.
84 (eval-when-compile (require 'cl
))
86 ;; (`facep' `face-attr-construct' `x-color-values' `color-values' `face-name')
88 ;; (`defgroup' `defcustom')
90 ;; (`font-lock-fontify-region')
94 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
95 ;; I want these - can't be bothered requiring all of cl though.
96 (if (not (fboundp 'caddr
))
98 "Return the `car' of the `cddr' of LIST."
101 (if (not (fboundp 'cadddr
))
103 "Return the `cadr' of the `cddr' of LIST."
105 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
108 'htmlfontify-load-rgb-file
110 "Load an rgb.txt file for color name -> rgb translation purposes."
114 'htmlfontify-unload-rgb-file
116 "Unload the current color name -> rgb translation map."
120 'hfy-fallback-colour-values
122 "Use a fallback method for obtaining the rgb values for a color."
126 (defconst htmlfontify-version
0.21)
128 (defconst hfy-meta-tags
129 (format "<meta name=\"generator\" content=\"emacs %s; htmlfontify %0.2f\" />"
130 emacs-version htmlfontify-version
)
131 "The generator meta tag for this version of htmlfontify.")
133 (defconst htmlfontify-manual
"Htmlfontify Manual"
134 "Copy and convert buffers and files to HTML, adding hyperlinks between files
135 \(driven by etags) if requested.
136 \nInteractive functions:
138 `htmlfontify-run-etags'
139 `htmlfontify-copy-and-link-dir'
140 `htmlfontify-load-rgb-file'
141 `htmlfontify-unload-rgb-file'\n
143 fontify a file you have open: M-x htmlfontify-buffer
144 prepare the etags map for a directory: M-x htmlfontify-run-etags
145 copy a directory, fontifying as you go: M-x htmlfontify-copy-and-link-dir\n
146 The following might be useful when running non-windowed or in batch mode:
147 \(note that they shouldn't be necessary - we have a built in map)\n
148 load an X11 style rgb.txt file: M-x htmlfontify-load-rgb-file
149 unload the current rgb.txt file: M-x htmlfontify-unload-rgb-file\n
150 And here's a programmatic example:\n
151 \(defun rtfm-build-page-header (file style)
152 (format \"#define TEMPLATE red+black.html
154 #include <build/menu-dirlist|>\\n
155 html-css-url := /css/red+black.css
156 title := rtfm.etla.org ( %s / src/%s )
158 head <=STYLESHEET;\\n
161 main-title := rtfm / %s / src/%s\\n
162 main-content <=MAIN_CONTENT;\\n\" rtfm-section file style rtfm-section file))
164 \(defun rtfm-build-page-footer (file) \"\\nMAIN_CONTENT\\n\")
166 \(defun rtfm-build-source-docs (section srcdir destdir)
168 \"s section[eg- emacs / p4-blame]:\\nD source-dir: \\nD output-dir: \")
169 (require 'htmlfontify)
170 (hfy-load-tags-cache srcdir)
171 (let ((hfy-page-header 'rtfm-build-page-header)
172 (hfy-page-footer 'rtfm-build-page-footer)
173 (rtfm-section section)
174 (hfy-index-file \"index\"))
175 (htmlfontify-run-etags srcdir)
176 (htmlfontify-copy-and-link-dir srcdir destdir \".src\" \".html\")))")
178 (defgroup htmlfontify nil
179 "Convert buffers and files to HTML."
181 :link
'(variable-link htmlfontify-manual
)
184 (defcustom hfy-page-header
'hfy-default-header
185 "Function called with two arguments (the filename relative to the top
186 level source directory being etag'd and fontified), and a string containing
187 the <style>...</style> text to embed in the document- the string returned will
188 be used as the header for the htmlfontified version of the source file.\n
189 See also `hfy-page-footer'."
191 ;; FIXME: Why place such a :tag everywhere? Isn't it imposing your
192 ;; own Custom preference on your users? --Stef
196 (defcustom hfy-split-index nil
197 "Whether or not to split the index `hfy-index-file' alphabetically
198 on the first letter of each tag. Useful when the index would otherwise
199 be large and take a long time to render or be difficult to navigate."
204 (defcustom hfy-page-footer
'hfy-default-footer
205 "As `hfy-page-header', but generates the output footer
206 \(and takes only one argument, the filename)."
211 (defcustom hfy-extn
".html"
212 "File extension used for output files."
217 (defcustom hfy-src-doc-link-style
"text-decoration: underline;"
218 "String to add to the '<style> a' variant of an htmlfontify CSS class."
220 :tag
"src-doc-link-style"
223 (defcustom hfy-src-doc-link-unstyle
" text-decoration: none;"
224 "Regex to remove from the <style> a variant of an htmlfontify CSS class."
226 :tag
"src-doc-link-unstyle"
229 (defcustom hfy-link-extn nil
230 "File extension used for href links.
231 Useful where the htmlfontify output files are going to be processed
232 again, with a resulting change in file extension. If nil, then any
233 code using this should fall back to `hfy-extn'."
235 :tag
"link-extension"
236 :type
'(choice string
(const nil
)))
238 (defcustom hfy-link-style-fun
'hfy-link-style-string
239 "Set this to a function, which will be called with one argument
240 \(a \"{ foo: bar; ...}\" CSS style-string) - it should return a copy of
241 its argument, altered so as to make any changes you want made for text which
242 is a hyperlink, in addition to being in the class to which that style would
243 normally be applied."
245 :tag
"link-style-function"
248 (defcustom hfy-index-file
"hfy-index"
249 "Name (sans extension) of the tag definition index file produced during
250 fontification-and-hyperlinking."
255 (defcustom hfy-instance-file
"hfy-instance"
256 "Name (sans extension) of the tag usage index file produced during
257 fontification-and-hyperlinking."
262 (defcustom hfy-html-quote-regex
"\\(<\\|\"\\|&\\|>\\)"
263 "Regex to match (with a single back-reference per match) strings in HTML
264 which should be quoted with `hfy-html-quote' (and `hfy-html-quote-map')
267 :tag
"html-quote-regex"
270 (define-obsolete-variable-alias 'hfy-init-kludge-hooks
'hfy-init-kludge-hook
272 (defcustom hfy-init-kludge-hook
'(hfy-kludge-cperl-mode)
273 "List of functions to call when starting `htmlfontify-buffer' to do any
274 kludging necessary to get highlighting modes to behave as you want, even
275 when not running under a window system."
277 :tag
"init-kludge-hooks"
280 (defcustom hfy-post-html-hooks nil
281 "List of functions to call after creating and filling the HTML buffer.
282 These functions will be called with the html buffer as the current buffer."
284 :tag
"post-html-hooks"
285 :options
'(set-auto-mode)
288 (defcustom hfy-default-face-def nil
289 "Fallback `defface' specification for the face 'default, used when
290 `hfy-display-class' has been set (the normal htmlfontify way of extracting
291 potentially non-current face information doesn't necessarily work for
293 Example: I customize this to:\n
294 \((t :background \"black\" :foreground \"white\" :family \"misc-fixed\"))"
296 :tag
"default-face-definition"
299 (defcustom hfy-etag-regex
(concat ".*"
300 "\x7f" "\\([^\x01\n]+\\)"
301 "\x01" "\\([0-9]+\\)"
303 "\\|" ".*\x7f[0-9]+,[0-9]+$")
304 "Regex used to parse an etags entry: must have 3 subexps, corresponding,
308 3 - The char (point) at which the tag occurs."
313 (defcustom hfy-html-quote-map
'(("\"" """)
317 "Alist of char -> entity mappings used to make the text HTML-safe."
319 :tag
"html-quote-map"
320 :type
'(alist :key-type
(string)))
322 (defconst hfy-e2x-etags-cmd
"for src in `find . -type f`;
326 *.ad[absm]|*.[CFHMSacfhlmpsty]|*.def|*.in[cs]|*.s[as]|*.src|*.cc|\\
327 *.hh|*.[chy]++|*.[ch]pp|*.[chy]xx|*.pdb|*.[ch]s|*.[Cc][Oo][Bb]|\\
328 *.[eh]rl|*.f90|*.for|*.java|*.[cem]l|*.clisp|*.lisp|*.[Ll][Ss][Pp]|\\
329 [Mm]akefile*|*.pas|*.[Pp][LlMm]|*.psw|*.lm|*.pc|*.prolog|*.oak|\\
330 *.p[sy]|*.sch|*.scheme|*.[Ss][Cc][Mm]|*.[Ss][Mm]|*.bib|*.cl[os]|\\
331 *.ltx|*.sty|*.TeX|*.tex|*.texi|*.texinfo|*.txi|*.x[bp]m|*.yy|\\
342 SHEBANG=`head -n1 ${src} | grep '#!' -c`;
343 if [ ${SHEBANG} -eq 1 ];
353 (defconst hfy-etags-cmd-alist-default
354 `(("emacs etags" .
,hfy-e2x-etags-cmd
)
355 ("exuberant ctags" .
"%s -R -f -" )))
357 (defcustom hfy-etags-cmd-alist
358 hfy-etags-cmd-alist-default
359 "Alist of possible shell commands that will generate etags output that
360 `htmlfontify' can use. '%s' will be replaced by `hfy-etags-bin'."
362 :tag
"etags-cmd-alist"
363 :type
'(alist :key-type
(string) :value-type
(string)) ))
365 (defcustom hfy-etags-bin
"etags"
366 "Location of etags binary (we begin by assuming it's in your path).\n
367 Note that if etags is not in your path, you will need to alter the shell
368 commands in `hfy-etags-cmd-alist'."
373 (defcustom hfy-shell-file-name
"/bin/sh"
374 "Shell (bourne or compatible) to invoke for complex shell operations."
376 :tag
"shell-file-name"
379 (defcustom hfy-ignored-properties
'(read-only
382 insert-in-front-hooks
386 "Properties to omit when copying a fontified buffer for HTML transformation."
388 :tag
"ignored-properties"
389 :type
'(repeat symbol
))
391 (defun hfy-which-etags ()
392 "Return a string indicating which flavour of etags we are using."
393 (let ((v (shell-command-to-string (concat hfy-etags-bin
" --version"))))
394 (cond ((string-match "exube" v
) "exuberant ctags")
395 ((string-match "GNU E" v
) "emacs etags" )) ))
397 (defcustom hfy-etags-cmd
398 (eval-and-compile (cdr (assoc (hfy-which-etags) hfy-etags-cmd-alist
)))
399 "The etags equivalent command to run in a source directory to generate a tags
400 file for the whole source tree from there on down. The command should emit
401 the etags output on stdout.\n
402 Two canned commands are provided - they drive Emacs' etags and
403 exuberant-ctags' etags respectively."
406 :type
(eval-and-compile
407 (let ((clist (list '(string))))
408 (dolist (C hfy-etags-cmd-alist
)
409 (push (list 'const
:tag
(car C
) (cdr C
)) clist
))
410 (cons 'choice clist
)) ))
412 (defcustom hfy-istext-command
"file %s | sed -e 's@^[^:]*:[ \t]*@@'"
413 "Command to run with the name of a file, to see whether it is a text file
414 or not. The command should emit a string containing the word 'text' if
415 the file is a text file, and a string not containing 'text' otherwise."
417 :tag
"istext-command"
420 (defcustom hfy-find-cmd
421 "find . -type f \\! -name \\*~ \\! -name \\*.flc \\! -path \\*/CVS/\\*"
422 "Find command used to harvest a list of files to attempt to fontify."
427 (defcustom hfy-display-class nil
428 "Display class to use to determine which display class to use when
429 calculating a face's attributes. This is useful when, for example, you
430 are running Emacs on a tty or in batch mode, and want htmlfontify to have
431 access to the face spec you would use if you were connected to an X display.\n
432 Some valid class specification elements are:\n
441 Multiple values for a tag may be combined, to indicate that any one or more
442 of these values in the specification key constitutes a match, eg:\n
443 '((class color grayscale) (type tty)) would match any of:\n
446 '((class color grayscale))
449 '((type tty) (class color))\n
451 :type
'(alist :key-type
(symbol) :value-type
(symbol))
454 :options
'((type (choice (const :tag
"X11" x-toolkit
)
455 (const :tag
"Terminal" tty
)
456 (const :tag
"Lucid Toolkit" lucid
)
457 (const :tag
"Motif Toolkit" motif
)))
459 (class (choice (const :tag
"Colour" color
)
460 (const :tag
"Greyscale" grayscale
)))
462 (background (choice (const :tag
"Dark" dark
)
463 (const :tag
"Bright" light
))) ))
465 (defcustom hfy-optimisations
(list 'keep-overlays
)
466 "Optimizations to turn on: So far, the following have been implemented:\n
467 merge-adjacent-tags: If two (or more) span tags are adjacent, identical and
468 separated by nothing more than whitespace, they will
469 be merged into one span.
470 zap-comment-links : Suppress hyperlinking of tags found in comments.
471 zap-string-links : Suppress hyperlinking of tags found in strings.
472 div-wrapper : Add <div class=\"default\"> </div> tags around the
474 keep-overlays : More of a bell (or possibly whistle) than an
475 optimization - If on, preserve overlay highlighting
476 (cf ediff or goo-font-lock) as well as basic faces.\n
477 And the following are planned but not yet available:\n
478 kill-context-leak : Suppress hyperlinking between files highlighted by
480 Note: like compiler optimizations, these optimize the _output_ of the code,
481 not the processing of the source itself, and are therefore likely to slow
482 htmlfontify down, at least a little. Except for skip-refontification,
483 which can never slow you down, but may result in incomplete fontification."
484 :type
'(set (const :tag
"merge-adjacent-tags" merge-adjacent-tags
)
485 (const :tag
"zap-comment-links" zap-comment-links
)
486 (const :tag
"zap-string-links" zap-string-links
)
487 (const :tag
"skip-refontification" skip-refontification
)
488 (const :tag
"kill-context-leak" kill-context-leak
)
489 (const :tag
"div-wrapper" div-wrapper
)
490 (const :tag
"keep-overlays" keep-overlays
))
492 :tag
"optimizations")
494 (defvar hfy-tags-cache nil
495 "Alist of the form:\n
496 \((\"/src/dir/0\" . tag-hash0) (\"/src/dir/1\" tag-hash1) ...)\n
497 Each tag hash entry then contains entries of the form:\n
498 \"tag_string\" => ((\"file/name.ext\" line char) ... )\n
499 ie an alist mapping (relative) file paths to line and character offsets.\n
500 See also `hfy-load-tags-cache'.")
502 (defvar hfy-tags-sortl nil
503 "Alist of the form ((\"/src/dir\" . (tag0 tag1 tag2)) ... )\n
504 where the tags are stored in descending order of length.\n
505 See also `hfy-load-tags-cache'.")
507 (defvar hfy-tags-rmap nil
508 "Alist of the form ((\"/src/dir\" . tag-rmap-hash))\n
509 where tag-rmap-hash has entries of the form:
510 \"tag_string\" => ( \"file/name.ext\" line char )
511 Unlike `hfy-tags-cache' these are the locations of occurrences of
512 tagged items, not the locations of their definitions.")
514 (defvar hfy-style-assoc
'please-ignore-this-line
515 "An assoc representing/describing an Emacs face.
516 Properties may be repeated, in which case later properties should be
517 treated as if they were inherited from a 'parent' font.
518 \(For some properties, only the first encountered value is of any importance,
519 for others the values might be cumulative, and for others they might be
520 cumulative in a complex way.)\n
522 \(hfy-face-to-style 'default) =>
523 ((\"background\" . \"rgb(0, 0, 0)\")
524 (\"color\" . \"rgb(255, 255, 255)\")
525 (\"font-style\" . \"normal\")
526 (\"font-weight\" . \"500\")
527 (\"font-stretch\" . \"normal\")
528 (\"font-family\" . \"misc-fixed\")
529 (\"font-size\" . \"13pt\")
530 (\"text-decoration\" . \"none\"))\n
531 \(hfy-face-to-style 'Info-title-3-face) =>
532 ((\"font-weight\" . \"700\")
533 (\"font-family\" . \"helv\")
534 (\"font-size\" . \"120%\")
535 (\"text-decoration\" . \"none\"))\n")
537 (defvar hfy-sheet-assoc
'please-ignore-this-line
538 "An assoc with elements of the form (face-name style-name . style-string):\n
539 '((default \"default\" . \"{background: black; color: white}\")
540 (font-lock-string-face \"string\" . \"{color: rgb(64,224,208)}\"))" )
542 (defvar hfy-facemap-assoc
'please-ignore-this-line
543 "An assoc of (point . FACE-SYMBOL) or (point . DEFFACE-LIST)
544 and (point . 'end) elements, in descending order of point value
545 \(ie from the file's end to its beginning).\n
546 The map is in reverse order because inserting a <style> tag (or any other
547 string) at `point' invalidates the map for all entries with a greater value of
548 point. By traversing the map from greatest to least point, we still invalidate
549 the map as we go, but only those points we have already dealt with (and
550 therefore no longer care about) will be invalid at any time.\n
552 (64744 . font-lock-comment-face)
554 (64722 . font-lock-string-face)
556 (64623 . font-lock-string-face)
558 (64446 . font-lock-keyword-face)
560 (64395 . font-lock-constant-face)
562 (64386 . font-lock-keyword-face)
564 ;; big similar section elided. You get the idea.
565 (4285 . font-lock-constant-face)
567 (4221 . font-lock-comment-face)
569 (4197 . font-lock-constant-face)
571 (1 . font-lock-comment-face))")
573 (defvar hfy-tmpfont-stack nil
574 "An alist of derived fonts resulting from overlays.")
576 (defconst hfy-hex-regex
"[0-9A-Fa-f]")
578 (defconst hfy-triplet-regex
580 "\\(" hfy-hex-regex hfy-hex-regex
"\\)"
581 "\\(" hfy-hex-regex hfy-hex-regex
"\\)"
582 "\\(" hfy-hex-regex hfy-hex-regex
"\\)"))
584 (defun hfy-interq (set-a set-b
)
585 "Return the intersection (using `eq') of two lists SET-A and SET-B."
586 (let ((sa set-a
) (interq nil
) (elt nil
))
590 (if (memq elt set-b
) (setq interq
(cons elt interq
)))) interq
))
592 (defun hfy-colour-vals (colour)
593 "Where COLOUR is a color name or #XXXXXX style triplet, return a
594 list of three (16 bit) rgb values for said color.\n
595 If a window system is unavailable, calls `hfy-fallback-colour-values'."
596 (if (string-match hfy-triplet-regex colour
)
598 (lambda (x) (* (string-to-number (match-string x colour
) 16) 257))
600 ;;(message ">> %s" colour)
602 (if (fboundp 'color-values
)
603 (color-values colour
)
604 ;;(message "[%S]" window-system)
605 (x-color-values colour
))
606 ;; blarg - tty colours are no good - go fetch some X colours:
607 (hfy-fallback-colour-values colour
))))
609 (defvar hfy-cperl-mode-kludged-p nil
)
611 (defun hfy-kludge-cperl-mode ()
612 "CPerl mode does its damndest not to do some of its fontification when not
613 in a windowing system - try to trick it..."
614 (if (not hfy-cperl-mode-kludged-p
)
615 (progn (if (not window-system
)
616 (let ((window-system 'htmlfontify
))
617 (eval-and-compile (require 'cperl-mode
))
618 (setq cperl-syntaxify-by-font-lock t
)))
619 (setq hfy-cperl-mode-kludged-p t
))) )
621 (defun hfy-opt (symbol) "Is option SYMBOL set." (memq symbol hfy-optimisations
))
623 (defun hfy-default-header (file style
)
624 "Default value for `hfy-page-header'.
625 FILE is the name of the file.
626 STYLE is the inline CSS stylesheet (or tag referring to an external sheet)."
627 ;; (format "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">
628 ;; <html>\n <head>\n <title>%s</title>\n %s\n </head>\n <body>\n" file style))
629 (format "<?xml version=\"1.0\" encoding=\"utf-8\"?>
630 <!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.1//EN\"
631 \"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd\">
632 <html xmlns=\"http://www.w3.org/1999/xhtml\">
636 <script type=\"text/javascript\"><!--
637 // this function is needed to work around
638 // a bug in IE related to element attributes
639 function hasClass(obj)
642 if (obj.getAttributeNode(\"class\") != null)
644 result = obj.getAttributeNode(\"class\").value;
651 // the flag we'll use to keep track of
652 // whether the current row is odd or even
655 // if arguments are provided to specify the colors
656 // of the even & odd rows, then use the them;
657 // otherwise use the following defaults:
658 var evenColor = arguments[1] ? arguments[1] : \"#fff\";
659 var oddColor = arguments[2] ? arguments[2] : \"#ddd\";
661 // obtain a reference to the desired table
662 // if no such table exists, abort
663 var table = document.getElementById(id);
664 if (! table) { return; }
666 // by definition, tables can have more than one tbody
667 // element, so we'll have to get the list of child
669 var tbodies = table.getElementsByTagName(\"tbody\");
671 // and iterate through them...
672 for (var h = 0; h < tbodies.length; h++)
674 // find all the <tr> elements...
675 var trs = tbodies[h].getElementsByTagName(\"tr\");
677 // ... and iterate through them
678 for (var i = 0; i < trs.length; i++)
680 // avoid rows that have a class attribute
681 // or backgroundColor style
682 if (! hasClass(trs[i]) &&
683 ! trs[i].style.backgroundColor)
685 // get all the cells in this row...
686 var tds = trs[i].getElementsByTagName(\"td\");
688 // and iterate through them...
689 for (var j = 0; j < tds.length; j++)
693 // avoid cells that have a class attribute
694 // or backgroundColor style
695 if (! hasClass(mytd) &&
696 ! mytd.style.backgroundColor)
698 mytd.style.backgroundColor =
699 even ? evenColor : oddColor;
703 // flip from odd to even, or vice-versa
709 function toggle_invis( name )
714 { var classname = node.id;
716 { var classbase = classname.substr( 0, name.length );
717 if( classbase == name ) { return NodeFilter.FILTER_ACCEPT; } }
718 return NodeFilter.FILTER_SKIP; } };
719 var walker = document.createTreeWalker( document.body ,
720 NodeFilter.SHOW_ELEMENT ,
723 while( walker.nextNode() )
725 var e = walker.currentNode;
726 if( e.style.display == \"none\" ) { e.style.display = \"inline\"; }
727 else { e.style.display = \"none\"; }
732 <body onload=\"stripe('index'); return true;\">\n"
735 (defun hfy-default-footer (file)
736 "Default value for `hfy-page-footer'.
737 FILE is the name of the file being rendered, in case it is needed."
738 "\n </body>\n</html>\n")
740 (defun hfy-link-style-string (style-string)
741 "Replace the end of a CSS style declaration STYLE-STRING with the contents
742 of the variable `hfy-src-doc-link-style', removing text matching the regex
743 `hfy-src-doc-link-unstyle' first, if necessary."
744 ;;(message "hfy-colour-vals");;DBUG
745 (if (string-match hfy-src-doc-link-unstyle style-string
)
746 (setq style-string
(replace-match "" 'fixed-case
'literal style-string
)))
747 (if (and (not (string-match hfy-src-doc-link-style style-string
))
748 (string-match "} *$" style-string
))
749 (concat (replace-match hfy-src-doc-link-style
752 style-string
) " }") style-string
))
754 ;; utility functions - cast emacs style specification values into their
756 (defun hfy-triplet (colour)
757 "Takes a COLOUR name (string) and return a CSS rgb(R, G, B) triplet string.
758 Uses the definition of \"white\" to map the numbers to the 0-255 range, so
759 if you've redefined white, (esp. if you've redefined it to have a triplet
760 member lower than that of the color you are processing) strange things
762 ;;(message "hfy-colour-vals");;DBUG
763 (let ((white (mapcar (lambda (I) (float (1+ I
))) (hfy-colour-vals "white")))
764 (rgb16 (mapcar (lambda (I) (float (1+ I
))) (hfy-colour-vals colour
))))
766 ;;(apply 'format "rgb(%d, %d, %d)"
767 ;; Use #rrggbb instead, it is smaller
768 (apply 'format
"#%02x%02x%02x"
774 (defun hfy-family (family) (list (cons "font-family" family
)))
775 (defun hfy-bgcol (colour) (list (cons "background" (hfy-triplet colour
))))
776 (defun hfy-colour (colour) (list (cons "color" (hfy-triplet colour
))))
777 (defun hfy-width (width) (list (cons "font-stretch" (symbol-name width
))))
779 (defcustom hfy-font-zoom
1.05
780 "Font scaling from Emacs to HTML."
784 (defun hfy-size (height)
785 "Derive a CSS font-size specifier from an Emacs font :height attribute HEIGHT.
786 Does not cope with the case where height is a function to be applied to
787 the height of the underlying font."
790 ;;(t (cons "font-size" ": 1em"))
792 (cons "font-size" (format "%d%%" (* (* hfy-font-zoom height
) 100))))
794 (cons "font-size" (format "%dpt" (/ (* hfy-font-zoom height
) 10 )))) )) )
796 (defun hfy-slant (slant)
797 "Derive a font-style CSS specifier from the Emacs :slant attribute SLANT:
798 CSS does not define the reverse-* styles, so just maps those to the
800 (list (cons "font-style"
801 (or (cdr (assq slant
'((italic .
"italic")
802 (reverse-italic .
"italic" )
803 (oblique .
"oblique")
804 (reverse-oblique .
"oblique"))))
807 (defun hfy-weight (weight)
808 "Derive a font-weight CSS specifier from an Emacs weight spec symbol WEIGHT."
809 (list (cons "font-weight" (cdr (assq weight
'((ultra-bold .
"900")
816 (extra-light .
"200")
817 (ultra-light .
"100")))))))
819 (defun hfy-box-to-border-assoc (spec)
821 (let ((tag (car spec
))
824 (:color
(cons "colour" val
))
825 (:width
(cons "width" val
))
826 (:style
(cons "style" val
)))
827 (hfy-box-to-border-assoc (cddr spec
))))))
829 (defun hfy-box-to-style (spec)
830 (let* ((css (hfy-box-to-border-assoc spec
))
831 (col (cdr (assoc "colour" css
)))
832 (s (cdr (assoc "style" css
))))
834 (if col
(cons "border-color" (cdr (assoc "colour" css
))))
835 (cons "border-width" (format "%dpx" (or (cdr (assoc "width" css
)) 1)))
836 (cons "border-style" (case s
837 (released-button "outset")
838 (pressed-button "inset" )
842 "Derive CSS border-* attributes from the Emacs :box attribute BOX."
845 ((integerp box
) (list (cons "border-width" (format "%dpx" box
))))
846 ((stringp box
) (list (cons "border" (format "solid %s 1px" box
))))
847 ((listp box
) (hfy-box-to-style box
) ))) )
849 (defun hfy-decor (tag val
)
850 "Derive CSS text-decoration specifiers from various Emacs font attributes.
851 TAG is an Emacs font attribute key (eg :underline).
854 ;; FIXME: Why not '("text-decoration" . "underline")? --Stef
856 (:underline
(cons "text-decoration" "underline" ))
857 (:overline
(cons "text-decoration" "overline" ))
858 (:strike-through
(cons "text-decoration" "line-through")))))
860 (defun hfy-invisible (&optional val
)
861 "This text should be invisible.
862 Do something in CSS to make that happen.
863 VAL is ignored here."
864 '(("display" .
"none")))
866 (defun hfy-combined-face-spec (face)
867 "Return a `defface' style alist of possible specifications for FACE.
868 Entries resulting from customization (`custom-set-faces') will take
871 (setq spec
(append (or (get face
'saved-face
) (list))
872 (or (get face
'face-defface-spec
) (list))))
873 (if (and hfy-display-class hfy-default-face-def
(eq face
'default
))
874 (setq spec
(append hfy-default-face-def spec
))) spec
))
876 (defun hfy-face-attr-for-class (face &optional class
)
877 "Return the face attributes for FACE.
878 If CLASS is set, it must be a `defface' alist key [see below],
879 in which case the first face specification returned by `hfy-combined-face-spec'
880 which *doesn't* clash with CLASS is returned.\n
881 \(A specification with a class of t is considered to match any class you
882 specify - this matches Emacs' behavior when deciding on which face attributes
883 to use, to the best of my understanding).\n
884 If CLASS is nil, then you just get get whatever `face-attr-construct' returns,
885 ie the current specification in effect for FACE.\n
886 *NOTE*: This function forces any face that is not 'default and which has
887 no :inherit property to inherit from 'default (this is because 'default
888 is magical in that Emacs' fonts behave as if they inherit implicitly from
889 'default, but no such behavior exists in HTML/CSS).\n
890 See also `hfy-display-class' for details of valid values for CLASS."
893 (let ((face-props (hfy-combined-face-spec face
))
900 (setq face-specn
(car face-props
)
901 face-class
(car face-specn
)
902 face-attrs
(cdr face-specn
)
903 face-props
(cdr face-props
))
904 ;; if the current element CEL of CLASS is t we match
905 ;; if the current face-class is t, we match
906 ;; if the cdr of CEL has a non-nil
907 ;; intersection with the cdr of the first member of
908 ;; the current face-class with the same car as CEL, we match
909 ;; if we actually clash, then we can't match
917 (while (and cbuf
(not next
))
922 val
(if (listp val
) val
(list val
)))
925 (memq face-class
'(t default
))) ;Default match.
926 (setq score
0) (ignore "t match"))
927 ((not (cdr (assq key face-class
))) ;Neither good nor bad.
928 nil
(ignore "non match, non collision"))
929 ((setq x
(hfy-interq val
(cdr (assq key face-class
))))
930 (setq score
(+ score
(length x
)))
931 (ignore "intersection"))
933 (setq next t score -
10) (ignore "collision")) ))
934 (if (> score face-score
)
936 (setq face-match face-attrs
938 (ignore "%d << %S/%S" score face-class class
))
939 (ignore "--- %d ---- (insufficient)" score
)) ))
940 ;; matched ? last attrs : nil
942 (if (listp (car face-match
)) (car face-match
) face-match
)
944 ;; Unfortunately the default face returns a
945 ;; :background. Fortunately we can remove it, but how do we do
946 ;; that in a non-system specific way?
947 (let ((spec (face-attr-construct face
))
949 (if (not (memq :background spec
))
952 (let ((a (nth 0 spec
))
954 (unless (and (eq a
:background
)
956 (string= b
"SystemWindow"))
957 (setq new-spec
(cons a
(cons b new-spec
)))))
958 (setq spec
(cddr spec
)))
960 (if (or (memq :inherit face-spec
) (eq 'default face
))
962 (nconc face-spec
(list :inherit
'default
))) ))
964 ;; construct an assoc of (css-tag-name . css-tag-value) pairs
965 ;; from a face or assoc of face attributes:
968 ;; (mumamo-message-with-face "testing face" 'highlight)
969 ;; (mumamo-message-with-face "testing face" '(:foreground "red" :background "yellow"))
970 ;; (hfy-face-to-style-i '(:inherit default foreground-color "red"))
971 ;; default face=(:stipple nil :background "SystemWindow" :foreground
972 ;; "SystemWindowText" :inverse-video nil :box nil :strike-through
973 ;; nil :overline nil :underline nil :slant normal :weight normal
974 ;; :height 98 :width normal :family "outline-courier new")
975 (defun hfy-face-to-style-i (fn)
976 "The guts of `hfy-face-to-style': FN should be a `defface' font spec,
977 as returned by `face-attr-construct' or `hfy-face-attr-for-class'.
978 Note that this function does not get font-sizes right if they are based
979 on inherited modifiers (via the :inherit) attribute, and any other
980 modifiers that are cumulative if they appear multiple times need to be
981 merged by the user - `hfy-flatten-style' should do this."
982 ;;(message "hfy-face-to-style-i");;DBUG
984 ;; fn's value could be something like
986 ;; ((foreground-color . "blue"))
987 ;; (foreground-color . "blue")
997 (if (eq key
:inherit
)
998 (let ((vs (if (listp val
) val
(list val
))))
999 ;; (let ((x '(a b))) (setq x (append '(c d) x)))
1000 ;; (let ((x '(a b))) (setq x (append '(c d) x)))
1005 (hfy-face-to-style-i
1006 (hfy-face-attr-for-class v hfy-display-class
)) ))))
1009 (:family
(hfy-family val
))
1010 (:width
(hfy-width val
))
1011 (:weight
(hfy-weight val
))
1012 (:slant
(hfy-slant val
))
1013 (:foreground
(hfy-colour val
))
1014 (:background
(hfy-bgcol val
))
1015 (:box
(hfy-box val
))
1016 (:height
(hfy-size val
))
1017 (:underline
(hfy-decor key val
))
1018 (:overline
(hfy-decor key val
))
1019 (:strike-through
(hfy-decor key val
))
1020 (:invisible
(hfy-invisible val
))
1021 (:bold
(hfy-weight 'bold
))
1022 (:italic
(hfy-slant 'italic
))))))
1023 (setq that
(hfy-face-to-style-i next
))
1024 ;;(lwarn t :warning "%S => %S" fn (nconc this that parent))
1025 (nconc this that parent
))) )
1027 (defun hfy-size-to-int (spec)
1028 "Convert SPEC, a CSS font-size specifier, to an Emacs :height attribute value.
1029 Used while merging multiple font-size attributes."
1030 ;;(message "hfy-size-to-int");;DBUG
1032 (if (string-match "\\([0-9]+\\)\\(%\\|pt\\)" spec
)
1033 (cond ((string= "%" (match-string 2 spec
))
1034 (/ (string-to-number (match-string 1 spec
)) 100.0))
1035 ((string= "pt" (match-string 2 spec
))
1036 (* (string-to-number (match-string 1 spec
)) 10)))
1037 (string-to-number spec
))) )
1039 ;; size is different, in that in order to get it right at all,
1040 ;; we have to trawl the inheritance path, accumulating modifiers,
1041 ;; _until_ we get to an absolute (pt) specifier, then combine the lot
1042 (defun hfy-flatten-style (style)
1043 "Take STYLE (see `hfy-face-to-style-i', `hfy-face-to-style') and merge
1044 any multiple attributes appropriately. Currently only font-size is merged
1045 down to a single occurrence - others may need special handling, but I
1046 haven't encountered them yet. Returns a `hfy-style-assoc'."
1047 ;;(message "(hfy-flatten-style %S)" style) ;;DBUG
1053 (if (string= (car css
) "font-size")
1055 (when (not x
) (setq m
(nconc m
(hfy-size-to-int (cdr css
)))))
1056 (when (string-match "pt" (cdr css
)) (setq x t
)))
1057 (setq r
(nconc r
(list css
)))))
1058 ;;(message "r: %S" r)
1059 (setq n
(apply '* m
))
1060 (nconc r
(hfy-size (if x
(round n
) (* n
1.0)))) ))
1062 (defun hfy-face-resolve-face (fn)
1065 (hfy-face-attr-for-class fn hfy-display-class
))
1067 (facep (symbol-value fn
)))
1068 ;; Obsolete faces like `font-lock-reference-face' are defined as
1069 ;; aliases for another face.
1070 (hfy-face-attr-for-class (symbol-value fn
) hfy-display-class
))
1074 (defun hfy-face-to-style (fn)
1075 "Take FN, a font or `defface' style font specification,
1076 \(as returned by `face-attr-construct' or `hfy-face-attr-for-class')
1077 and return a `hfy-style-assoc'.\n
1078 See also `hfy-face-to-style-i', `hfy-flatten-style'."
1079 ;;(message "hfy-face-to-style");;DBUG
1080 (let ((face-def (hfy-face-resolve-face fn
))
1083 (setq final-style
(hfy-flatten-style (hfy-face-to-style-i face-def
)))
1084 ;;(message "%S" final-style)
1085 (if (not (assoc "text-decoration" final-style
))
1086 (progn (setq final-style
1087 ;; Fix-me: there is no need for this since
1088 ;; text-decoration is not inherited.
1089 ;; but it's not wrong and if this ever changes it will
1090 ;; be needed, so I think it's better to leave it in? -- v
1091 (nconc final-style
'(("text-decoration".
"none"))))))
1094 ;; strip redundant bits from a name. Technically, this could result in
1095 ;; a collision, but it is pretty unlikely - will fix later...
1096 ;; also handle ephemeral fonts created by overlays, which don't actually
1098 (defun hfy-face-or-def-to-name (fn)
1099 "Render a font symbol or `defface' font spec FN into a name (string)."
1100 ;;(message "generating name for %s" fn)
1101 (if (not (listp fn
))
1103 (let* ((key (format "%s" fn
))
1104 (entry (assoc key hfy-tmpfont-stack
))
1105 (base (cadr (memq :inherit fn
)))
1107 ;;(message "checking for key «%s» in font stack [%d]"
1108 ;; key (if entry 1 0))
1109 (if entry nil
;; noop
1110 (setq tag
(format "%04d" (length hfy-tmpfont-stack
))
1111 entry
(cons key tag
)
1112 hfy-tmpfont-stack
(cons entry hfy-tmpfont-stack
)))
1113 ;;(message " -> name: %s-%s" (or base 'default) tag)
1114 (format "%s-%s" (or base
'default
) tag
)) ))
1116 (defun hfy-css-name (fn)
1117 "Strip the boring bits from a font-name FN and return a CSS style name."
1118 ;;(message "hfy-css-name");;DBUG
1119 (let ((face-name (hfy-face-or-def-to-name fn
)))
1120 (if (or (string-match "font-lock-\\(.*\\)" face-name
)
1121 (string-match "cperl-\\(.*\\)" face-name
)
1122 (string-match "^[Ii]nfo-\\(.*\\)" face-name
))
1124 (setq face-name
(match-string 1 face-name
))
1125 (if (string-match "\\(.*\\)-face$" face-name
)
1126 (setq face-name
(match-string 1 face-name
))) face-name
)
1129 ;; construct an assoc of (stripped-name . "{ css-stuff-here }") pairs
1131 (defun hfy-face-to-css (fn)
1132 "Take FN, a font or `defface' specification (cf `face-attr-construct')
1133 and return a CSS style specification.\n
1134 See also `hfy-face-to-style'."
1135 ;;(message "hfy-face-to-css");;DBUG
1136 (let ((css-list nil
)
1139 ;;(message "(hfy-face-to-style %S)" fn)
1140 (setq css-list
(hfy-face-to-style fn
))
1145 (unless (member (car E
) seen
)
1147 (format " %s: %s; " (car E
) (cdr E
)))))
1149 (cons (hfy-css-name fn
) (format "{%s}" (apply 'concat css-text
)))) )
1151 ;; extract a face from a list of char properties, if there is one:
1152 (defun hfy-p-to-face (props)
1153 "Given PROPS, a list of text properties, return the value of the face
1156 (if (string= (car props
) "face")
1157 (let ((propval (cadr props
)))
1158 (if (and (listp propval
) (not (cdr propval
)))
1161 (hfy-p-to-face (cddr props
)))
1164 (defun hfy-p-to-face-lennart (props)
1165 "Given PROPS, a list of text properties, return the value of the face
1168 (let ((face (plist-get props
'face
))
1169 (font-lock-face (plist-get props
'font-lock-face
))
1170 (button (plist-get props
'button
))
1171 ;;(face-rec (memq 'face props))
1172 ;;(button-rec (memq 'button props)))
1175 (let* ((category (plist-get props
'category
))
1176 (face (when category
(plist-get (symbol-plist category
) 'face
))))
1181 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1182 ;; (defun hfy-get-face-at (pos)
1183 ;; ;; (let ((face (get-char-property-and-overlay pos 'face)))
1184 ;; ;; (when (and face (listp face)) (setq face (car face)))
1185 ;; ;; (unless (listp face)
1187 ;; ;;(get-char-property pos 'face)
1188 ;; ;; Overlays are handled later
1189 ;; (if (or (not show-trailing-whitespace)
1190 ;; (not (get-text-property pos 'hfy-show-trailing-whitespace)))
1191 ;; (get-text-property pos 'face)
1192 ;; (list 'trailing-whitespace (get-text-property pos 'face)))
1195 (defun hfy-prop-invisible-p (prop)
1196 "Is text property PROP an active invisibility property?"
1197 (or (and (eq buffer-invisibility-spec t
) prop
)
1198 (or (memq prop buffer-invisibility-spec
)
1199 (assq prop buffer-invisibility-spec
))))
1201 (defun hfy-find-invisible-ranges ()
1202 "Return a list of (start-point . end-point) cons cells of invisible regions."
1203 (let (invisible p i e s
) ;; return-value pos invisible end start
1205 (setq p
(goto-char (point-min)))
1206 (when (invisible-p p
) (setq s p i t
))
1207 (while (< p
(point-max))
1208 (if i
;; currently invisible
1209 (when (not (invisible-p p
)) ;; but became visible
1212 invisible
(cons (cons s e
) invisible
)))
1213 ;; currently visible:
1214 (when (invisible-p p
) ;; but have become invisible
1216 (setq p
(next-char-property-change p
)))
1217 ;; still invisible at buffer end?
1220 invisible
(cons (cons s e
) invisible
))) ) invisible
))
1222 (defun hfy-invisible-name (point map
)
1223 "Generate a CSS style name for an invisible section of the buffer.
1224 POINT is the point inside the invisible region.
1225 MAP is the invisibility map as returned by `hfy-find-invisible-ranges'."
1226 ;;(message "(hfy-invisible-name %S %S)" point map)
1229 (when (and (>= point
(car range
))
1230 (< point
(cdr range
)))
1231 (setq name
(format "invisible-%S-%S" (car range
) (cdr range
)))))
1234 ;; Fix-me: This function needs some cleanup by someone who understand
1235 ;; all the formats that face properties can have.
1237 ;; overlay handling should be fine. haven't tested multiple stacked overlapping
1238 ;; overlays recently, but the common case of a text property face + an overlay
1239 ;; face produces the correct merged css style (or as close to it as css can get)
1241 (defun hfy-face-at (p)
1242 "Find face in effect at point P.
1243 If overlays are to be considered (see `hfy-optimisations') then this may
1244 return a `defface' style list of face properties instead of a face symbol."
1245 ;;(message "hfy-face-at");;DBUG
1246 ;; Fix-me: clean up, remove face-name etc
1247 ;; not sure why we'd want to remove face-name? -- v
1248 (let ((overlay-data nil
)
1250 ;; restored hfy-p-to-face as it handles faces like (bold) as
1251 ;; well as face like 'bold - hfy-get-face-at doesn't dtrt -- v
1252 (face-name (hfy-p-to-face (text-properties-at p
)))
1253 ;; (face-name (hfy-get-face-at p))
1256 (text-props (text-properties-at p
)))
1257 ;;(message "face-name: %S" face-name)
1258 (when (and face-name
(listp face-name
) (facep (car face-name
)))
1259 ;;(message "face-name is a list %S" face-name)
1260 ;;(setq text-props (cons 'face face-name))
1261 (dolist (f face-name
)
1262 (setq extra-props
(if (listp f
)
1263 ;; for things like (variable-pitch
1264 ;; (:foreground "red"))
1265 (cons f extra-props
)
1266 (cons :inherit
(cons f extra-props
)))))
1267 (setq base-face
(car face-name
)
1269 ;; text-properties-at => (face (:foreground "red" ...))
1270 ;; or => (face (compilation-info underline)) list of faces
1271 ;; overlay-properties
1272 ;; format= (evaporate t face ((foreground-color . "red")))
1274 ;; SO: if we have turned overlays off,
1275 ;; or if there's no overlay data
1276 ;; just bail out and return whatever face data we've accumulated so far
1277 (if (or (not (hfy-opt 'keep-overlays
))
1278 (not (setq overlay-data
(hfy-overlay-props-at p
))))
1280 ;;(message "· %d: %s; %S; %s"
1281 ;; p face-name extra-props text-props)
1282 (or face-name base-face
)) ;; no overlays or extra properties
1283 ;; collect any face data and any overlay data for processing:
1285 (push text-props overlay-data
))
1286 (setq overlay-data
(nreverse overlay-data
))
1287 ;;(message "- %d: %s; %S; %s; %s"
1288 ;; p face-name extra-props text-props overlay-data)
1289 ;; remember the basic face name so we don't keep repeating its specs:
1290 (when face-name
(setq base-face face-name
))
1291 (dolist (P overlay-data
)
1292 (let ((iprops (cadr (memq 'invisible P
)))) ;FIXME: plist-get?
1293 ;;(message "(hfy-prop-invisible-p %S)" iprops)
1294 (when (and iprops
(hfy-prop-invisible-p iprops
))
1296 (cons :invisible
(cons t extra-props
))) ))
1297 (let ((fprops (cadr (or (memq 'face P
)
1298 (memq 'font-lock-face P
)))))
1299 ;;(message "overlay face: %s" fprops)
1300 (if (not (listp fprops
))
1301 (let ((this-face (if (stringp fprops
) (intern fprops
) fprops
)))
1302 (when (not (eq this-face base-face
))
1305 (cons this-face extra-props
))) ))
1307 (if (facep (car fprops
))
1308 (let ((face (car fprops
)))
1309 (when (stringp face
) (setq face
(intern fprops
)))
1314 (setq fprops
(cdr fprops
)))
1317 (if (listp (car fprops
))
1318 (if (nlistp (cdr (car fprops
)))
1321 (setq p
(caar fprops
))
1322 (setq v
(cdar fprops
))
1323 (setq fprops
(cdr fprops
)))
1325 (setq p
(caar fprops
))
1326 (setq v
(cadar fprops
))
1327 (setq fprops
(cdr fprops
)))
1328 (if (listp (cdr fprops
))
1330 ;; (:prop val :prop val ...)
1331 (setq p
(car fprops
))
1332 (setq v
(cadr fprops
))
1333 (setq fprops
(cddr fprops
)))
1334 (if (and (listp fprops
)
1335 (not (listp (cdr fprops
))))
1336 ;;(and (consp x) (cdr (last x)))
1339 (setq p
(car fprops
))
1340 (setq v
(cdr fprops
))
1342 (error "Eh... another format! fprops=%s" fprops
) )))
1344 ;; These are all the properties handled
1345 ;; in `hfy-face-to-style-i'.
1347 ;; Are these translations right?
1348 ;; yes, they are -- v
1354 (underline :underline
)
1355 (overline :overline
)
1356 (strike-through :strike-through
)
1358 (foreground-color :foreground
)
1359 (background-color :background
)
1363 (if (memq p prop-seen
) nil
;; noop
1364 (setq prop-seen
(cons p prop-seen
)
1365 extra-props
(cons p
(cons v extra-props
))))))))))
1366 ;;(message "+ %d: %s; %S" p face-name extra-props)
1368 (if (listp face-name
)
1369 (nconc extra-props face-name
)
1370 (nconc extra-props
(face-attr-construct face-name
)))
1373 (defun hfy-overlay-props-at (p)
1374 "Grab overlay properties at point P.
1375 The plists are returned in descending priority order."
1376 (sort (mapcar #'overlay-properties
(overlays-at p
))
1377 (lambda (A B
) (> (or (cadr (memq 'priority A
)) 0) ;FIXME: plist-get?
1378 (or (cadr (memq 'priority B
)) 0)))))
1380 ;; construct an assoc of (face-name . (css-name . "{ css-style }")) elements:
1381 (defun hfy-compile-stylesheet ()
1382 "Trawl the current buffer, construct and return a `hfy-sheet-assoc'."
1383 ;;(message "hfy-compile-stylesheet");;DBUG
1384 (let ((pt (point-min))
1385 ;; Make the font stack stay:
1386 ;;(hfy-tmpfont-stack nil)
1391 (while (< pt
(point-max))
1392 (if (and (setq fn
(hfy-face-at pt
)) (not (assoc fn style
)))
1393 (push (cons fn
(hfy-face-to-css fn
)) style
))
1394 (setq pt
(next-char-property-change pt
))) )
1395 (push (cons 'default
(hfy-face-to-css 'default
)) style
)))
1397 (defun hfy-fontified-p ()
1398 "`font-lock' doesn't like to say it's been fontified when in batch
1399 mode, but we want to know if we should fontify or raw copy, so in batch
1400 mode we check for non-default face properties. Otherwise we test
1401 variable `font-lock-mode' and variable `font-lock-fontified' for truth."
1402 ;;(message "font-lock-fontified: %S" font-lock-fontified)
1403 ;;(message "noninteractive : %S" noninteractive)
1404 ;;(message "font-lock-mode : %S" font-lock-mode)
1405 (and font-lock-fontified
1407 (let ((pt (point-min))
1411 (while (and (< pt
(point-max)) (not face-name
))
1412 (setq face-name
(hfy-face-at pt
))
1413 (setq pt
(next-char-property-change pt
)))) face-name
)
1416 ;; remember, the map is in reverse point order:
1417 ;; I wrote this while suffering the effects of a cold, and maybe a
1418 ;; mild fever - I think it's correct, but it might be a little warped
1419 ;; as my minfd keeps ... where was I? Oh yes, the bunnies...
1420 (defun hfy-merge-adjacent-spans (face-map)
1421 "Where FACE-MAP is a `hfy-facemap-assoc' for the current buffer,
1422 this function merges adjacent style blocks which are of the same value
1423 and are separated by nothing more interesting than whitespace.\n
1424 <span class=\"foo\">narf</span> <span class=\"foo\">brain</span>\n
1425 \(as interpreted from FACE-MAP) would become:\n
1426 <span class=\"foo\">narf brain</span>\n
1427 Returns a modified copy of FACE-MAP."
1428 (let ((tmp-map face-map
)
1437 ;;(push (car tmp-map) reduced-map)
1438 ;;(push (cadr tmp-map) reduced-map)
1440 (setq first-start
(cadddr tmp-map
)
1441 first-stop
(caddr tmp-map
)
1442 last-start
(cadr tmp-map
)
1443 last-stop
(car tmp-map
)
1445 span-start last-start
1446 span-stop last-stop
)
1447 (while (and (equal (cdr first-start
)
1450 (goto-char (car first-stop
))
1451 (not (re-search-forward "[^ \t\n\r]" (car last-start
) t
))))
1452 (setq map-buf
(cddr map-buf
)
1453 span-start first-start
1454 first-start
(cadddr map-buf
)
1455 first-stop
(caddr map-buf
)
1456 last-start
(cadr map-buf
)
1457 last-stop
(car map-buf
)))
1458 (push span-stop reduced-map
)
1459 (push span-start reduced-map
)
1460 (setq tmp-map
(memq last-start tmp-map
))
1461 (setq tmp-map
(cdr tmp-map
)))
1462 (setq reduced-map
(nreverse reduced-map
))))
1464 ;; remember to generate 'synthetic' </span> entries -
1465 ;; emacs copes by just having a stack of styles in effect
1466 ;; and only using the top one: html has a more simplistic approach -
1467 ;; we have to explicitly end a style, there's no way of temporarily
1468 ;; overriding it w. another one... (afaik)
1469 (defun hfy-compile-face-map ()
1470 ;; no need for special <a> version.
1471 ;; IME hyperlinks don't get underlined, esp when you htmlfontify a whole
1472 ;; source tree, so the <a> version is needed -- v
1473 ;; Fix-me: save table for multi-buffer
1474 "Compile and return a `hfy-facemap-assoc' for the current buffer."
1475 ;;(message "hfy-compile-face-map");;DBUG
1476 (let ((pt (point-min))
1480 (prev-tag nil
)) ;; t if the last tag-point was a span-start
1481 ;; nil if it was a span-stop
1484 (while (< pt
(point-max))
1485 (if (setq fn
(hfy-face-at pt
))
1486 (progn (if prev-tag
(push (cons pt-narrow
'end
) map
))
1487 (push (cons pt-narrow fn
) map
)
1489 (if prev-tag
(push (cons pt-narrow
'end
) map
))
1490 (setq prev-tag nil
))
1491 (setq pt
(next-char-property-change pt
))
1492 (setq pt-narrow
(1+ (- pt
(point-min)))))
1493 (if (and map
(not (eq 'end
(cdar map
))))
1494 (push (cons (- (point-max) (point-min)) 'end
) map
)))
1495 (if (hfy-opt 'merge-adjacent-tags
) (hfy-merge-adjacent-spans map
) map
)))
1497 (defun hfy-buffer ()
1498 "Generate a buffer to hold the HTML output.
1499 The filename of this buffer is derived from the source (current) buffer's
1500 variable `buffer-file-name', if it is set, plus `hfy-extn'.
1501 Otherwise a plausible filename is constructed from `default-directory',
1502 `buffer-name' and `hfy-extn'."
1503 (let* ((name (concat (buffer-name) hfy-extn
))
1504 (src (buffer-file-name))
1505 (buf (get-buffer-create name
)))
1506 (with-current-buffer buf
1507 (setq buffer-file-name
1508 (if src
(concat src hfy-extn
)
1509 (expand-file-name (if (string-match "^.*/\\([^/]*\\)$" name
)
1510 (match-string 1 name
)
1514 (defun hfy-lookup (face style
)
1515 "Get a CSS style name for FACE from STYLE."
1516 (cadr (assoc face style
)))
1518 (defun hfy-link-style (style-string)
1519 "Copy, alter and return a STYLE-STRING to make it suitable for a hyperlink.
1520 Uses `hfy-link-style-fun' to do this."
1521 (if (functionp hfy-link-style-fun
)
1522 (funcall hfy-link-style-fun style-string
)
1525 (defun hfy-sprintf-stylesheet (css file
)
1526 "Return the inline CSS style sheet for FILE as a string."
1527 (let ((stylesheet nil
))
1531 "\n<style type=\"text/css\"><!-- \n"
1532 ;; Fix-me: Add handling of page breaks here + scan for ^L
1533 ;; where appropriate.
1534 (format "body %s\n" (cddr (assq 'default css
)))
1539 "span.%s %s\nspan.%s a %s\n"
1540 (cadr style
) (cddr style
)
1541 (cadr style
) (hfy-link-style (cddr style
))))
1544 (funcall hfy-page-header file stylesheet
)))
1546 ;; tag all the dangerous characters we want to escape
1547 ;; (ie any "<> chars we _didn't_ put there explicitly for css markup)
1548 (defun hfy-html-enkludge-buffer ()
1549 "Mark dangerous [\"<>] characters with the `hfy-quoteme' property.\n
1550 See also `hfy-html-dekludge-buffer'."
1551 ;;(message "hfy-html-enkludge-buffer");;DBUG
1553 (goto-char (point-min))
1554 (while (re-search-forward hfy-html-quote-regex nil t
)
1555 (put-text-property (match-beginning 0) (point) 'hfy-quoteme t
))) )
1557 ;; dangerous char -> &entity;
1558 (defun hfy-html-quote (char-string)
1559 "Map CHAR-STRING to an HTML safe string (entity) if need be."
1560 ;;(message "hfy-html-quote");;DBUG
1561 (or (cadr (assoc char-string hfy-html-quote-map
)) char-string
) )
1563 ;; actually entity-ise dangerous chars.
1564 ;; note that we can't do this until _after_ we have inserted the css
1565 ;; markup, since we use a position-based map to insert this, and if we
1566 ;; enter any other text before we do this, we'd have to track another
1567 ;; map of offsets, which would be tedious...
1568 (defun hfy-html-dekludge-buffer ()
1569 "Transform all dangerous characters marked with the `hfy-quoteme' property
1570 using `hfy-html-quote'.\n
1571 See also `hfy-html-enkludge-buffer'."
1572 ;;(message "hfy-html-dekludge-buffer");;DBUG
1574 (goto-char (point-min))
1575 (while (re-search-forward hfy-html-quote-regex nil t
)
1576 (if (get-text-property (match-beginning 0) 'hfy-quoteme
)
1577 (replace-match (hfy-html-quote (match-string 1))) )) ))
1579 ;; Borrowed from font-lock.el
1580 (defmacro hfy-save-buffer-state
(varlist &rest body
)
1581 "Bind variables according to VARLIST and eval BODY restoring buffer state.
1582 Do not record undo information during evaluation of BODY."
1583 (declare (indent 1) (debug let
))
1584 (let ((modified (make-symbol "modified")))
1585 `(let* ,(append varlist
1586 `((,modified
(buffer-modified-p))
1587 (buffer-undo-list t
)
1588 (inhibit-read-only t
)
1589 (inhibit-point-motion-hooks t
)
1590 (inhibit-modification-hooks t
)
1593 buffer-file-truename
))
1597 (restore-buffer-modified-p nil
)))))
1599 (defun hfy-mark-trailing-whitespace ()
1600 "Tag trailing whitespace with a hfy property if it is currently highlighted."
1601 (when show-trailing-whitespace
1602 (let ((inhibit-read-only t
))
1604 (goto-char (point-min))
1605 (hfy-save-buffer-state nil
1606 (while (re-search-forward "[ \t]+$" nil t
)
1607 (put-text-property (match-beginning 0) (match-end 0)
1608 'hfy-show-trailing-whitespace t
)))))))
1610 (defun hfy-unmark-trailing-whitespace ()
1611 "Undo the effect of `hfy-mark-trailing-whitespace'."
1612 (when show-trailing-whitespace
1613 (hfy-save-buffer-state nil
1614 (remove-text-properties (point-min) (point-max)
1615 '(hfy-show-trailing-whitespace)))))
1617 (defun hfy-fontify-buffer (&optional srcdir file
)
1618 "Implement the guts of `htmlfontify-buffer'.
1619 SRCDIR, if set, is the directory being htmlfontified.
1620 FILE, if set, is the file name."
1621 (if srcdir
(setq srcdir
(directory-file-name srcdir
)))
1622 (let* ( (html-buffer (hfy-buffer))
1627 (orig-ovls (overlays-in (point-min) (point-max)))
1628 (rmin (when mark-active
(region-beginning)))
1629 (rmax (when mark-active
(region-end ))) )
1630 (when (and mark-active
1631 transient-mark-mode
)
1632 (unless (and (= rmin
(point-min))
1633 (= rmax
(point-max)))
1634 (setq rovl
(make-overlay rmin rmax
))
1635 (overlay-put rovl
'priority
1000)
1636 (overlay-put rovl
'face
'region
)))
1637 ;; copy the buffer, including fontification, and switch to it:
1638 (hfy-mark-trailing-whitespace)
1639 (setq css-sheet
(hfy-compile-stylesheet )
1640 css-map
(hfy-compile-face-map )
1641 invis-ranges
(hfy-find-invisible-ranges))
1642 (hfy-unmark-trailing-whitespace)
1644 (delete-overlay rovl
))
1645 (copy-to-buffer html-buffer
(point-min) (point-max))
1646 (set-buffer html-buffer
)
1647 ;; rip out props that could interfere with our htmlisation of the buffer:
1648 (remove-text-properties (point-min) (point-max) hfy-ignored-properties
)
1649 ;; Apply overlay invisible spec
1653 (> (or (cadr (memq 'priority
(overlay-properties A
))) 0)
1654 (or (cadr (memq 'priority
(overlay-properties B
))) 0)))))
1655 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1656 ;; at this point, html-buffer retains the fontification of the parent:
1657 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1658 ;; we don't really need or want text in the html buffer to be invisible, as
1659 ;; that can make it look like we've rendered invalid xhtml when all that's
1660 ;; happened is some tags are in the invisible portions of the buffer:
1661 (setq buffer-invisibility-spec nil
)
1662 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1663 ;; #####################################################################
1664 ;; if we are in etags mode, add properties to mark the anchors and links
1665 (if (and srcdir file
)
1667 (hfy-mark-tag-names srcdir file
) ;; mark anchors
1668 (hfy-mark-tag-hrefs srcdir file
))) ;; mark links
1669 ;; #####################################################################
1670 ;; mark the 'dangerous' characters
1671 ;;(message "marking dangerous characters")
1672 (hfy-html-enkludge-buffer)
1673 ;; trawl the position-based face-map, inserting span tags as we go
1674 ;; note that we cannot change any character positions before this point
1675 ;; or we will invalidate the map:
1676 ;; NB: This also means we have to trawl the map in descending file-offset
1677 ;; order, obviously.
1678 ;; ---------------------------------------------------------------------
1679 ;; Remember, inserting pushes properties to the right, which we don't
1680 ;; actually want to happen for link properties, so we have to flag
1681 ;; them and move them by hand - if you don't, you end up with
1683 ;; <span class="foo"><a href="bar">texta</span><span class="bletch"></a>...
1687 ;; <span class="foo"><a href="bar">texta</a></span><span class="bletch">...
1689 ;; If my analysis of the problem is correct, we can detect link-ness by
1690 ;; either hfy-linkp or hfy-endl properties at the insertion point, but I
1691 ;; think we only need to relocate the hfy-endl property, as the hfy-linkp
1692 ;; property has already served its main purpose by this point.
1693 ;;(message "mapcar over the CSS-MAP")
1694 (message "invis-ranges:\n%S" invis-ranges
)
1695 (dolist (point-face css-map
)
1696 (let ((pt (car point-face
))
1697 (fn (cdr point-face
))
1701 (or (get-text-property pt
'hfy-linkp
)
1702 (get-text-property pt
'hfy-endl
)))
1705 (if (not (and srcdir file
))
1708 (remove-text-properties (point) (1+ (point)) '(hfy-endl nil
))
1709 (put-text-property pt
(1+ pt
) 'hfy-endl t
) ))
1710 ;; if we have invisible blocks, we need to do some extra magic:
1712 (let ((iname (hfy-invisible-name pt invis-ranges
))
1713 (fname (hfy-lookup fn css-sheet
)))
1714 (when (assq pt invis-ranges
)
1716 (format "<span onclick=\"toggle_invis('%s');\">" iname
))
1717 (insert "…</span>"))
1719 (format "<span class=\"%s\" id=\"%s-%d\">" fname iname pt
)))
1720 (insert (format "<span class=\"%s\">" (hfy-lookup fn css-sheet
))))
1721 (if (not move-link
) nil
1722 ;;(message "removing prop2 @ %d" (point))
1723 (if (remove-text-properties (point) (1+ (point)) '(hfy-endl nil
))
1724 (put-text-property pt
(1+ pt
) 'hfy-endl t
))))))
1725 ;; #####################################################################
1727 ;; Maybe just make the text invisible in XHTML?
1728 ;; DONE -- big block of obsolete invisibility code elided here -- v
1729 ;; #####################################################################
1730 ;; (message "checking to see whether we should link...")
1731 (if (and srcdir file
)
1732 (let ((lp 'hfy-link
)
1736 ;; (message " yes we should.")
1737 ;; translate 'hfy-anchor properties to anchors
1738 (setq pt
(point-min))
1739 (while (setq pt
(next-single-property-change pt
'hfy-anchor
))
1740 (if (setq pr
(get-text-property pt
'hfy-anchor
))
1741 (progn (goto-char pt
)
1742 (remove-text-properties pt
(1+ pt
) '(hfy-anchor nil
))
1743 (insert (concat "<a name=\"" pr
"\"></a>")))))
1744 ;; translate alternate 'hfy-link and 'hfy-endl props to opening
1745 ;; and closing links. (this should avoid those spurious closes
1746 ;; we sometimes get by generating only paired tags)
1747 (setq pt
(point-min))
1748 (while (setq pt
(next-single-property-change pt lp
))
1749 (if (not (setq pr
(get-text-property pt lp
))) nil
1751 (remove-text-properties pt
(1+ pt
) (list lp nil
))
1754 (if (setq rr
(get-text-property pt
'hfy-inst
))
1755 (insert (format "<a name=\"%s\"></a>" rr
)))
1756 (insert (format "<a href=\"%s\">" pr
))
1757 (setq lp
'hfy-endl
))
1759 (insert "</a>") (setq lp
'hfy-link
)) ))) ))
1761 ;; #####################################################################
1762 ;; transform the dangerous chars. This changes character positions
1763 ;; since entities have > char length.
1764 ;; note that this deletes the dangerous characters, and therefore
1765 ;; destroys any properties they may contain (such as 'hfy-endl),
1766 ;; so we have to do this after we use said properties:
1767 ;; (message "munging dangerous characters")
1768 (hfy-html-dekludge-buffer)
1769 ;; insert the stylesheet at the top:
1770 (goto-char (point-min))
1771 ;;(message "inserting stylesheet")
1772 (insert (hfy-sprintf-stylesheet css-sheet file
))
1773 (if (hfy-opt 'div-wrapper
) (insert "<div class=\"default\">"))
1775 (goto-char (point-max))
1777 (if (hfy-opt 'div-wrapper
) (insert "</div>"))
1778 ;;(message "inserting footer")
1779 (insert (funcall hfy-page-footer file
))
1780 ;; call any post html-generation hooks:
1781 (run-hooks 'hfy-post-html-hooks
)
1782 ;; return the html buffer
1783 (set-buffer-modified-p nil
)
1786 (defun hfy-force-fontification ()
1787 "Try to force font-locking even when it is optimized away."
1788 (run-hooks 'hfy-init-kludge-hook
)
1789 (eval-and-compile (require 'font-lock
))
1790 (if (boundp 'font-lock-cache-position
)
1791 (or font-lock-cache-position
1792 (set 'font-lock-cache-position
(make-marker))))
1793 (if (not noninteractive
)
1795 (message "hfy interactive mode (%S %S)" window-system major-mode
)
1796 (when (and font-lock-defaults
1798 (font-lock-fontify-region (point-min) (point-max) nil
)))
1799 (message "hfy batch mode (%s:%S)"
1800 (or (buffer-file-name) (buffer-name)) major-mode
)
1801 (when font-lock-defaults
1802 (font-lock-fontify-buffer)) ))
1805 (defun htmlfontify-buffer (&optional srcdir file
)
1806 "Create a new buffer, named for the current buffer + a .html extension,
1807 containing an inline CSS-stylesheet and formatted CSS-markup HTML
1808 that reproduces the look of the current Emacs buffer as closely
1811 Dangerous characters in the existing buffer are turned into HTML
1812 entities, so you should even be able to do HTML-within-HTML
1815 You should, however, note that random control or eight-bit
1816 characters such as ^L (\x0c) or ¤ (\xa4) won't get mapped yet.
1818 If the SRCDIR and FILE arguments are set, lookup etags derived
1819 entries in the `hfy-tags-cache' and add HTML anchors and
1820 hyperlinks as appropriate."
1822 ;; pick up the file name in case we didn't receive it
1824 (progn (setq file
(or (buffer-file-name) (buffer-name)))
1825 (if (string-match "/\\([^/]*\\)$" file
)
1826 (setq file
(match-string 1 file
)))) )
1828 (if (not (hfy-opt 'skip-refontification
))
1829 (save-excursion ;; Keep region
1830 (hfy-force-fontification)))
1831 (if (interactive-p) ;; display the buffer in interactive mode:
1832 (switch-to-buffer (hfy-fontify-buffer srcdir file
))
1833 (hfy-fontify-buffer srcdir file
)))
1835 ;; recursive file listing
1836 (defun hfy-list-files (directory)
1837 "Return a list of files under DIRECTORY.
1838 Strips any leading \"./\" from each filename."
1839 ;;(message "hfy-list-files");;DBUG
1840 ;; FIXME: this changes the dir of the currrent buffer. Is that right??
1842 (mapcar (lambda (F) (if (string-match "^./\\(.*\\)" F
) (match-string 1 F
) F
))
1843 (split-string (shell-command-to-string hfy-find-cmd
))) )
1845 ;; strip the filename off, return a directiry name
1846 ;; not a particularly thorough implementaion, but it will be
1847 ;; fed pretty carefully, so it should be Ok:
1848 (defun hfy-dirname (file)
1849 "Return everything preceding the last \"/\" from a relative filename FILE,
1850 on the assumption that this will produce a relative directory name.
1851 Hardly bombproof, but good enough in the context in which it is being used."
1852 ;;(message "hfy-dirname");;DBUG
1853 (let ((f (directory-file-name file
)))
1854 (and (string-match "^\\(.*\\)/" f
) (match-string 1 f
))))
1856 ;; create a directory, cf mkdir -p
1857 (defun hfy-make-directory (dir)
1858 "Approx. equivalent of mkdir -p DIR."
1859 ;;(message "hfy-make-directory");;DBUG
1860 (if (file-exists-p dir
)
1861 (if (file-directory-p dir
) t
)
1862 (make-directory dir t
)))
1864 (defun hfy-text-p (srcdir file
)
1865 "Is SRCDIR/FILE text? Uses `hfy-istext-command' to determine this."
1866 (let* ((cmd (format hfy-istext-command
(expand-file-name file srcdir
)))
1867 (rsp (shell-command-to-string cmd
)))
1868 (if (string-match "text" rsp
) t nil
)))
1870 ;; open a file, check fontification, if fontified, write a fontified copy
1871 ;; to the destination directory, otherwise just copy the file:
1872 (defun hfy-copy-and-fontify-file (srcdir dstdir file
)
1873 "Open FILE in SRCDIR - if fontified, write a fontified copy to DSTDIR
1874 adding an extension of `hfy-extn'. Fontification is actually done by
1875 `htmlfontify-buffer'. If the buffer is not fontified, just copy it."
1876 ;;(message "hfy-copy-and-fontify-file");;DBUG
1877 (let (;;(fast-lock-minimum-size hfy-fast-lock-save)
1878 ;;(font-lock-support-mode 'fast-lock-mode)
1879 ;;(window-system (or window-system 'htmlfontify))
1884 (with-current-buffer (setq source
(find-file-noselect file
))
1885 ;; FIXME: Shouldn't this use expand-file-name? --Stef
1886 (setq target
(concat dstdir
"/" file
))
1887 (hfy-make-directory (hfy-dirname target
))
1888 (if (not (hfy-opt 'skip-refontification
)) (hfy-force-fontification))
1889 (if (or (hfy-fontified-p) (hfy-text-p srcdir file
))
1890 (progn (setq html
(hfy-fontify-buffer srcdir file
))
1892 (write-file (concat target hfy-extn
))
1894 ;; #o0200 == 128, but emacs20 doesn't know that
1895 (if (and (file-exists-p target
) (not (file-writable-p target
)))
1896 (set-file-modes target
(logior (file-modes target
) 128)))
1897 (copy-file (buffer-file-name source
) target
'overwrite
))
1898 (kill-buffer source
)) ))
1900 ;; list of tags in file in srcdir
1901 (defun hfy-tags-for-file (srcdir file
)
1902 "List of etags tags that have definitions in this FILE.
1903 Looks up the tags cache in `hfy-tags-cache' using SRCDIR as the key."
1904 ;;(message "hfy-tags-for-file");;DBUG
1905 (let ((cache-entry (assoc srcdir hfy-tags-cache
))
1908 (if (setq cache-hash
(cadr cache-entry
))
1912 (setq tag-list
(cons K tag-list
)))) cache-hash
))
1915 ;; mark the tags native to this file for anchors
1916 (defun hfy-mark-tag-names (srcdir file
)
1917 "Mark tags in FILE (lookup SRCDIR in `hfy-tags-cache') with the `hfy-anchor'
1918 property, with a value of \"tag.line-number\"."
1919 ;;(message "(hfy-mark-tag-names %s %s)" srcdir file);;DBUG
1920 (let ((cache-entry (assoc srcdir hfy-tags-cache
))
1922 (if (setq cache-hash
(cadr cache-entry
))
1927 (if (string= file
(car TLIST
))
1928 (let* ((line (cadr TLIST
) )
1929 (chr (caddr TLIST
) )
1930 (link (format "%s.%d" TAG line
) ))
1931 (put-text-property (+ 1 chr
)
1933 'hfy-anchor link
))))
1934 (gethash TAG cache-hash
)))
1935 (hfy-tags-for-file srcdir file
)))))
1937 (defun hfy-relstub (file &optional start
)
1938 "Return a \"../\" stub of the appropriate length for the current source
1939 tree depth, as determined from FILE (a filename).
1940 START is the offset at which to start looking for the / character in FILE."
1941 ;;(message "hfy-relstub");;DBUG
1943 (while (setq start
(string-match "/" file start
))
1944 (setq start
(1+ start
)) (setq c
(concat c
"../"))) c
))
1946 (defun hfy-href-stub (this-file def-files tag
)
1947 "Return an href stub for a tag href in THIS-FILE.
1948 If DEF-FILES (list of files containing definitions for the tag in question)
1949 contains only one entry, the href should link straight to that file.
1950 Otherwise, the link should be to the index file.\n
1951 We are not yet concerned with the file extensions/tag line number and so on at
1953 If `hfy-split-index' is set, and the href wil be to an index file rather than
1954 a source file, append a .X to `hfy-index-file', where X is the uppercased
1955 first character of TAG.\n
1956 See also `hfy-relstub', `hfy-index-file'."
1957 ;;(message "hfy-href-stub");;DBUG
1958 ;; FIXME: Why not use something like
1959 ;; (file-relative-name (if ...) (file-name-directory this-file)) ? --Stef
1961 (hfy-relstub this-file
)
1962 (if (= 1 (length def-files
)) (car def-files
)
1963 (if (not hfy-split-index
) hfy-index-file
1964 (concat hfy-index-file
"." (upcase (substring tag
0 1)))))) )
1966 (defun hfy-href (this-file def-files tag tag-map
)
1967 "Return a relative href to the tag in question, based on\n
1968 THIS-FILE `hfy-link-extn' `hfy-extn' DEF-FILES TAG and TAG-MAP\n
1969 THIS-FILE is the current source file
1970 DEF-FILES is a list of file containing possible link endpoints for TAG
1971 TAG is the tag in question
1972 TAG-MAP is the entry in `hfy-tags-cache'."
1973 ;;(message "hfy-href");;DBUG
1975 (hfy-href-stub this-file def-files tag
)
1976 (or hfy-link-extn hfy-extn
) "#" tag
;;(.src -> .html)
1977 (if (= 1 (length def-files
))
1978 (concat "." (format "%d" (cadr (assoc (car def-files
) tag-map
)))))) )
1980 (defun hfy-word-regex (string)
1981 "Return a regex that matches STRING as the first `match-string', with non
1982 word characters on either side."
1983 ;; FIXME: Should this use [^$[:alnum:]_] instead? --Stef
1984 (concat "[^$A-Za-z_0-9]\\(" (regexp-quote string
) "\\)[^A-Za-z_0-9]"))
1986 ;; mark all tags for hyperlinking, except the tags at
1987 ;; their own points of definition, iyswim:
1988 (defun hfy-mark-tag-hrefs (srcdir file
)
1989 "Mark href start points with the `hfy-link' prop (value: href string).\n
1990 Mark href end points with the `hfy-endl' prop (value t).\n
1991 Avoid overlapping links, and mark links in descending length of
1992 tag name in order to prevent subtags from usurping supertags,
1993 \(eg \"term\" for \"terminal\").
1994 SRCDIR is the directory being \"published\".
1995 FILE is the specific file we are rendering."
1996 ;;(message "hfy-mark-tag-hrefs");;DBUG
1997 (let ((cache-entry (assoc srcdir hfy-tags-cache
))
1998 (list-cache (assoc srcdir hfy-tags-sortl
))
1999 (rmap-cache (assoc srcdir hfy-tags-rmap
))
2000 (no-comment (hfy-opt 'zap-comment-links
))
2001 (no-strings (hfy-opt 'zap-string-links
))
2005 (case-fold-search nil
))
2006 ;; extract the tag mapping hashes (fwd and rev) and the tag list:
2007 (if (and (setq cache-hash
(cadr cache-entry
))
2008 (setq tags-rmap
(cadr rmap-cache
))
2009 (setq tags-list
(cadr list-cache
)))
2016 (case-fold-search nil
)
2023 (tag-regex (hfy-word-regex TAG
))
2024 (tag-map (gethash TAG cache-hash
))
2025 (tag-files (mapcar #'car tag-map
)))
2026 ;; find instances of TAG and do what needs to be done:
2027 (goto-char (point-min))
2028 (while (search-forward TAG nil
'NOERROR
)
2029 (setq tmp-point
(point)
2030 maybe-start
(- (match-beginning 0) 1))
2031 (goto-char maybe-start
)
2032 (if (not (looking-at tag-regex
))
2034 (setq start
(match-beginning 1))
2035 (setq stop
(match-end 1))
2037 (and (or no-comment no-strings
) (hfy-face-at start
)))
2039 (setq face-at
(cadr (memq :inherit face-at
))))
2040 (if (or (text-property-any start
(1+ stop
) 'hfy-linkp t
)
2041 (and no-comment
(eq 'font-lock-comment-face face-at
))
2042 (and no-strings
(eq 'font-lock-string-face face-at
)))
2043 nil
;; already a link, NOOP
2045 ;; set a reverse map entry:
2046 (setq rmap-line
(line-number-at-pos)
2047 rmap-entry
(gethash TAG tags-rmap
)
2048 rnew-elt
(list file rmap-line start
)
2049 rmap-entry
(cons rnew-elt rmap-entry
)
2050 name
(format "%s.%d" TAG rmap-line
))
2051 (put-text-property start
(1+ start
) 'hfy-inst name
)
2052 (puthash TAG rmap-entry tags-rmap
)
2054 ;; mark the link. link to index if the tag has > 1 def
2055 ;; add the line number to the #name if it does not:
2056 (setq href
(hfy-href file tag-files TAG tag-map
))
2057 (put-text-property start
(1+ start
) 'hfy-link href
)
2058 (put-text-property stop
(1+ stop
) 'hfy-endl t
)
2059 (put-text-property start
(1+ stop
) 'hfy-linkp t
)))
2060 (goto-char tmp-point
)) ))
2064 "Return `shell-file-name', or \"/bin/sh\" if it is a non-bourne shell."
2065 (if (string-match "\\<bash\\>\\|\\<sh\\>\\|\\<dash\\>" shell-file-name
)
2067 (or hfy-shell-file-name
"/bin/sh")))
2069 ;; cache the #(tag => file line point) entries for files under srcdir
2070 ;; and cache the descending sorted list of tags in the relevant alist,
2071 ;; also keyed by srcdir:
2072 (defun hfy-load-tags-cache (srcdir)
2073 "Run `hfy-etags-cmd' on SRCDIR, then call `hfy-parse-tags-buffer'."
2074 ;;(message "hfy-load-tags-cache");;DBUG
2075 (let ((etags-buffer (get-buffer-create "*hfy-tags*"))
2076 (etags-command (format hfy-etags-cmd hfy-etags-bin
))
2077 (shell-file-name (hfy-shell)))
2079 (shell-command etags-command etags-buffer
)
2080 (hfy-parse-tags-buffer srcdir etags-buffer
)) )
2082 ;; break this out from `hfy-load-tags-cache' to make the tar file
2083 ;; functionality easier to implement.
2084 ;; ( tar file functionality not merged here because it requires a
2085 ;; hacked copy of etags capable of tagging stdin: if Francesco
2086 ;; Potorti accepts a patch, or otherwise implements stdin tagging,
2087 ;; then I will provide a `htmlfontify-tar-file' defun )
2088 (defun hfy-parse-tags-buffer (srcdir buffer
)
2089 "Parse a BUFFER containing etags formatted output, loading the
2090 `hfy-tags-cache' and `hfy-tags-sortl' entries for SRCDIR."
2091 (let ((cache-entry (assoc srcdir hfy-tags-cache
))
2092 (tlist-cache (assoc srcdir hfy-tags-sortl
))
2093 (trmap-cache (assoc srcdir hfy-tags-rmap
))
2094 (cache-hash nil
) (trmap-hash nil
) (tags-list nil
)
2095 (hash-entry nil
) (tag-string nil
) (tag-line nil
)
2096 (tag-point nil
) (new-entry nil
) (etags-file nil
))
2098 ;; (re)initialise the tag reverse map:
2099 (if trmap-cache
(setq trmap-hash
(cadr trmap-cache
))
2100 (setq trmap-hash
(make-hash-table :test
'equal
))
2101 (setq hfy-tags-rmap
(list (list srcdir trmap-hash
) hfy-tags-rmap
)))
2102 (clrhash trmap-hash
)
2104 ;; (re)initialise the tag cache:
2105 (if cache-entry
(setq cache-hash
(cadr cache-entry
))
2106 (setq cache-hash
(make-hash-table :test
'equal
))
2107 (setq hfy-tags-cache
(list (list srcdir cache-hash
) hfy-tags-cache
)))
2108 (clrhash cache-hash
)
2110 ;; cache the TAG => ((file line point) (file line point) ... ) entries:
2111 (with-current-buffer buffer
2112 (goto-char (point-min))
2114 (while (and (looking-at "^\x0c") (= 0 (forward-line 1)))
2115 ;;(message "^L boundary")
2116 (if (and (looking-at "^\\(.+\\),\\([0-9]+\\)$")
2117 (= 0 (forward-line 1)))
2119 (setq etags-file
(match-string 1))
2120 ;;(message "TAGS for file: %s" etags-file)
2121 (while (and (looking-at hfy-etag-regex
) (= 0 (forward-line 1)))
2122 (setq tag-string
(match-string 1))
2123 (if (= 0 (length tag-string
)) nil
;; noop
2124 (setq tag-line
(round (string-to-number (match-string 2))))
2125 (setq tag-point
(round (string-to-number (match-string 3))))
2126 (setq hash-entry
(gethash tag-string cache-hash
))
2127 (setq new-entry
(list etags-file tag-line tag-point
))
2128 (push new-entry hash-entry
)
2129 ;;(message "HASH-ENTRY %s %S" tag-string new-entry)
2130 (puthash tag-string hash-entry cache-hash
)))) )))
2132 ;; cache a list of tags in descending length order:
2133 (maphash (lambda (K V
) (push K tags-list
)) cache-hash
)
2134 (setq tags-list
(sort tags-list
(lambda (A B
) (< (length B
) (length A
)))))
2136 ;; put the tag list into the cache:
2137 (if tlist-cache
(setcar (cdr tlist-cache
) tags-list
)
2138 (push (list srcdir tags-list
) hfy-tags-sortl
))
2140 ;; return the number of tags found:
2141 (length tags-list
) ))
2143 (defun hfy-prepare-index-i (srcdir dstdir filename
&optional stub map
)
2144 "Prepare a tags index buffer for SRCDIR.
2145 `hfy-tags-cache' must already have an entry for SRCDIR for this to work.
2146 `hfy-page-header', `hfy-page-footer', `hfy-link-extn' and `hfy-extn'
2147 all play a part here.\n
2148 If STUB is set, prepare an (appropriately named) index buffer
2149 specifically for entries beginning with STUB.\n
2150 If MAP is set, use that instead of `hfy-tags-cache'.
2151 FILENAME is the name of the file being indexed.
2152 DSTDIR is the output directory, where files will be written."
2153 ;;(message "hfy-write-index");;DBUG
2154 (let ((cache-entry (assoc srcdir
(or map hfy-tags-cache
)))
2158 (concat filename
(if stub
(concat "." stub
) "") hfy-extn
))
2160 (if (not (and cache-entry
2161 (setq cache-hash
(cadr cache-entry
))
2162 (setq index-buf
(get-buffer-create index-file
))))
2164 (maphash (lambda (K V
) (push K tag-list
)) cache-hash
)
2165 (setq tag-list
(sort tag-list
'string
<))
2166 (set-buffer index-buf
)
2168 (insert (funcall hfy-page-header filename
"<!-- CSS -->"))
2169 (insert "<table class=\"index\">\n")
2171 (dolist (TAG tag-list
)
2172 (let ((tag-started nil
))
2173 (dolist (DEF (gethash TAG cache-hash
))
2174 (if (and stub
(not (string-match (concat "^" stub
) TAG
)))
2175 nil
;; we have a stub and it didn't match: NOOP
2176 (let ((file (car DEF
))
2183 " <td><a href=\"%s%s\">%s</a></td> \n"
2184 " <td><a href=\"%s%s#%s.%d\">%d</a></td>\n"
2186 (if (string= TAG tag-started
) " "
2187 (format "<a name=\"%s\">%s</a>" TAG TAG
))
2188 file
(or hfy-link-extn hfy-extn
) file
2189 file
(or hfy-link-extn hfy-extn
) TAG line line
))
2190 (setq tag-started TAG
))))))
2191 (insert "</table>\n")
2192 (insert (funcall hfy-page-footer filename
))
2193 (and dstdir
(cd dstdir
))
2194 (set-visited-file-name index-file
)
2197 (defun hfy-prepare-index (srcdir dstdir
)
2198 "Return a list of index buffer(s), as determined by `hfy-split-index'.
2199 SRCDIR and DSTDIR are the source and output directories respectively."
2200 (if (not hfy-split-index
)
2201 (list (hfy-prepare-index-i srcdir dstdir hfy-index-file nil
))
2202 (let ((stub-list nil
)
2205 (cache-entry (assoc srcdir hfy-tags-cache
)))
2206 (if (and cache-entry
(setq cache-hash
(cadr cache-entry
)))
2209 (let ((stub (upcase (substring K
0 1))))
2210 (if (member stub stub-list
)
2211 nil
;; seen this already: NOOP
2213 stub-list
(cons stub stub-list
)
2214 index-list
(cons (hfy-prepare-index-i srcdir
2218 index-list
)) ))) cache-hash
) ) index-list
)))
2220 (defun hfy-prepare-tag-map (srcdir dstdir
)
2221 "Prepare the counterpart(s) to the index buffer(s) - a list of buffers
2222 with the same structure, but listing (and linking to) instances of tags
2223 \(as opposed to their definitions).\n
2224 SRCDIR and DSTDIR are the source and output directories respectively.
2225 See also `hfy-prepare-index', `hfy-split-index'."
2226 (if (not hfy-split-index
)
2227 (list (hfy-prepare-index-i srcdir
2232 (let ((stub-list nil
)
2235 (cache-entry (assoc srcdir hfy-tags-rmap
)))
2237 (if (and cache-entry
(setq cache-hash
(cadr cache-entry
)))
2240 (let ((stub (upcase (substring K
0 1))))
2241 (if (member stub stub-list
)
2242 nil
;; seen this already: NOOP
2244 stub-list
(cons stub stub-list
)
2245 index-list
(cons (hfy-prepare-index-i srcdir
2250 index-list
)) ))) cache-hash
) ) index-list
)))
2252 (defun hfy-subtract-maps (srcdir)
2253 "Internal function - strips definitions of tags from the instance map.
2254 SRCDIR is the directory being \"published\".
2255 See also `hfy-tags-cache', `hfy-tags-rmap'."
2256 (let ((new-list nil
)
2260 (fwd-map (cadr (assoc srcdir hfy-tags-cache
)))
2261 (rev-map (cadr (assoc srcdir hfy-tags-rmap
)))
2262 (taglist (cadr (assoc srcdir hfy-tags-sortl
))))
2263 (dolist (TAG taglist
)
2264 (setq def-list
(gethash TAG fwd-map
)
2265 old-list
(gethash TAG rev-map
)
2266 exc-list
(mapcar (lambda (P) (list (car P
) (cadr P
))) def-list
)
2268 (dolist (P old-list
)
2269 (or (member (list (car P
) (cadr P
)) exc-list
)
2271 (puthash TAG new-list rev-map
))))
2273 (defun htmlfontify-run-etags (srcdir)
2274 "Load the etags cache for SRCDIR.
2275 See also `hfy-load-tags-cache'."
2276 (interactive "D source directory: ")
2277 (setq srcdir
(directory-file-name srcdir
))
2278 (hfy-load-tags-cache srcdir
))
2280 ;;(defun hfy-test-read-args (foo bar)
2281 ;; (interactive "D source directory: \nD target directory: ")
2282 ;; (message "foo: %S\nbar: %S" foo bar))
2284 (defun hfy-save-kill-buffers (buffer-list &optional dstdir
)
2285 (dolist (B buffer-list
)
2287 (and dstdir
(file-directory-p dstdir
) (cd dstdir
))
2292 (defun htmlfontify-copy-and-link-dir (srcdir dstdir
&optional f-ext l-ext
)
2293 "Trawl SRCDIR and write fontified-and-hyperlinked output in DSTDIR.
2294 F-EXT and L-EXT specify values for `hfy-extn' and `hfy-link-extn'.\n
2295 You may also want to set `hfy-page-header' and `hfy-page-footer'."
2296 (interactive "D source directory: \nD output directory: ")
2297 ;;(message "htmlfontify-copy-and-link-dir")
2298 (setq srcdir
(directory-file-name srcdir
))
2299 (setq dstdir
(directory-file-name dstdir
))
2300 (let ((source-files "SETME: list of source files, relative to srcdir")
2301 (tr-cache (assoc srcdir hfy-tags-rmap
))
2302 (hfy-extn (or f-ext
".html"))
2303 (hfy-link-extn (or l-ext
".html")))
2304 ;; oops, forgot to load etags for srcdir:
2306 (message "autoload of tags cache")
2307 (hfy-load-tags-cache srcdir
)
2308 (setq tr-cache
(assoc srcdir hfy-tags-rmap
)))
2309 ;; clear out the old cache:
2310 (clrhash (cadr tr-cache
))
2311 (hfy-make-directory dstdir
)
2312 (setq source-files
(hfy-list-files srcdir
))
2313 (dolist (file source-files
)
2314 (hfy-copy-and-fontify-file srcdir dstdir file
))
2315 (hfy-subtract-maps srcdir
)
2316 (hfy-save-kill-buffers (hfy-prepare-index srcdir dstdir
) dstdir
)
2317 (hfy-save-kill-buffers (hfy-prepare-tag-map srcdir dstdir
) dstdir
) ))
2319 ;; name of the init file we want:
2320 (defun hfy-initfile ()
2321 "Return the expected location of the htmlfontify specific init/custom file."
2322 (let* ((file (or (getenv "HFY_INITFILE") ".hfy.el")))
2323 (expand-file-name file
"~") ))
2326 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2327 ;; incomplete as yet : transfer hook settings to hfy init file:
2328 ;; (defalias 'hfy-set-hooks 'custom-set-variables)
2330 ;; (defun hfy-pp-hook (H)
2331 ;; (and (string-match "-hook$" (symbol-name H))
2334 ;; (insert (format "\n '(%S %S)" H (symbol-value H)))
2338 ;; (defun hfy-save-hooks ()
2339 ;; (let ((custom-file (hfy-initfile)))
2340 ;; (custom-save-delete 'hfy-set-hooks)
2341 ;; (let ((standard-output (current-buffer)))
2342 ;; (princ "(hfy-set-hooks\n;;auto-generated, only one copy allowed\n")
2343 ;; (mapatoms 'hfy-pp-hook)
2348 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2350 (defalias 'hfy-init-progn
'progn
)
2352 (defun hfy-save-initvar (sym)
2353 (princ (format "(setq %s\n '" sym
))
2354 (pp (symbol-value sym
))
2357 (defun htmlfontify-save-initfile ()
2358 "Save the htmlfontify settings to the htmlfontify init file."
2360 (let* ((start-pos nil
)
2361 (custom-file (hfy-initfile))
2362 (standard-output (find-file-noselect custom-file
'nowarn
)))
2364 (custom-save-delete 'hfy-init-progn
)
2365 (setq start-pos
(point))
2366 (princ "(hfy-init-progn\n;;auto-generated, only one copy allowed\n")
2367 ;; FIXME: This saving&restoring of global customization
2368 ;; variables can interfere with other customization settings for
2369 ;; those vars (in .emacs or in Customize).
2370 (mapc 'hfy-save-initvar
2371 '(auto-mode-alist interpreter-mode-alist
))
2373 (indent-region start-pos
(point) nil
))
2374 (custom-save-all) ))
2376 (defun htmlfontify-load-initfile ()
2377 "Load the htmlfontify specific init/custom file."
2379 (let ((file (hfy-initfile)))
2380 (load file
'NOERROR nil nil
) ))
2382 (provide 'htmlfontify
)
2383 ;;; htmlfontify.el ends here
2385 ;; arch-tag: 944e5e63-c81d-4baa-a82a-0275f9c30e61