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')
93 (defconst htmlfontify-version
0.21)
95 (defconst hfy-meta-tags
96 (format "<meta name=\"generator\" content=\"emacs %s; htmlfontify %0.2f\" />"
97 emacs-version htmlfontify-version
)
98 "The generator meta tag for this version of htmlfontify.")
100 (defconst htmlfontify-manual
"Htmlfontify Manual"
101 "Copy and convert buffers and files to HTML, adding hyperlinks between files
102 \(driven by etags) if requested.
103 \nInteractive functions:
105 `htmlfontify-run-etags'
106 `htmlfontify-copy-and-link-dir'
107 `htmlfontify-load-rgb-file'
108 `htmlfontify-unload-rgb-file'\n
110 fontify a file you have open: M-x htmlfontify-buffer
111 prepare the etags map for a directory: M-x htmlfontify-run-etags
112 copy a directory, fontifying as you go: M-x htmlfontify-copy-and-link-dir\n
113 The following might be useful when running non-windowed or in batch mode:
114 \(note that they shouldn't be necessary - we have a built in map)\n
115 load an X11 style rgb.txt file: M-x htmlfontify-load-rgb-file
116 unload the current rgb.txt file: M-x htmlfontify-unload-rgb-file\n
117 And here's a programmatic example:\n
118 \(defun rtfm-build-page-header (file style)
119 (format \"#define TEMPLATE red+black.html
121 #include <build/menu-dirlist|>\\n
122 html-css-url := /css/red+black.css
123 title := rtfm.etla.org ( %s / src/%s )
125 head <=STYLESHEET;\\n
128 main-title := rtfm / %s / src/%s\\n
129 main-content <=MAIN_CONTENT;\\n\" rtfm-section file style rtfm-section file))
131 \(defun rtfm-build-page-footer (file) \"\\nMAIN_CONTENT\\n\")
133 \(defun rtfm-build-source-docs (section srcdir destdir)
135 \"s section[eg- emacs / p4-blame]:\\nD source-dir: \\nD output-dir: \")
136 (require 'htmlfontify)
137 (hfy-load-tags-cache srcdir)
138 (let ((hfy-page-header 'rtfm-build-page-header)
139 (hfy-page-footer 'rtfm-build-page-footer)
140 (rtfm-section section)
141 (hfy-index-file \"index\"))
142 (htmlfontify-run-etags srcdir)
143 (htmlfontify-copy-and-link-dir srcdir destdir \".src\" \".html\")))")
145 (defgroup htmlfontify nil
146 "Convert buffers and files to HTML."
148 :link
'(variable-link htmlfontify-manual
)
151 (defcustom hfy-page-header
'hfy-default-header
152 "Function called with two arguments (the filename relative to the top
153 level source directory being etag'd and fontified), and a string containing
154 the <style>...</style> text to embed in the document- the string returned will
155 be used as the header for the htmlfontified version of the source file.\n
156 See also `hfy-page-footer'."
158 ;; FIXME: Why place such a :tag everywhere? Isn't it imposing your
159 ;; own Custom preference on your users? --Stef
163 (defcustom hfy-split-index nil
164 "Whether or not to split the index `hfy-index-file' alphabetically
165 on the first letter of each tag. Useful when the index would otherwise
166 be large and take a long time to render or be difficult to navigate."
171 (defcustom hfy-page-footer
'hfy-default-footer
172 "As `hfy-page-header', but generates the output footer
173 \(and takes only one argument, the filename)."
178 (defcustom hfy-extn
".html"
179 "File extension used for output files."
184 (defcustom hfy-src-doc-link-style
"text-decoration: underline;"
185 "String to add to the '<style> a' variant of an htmlfontify CSS class."
187 :tag
"src-doc-link-style"
190 (defcustom hfy-src-doc-link-unstyle
" text-decoration: none;"
191 "Regex to remove from the <style> a variant of an htmlfontify CSS class."
193 :tag
"src-doc-link-unstyle"
196 (defcustom hfy-link-extn nil
197 "File extension used for href links.
198 Useful where the htmlfontify output files are going to be processed
199 again, with a resulting change in file extension. If nil, then any
200 code using this should fall back to `hfy-extn'."
202 :tag
"link-extension"
203 :type
'(choice string
(const nil
)))
205 (defcustom hfy-link-style-fun
'hfy-link-style-string
206 "Set this to a function, which will be called with one argument
207 \(a \"{ foo: bar; ...}\" CSS style-string) - it should return a copy of
208 its argument, altered so as to make any changes you want made for text which
209 is a hyperlink, in addition to being in the class to which that style would
210 normally be applied."
212 :tag
"link-style-function"
215 (defcustom hfy-index-file
"hfy-index"
216 "Name (sans extension) of the tag definition index file produced during
217 fontification-and-hyperlinking."
222 (defcustom hfy-instance-file
"hfy-instance"
223 "Name (sans extension) of the tag usage index file produced during
224 fontification-and-hyperlinking."
229 (defcustom hfy-html-quote-regex
"\\(<\\|\"\\|&\\|>\\)"
230 "Regex to match (with a single back-reference per match) strings in HTML
231 which should be quoted with `hfy-html-quote' (and `hfy-html-quote-map')
234 :tag
"html-quote-regex"
237 (define-obsolete-variable-alias 'hfy-init-kludge-hooks
'hfy-init-kludge-hook
239 (defcustom hfy-init-kludge-hook
'(hfy-kludge-cperl-mode)
240 "List of functions to call when starting `htmlfontify-buffer' to do any
241 kludging necessary to get highlighting modes to behave as you want, even
242 when not running under a window system."
244 :tag
"init-kludge-hooks"
247 (defcustom hfy-post-html-hooks nil
248 "List of functions to call after creating and filling the HTML buffer.
249 These functions will be called with the html buffer as the current buffer."
251 :tag
"post-html-hooks"
252 :options
'(set-auto-mode)
255 (defcustom hfy-default-face-def nil
256 "Fallback `defface' specification for the face 'default, used when
257 `hfy-display-class' has been set (the normal htmlfontify way of extracting
258 potentially non-current face information doesn't necessarily work for
260 Example: I customize this to:\n
261 \((t :background \"black\" :foreground \"white\" :family \"misc-fixed\"))"
263 :tag
"default-face-definition"
266 (defcustom hfy-etag-regex
(concat ".*"
267 "\x7f" "\\([^\x01\n]+\\)"
268 "\x01" "\\([0-9]+\\)"
270 "\\|" ".*\x7f[0-9]+,[0-9]+$")
271 "Regex used to parse an etags entry: must have 3 subexps, corresponding,
275 3 - The char (point) at which the tag occurs."
280 (defcustom hfy-html-quote-map
'(("\"" """)
284 "Alist of char -> entity mappings used to make the text HTML-safe."
286 :tag
"html-quote-map"
287 :type
'(alist :key-type
(string)))
289 (defconst hfy-e2x-etags-cmd
"for src in `find . -type f`;
293 *.ad[absm]|*.[CFHMSacfhlmpsty]|*.def|*.in[cs]|*.s[as]|*.src|*.cc|\\
294 *.hh|*.[chy]++|*.[ch]pp|*.[chy]xx|*.pdb|*.[ch]s|*.[Cc][Oo][Bb]|\\
295 *.[eh]rl|*.f90|*.for|*.java|*.[cem]l|*.clisp|*.lisp|*.[Ll][Ss][Pp]|\\
296 [Mm]akefile*|*.pas|*.[Pp][LlMm]|*.psw|*.lm|*.pc|*.prolog|*.oak|\\
297 *.p[sy]|*.sch|*.scheme|*.[Ss][Cc][Mm]|*.[Ss][Mm]|*.bib|*.cl[os]|\\
298 *.ltx|*.sty|*.TeX|*.tex|*.texi|*.texinfo|*.txi|*.x[bp]m|*.yy|\\
309 SHEBANG=`head -n1 ${src} | grep '#!' -c`;
310 if [ ${SHEBANG} -eq 1 ];
320 (defconst hfy-etags-cmd-alist-default
321 `(("emacs etags" .
,hfy-e2x-etags-cmd
)
322 ("exuberant ctags" .
"%s -R -f -" )))
324 (defcustom hfy-etags-cmd-alist
325 hfy-etags-cmd-alist-default
326 "Alist of possible shell commands that will generate etags output that
327 `htmlfontify' can use. '%s' will be replaced by `hfy-etags-bin'."
329 :tag
"etags-cmd-alist"
330 :type
'(alist :key-type
(string) :value-type
(string)) ))
332 (defcustom hfy-etags-bin
"etags"
333 "Location of etags binary (we begin by assuming it's in your path).\n
334 Note that if etags is not in your path, you will need to alter the shell
335 commands in `hfy-etags-cmd-alist'."
340 (defcustom hfy-shell-file-name
"/bin/sh"
341 "Shell (bourne or compatible) to invoke for complex shell operations."
343 :tag
"shell-file-name"
346 (defcustom hfy-ignored-properties
'(read-only
349 insert-in-front-hooks
353 "Properties to omit when copying a fontified buffer for HTML transformation."
355 :tag
"ignored-properties"
356 :type
'(repeat symbol
))
358 (defun hfy-which-etags ()
359 "Return a string indicating which flavour of etags we are using."
360 (let ((v (shell-command-to-string (concat hfy-etags-bin
" --version"))))
361 (cond ((string-match "exube" v
) "exuberant ctags")
362 ((string-match "GNU E" v
) "emacs etags" )) ))
364 (defcustom hfy-etags-cmd
365 (eval-and-compile (cdr (assoc (hfy-which-etags) hfy-etags-cmd-alist
)))
366 "The etags equivalent command to run in a source directory to generate a tags
367 file for the whole source tree from there on down. The command should emit
368 the etags output on stdout.\n
369 Two canned commands are provided - they drive Emacs' etags and
370 exuberant-ctags' etags respectively."
373 :type
(eval-and-compile
374 (let ((clist (list '(string))))
375 (dolist (C hfy-etags-cmd-alist
)
376 (push (list 'const
:tag
(car C
) (cdr C
)) clist
))
377 (cons 'choice clist
)) ))
379 (defcustom hfy-istext-command
"file %s | sed -e 's@^[^:]*:[ \t]*@@'"
380 "Command to run with the name of a file, to see whether it is a text file
381 or not. The command should emit a string containing the word 'text' if
382 the file is a text file, and a string not containing 'text' otherwise."
384 :tag
"istext-command"
387 (defcustom hfy-find-cmd
388 "find . -type f \\! -name \\*~ \\! -name \\*.flc \\! -path \\*/CVS/\\*"
389 "Find command used to harvest a list of files to attempt to fontify."
394 (defcustom hfy-display-class nil
395 "Display class to use to determine which display class to use when
396 calculating a face's attributes. This is useful when, for example, you
397 are running Emacs on a tty or in batch mode, and want htmlfontify to have
398 access to the face spec you would use if you were connected to an X display.\n
399 Some valid class specification elements are:\n
408 Multiple values for a tag may be combined, to indicate that any one or more
409 of these values in the specification key constitutes a match, eg:\n
410 '((class color grayscale) (type tty)) would match any of:\n
413 '((class color grayscale))
416 '((type tty) (class color))\n
418 :type
'(alist :key-type
(symbol) :value-type
(symbol))
421 :options
'((type (choice (const :tag
"X11" x-toolkit
)
422 (const :tag
"Terminal" tty
)
423 (const :tag
"Lucid Toolkit" lucid
)
424 (const :tag
"Motif Toolkit" motif
)))
426 (class (choice (const :tag
"Colour" color
)
427 (const :tag
"Greyscale" grayscale
)))
429 (background (choice (const :tag
"Dark" dark
)
430 (const :tag
"Bright" light
))) ))
432 (defcustom hfy-optimisations
(list 'keep-overlays
)
433 "Optimizations to turn on: So far, the following have been implemented:\n
434 merge-adjacent-tags: If two (or more) span tags are adjacent, identical and
435 separated by nothing more than whitespace, they will
436 be merged into one span.
437 zap-comment-links : Suppress hyperlinking of tags found in comments.
438 zap-string-links : Suppress hyperlinking of tags found in strings.
439 div-wrapper : Add <div class=\"default\"> </div> tags around the
441 keep-overlays : More of a bell (or possibly whistle) than an
442 optimization - If on, preserve overlay highlighting
443 (cf ediff or goo-font-lock) as well as basic faces.\n
444 And the following are planned but not yet available:\n
445 kill-context-leak : Suppress hyperlinking between files highlighted by
447 Note: like compiler optimizations, these optimize the _output_ of the code,
448 not the processing of the source itself, and are therefore likely to slow
449 htmlfontify down, at least a little. Except for skip-refontification,
450 which can never slow you down, but may result in incomplete fontification."
451 :type
'(set (const :tag
"merge-adjacent-tags" merge-adjacent-tags
)
452 (const :tag
"zap-comment-links" zap-comment-links
)
453 (const :tag
"zap-string-links" zap-string-links
)
454 (const :tag
"skip-refontification" skip-refontification
)
455 (const :tag
"kill-context-leak" kill-context-leak
)
456 (const :tag
"div-wrapper" div-wrapper
)
457 (const :tag
"keep-overlays" keep-overlays
))
459 :tag
"optimizations")
461 (defvar hfy-tags-cache nil
462 "Alist of the form:\n
463 \((\"/src/dir/0\" . tag-hash0) (\"/src/dir/1\" tag-hash1) ...)\n
464 Each tag hash entry then contains entries of the form:\n
465 \"tag_string\" => ((\"file/name.ext\" line char) ... )\n
466 ie an alist mapping (relative) file paths to line and character offsets.\n
467 See also `hfy-load-tags-cache'.")
469 (defvar hfy-tags-sortl nil
470 "Alist of the form ((\"/src/dir\" . (tag0 tag1 tag2)) ... )\n
471 where the tags are stored in descending order of length.\n
472 See also `hfy-load-tags-cache'.")
474 (defvar hfy-tags-rmap nil
475 "Alist of the form ((\"/src/dir\" . tag-rmap-hash))\n
476 where tag-rmap-hash has entries of the form:
477 \"tag_string\" => ( \"file/name.ext\" line char )
478 Unlike `hfy-tags-cache' these are the locations of occurrences of
479 tagged items, not the locations of their definitions.")
481 (defvar hfy-style-assoc
'please-ignore-this-line
482 "An assoc representing/describing an Emacs face.
483 Properties may be repeated, in which case later properties should be
484 treated as if they were inherited from a 'parent' font.
485 \(For some properties, only the first encountered value is of any importance,
486 for others the values might be cumulative, and for others they might be
487 cumulative in a complex way.)\n
489 \(hfy-face-to-style 'default) =>
490 ((\"background\" . \"rgb(0, 0, 0)\")
491 (\"color\" . \"rgb(255, 255, 255)\")
492 (\"font-style\" . \"normal\")
493 (\"font-weight\" . \"500\")
494 (\"font-stretch\" . \"normal\")
495 (\"font-family\" . \"misc-fixed\")
496 (\"font-size\" . \"13pt\")
497 (\"text-decoration\" . \"none\"))\n
498 \(hfy-face-to-style 'Info-title-3-face) =>
499 ((\"font-weight\" . \"700\")
500 (\"font-family\" . \"helv\")
501 (\"font-size\" . \"120%\")
502 (\"text-decoration\" . \"none\"))\n")
504 (defvar hfy-sheet-assoc
'please-ignore-this-line
505 "An assoc with elements of the form (face-name style-name . style-string):\n
506 '((default \"default\" . \"{background: black; color: white}\")
507 (font-lock-string-face \"string\" . \"{color: rgb(64,224,208)}\"))" )
509 (defvar hfy-facemap-assoc
'please-ignore-this-line
510 "An assoc of (point . FACE-SYMBOL) or (point . DEFFACE-LIST)
511 and (point . 'end) elements, in descending order of point value
512 \(ie from the file's end to its beginning).\n
513 The map is in reverse order because inserting a <style> tag (or any other
514 string) at `point' invalidates the map for all entries with a greater value of
515 point. By traversing the map from greatest to least point, we still invalidate
516 the map as we go, but only those points we have already dealt with (and
517 therefore no longer care about) will be invalid at any time.\n
519 (64744 . font-lock-comment-face)
521 (64722 . font-lock-string-face)
523 (64623 . font-lock-string-face)
525 (64446 . font-lock-keyword-face)
527 (64395 . font-lock-constant-face)
529 (64386 . font-lock-keyword-face)
531 ;; big similar section elided. You get the idea.
532 (4285 . font-lock-constant-face)
534 (4221 . font-lock-comment-face)
536 (4197 . font-lock-constant-face)
538 (1 . font-lock-comment-face))")
540 (defvar hfy-tmpfont-stack nil
541 "An alist of derived fonts resulting from overlays.")
543 (defconst hfy-hex-regex
"[0-9A-Fa-f]")
545 (defconst hfy-triplet-regex
547 "\\(" hfy-hex-regex hfy-hex-regex
"\\)"
548 "\\(" hfy-hex-regex hfy-hex-regex
"\\)"
549 "\\(" hfy-hex-regex hfy-hex-regex
"\\)"))
551 (defun hfy-interq (set-a set-b
)
552 "Return the intersection (using `eq') of two lists SET-A and SET-B."
553 (let ((sa set-a
) (interq nil
) (elt nil
))
557 (if (memq elt set-b
) (setq interq
(cons elt interq
)))) interq
))
559 (defun hfy-colour-vals (colour)
560 "Where COLOUR is a color name or #XXXXXX style triplet, return a
561 list of three (16 bit) rgb values for said color.\n
562 If a window system is unavailable, calls `hfy-fallback-colour-values'."
563 (if (string-match hfy-triplet-regex colour
)
565 (lambda (x) (* (string-to-number (match-string x colour
) 16) 257))
567 ;;(message ">> %s" colour)
569 (if (fboundp 'color-values
)
570 (color-values colour
)
571 ;;(message "[%S]" window-system)
572 (x-color-values colour
))
573 ;; blarg - tty colours are no good - go fetch some X colours:
574 (hfy-fallback-colour-values colour
))))
576 (defvar hfy-cperl-mode-kludged-p nil
)
578 (defun hfy-kludge-cperl-mode ()
579 "CPerl mode does its damndest not to do some of its fontification when not
580 in a windowing system - try to trick it..."
581 (if (not hfy-cperl-mode-kludged-p
)
582 (progn (if (not window-system
)
583 (let ((window-system 'htmlfontify
))
584 (eval-and-compile (require 'cperl-mode
))
585 (setq cperl-syntaxify-by-font-lock t
)))
586 (setq hfy-cperl-mode-kludged-p t
))) )
588 (defun hfy-opt (symbol) "Is option SYMBOL set." (memq symbol hfy-optimisations
))
590 (defun hfy-default-header (file style
)
591 "Default value for `hfy-page-header'.
592 FILE is the name of the file.
593 STYLE is the inline CSS stylesheet (or tag referring to an external sheet)."
594 ;; (format "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">
595 ;; <html>\n <head>\n <title>%s</title>\n %s\n </head>\n <body>\n" file style))
596 (format "<?xml version=\"1.0\" encoding=\"utf-8\"?>
597 <!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.1//EN\"
598 \"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd\">
599 <html xmlns=\"http://www.w3.org/1999/xhtml\">
603 <script type=\"text/javascript\"><!--
604 // this function is needed to work around
605 // a bug in IE related to element attributes
606 function hasClass(obj)
609 if (obj.getAttributeNode(\"class\") != null)
611 result = obj.getAttributeNode(\"class\").value;
618 // the flag we'll use to keep track of
619 // whether the current row is odd or even
622 // if arguments are provided to specify the colors
623 // of the even & odd rows, then use the them;
624 // otherwise use the following defaults:
625 var evenColor = arguments[1] ? arguments[1] : \"#fff\";
626 var oddColor = arguments[2] ? arguments[2] : \"#ddd\";
628 // obtain a reference to the desired table
629 // if no such table exists, abort
630 var table = document.getElementById(id);
631 if (! table) { return; }
633 // by definition, tables can have more than one tbody
634 // element, so we'll have to get the list of child
636 var tbodies = table.getElementsByTagName(\"tbody\");
638 // and iterate through them...
639 for (var h = 0; h < tbodies.length; h++)
641 // find all the <tr> elements...
642 var trs = tbodies[h].getElementsByTagName(\"tr\");
644 // ... and iterate through them
645 for (var i = 0; i < trs.length; i++)
647 // avoid rows that have a class attribute
648 // or backgroundColor style
649 if (! hasClass(trs[i]) &&
650 ! trs[i].style.backgroundColor)
652 // get all the cells in this row...
653 var tds = trs[i].getElementsByTagName(\"td\");
655 // and iterate through them...
656 for (var j = 0; j < tds.length; j++)
660 // avoid cells that have a class attribute
661 // or backgroundColor style
662 if (! hasClass(mytd) &&
663 ! mytd.style.backgroundColor)
665 mytd.style.backgroundColor =
666 even ? evenColor : oddColor;
670 // flip from odd to even, or vice-versa
676 function toggle_invis( name )
681 { var classname = node.id;
683 { var classbase = classname.substr( 0, name.length );
684 if( classbase == name ) { return NodeFilter.FILTER_ACCEPT; } }
685 return NodeFilter.FILTER_SKIP; } };
686 var walker = document.createTreeWalker( document.body ,
687 NodeFilter.SHOW_ELEMENT ,
690 while( walker.nextNode() )
692 var e = walker.currentNode;
693 if( e.style.display == \"none\" ) { e.style.display = \"inline\"; }
694 else { e.style.display = \"none\"; }
699 <body onload=\"stripe('index'); return true;\">\n"
702 (defun hfy-default-footer (file)
703 "Default value for `hfy-page-footer'.
704 FILE is the name of the file being rendered, in case it is needed."
705 "\n </body>\n</html>\n")
707 (defun hfy-link-style-string (style-string)
708 "Replace the end of a CSS style declaration STYLE-STRING with the contents
709 of the variable `hfy-src-doc-link-style', removing text matching the regex
710 `hfy-src-doc-link-unstyle' first, if necessary."
711 ;;(message "hfy-colour-vals");;DBUG
712 (if (string-match hfy-src-doc-link-unstyle style-string
)
713 (setq style-string
(replace-match "" 'fixed-case
'literal style-string
)))
714 (if (and (not (string-match hfy-src-doc-link-style style-string
))
715 (string-match "} *$" style-string
))
716 (concat (replace-match hfy-src-doc-link-style
719 style-string
) " }") style-string
))
721 ;; utility functions - cast emacs style specification values into their
723 (defun hfy-triplet (colour)
724 "Takes a COLOUR name (string) and return a CSS rgb(R, G, B) triplet string.
725 Uses the definition of \"white\" to map the numbers to the 0-255 range, so
726 if you've redefined white, (esp. if you've redefined it to have a triplet
727 member lower than that of the color you are processing) strange things
729 ;;(message "hfy-colour-vals");;DBUG
730 (let ((white (mapcar (lambda (I) (float (1+ I
))) (hfy-colour-vals "white")))
731 (rgb16 (mapcar (lambda (I) (float (1+ I
))) (hfy-colour-vals colour
))))
733 ;;(apply 'format "rgb(%d, %d, %d)"
734 ;; Use #rrggbb instead, it is smaller
735 (apply 'format
"#%02x%02x%02x"
741 (defun hfy-family (family) (list (cons "font-family" family
)))
742 (defun hfy-bgcol (colour) (list (cons "background" (hfy-triplet colour
))))
743 (defun hfy-colour (colour) (list (cons "color" (hfy-triplet colour
))))
744 (defun hfy-width (width) (list (cons "font-stretch" (symbol-name width
))))
746 (defcustom hfy-font-zoom
1.05
747 "Font scaling from Emacs to HTML."
751 (defun hfy-size (height)
752 "Derive a CSS font-size specifier from an Emacs font :height attribute HEIGHT.
753 Does not cope with the case where height is a function to be applied to
754 the height of the underlying font."
757 ;;(t (cons "font-size" ": 1em"))
759 (cons "font-size" (format "%d%%" (* (* hfy-font-zoom height
) 100))))
761 (cons "font-size" (format "%dpt" (/ (* hfy-font-zoom height
) 10 )))) )) )
763 (defun hfy-slant (slant)
764 "Derive a font-style CSS specifier from the Emacs :slant attribute SLANT:
765 CSS does not define the reverse-* styles, so just maps those to the
767 (list (cons "font-style"
768 (or (cdr (assq slant
'((italic .
"italic")
769 (reverse-italic .
"italic" )
770 (oblique .
"oblique")
771 (reverse-oblique .
"oblique"))))
774 (defun hfy-weight (weight)
775 "Derive a font-weight CSS specifier from an Emacs weight spec symbol WEIGHT."
776 (list (cons "font-weight" (cdr (assq weight
'((ultra-bold .
"900")
783 (extra-light .
"200")
784 (ultra-light .
"100")))))))
786 (defun hfy-box-to-border-assoc (spec)
788 (let ((tag (car spec
))
791 (:color
(cons "colour" val
))
792 (:width
(cons "width" val
))
793 (:style
(cons "style" val
)))
794 (hfy-box-to-border-assoc (cddr spec
))))))
796 (defun hfy-box-to-style (spec)
797 (let* ((css (hfy-box-to-border-assoc spec
))
798 (col (cdr (assoc "colour" css
)))
799 (s (cdr (assoc "style" css
))))
801 (if col
(cons "border-color" (cdr (assoc "colour" css
))))
802 (cons "border-width" (format "%dpx" (or (cdr (assoc "width" css
)) 1)))
803 (cons "border-style" (case s
804 (released-button "outset")
805 (pressed-button "inset" )
809 "Derive CSS border-* attributes from the Emacs :box attribute BOX."
812 ((integerp box
) (list (cons "border-width" (format "%dpx" box
))))
813 ((stringp box
) (list (cons "border" (format "solid %s 1px" box
))))
814 ((listp box
) (hfy-box-to-style box
) ))) )
816 (defun hfy-decor (tag val
)
817 "Derive CSS text-decoration specifiers from various Emacs font attributes.
818 TAG is an Emacs font attribute key (eg :underline).
821 ;; FIXME: Why not '("text-decoration" . "underline")? --Stef
823 (:underline
(cons "text-decoration" "underline" ))
824 (:overline
(cons "text-decoration" "overline" ))
825 (:strike-through
(cons "text-decoration" "line-through")))))
827 (defun hfy-invisible (&optional val
)
828 "This text should be invisible.
829 Do something in CSS to make that happen.
830 VAL is ignored here."
831 '(("display" .
"none")))
833 (defun hfy-combined-face-spec (face)
834 "Return a `defface' style alist of possible specifications for FACE.
835 Entries resulting from customization (`custom-set-faces') will take
838 (setq spec
(append (or (get face
'saved-face
) (list))
839 (or (get face
'face-defface-spec
) (list))))
840 (if (and hfy-display-class hfy-default-face-def
(eq face
'default
))
841 (setq spec
(append hfy-default-face-def spec
))) spec
))
843 (defun hfy-face-attr-for-class (face &optional class
)
844 "Return the face attributes for FACE.
845 If CLASS is set, it must be a `defface' alist key [see below],
846 in which case the first face specification returned by `hfy-combined-face-spec'
847 which *doesn't* clash with CLASS is returned.\n
848 \(A specification with a class of t is considered to match any class you
849 specify - this matches Emacs' behavior when deciding on which face attributes
850 to use, to the best of my understanding).\n
851 If CLASS is nil, then you just get get whatever `face-attr-construct' returns,
852 ie the current specification in effect for FACE.\n
853 *NOTE*: This function forces any face that is not 'default and which has
854 no :inherit property to inherit from 'default (this is because 'default
855 is magical in that Emacs' fonts behave as if they inherit implicitly from
856 'default, but no such behavior exists in HTML/CSS).\n
857 See also `hfy-display-class' for details of valid values for CLASS."
860 (let ((face-props (hfy-combined-face-spec face
))
867 (setq face-specn
(car face-props
)
868 face-class
(car face-specn
)
869 face-attrs
(cdr face-specn
)
870 face-props
(cdr face-props
))
871 ;; if the current element CEL of CLASS is t we match
872 ;; if the current face-class is t, we match
873 ;; if the cdr of CEL has a non-nil
874 ;; intersection with the cdr of the first member of
875 ;; the current face-class with the same car as CEL, we match
876 ;; if we actually clash, then we can't match
884 (while (and cbuf
(not next
))
889 val
(if (listp val
) val
(list val
)))
892 (memq face-class
'(t default
))) ;Default match.
893 (setq score
0) (ignore "t match"))
894 ((not (cdr (assq key face-class
))) ;Neither good nor bad.
895 nil
(ignore "non match, non collision"))
896 ((setq x
(hfy-interq val
(cdr (assq key face-class
))))
897 (setq score
(+ score
(length x
)))
898 (ignore "intersection"))
900 (setq next t score -
10) (ignore "collision")) ))
901 (if (> score face-score
)
903 (setq face-match face-attrs
905 (ignore "%d << %S/%S" score face-class class
))
906 (ignore "--- %d ---- (insufficient)" score
)) ))
907 ;; matched ? last attrs : nil
909 (if (listp (car face-match
)) (car face-match
) face-match
)
911 ;; Unfortunately the default face returns a
912 ;; :background. Fortunately we can remove it, but how do we do
913 ;; that in a non-system specific way?
914 (let ((spec (face-attr-construct face
))
916 (if (not (memq :background spec
))
919 (let ((a (nth 0 spec
))
921 (unless (and (eq a
:background
)
923 (string= b
"SystemWindow"))
924 (setq new-spec
(cons a
(cons b new-spec
)))))
925 (setq spec
(cddr spec
)))
927 (if (or (memq :inherit face-spec
) (eq 'default face
))
929 (append face-spec
(list :inherit
'default
)))))
931 ;; construct an assoc of (css-tag-name . css-tag-value) pairs
932 ;; from a face or assoc of face attributes:
935 ;; (mumamo-message-with-face "testing face" 'highlight)
936 ;; (mumamo-message-with-face "testing face" '(:foreground "red" :background "yellow"))
937 ;; (hfy-face-to-style-i '(:inherit default foreground-color "red"))
938 ;; default face=(:stipple nil :background "SystemWindow" :foreground
939 ;; "SystemWindowText" :inverse-video nil :box nil :strike-through
940 ;; nil :overline nil :underline nil :slant normal :weight normal
941 ;; :height 98 :width normal :family "outline-courier new")
942 (defun hfy-face-to-style-i (fn)
943 "The guts of `hfy-face-to-style': FN should be a `defface' font spec,
944 as returned by `face-attr-construct' or `hfy-face-attr-for-class'.
945 Note that this function does not get font-sizes right if they are based
946 on inherited modifiers (via the :inherit) attribute, and any other
947 modifiers that are cumulative if they appear multiple times need to be
948 merged by the user - `hfy-flatten-style' should do this."
949 ;;(message "hfy-face-to-style-i");;DBUG
951 ;; fn's value could be something like
953 ;; ((foreground-color . "blue"))
954 ;; (foreground-color . "blue")
964 (if (eq key
:inherit
)
965 (let ((vs (if (listp val
) val
(list val
))))
966 ;; (let ((x '(a b))) (setq x (append '(c d) x)))
967 ;; (let ((x '(a b))) (setq x (append '(c d) x)))
973 (hfy-face-attr-for-class v hfy-display-class
)) ))))
976 (:family
(hfy-family val
))
977 (:width
(hfy-width val
))
978 (:weight
(hfy-weight val
))
979 (:slant
(hfy-slant val
))
980 (:foreground
(hfy-colour val
))
981 (:background
(hfy-bgcol val
))
983 (:height
(hfy-size val
))
984 (:underline
(hfy-decor key val
))
985 (:overline
(hfy-decor key val
))
986 (:strike-through
(hfy-decor key val
))
987 (:invisible
(hfy-invisible val
))
988 (:bold
(hfy-weight 'bold
))
989 (:italic
(hfy-slant 'italic
))))))
990 (setq that
(hfy-face-to-style-i next
))
991 ;;(lwarn t :warning "%S => %S" fn (nconc this that parent))
992 (nconc this that parent
))) )
994 (defun hfy-size-to-int (spec)
995 "Convert SPEC, a CSS font-size specifier, to an Emacs :height attribute value.
996 Used while merging multiple font-size attributes."
997 ;;(message "hfy-size-to-int");;DBUG
999 (if (string-match "\\([0-9]+\\)\\(%\\|pt\\)" spec
)
1000 (cond ((string= "%" (match-string 2 spec
))
1001 (/ (string-to-number (match-string 1 spec
)) 100.0))
1002 ((string= "pt" (match-string 2 spec
))
1003 (* (string-to-number (match-string 1 spec
)) 10)))
1004 (string-to-number spec
))) )
1006 ;; size is different, in that in order to get it right at all,
1007 ;; we have to trawl the inheritance path, accumulating modifiers,
1008 ;; _until_ we get to an absolute (pt) specifier, then combine the lot
1009 (defun hfy-flatten-style (style)
1010 "Take STYLE (see `hfy-face-to-style-i', `hfy-face-to-style') and merge
1011 any multiple attributes appropriately. Currently only font-size is merged
1012 down to a single occurrence - others may need special handling, but I
1013 haven't encountered them yet. Returns a `hfy-style-assoc'."
1014 ;;(message "(hfy-flatten-style %S)" style) ;;DBUG
1020 (if (string= (car css
) "font-size")
1022 (when (not x
) (setq m
(nconc m
(hfy-size-to-int (cdr css
)))))
1023 (when (string-match "pt" (cdr css
)) (setq x t
)))
1024 (setq r
(nconc r
(list css
)))))
1025 ;;(message "r: %S" r)
1026 (setq n
(apply '* m
))
1027 (nconc r
(hfy-size (if x
(round n
) (* n
1.0)))) ))
1029 (defun hfy-face-resolve-face (fn)
1032 (hfy-face-attr-for-class fn hfy-display-class
))
1034 (facep (symbol-value fn
)))
1035 ;; Obsolete faces like `font-lock-reference-face' are defined as
1036 ;; aliases for another face.
1037 (hfy-face-attr-for-class (symbol-value fn
) hfy-display-class
))
1041 (defun hfy-face-to-style (fn)
1042 "Take FN, a font or `defface' style font specification,
1043 \(as returned by `face-attr-construct' or `hfy-face-attr-for-class')
1044 and return a `hfy-style-assoc'.\n
1045 See also `hfy-face-to-style-i', `hfy-flatten-style'."
1046 ;;(message "hfy-face-to-style");;DBUG
1047 (let ((face-def (hfy-face-resolve-face fn
))
1050 (setq final-style
(hfy-flatten-style (hfy-face-to-style-i face-def
)))
1051 ;;(message "%S" final-style)
1052 (if (not (assoc "text-decoration" final-style
))
1053 (progn (setq final-style
1054 ;; Fix-me: there is no need for this since
1055 ;; text-decoration is not inherited.
1056 ;; but it's not wrong and if this ever changes it will
1057 ;; be needed, so I think it's better to leave it in? -- v
1058 (nconc final-style
'(("text-decoration".
"none"))))))
1061 ;; strip redundant bits from a name. Technically, this could result in
1062 ;; a collision, but it is pretty unlikely - will fix later...
1063 ;; also handle ephemeral fonts created by overlays, which don't actually
1065 (defun hfy-face-or-def-to-name (fn)
1066 "Render a font symbol or `defface' font spec FN into a name (string)."
1067 ;;(message "generating name for %s" fn)
1068 (if (not (listp fn
))
1070 (let* ((key (format "%s" fn
))
1071 (entry (assoc key hfy-tmpfont-stack
))
1072 (base (cadr (memq :inherit fn
)))
1074 ;;(message "checking for key «%s» in font stack [%d]"
1075 ;; key (if entry 1 0))
1076 (if entry nil
;; noop
1077 (setq tag
(format "%04d" (length hfy-tmpfont-stack
))
1078 entry
(cons key tag
)
1079 hfy-tmpfont-stack
(cons entry hfy-tmpfont-stack
)))
1080 ;;(message " -> name: %s-%s" (or base 'default) tag)
1081 (format "%s-%s" (or base
'default
) tag
)) ))
1083 (defun hfy-css-name (fn)
1084 "Strip the boring bits from a font-name FN and return a CSS style name."
1085 ;;(message "hfy-css-name");;DBUG
1086 (let ((face-name (hfy-face-or-def-to-name fn
)))
1087 (if (or (string-match "font-lock-\\(.*\\)" face-name
)
1088 (string-match "cperl-\\(.*\\)" face-name
)
1089 (string-match "^[Ii]nfo-\\(.*\\)" face-name
))
1091 (setq face-name
(match-string 1 face-name
))
1092 (if (string-match "\\(.*\\)-face$" face-name
)
1093 (setq face-name
(match-string 1 face-name
))) face-name
)
1096 ;; construct an assoc of (stripped-name . "{ css-stuff-here }") pairs
1098 (defun hfy-face-to-css (fn)
1099 "Take FN, a font or `defface' specification (cf `face-attr-construct')
1100 and return a CSS style specification.\n
1101 See also `hfy-face-to-style'."
1102 ;;(message "hfy-face-to-css");;DBUG
1103 (let ((css-list nil
)
1106 ;;(message "(hfy-face-to-style %S)" fn)
1107 (setq css-list
(hfy-face-to-style fn
))
1112 (unless (member (car E
) seen
)
1114 (format " %s: %s; " (car E
) (cdr E
)))))
1116 (cons (hfy-css-name fn
) (format "{%s}" (apply 'concat css-text
)))) )
1118 ;; extract a face from a list of char properties, if there is one:
1119 (defun hfy-p-to-face (props)
1120 "Given PROPS, a list of text properties, return the value of the face
1123 (if (string= (car props
) "face")
1124 (let ((propval (cadr props
)))
1125 (if (and (listp propval
) (not (cdr propval
)))
1128 (hfy-p-to-face (cddr props
)))
1131 (defun hfy-p-to-face-lennart (props)
1132 "Given PROPS, a list of text properties, return the value of the face
1135 (let ((face (plist-get props
'face
))
1136 (font-lock-face (plist-get props
'font-lock-face
))
1137 (button (plist-get props
'button
))
1138 ;;(face-rec (memq 'face props))
1139 ;;(button-rec (memq 'button props)))
1142 (let* ((category (plist-get props
'category
))
1143 (face (when category
(plist-get (symbol-plist category
) 'face
))))
1148 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1149 ;; (defun hfy-get-face-at (pos)
1150 ;; ;; (let ((face (get-char-property-and-overlay pos 'face)))
1151 ;; ;; (when (and face (listp face)) (setq face (car face)))
1152 ;; ;; (unless (listp face)
1154 ;; ;;(get-char-property pos 'face)
1155 ;; ;; Overlays are handled later
1156 ;; (if (or (not show-trailing-whitespace)
1157 ;; (not (get-text-property pos 'hfy-show-trailing-whitespace)))
1158 ;; (get-text-property pos 'face)
1159 ;; (list 'trailing-whitespace (get-text-property pos 'face)))
1162 (defun hfy-prop-invisible-p (prop)
1163 "Is text property PROP an active invisibility property?"
1164 (or (and (eq buffer-invisibility-spec t
) prop
)
1165 (or (memq prop buffer-invisibility-spec
)
1166 (assq prop buffer-invisibility-spec
))))
1168 (defun hfy-find-invisible-ranges ()
1169 "Return a list of (start-point . end-point) cons cells of invisible regions."
1170 (let (invisible p i e s
) ;; return-value pos invisible end start
1172 (setq p
(goto-char (point-min)))
1173 (when (invisible-p p
) (setq s p i t
))
1174 (while (< p
(point-max))
1175 (if i
;; currently invisible
1176 (when (not (invisible-p p
)) ;; but became visible
1179 invisible
(cons (cons s e
) invisible
)))
1180 ;; currently visible:
1181 (when (invisible-p p
) ;; but have become invisible
1183 (setq p
(next-char-property-change p
)))
1184 ;; still invisible at buffer end?
1187 invisible
(cons (cons s e
) invisible
))) ) invisible
))
1189 (defun hfy-invisible-name (point map
)
1190 "Generate a CSS style name for an invisible section of the buffer.
1191 POINT is the point inside the invisible region.
1192 MAP is the invisibility map as returned by `hfy-find-invisible-ranges'."
1193 ;;(message "(hfy-invisible-name %S %S)" point map)
1196 (when (and (>= point
(car range
))
1197 (< point
(cdr range
)))
1198 (setq name
(format "invisible-%S-%S" (car range
) (cdr range
)))))
1201 ;; Fix-me: This function needs some cleanup by someone who understand
1202 ;; all the formats that face properties can have.
1204 ;; overlay handling should be fine. haven't tested multiple stacked overlapping
1205 ;; overlays recently, but the common case of a text property face + an overlay
1206 ;; face produces the correct merged css style (or as close to it as css can get)
1208 (defun hfy-face-at (p)
1209 "Find face in effect at point P.
1210 If overlays are to be considered (see `hfy-optimisations') then this may
1211 return a `defface' style list of face properties instead of a face symbol."
1212 ;;(message "hfy-face-at");;DBUG
1213 ;; Fix-me: clean up, remove face-name etc
1214 ;; not sure why we'd want to remove face-name? -- v
1215 (let ((overlay-data nil
)
1217 ;; restored hfy-p-to-face as it handles faces like (bold) as
1218 ;; well as face like 'bold - hfy-get-face-at doesn't dtrt -- v
1219 (face-name (hfy-p-to-face (text-properties-at p
)))
1220 ;; (face-name (hfy-get-face-at p))
1223 (text-props (text-properties-at p
)))
1224 ;;(message "face-name: %S" face-name)
1225 (when (and face-name
(listp face-name
) (facep (car face-name
)))
1226 ;;(message "face-name is a list %S" face-name)
1227 ;;(setq text-props (cons 'face face-name))
1228 (dolist (f face-name
)
1229 (setq extra-props
(if (listp f
)
1230 ;; for things like (variable-pitch
1231 ;; (:foreground "red"))
1232 (cons f extra-props
)
1233 (cons :inherit
(cons f extra-props
)))))
1234 (setq base-face
(car face-name
)
1236 ;; text-properties-at => (face (:foreground "red" ...))
1237 ;; or => (face (compilation-info underline)) list of faces
1238 ;; overlay-properties
1239 ;; format= (evaporate t face ((foreground-color . "red")))
1241 ;; SO: if we have turned overlays off,
1242 ;; or if there's no overlay data
1243 ;; just bail out and return whatever face data we've accumulated so far
1244 (if (or (not (hfy-opt 'keep-overlays
))
1245 (not (setq overlay-data
(hfy-overlay-props-at p
))))
1247 ;;(message "· %d: %s; %S; %s"
1248 ;; p face-name extra-props text-props)
1249 (or face-name base-face
)) ;; no overlays or extra properties
1250 ;; collect any face data and any overlay data for processing:
1252 (push text-props overlay-data
))
1253 (setq overlay-data
(nreverse overlay-data
))
1254 ;;(message "- %d: %s; %S; %s; %s"
1255 ;; p face-name extra-props text-props overlay-data)
1256 ;; remember the basic face name so we don't keep repeating its specs:
1257 (when face-name
(setq base-face face-name
))
1258 (dolist (P overlay-data
)
1259 (let ((iprops (cadr (memq 'invisible P
)))) ;FIXME: plist-get?
1260 ;;(message "(hfy-prop-invisible-p %S)" iprops)
1261 (when (and iprops
(hfy-prop-invisible-p iprops
))
1263 (cons :invisible
(cons t extra-props
))) ))
1264 (let ((fprops (cadr (or (memq 'face P
)
1265 (memq 'font-lock-face P
)))))
1266 ;;(message "overlay face: %s" fprops)
1267 (if (not (listp fprops
))
1268 (let ((this-face (if (stringp fprops
) (intern fprops
) fprops
)))
1269 (when (not (eq this-face base-face
))
1272 (cons this-face extra-props
))) ))
1274 (if (facep (car fprops
))
1275 (let ((face (car fprops
)))
1276 (when (stringp face
) (setq face
(intern fprops
)))
1281 (setq fprops
(cdr fprops
)))
1284 (if (listp (car fprops
))
1285 (if (nlistp (cdr (car fprops
)))
1288 (setq p
(caar fprops
))
1289 (setq v
(cdar fprops
))
1290 (setq fprops
(cdr fprops
)))
1292 (setq p
(caar fprops
))
1293 (setq v
(cadar fprops
))
1294 (setq fprops
(cdr fprops
)))
1295 (if (listp (cdr fprops
))
1297 ;; (:prop val :prop val ...)
1298 (setq p
(car fprops
))
1299 (setq v
(cadr fprops
))
1300 (setq fprops
(cddr fprops
)))
1301 (if (and (listp fprops
)
1302 (not (listp (cdr fprops
))))
1303 ;;(and (consp x) (cdr (last x)))
1306 (setq p
(car fprops
))
1307 (setq v
(cdr fprops
))
1309 (error "Eh... another format! fprops=%s" fprops
) )))
1311 ;; These are all the properties handled
1312 ;; in `hfy-face-to-style-i'.
1314 ;; Are these translations right?
1315 ;; yes, they are -- v
1321 (underline :underline
)
1322 (overline :overline
)
1323 (strike-through :strike-through
)
1325 (foreground-color :foreground
)
1326 (background-color :background
)
1330 (if (memq p prop-seen
) nil
;; noop
1331 (setq prop-seen
(cons p prop-seen
)
1332 extra-props
(cons p
(cons v extra-props
))))))))))
1333 ;;(message "+ %d: %s; %S" p face-name extra-props)
1335 (if (listp face-name
)
1336 (nconc extra-props face-name
)
1337 (nconc extra-props
(face-attr-construct face-name
)))
1340 (defun hfy-overlay-props-at (p)
1341 "Grab overlay properties at point P.
1342 The plists are returned in descending priority order."
1343 (sort (mapcar #'overlay-properties
(overlays-at p
))
1344 (lambda (A B
) (> (or (cadr (memq 'priority A
)) 0) ;FIXME: plist-get?
1345 (or (cadr (memq 'priority B
)) 0)))))
1347 ;; construct an assoc of (face-name . (css-name . "{ css-style }")) elements:
1348 (defun hfy-compile-stylesheet ()
1349 "Trawl the current buffer, construct and return a `hfy-sheet-assoc'."
1350 ;;(message "hfy-compile-stylesheet");;DBUG
1351 (let ((pt (point-min))
1352 ;; Make the font stack stay:
1353 ;;(hfy-tmpfont-stack nil)
1358 (while (< pt
(point-max))
1359 (if (and (setq fn
(hfy-face-at pt
)) (not (assoc fn style
)))
1360 (push (cons fn
(hfy-face-to-css fn
)) style
))
1361 (setq pt
(next-char-property-change pt
))) )
1362 (push (cons 'default
(hfy-face-to-css 'default
)) style
)))
1364 (defun hfy-fontified-p ()
1365 "`font-lock' doesn't like to say it's been fontified when in batch
1366 mode, but we want to know if we should fontify or raw copy, so in batch
1367 mode we check for non-default face properties. Otherwise we test
1368 variable `font-lock-mode' and variable `font-lock-fontified' for truth."
1369 ;;(message "font-lock-fontified: %S" font-lock-fontified)
1370 ;;(message "noninteractive : %S" noninteractive)
1371 ;;(message "font-lock-mode : %S" font-lock-mode)
1372 (and font-lock-fontified
1374 (let ((pt (point-min))
1378 (while (and (< pt
(point-max)) (not face-name
))
1379 (setq face-name
(hfy-face-at pt
))
1380 (setq pt
(next-char-property-change pt
)))) face-name
)
1383 ;; remember, the map is in reverse point order:
1384 ;; I wrote this while suffering the effects of a cold, and maybe a
1385 ;; mild fever - I think it's correct, but it might be a little warped
1386 ;; as my minfd keeps ... where was I? Oh yes, the bunnies...
1387 (defun hfy-merge-adjacent-spans (face-map)
1388 "Where FACE-MAP is a `hfy-facemap-assoc' for the current buffer,
1389 this function merges adjacent style blocks which are of the same value
1390 and are separated by nothing more interesting than whitespace.\n
1391 <span class=\"foo\">narf</span> <span class=\"foo\">brain</span>\n
1392 \(as interpreted from FACE-MAP) would become:\n
1393 <span class=\"foo\">narf brain</span>\n
1394 Returns a modified copy of FACE-MAP."
1395 (let ((tmp-map face-map
)
1404 ;;(push (car tmp-map) reduced-map)
1405 ;;(push (cadr tmp-map) reduced-map)
1407 (setq first-start
(cadddr tmp-map
)
1408 first-stop
(caddr tmp-map
)
1409 last-start
(cadr tmp-map
)
1410 last-stop
(car tmp-map
)
1412 span-start last-start
1413 span-stop last-stop
)
1414 (while (and (equal (cdr first-start
)
1417 (goto-char (car first-stop
))
1418 (not (re-search-forward "[^ \t\n\r]" (car last-start
) t
))))
1419 (setq map-buf
(cddr map-buf
)
1420 span-start first-start
1421 first-start
(cadddr map-buf
)
1422 first-stop
(caddr map-buf
)
1423 last-start
(cadr map-buf
)
1424 last-stop
(car map-buf
)))
1425 (push span-stop reduced-map
)
1426 (push span-start reduced-map
)
1427 (setq tmp-map
(memq last-start tmp-map
))
1428 (setq tmp-map
(cdr tmp-map
)))
1429 (setq reduced-map
(nreverse reduced-map
))))
1431 ;; remember to generate 'synthetic' </span> entries -
1432 ;; emacs copes by just having a stack of styles in effect
1433 ;; and only using the top one: html has a more simplistic approach -
1434 ;; we have to explicitly end a style, there's no way of temporarily
1435 ;; overriding it w. another one... (afaik)
1436 (defun hfy-compile-face-map ()
1437 ;; no need for special <a> version.
1438 ;; IME hyperlinks don't get underlined, esp when you htmlfontify a whole
1439 ;; source tree, so the <a> version is needed -- v
1440 ;; Fix-me: save table for multi-buffer
1441 "Compile and return a `hfy-facemap-assoc' for the current buffer."
1442 ;;(message "hfy-compile-face-map");;DBUG
1443 (let ((pt (point-min))
1447 (prev-tag nil
)) ;; t if the last tag-point was a span-start
1448 ;; nil if it was a span-stop
1451 (while (< pt
(point-max))
1452 (if (setq fn
(hfy-face-at pt
))
1453 (progn (if prev-tag
(push (cons pt-narrow
'end
) map
))
1454 (push (cons pt-narrow fn
) map
)
1456 (if prev-tag
(push (cons pt-narrow
'end
) map
))
1457 (setq prev-tag nil
))
1458 (setq pt
(next-char-property-change pt
))
1459 (setq pt-narrow
(1+ (- pt
(point-min)))))
1460 (if (and map
(not (eq 'end
(cdar map
))))
1461 (push (cons (- (point-max) (point-min)) 'end
) map
)))
1462 (if (hfy-opt 'merge-adjacent-tags
) (hfy-merge-adjacent-spans map
) map
)))
1464 (defun hfy-buffer ()
1465 "Generate a buffer to hold the HTML output.
1466 The filename of this buffer is derived from the source (current) buffer's
1467 variable `buffer-file-name', if it is set, plus `hfy-extn'.
1468 Otherwise a plausible filename is constructed from `default-directory',
1469 `buffer-name' and `hfy-extn'."
1470 (let* ((name (concat (buffer-name) hfy-extn
))
1471 (src (buffer-file-name))
1472 (buf (get-buffer-create name
)))
1473 (with-current-buffer buf
1474 (setq buffer-file-name
1475 (if src
(concat src hfy-extn
)
1476 (expand-file-name (if (string-match "^.*/\\([^/]*\\)$" name
)
1477 (match-string 1 name
)
1481 (defun hfy-lookup (face style
)
1482 "Get a CSS style name for FACE from STYLE."
1483 (cadr (assoc face style
)))
1485 (defun hfy-link-style (style-string)
1486 "Copy, alter and return a STYLE-STRING to make it suitable for a hyperlink.
1487 Uses `hfy-link-style-fun' to do this."
1488 (if (functionp hfy-link-style-fun
)
1489 (funcall hfy-link-style-fun style-string
)
1492 (defun hfy-sprintf-stylesheet (css file
)
1493 "Return the inline CSS style sheet for FILE as a string."
1494 (let ((stylesheet nil
))
1498 "\n<style type=\"text/css\"><!-- \n"
1499 ;; Fix-me: Add handling of page breaks here + scan for ^L
1500 ;; where appropriate.
1501 (format "body %s\n" (cddr (assq 'default css
)))
1506 "span.%s %s\nspan.%s a %s\n"
1507 (cadr style
) (cddr style
)
1508 (cadr style
) (hfy-link-style (cddr style
))))
1511 (funcall hfy-page-header file stylesheet
)))
1513 ;; tag all the dangerous characters we want to escape
1514 ;; (ie any "<> chars we _didn't_ put there explicitly for css markup)
1515 (defun hfy-html-enkludge-buffer ()
1516 "Mark dangerous [\"<>] characters with the `hfy-quoteme' property.\n
1517 See also `hfy-html-dekludge-buffer'."
1518 ;;(message "hfy-html-enkludge-buffer");;DBUG
1520 (goto-char (point-min))
1521 (while (re-search-forward hfy-html-quote-regex nil t
)
1522 (put-text-property (match-beginning 0) (point) 'hfy-quoteme t
))) )
1524 ;; dangerous char -> &entity;
1525 (defun hfy-html-quote (char-string)
1526 "Map CHAR-STRING to an HTML safe string (entity) if need be."
1527 ;;(message "hfy-html-quote");;DBUG
1528 (or (cadr (assoc char-string hfy-html-quote-map
)) char-string
) )
1530 ;; actually entity-ise dangerous chars.
1531 ;; note that we can't do this until _after_ we have inserted the css
1532 ;; markup, since we use a position-based map to insert this, and if we
1533 ;; enter any other text before we do this, we'd have to track another
1534 ;; map of offsets, which would be tedious...
1535 (defun hfy-html-dekludge-buffer ()
1536 "Transform all dangerous characters marked with the `hfy-quoteme' property
1537 using `hfy-html-quote'.\n
1538 See also `hfy-html-enkludge-buffer'."
1539 ;;(message "hfy-html-dekludge-buffer");;DBUG
1541 (goto-char (point-min))
1542 (while (re-search-forward hfy-html-quote-regex nil t
)
1543 (if (get-text-property (match-beginning 0) 'hfy-quoteme
)
1544 (replace-match (hfy-html-quote (match-string 1))) )) ))
1546 ;; Borrowed from font-lock.el
1547 (defmacro hfy-save-buffer-state
(varlist &rest body
)
1548 "Bind variables according to VARLIST and eval BODY restoring buffer state.
1549 Do not record undo information during evaluation of BODY."
1550 (declare (indent 1) (debug let
))
1551 (let ((modified (make-symbol "modified")))
1552 `(let* ,(append varlist
1553 `((,modified
(buffer-modified-p))
1554 (buffer-undo-list t
)
1555 (inhibit-read-only t
)
1556 (inhibit-point-motion-hooks t
)
1557 (inhibit-modification-hooks t
)
1560 buffer-file-truename
))
1564 (restore-buffer-modified-p nil
)))))
1566 (defun hfy-mark-trailing-whitespace ()
1567 "Tag trailing whitespace with a hfy property if it is currently highlighted."
1568 (when show-trailing-whitespace
1569 (let ((inhibit-read-only t
))
1571 (goto-char (point-min))
1572 (hfy-save-buffer-state nil
1573 (while (re-search-forward "[ \t]+$" nil t
)
1574 (put-text-property (match-beginning 0) (match-end 0)
1575 'hfy-show-trailing-whitespace t
)))))))
1577 (defun hfy-unmark-trailing-whitespace ()
1578 "Undo the effect of `hfy-mark-trailing-whitespace'."
1579 (when show-trailing-whitespace
1580 (hfy-save-buffer-state nil
1581 (remove-text-properties (point-min) (point-max)
1582 '(hfy-show-trailing-whitespace)))))
1584 (defun hfy-fontify-buffer (&optional srcdir file
)
1585 "Implement the guts of `htmlfontify-buffer'.
1586 SRCDIR, if set, is the directory being htmlfontified.
1587 FILE, if set, is the file name."
1588 (if srcdir
(setq srcdir
(directory-file-name srcdir
)))
1589 (let* ( (html-buffer (hfy-buffer))
1594 (orig-ovls (overlays-in (point-min) (point-max)))
1595 (rmin (when mark-active
(region-beginning)))
1596 (rmax (when mark-active
(region-end ))) )
1597 (when (and mark-active
1598 transient-mark-mode
)
1599 (unless (and (= rmin
(point-min))
1600 (= rmax
(point-max)))
1601 (setq rovl
(make-overlay rmin rmax
))
1602 (overlay-put rovl
'priority
1000)
1603 (overlay-put rovl
'face
'region
)))
1604 ;; copy the buffer, including fontification, and switch to it:
1605 (hfy-mark-trailing-whitespace)
1606 (setq css-sheet
(hfy-compile-stylesheet )
1607 css-map
(hfy-compile-face-map )
1608 invis-ranges
(hfy-find-invisible-ranges))
1609 (hfy-unmark-trailing-whitespace)
1611 (delete-overlay rovl
))
1612 (copy-to-buffer html-buffer
(point-min) (point-max))
1613 (set-buffer html-buffer
)
1614 ;; rip out props that could interfere with our htmlisation of the buffer:
1615 (remove-text-properties (point-min) (point-max) hfy-ignored-properties
)
1616 ;; Apply overlay invisible spec
1620 (> (or (cadr (memq 'priority
(overlay-properties A
))) 0)
1621 (or (cadr (memq 'priority
(overlay-properties B
))) 0)))))
1622 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1623 ;; at this point, html-buffer retains the fontification of the parent:
1624 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1625 ;; we don't really need or want text in the html buffer to be invisible, as
1626 ;; that can make it look like we've rendered invalid xhtml when all that's
1627 ;; happened is some tags are in the invisible portions of the buffer:
1628 (setq buffer-invisibility-spec nil
)
1629 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1630 ;; #####################################################################
1631 ;; if we are in etags mode, add properties to mark the anchors and links
1632 (if (and srcdir file
)
1634 (hfy-mark-tag-names srcdir file
) ;; mark anchors
1635 (hfy-mark-tag-hrefs srcdir file
))) ;; mark links
1636 ;; #####################################################################
1637 ;; mark the 'dangerous' characters
1638 ;;(message "marking dangerous characters")
1639 (hfy-html-enkludge-buffer)
1640 ;; trawl the position-based face-map, inserting span tags as we go
1641 ;; note that we cannot change any character positions before this point
1642 ;; or we will invalidate the map:
1643 ;; NB: This also means we have to trawl the map in descending file-offset
1644 ;; order, obviously.
1645 ;; ---------------------------------------------------------------------
1646 ;; Remember, inserting pushes properties to the right, which we don't
1647 ;; actually want to happen for link properties, so we have to flag
1648 ;; them and move them by hand - if you don't, you end up with
1650 ;; <span class="foo"><a href="bar">texta</span><span class="bletch"></a>...
1654 ;; <span class="foo"><a href="bar">texta</a></span><span class="bletch">...
1656 ;; If my analysis of the problem is correct, we can detect link-ness by
1657 ;; either hfy-linkp or hfy-endl properties at the insertion point, but I
1658 ;; think we only need to relocate the hfy-endl property, as the hfy-linkp
1659 ;; property has already served its main purpose by this point.
1660 ;;(message "mapcar over the CSS-MAP")
1661 (message "invis-ranges:\n%S" invis-ranges
)
1662 (dolist (point-face css-map
)
1663 (let ((pt (car point-face
))
1664 (fn (cdr point-face
))
1668 (or (get-text-property pt
'hfy-linkp
)
1669 (get-text-property pt
'hfy-endl
)))
1672 (if (not (and srcdir file
))
1675 (remove-text-properties (point) (1+ (point)) '(hfy-endl nil
))
1676 (put-text-property pt
(1+ pt
) 'hfy-endl t
) ))
1677 ;; if we have invisible blocks, we need to do some extra magic:
1679 (let ((iname (hfy-invisible-name pt invis-ranges
))
1680 (fname (hfy-lookup fn css-sheet
)))
1681 (when (assq pt invis-ranges
)
1683 (format "<span onclick=\"toggle_invis('%s');\">" iname
))
1684 (insert "…</span>"))
1686 (format "<span class=\"%s\" id=\"%s-%d\">" fname iname pt
)))
1687 (insert (format "<span class=\"%s\">" (hfy-lookup fn css-sheet
))))
1688 (if (not move-link
) nil
1689 ;;(message "removing prop2 @ %d" (point))
1690 (if (remove-text-properties (point) (1+ (point)) '(hfy-endl nil
))
1691 (put-text-property pt
(1+ pt
) 'hfy-endl t
))))))
1692 ;; #####################################################################
1694 ;; Maybe just make the text invisible in XHTML?
1695 ;; DONE -- big block of obsolete invisibility code elided here -- v
1696 ;; #####################################################################
1697 ;; (message "checking to see whether we should link...")
1698 (if (and srcdir file
)
1699 (let ((lp 'hfy-link
)
1703 ;; (message " yes we should.")
1704 ;; translate 'hfy-anchor properties to anchors
1705 (setq pt
(point-min))
1706 (while (setq pt
(next-single-property-change pt
'hfy-anchor
))
1707 (if (setq pr
(get-text-property pt
'hfy-anchor
))
1708 (progn (goto-char pt
)
1709 (remove-text-properties pt
(1+ pt
) '(hfy-anchor nil
))
1710 (insert (concat "<a name=\"" pr
"\"></a>")))))
1711 ;; translate alternate 'hfy-link and 'hfy-endl props to opening
1712 ;; and closing links. (this should avoid those spurious closes
1713 ;; we sometimes get by generating only paired tags)
1714 (setq pt
(point-min))
1715 (while (setq pt
(next-single-property-change pt lp
))
1716 (if (not (setq pr
(get-text-property pt lp
))) nil
1718 (remove-text-properties pt
(1+ pt
) (list lp nil
))
1721 (if (setq rr
(get-text-property pt
'hfy-inst
))
1722 (insert (format "<a name=\"%s\"></a>" rr
)))
1723 (insert (format "<a href=\"%s\">" pr
))
1724 (setq lp
'hfy-endl
))
1726 (insert "</a>") (setq lp
'hfy-link
)) ))) ))
1728 ;; #####################################################################
1729 ;; transform the dangerous chars. This changes character positions
1730 ;; since entities have > char length.
1731 ;; note that this deletes the dangerous characters, and therefore
1732 ;; destroys any properties they may contain (such as 'hfy-endl),
1733 ;; so we have to do this after we use said properties:
1734 ;; (message "munging dangerous characters")
1735 (hfy-html-dekludge-buffer)
1736 ;; insert the stylesheet at the top:
1737 (goto-char (point-min))
1738 ;;(message "inserting stylesheet")
1739 (insert (hfy-sprintf-stylesheet css-sheet file
))
1740 (if (hfy-opt 'div-wrapper
) (insert "<div class=\"default\">"))
1742 (goto-char (point-max))
1744 (if (hfy-opt 'div-wrapper
) (insert "</div>"))
1745 ;;(message "inserting footer")
1746 (insert (funcall hfy-page-footer file
))
1747 ;; call any post html-generation hooks:
1748 (run-hooks 'hfy-post-html-hooks
)
1749 ;; return the html buffer
1750 (set-buffer-modified-p nil
)
1753 (defun hfy-force-fontification ()
1754 "Try to force font-locking even when it is optimized away."
1755 (run-hooks 'hfy-init-kludge-hook
)
1756 (eval-and-compile (require 'font-lock
))
1757 (if (boundp 'font-lock-cache-position
)
1758 (or font-lock-cache-position
1759 (set 'font-lock-cache-position
(make-marker))))
1760 (if (not noninteractive
)
1762 (message "hfy interactive mode (%S %S)" window-system major-mode
)
1763 (when (and font-lock-defaults
1765 (font-lock-fontify-region (point-min) (point-max) nil
)))
1766 (message "hfy batch mode (%s:%S)"
1767 (or (buffer-file-name) (buffer-name)) major-mode
)
1768 (when font-lock-defaults
1769 (font-lock-fontify-buffer)) ))
1772 (defun htmlfontify-buffer (&optional srcdir file
)
1773 "Create a new buffer, named for the current buffer + a .html extension,
1774 containing an inline CSS-stylesheet and formatted CSS-markup HTML
1775 that reproduces the look of the current Emacs buffer as closely
1778 Dangerous characters in the existing buffer are turned into HTML
1779 entities, so you should even be able to do HTML-within-HTML
1782 You should, however, note that random control or eight-bit
1783 characters such as ^L (\x0c) or ¤ (\xa4) won't get mapped yet.
1785 If the SRCDIR and FILE arguments are set, lookup etags derived
1786 entries in the `hfy-tags-cache' and add HTML anchors and
1787 hyperlinks as appropriate."
1789 ;; pick up the file name in case we didn't receive it
1791 (progn (setq file
(or (buffer-file-name) (buffer-name)))
1792 (if (string-match "/\\([^/]*\\)$" file
)
1793 (setq file
(match-string 1 file
)))) )
1795 (if (not (hfy-opt 'skip-refontification
))
1796 (save-excursion ;; Keep region
1797 (hfy-force-fontification)))
1798 (if (interactive-p) ;; display the buffer in interactive mode:
1799 (switch-to-buffer (hfy-fontify-buffer srcdir file
))
1800 (hfy-fontify-buffer srcdir file
)))
1802 ;; recursive file listing
1803 (defun hfy-list-files (directory)
1804 "Return a list of files under DIRECTORY.
1805 Strips any leading \"./\" from each filename."
1806 ;;(message "hfy-list-files");;DBUG
1807 ;; FIXME: this changes the dir of the currrent buffer. Is that right??
1809 (mapcar (lambda (F) (if (string-match "^./\\(.*\\)" F
) (match-string 1 F
) F
))
1810 (split-string (shell-command-to-string hfy-find-cmd
))) )
1812 ;; strip the filename off, return a directiry name
1813 ;; not a particularly thorough implementaion, but it will be
1814 ;; fed pretty carefully, so it should be Ok:
1815 (defun hfy-dirname (file)
1816 "Return everything preceding the last \"/\" from a relative filename FILE,
1817 on the assumption that this will produce a relative directory name.
1818 Hardly bombproof, but good enough in the context in which it is being used."
1819 ;;(message "hfy-dirname");;DBUG
1820 (let ((f (directory-file-name file
)))
1821 (and (string-match "^\\(.*\\)/" f
) (match-string 1 f
))))
1823 ;; create a directory, cf mkdir -p
1824 (defun hfy-make-directory (dir)
1825 "Approx. equivalent of mkdir -p DIR."
1826 ;;(message "hfy-make-directory");;DBUG
1827 (if (file-exists-p dir
)
1828 (if (file-directory-p dir
) t
)
1829 (make-directory dir t
)))
1831 (defun hfy-text-p (srcdir file
)
1832 "Is SRCDIR/FILE text? Uses `hfy-istext-command' to determine this."
1833 (let* ((cmd (format hfy-istext-command
(expand-file-name file srcdir
)))
1834 (rsp (shell-command-to-string cmd
)))
1835 (if (string-match "text" rsp
) t nil
)))
1837 ;; open a file, check fontification, if fontified, write a fontified copy
1838 ;; to the destination directory, otherwise just copy the file:
1839 (defun hfy-copy-and-fontify-file (srcdir dstdir file
)
1840 "Open FILE in SRCDIR - if fontified, write a fontified copy to DSTDIR
1841 adding an extension of `hfy-extn'. Fontification is actually done by
1842 `htmlfontify-buffer'. If the buffer is not fontified, just copy it."
1843 ;;(message "hfy-copy-and-fontify-file");;DBUG
1844 (let (;;(fast-lock-minimum-size hfy-fast-lock-save)
1845 ;;(font-lock-support-mode 'fast-lock-mode)
1846 ;;(window-system (or window-system 'htmlfontify))
1851 (with-current-buffer (setq source
(find-file-noselect file
))
1852 ;; FIXME: Shouldn't this use expand-file-name? --Stef
1853 (setq target
(concat dstdir
"/" file
))
1854 (hfy-make-directory (hfy-dirname target
))
1855 (if (not (hfy-opt 'skip-refontification
)) (hfy-force-fontification))
1856 (if (or (hfy-fontified-p) (hfy-text-p srcdir file
))
1857 (progn (setq html
(hfy-fontify-buffer srcdir file
))
1859 (write-file (concat target hfy-extn
))
1861 ;; #o0200 == 128, but emacs20 doesn't know that
1862 (if (and (file-exists-p target
) (not (file-writable-p target
)))
1863 (set-file-modes target
(logior (file-modes target
) 128)))
1864 (copy-file (buffer-file-name source
) target
'overwrite
))
1865 (kill-buffer source
)) ))
1867 ;; list of tags in file in srcdir
1868 (defun hfy-tags-for-file (srcdir file
)
1869 "List of etags tags that have definitions in this FILE.
1870 Looks up the tags cache in `hfy-tags-cache' using SRCDIR as the key."
1871 ;;(message "hfy-tags-for-file");;DBUG
1872 (let ((cache-entry (assoc srcdir hfy-tags-cache
))
1875 (if (setq cache-hash
(cadr cache-entry
))
1879 (setq tag-list
(cons K tag-list
)))) cache-hash
))
1882 ;; mark the tags native to this file for anchors
1883 (defun hfy-mark-tag-names (srcdir file
)
1884 "Mark tags in FILE (lookup SRCDIR in `hfy-tags-cache') with the `hfy-anchor'
1885 property, with a value of \"tag.line-number\"."
1886 ;;(message "(hfy-mark-tag-names %s %s)" srcdir file);;DBUG
1887 (let ((cache-entry (assoc srcdir hfy-tags-cache
))
1889 (if (setq cache-hash
(cadr cache-entry
))
1894 (if (string= file
(car TLIST
))
1895 (let* ((line (cadr TLIST
) )
1896 (chr (caddr TLIST
) )
1897 (link (format "%s.%d" TAG line
) ))
1898 (put-text-property (+ 1 chr
)
1900 'hfy-anchor link
))))
1901 (gethash TAG cache-hash
)))
1902 (hfy-tags-for-file srcdir file
)))))
1904 (defun hfy-relstub (file &optional start
)
1905 "Return a \"../\" stub of the appropriate length for the current source
1906 tree depth, as determined from FILE (a filename).
1907 START is the offset at which to start looking for the / character in FILE."
1908 ;;(message "hfy-relstub");;DBUG
1910 (while (setq start
(string-match "/" file start
))
1911 (setq start
(1+ start
)) (setq c
(concat c
"../"))) c
))
1913 (defun hfy-href-stub (this-file def-files tag
)
1914 "Return an href stub for a tag href in THIS-FILE.
1915 If DEF-FILES (list of files containing definitions for the tag in question)
1916 contains only one entry, the href should link straight to that file.
1917 Otherwise, the link should be to the index file.\n
1918 We are not yet concerned with the file extensions/tag line number and so on at
1920 If `hfy-split-index' is set, and the href wil be to an index file rather than
1921 a source file, append a .X to `hfy-index-file', where X is the uppercased
1922 first character of TAG.\n
1923 See also `hfy-relstub', `hfy-index-file'."
1924 ;;(message "hfy-href-stub");;DBUG
1925 ;; FIXME: Why not use something like
1926 ;; (file-relative-name (if ...) (file-name-directory this-file)) ? --Stef
1928 (hfy-relstub this-file
)
1929 (if (= 1 (length def-files
)) (car def-files
)
1930 (if (not hfy-split-index
) hfy-index-file
1931 (concat hfy-index-file
"." (upcase (substring tag
0 1)))))) )
1933 (defun hfy-href (this-file def-files tag tag-map
)
1934 "Return a relative href to the tag in question, based on\n
1935 THIS-FILE `hfy-link-extn' `hfy-extn' DEF-FILES TAG and TAG-MAP\n
1936 THIS-FILE is the current source file
1937 DEF-FILES is a list of file containing possible link endpoints for TAG
1938 TAG is the tag in question
1939 TAG-MAP is the entry in `hfy-tags-cache'."
1940 ;;(message "hfy-href");;DBUG
1942 (hfy-href-stub this-file def-files tag
)
1943 (or hfy-link-extn hfy-extn
) "#" tag
;;(.src -> .html)
1944 (if (= 1 (length def-files
))
1945 (concat "." (format "%d" (cadr (assoc (car def-files
) tag-map
)))))) )
1947 (defun hfy-word-regex (string)
1948 "Return a regex that matches STRING as the first `match-string', with non
1949 word characters on either side."
1950 ;; FIXME: Should this use [^$[:alnum:]_] instead? --Stef
1951 (concat "[^$A-Za-z_0-9]\\(" (regexp-quote string
) "\\)[^A-Za-z_0-9]"))
1953 ;; mark all tags for hyperlinking, except the tags at
1954 ;; their own points of definition, iyswim:
1955 (defun hfy-mark-tag-hrefs (srcdir file
)
1956 "Mark href start points with the `hfy-link' prop (value: href string).\n
1957 Mark href end points with the `hfy-endl' prop (value t).\n
1958 Avoid overlapping links, and mark links in descending length of
1959 tag name in order to prevent subtags from usurping supertags,
1960 \(eg \"term\" for \"terminal\").
1961 SRCDIR is the directory being \"published\".
1962 FILE is the specific file we are rendering."
1963 ;;(message "hfy-mark-tag-hrefs");;DBUG
1964 (let ((cache-entry (assoc srcdir hfy-tags-cache
))
1965 (list-cache (assoc srcdir hfy-tags-sortl
))
1966 (rmap-cache (assoc srcdir hfy-tags-rmap
))
1967 (no-comment (hfy-opt 'zap-comment-links
))
1968 (no-strings (hfy-opt 'zap-string-links
))
1972 (case-fold-search nil
))
1973 ;; extract the tag mapping hashes (fwd and rev) and the tag list:
1974 (if (and (setq cache-hash
(cadr cache-entry
))
1975 (setq tags-rmap
(cadr rmap-cache
))
1976 (setq tags-list
(cadr list-cache
)))
1983 (case-fold-search nil
)
1990 (tag-regex (hfy-word-regex TAG
))
1991 (tag-map (gethash TAG cache-hash
))
1992 (tag-files (mapcar #'car tag-map
)))
1993 ;; find instances of TAG and do what needs to be done:
1994 (goto-char (point-min))
1995 (while (search-forward TAG nil
'NOERROR
)
1996 (setq tmp-point
(point)
1997 maybe-start
(- (match-beginning 0) 1))
1998 (goto-char maybe-start
)
1999 (if (not (looking-at tag-regex
))
2001 (setq start
(match-beginning 1))
2002 (setq stop
(match-end 1))
2004 (and (or no-comment no-strings
) (hfy-face-at start
)))
2006 (setq face-at
(cadr (memq :inherit face-at
))))
2007 (if (or (text-property-any start
(1+ stop
) 'hfy-linkp t
)
2008 (and no-comment
(eq 'font-lock-comment-face face-at
))
2009 (and no-strings
(eq 'font-lock-string-face face-at
)))
2010 nil
;; already a link, NOOP
2012 ;; set a reverse map entry:
2013 (setq rmap-line
(line-number-at-pos)
2014 rmap-entry
(gethash TAG tags-rmap
)
2015 rnew-elt
(list file rmap-line start
)
2016 rmap-entry
(cons rnew-elt rmap-entry
)
2017 name
(format "%s.%d" TAG rmap-line
))
2018 (put-text-property start
(1+ start
) 'hfy-inst name
)
2019 (puthash TAG rmap-entry tags-rmap
)
2021 ;; mark the link. link to index if the tag has > 1 def
2022 ;; add the line number to the #name if it does not:
2023 (setq href
(hfy-href file tag-files TAG tag-map
))
2024 (put-text-property start
(1+ start
) 'hfy-link href
)
2025 (put-text-property stop
(1+ stop
) 'hfy-endl t
)
2026 (put-text-property start
(1+ stop
) 'hfy-linkp t
)))
2027 (goto-char tmp-point
)) ))
2031 "Return `shell-file-name', or \"/bin/sh\" if it is a non-bourne shell."
2032 (if (string-match "\\<bash\\>\\|\\<sh\\>\\|\\<dash\\>" shell-file-name
)
2034 (or hfy-shell-file-name
"/bin/sh")))
2036 ;; cache the #(tag => file line point) entries for files under srcdir
2037 ;; and cache the descending sorted list of tags in the relevant alist,
2038 ;; also keyed by srcdir:
2039 (defun hfy-load-tags-cache (srcdir)
2040 "Run `hfy-etags-cmd' on SRCDIR, then call `hfy-parse-tags-buffer'."
2041 ;;(message "hfy-load-tags-cache");;DBUG
2042 (let ((etags-buffer (get-buffer-create "*hfy-tags*"))
2043 (etags-command (format hfy-etags-cmd hfy-etags-bin
))
2044 (shell-file-name (hfy-shell)))
2046 (shell-command etags-command etags-buffer
)
2047 (hfy-parse-tags-buffer srcdir etags-buffer
)) )
2049 ;; break this out from `hfy-load-tags-cache' to make the tar file
2050 ;; functionality easier to implement.
2051 ;; ( tar file functionality not merged here because it requires a
2052 ;; hacked copy of etags capable of tagging stdin: if Francesco
2053 ;; Potorti accepts a patch, or otherwise implements stdin tagging,
2054 ;; then I will provide a `htmlfontify-tar-file' defun )
2055 (defun hfy-parse-tags-buffer (srcdir buffer
)
2056 "Parse a BUFFER containing etags formatted output, loading the
2057 `hfy-tags-cache' and `hfy-tags-sortl' entries for SRCDIR."
2058 (let ((cache-entry (assoc srcdir hfy-tags-cache
))
2059 (tlist-cache (assoc srcdir hfy-tags-sortl
))
2060 (trmap-cache (assoc srcdir hfy-tags-rmap
))
2061 (cache-hash nil
) (trmap-hash nil
) (tags-list nil
)
2062 (hash-entry nil
) (tag-string nil
) (tag-line nil
)
2063 (tag-point nil
) (new-entry nil
) (etags-file nil
))
2065 ;; (re)initialise the tag reverse map:
2066 (if trmap-cache
(setq trmap-hash
(cadr trmap-cache
))
2067 (setq trmap-hash
(make-hash-table :test
'equal
))
2068 (setq hfy-tags-rmap
(list (list srcdir trmap-hash
) hfy-tags-rmap
)))
2069 (clrhash trmap-hash
)
2071 ;; (re)initialise the tag cache:
2072 (if cache-entry
(setq cache-hash
(cadr cache-entry
))
2073 (setq cache-hash
(make-hash-table :test
'equal
))
2074 (setq hfy-tags-cache
(list (list srcdir cache-hash
) hfy-tags-cache
)))
2075 (clrhash cache-hash
)
2077 ;; cache the TAG => ((file line point) (file line point) ... ) entries:
2078 (with-current-buffer buffer
2079 (goto-char (point-min))
2081 (while (and (looking-at "^\x0c") (= 0 (forward-line 1)))
2082 ;;(message "^L boundary")
2083 (if (and (looking-at "^\\(.+\\),\\([0-9]+\\)$")
2084 (= 0 (forward-line 1)))
2086 (setq etags-file
(match-string 1))
2087 ;;(message "TAGS for file: %s" etags-file)
2088 (while (and (looking-at hfy-etag-regex
) (= 0 (forward-line 1)))
2089 (setq tag-string
(match-string 1))
2090 (if (= 0 (length tag-string
)) nil
;; noop
2091 (setq tag-line
(round (string-to-number (match-string 2))))
2092 (setq tag-point
(round (string-to-number (match-string 3))))
2093 (setq hash-entry
(gethash tag-string cache-hash
))
2094 (setq new-entry
(list etags-file tag-line tag-point
))
2095 (push new-entry hash-entry
)
2096 ;;(message "HASH-ENTRY %s %S" tag-string new-entry)
2097 (puthash tag-string hash-entry cache-hash
)))) )))
2099 ;; cache a list of tags in descending length order:
2100 (maphash (lambda (K V
) (push K tags-list
)) cache-hash
)
2101 (setq tags-list
(sort tags-list
(lambda (A B
) (< (length B
) (length A
)))))
2103 ;; put the tag list into the cache:
2104 (if tlist-cache
(setcar (cdr tlist-cache
) tags-list
)
2105 (push (list srcdir tags-list
) hfy-tags-sortl
))
2107 ;; return the number of tags found:
2108 (length tags-list
) ))
2110 (defun hfy-prepare-index-i (srcdir dstdir filename
&optional stub map
)
2111 "Prepare a tags index buffer for SRCDIR.
2112 `hfy-tags-cache' must already have an entry for SRCDIR for this to work.
2113 `hfy-page-header', `hfy-page-footer', `hfy-link-extn' and `hfy-extn'
2114 all play a part here.\n
2115 If STUB is set, prepare an (appropriately named) index buffer
2116 specifically for entries beginning with STUB.\n
2117 If MAP is set, use that instead of `hfy-tags-cache'.
2118 FILENAME is the name of the file being indexed.
2119 DSTDIR is the output directory, where files will be written."
2120 ;;(message "hfy-write-index");;DBUG
2121 (let ((cache-entry (assoc srcdir
(or map hfy-tags-cache
)))
2125 (concat filename
(if stub
(concat "." stub
) "") hfy-extn
))
2127 (if (not (and cache-entry
2128 (setq cache-hash
(cadr cache-entry
))
2129 (setq index-buf
(get-buffer-create index-file
))))
2131 (maphash (lambda (K V
) (push K tag-list
)) cache-hash
)
2132 (setq tag-list
(sort tag-list
'string
<))
2133 (set-buffer index-buf
)
2135 (insert (funcall hfy-page-header filename
"<!-- CSS -->"))
2136 (insert "<table class=\"index\">\n")
2138 (dolist (TAG tag-list
)
2139 (let ((tag-started nil
))
2140 (dolist (DEF (gethash TAG cache-hash
))
2141 (if (and stub
(not (string-match (concat "^" stub
) TAG
)))
2142 nil
;; we have a stub and it didn't match: NOOP
2143 (let ((file (car DEF
))
2150 " <td><a href=\"%s%s\">%s</a></td> \n"
2151 " <td><a href=\"%s%s#%s.%d\">%d</a></td>\n"
2153 (if (string= TAG tag-started
) " "
2154 (format "<a name=\"%s\">%s</a>" TAG TAG
))
2155 file
(or hfy-link-extn hfy-extn
) file
2156 file
(or hfy-link-extn hfy-extn
) TAG line line
))
2157 (setq tag-started TAG
))))))
2158 (insert "</table>\n")
2159 (insert (funcall hfy-page-footer filename
))
2160 (and dstdir
(cd dstdir
))
2161 (set-visited-file-name index-file
)
2164 (defun hfy-prepare-index (srcdir dstdir
)
2165 "Return a list of index buffer(s), as determined by `hfy-split-index'.
2166 SRCDIR and DSTDIR are the source and output directories respectively."
2167 (if (not hfy-split-index
)
2168 (list (hfy-prepare-index-i srcdir dstdir hfy-index-file nil
))
2169 (let ((stub-list nil
)
2172 (cache-entry (assoc srcdir hfy-tags-cache
)))
2173 (if (and cache-entry
(setq cache-hash
(cadr cache-entry
)))
2176 (let ((stub (upcase (substring K
0 1))))
2177 (if (member stub stub-list
)
2178 nil
;; seen this already: NOOP
2180 stub-list
(cons stub stub-list
)
2181 index-list
(cons (hfy-prepare-index-i srcdir
2185 index-list
)) ))) cache-hash
) ) index-list
)))
2187 (defun hfy-prepare-tag-map (srcdir dstdir
)
2188 "Prepare the counterpart(s) to the index buffer(s) - a list of buffers
2189 with the same structure, but listing (and linking to) instances of tags
2190 \(as opposed to their definitions).\n
2191 SRCDIR and DSTDIR are the source and output directories respectively.
2192 See also `hfy-prepare-index', `hfy-split-index'."
2193 (if (not hfy-split-index
)
2194 (list (hfy-prepare-index-i srcdir
2199 (let ((stub-list nil
)
2202 (cache-entry (assoc srcdir hfy-tags-rmap
)))
2204 (if (and cache-entry
(setq cache-hash
(cadr cache-entry
)))
2207 (let ((stub (upcase (substring K
0 1))))
2208 (if (member stub stub-list
)
2209 nil
;; seen this already: NOOP
2211 stub-list
(cons stub stub-list
)
2212 index-list
(cons (hfy-prepare-index-i srcdir
2217 index-list
)) ))) cache-hash
) ) index-list
)))
2219 (defun hfy-subtract-maps (srcdir)
2220 "Internal function - strips definitions of tags from the instance map.
2221 SRCDIR is the directory being \"published\".
2222 See also `hfy-tags-cache', `hfy-tags-rmap'."
2223 (let ((new-list nil
)
2227 (fwd-map (cadr (assoc srcdir hfy-tags-cache
)))
2228 (rev-map (cadr (assoc srcdir hfy-tags-rmap
)))
2229 (taglist (cadr (assoc srcdir hfy-tags-sortl
))))
2230 (dolist (TAG taglist
)
2231 (setq def-list
(gethash TAG fwd-map
)
2232 old-list
(gethash TAG rev-map
)
2233 exc-list
(mapcar (lambda (P) (list (car P
) (cadr P
))) def-list
)
2235 (dolist (P old-list
)
2236 (or (member (list (car P
) (cadr P
)) exc-list
)
2238 (puthash TAG new-list rev-map
))))
2240 (defun htmlfontify-run-etags (srcdir)
2241 "Load the etags cache for SRCDIR.
2242 See also `hfy-load-tags-cache'."
2243 (interactive "D source directory: ")
2244 (setq srcdir
(directory-file-name srcdir
))
2245 (hfy-load-tags-cache srcdir
))
2247 ;;(defun hfy-test-read-args (foo bar)
2248 ;; (interactive "D source directory: \nD target directory: ")
2249 ;; (message "foo: %S\nbar: %S" foo bar))
2251 (defun hfy-save-kill-buffers (buffer-list &optional dstdir
)
2252 (dolist (B buffer-list
)
2254 (and dstdir
(file-directory-p dstdir
) (cd dstdir
))
2259 (defun htmlfontify-copy-and-link-dir (srcdir dstdir
&optional f-ext l-ext
)
2260 "Trawl SRCDIR and write fontified-and-hyperlinked output in DSTDIR.
2261 F-EXT and L-EXT specify values for `hfy-extn' and `hfy-link-extn'.\n
2262 You may also want to set `hfy-page-header' and `hfy-page-footer'."
2263 (interactive "D source directory: \nD output directory: ")
2264 ;;(message "htmlfontify-copy-and-link-dir")
2265 (setq srcdir
(directory-file-name srcdir
))
2266 (setq dstdir
(directory-file-name dstdir
))
2267 (let ((source-files "SETME: list of source files, relative to srcdir")
2268 (tr-cache (assoc srcdir hfy-tags-rmap
))
2269 (hfy-extn (or f-ext
".html"))
2270 (hfy-link-extn (or l-ext
".html")))
2271 ;; oops, forgot to load etags for srcdir:
2273 (message "autoload of tags cache")
2274 (hfy-load-tags-cache srcdir
)
2275 (setq tr-cache
(assoc srcdir hfy-tags-rmap
)))
2276 ;; clear out the old cache:
2277 (clrhash (cadr tr-cache
))
2278 (hfy-make-directory dstdir
)
2279 (setq source-files
(hfy-list-files srcdir
))
2280 (dolist (file source-files
)
2281 (hfy-copy-and-fontify-file srcdir dstdir file
))
2282 (hfy-subtract-maps srcdir
)
2283 (hfy-save-kill-buffers (hfy-prepare-index srcdir dstdir
) dstdir
)
2284 (hfy-save-kill-buffers (hfy-prepare-tag-map srcdir dstdir
) dstdir
) ))
2286 ;; name of the init file we want:
2287 (defun hfy-initfile ()
2288 "Return the expected location of the htmlfontify specific init/custom file."
2289 (let* ((file (or (getenv "HFY_INITFILE") ".hfy.el")))
2290 (expand-file-name file
"~") ))
2293 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2294 ;; incomplete as yet : transfer hook settings to hfy init file:
2295 ;; (defalias 'hfy-set-hooks 'custom-set-variables)
2297 ;; (defun hfy-pp-hook (H)
2298 ;; (and (string-match "-hook$" (symbol-name H))
2301 ;; (insert (format "\n '(%S %S)" H (symbol-value H)))
2305 ;; (defun hfy-save-hooks ()
2306 ;; (let ((custom-file (hfy-initfile)))
2307 ;; (custom-save-delete 'hfy-set-hooks)
2308 ;; (let ((standard-output (current-buffer)))
2309 ;; (princ "(hfy-set-hooks\n;;auto-generated, only one copy allowed\n")
2310 ;; (mapatoms 'hfy-pp-hook)
2315 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2317 (defalias 'hfy-init-progn
'progn
)
2319 (defun hfy-save-initvar (sym)
2320 (princ (format "(setq %s\n '" sym
))
2321 (pp (symbol-value sym
))
2324 (defun htmlfontify-save-initfile ()
2325 "Save the htmlfontify settings to the htmlfontify init file."
2327 (let* ((start-pos nil
)
2328 (custom-file (hfy-initfile))
2329 (standard-output (find-file-noselect custom-file
'nowarn
)))
2331 (custom-save-delete 'hfy-init-progn
)
2332 (setq start-pos
(point))
2333 (princ "(hfy-init-progn\n;;auto-generated, only one copy allowed\n")
2334 ;; FIXME: This saving&restoring of global customization
2335 ;; variables can interfere with other customization settings for
2336 ;; those vars (in .emacs or in Customize).
2337 (mapc 'hfy-save-initvar
2338 '(auto-mode-alist interpreter-mode-alist
))
2340 (indent-region start-pos
(point) nil
))
2341 (custom-save-all) ))
2343 (defun htmlfontify-load-initfile ()
2344 "Load the htmlfontify specific init/custom file."
2346 (let ((file (hfy-initfile)))
2347 (load file
'NOERROR nil nil
) ))
2350 ;;;### (autoloads (hfy-fallback-colour-values htmlfontify-load-rgb-file)
2351 ;;;;;; "hfy-cmap" "hfy-cmap.el" "3de2db2d213813bb3afe170ffd66cdde")
2352 ;;; Generated autoloads from hfy-cmap.el
2354 (autoload 'htmlfontify-load-rgb-file
"hfy-cmap" "\
2355 Load an X11 style rgb.txt FILE.
2356 Search `hfy-rgb-load-path' if FILE is not specified.
2357 Loads the variable `hfy-rgb-txt-colour-map', which is used by
2358 `hfy-fallback-colour-values'.
2360 \(fn &optional FILE)" t nil
)
2362 (autoload 'hfy-fallback-colour-values
"hfy-cmap" "\
2363 Use a fallback method for obtaining the rgb values for a color.
2365 \(fn COLOUR-STRING)" nil nil
)
2370 (provide 'htmlfontify
)
2372 ;; arch-tag: 944e5e63-c81d-4baa-a82a-0275f9c30e61
2373 ;;; htmlfontify.el ends here