-
Notifications
You must be signed in to change notification settings - Fork 0
/
htmlize.el
1770 lines (1621 loc) · 68.8 KB
/
htmlize.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;; htmlize.el -- Convert buffer text and decorations to HTML.
;; Copyright (C) 1997,1998,1999,2000,2001,2002,2003,2005,2006,2009 Hrvoje Niksic
;; Author: Hrvoje Niksic <[email protected]>
;; Keywords: hypermedia, extensions
;; Version: 1.37
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 2, or (at your option)
;; any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program; see the file COPYING. If not, write to the
;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
;; Boston, MA 02111-1307, USA.
;;; Commentary:
;; This package converts the buffer text and the associated
;; decorations to HTML. Mail to <[email protected]> to discuss
;; features and additions. All suggestions are more than welcome.
;; To use this, just switch to the buffer you want HTML-ized and type
;; `M-x htmlize-buffer'. You will be switched to a new buffer that
;; contains the resulting HTML code. You can edit and inspect this
;; buffer, or you can just save it with C-x C-w. `M-x htmlize-file'
;; will find a file, fontify it, and save the HTML version in
;; FILE.html, without any additional intervention. `M-x
;; htmlize-many-files' allows you to htmlize any number of files in
;; the same manner. `M-x htmlize-many-files-dired' does the same for
;; files marked in a dired buffer.
;; htmlize supports three types of HTML output, selected by setting
;; `htmlize-output-type': `css', `inline-css', and `font'. In `css'
;; mode, htmlize uses cascading style sheets to specify colors; it
;; generates classes that correspond to Emacs faces and uses <span
;; class=FACE>...</span> to color parts of text. In this mode, the
;; produced HTML is valid under the 4.01 strict DTD, as confirmed by
;; the W3C validator. `inline-css' is like `css', except the CSS is
;; put directly in the STYLE attribute of the SPAN element, making it
;; possible to paste the generated HTML to other documents. In `font'
;; mode, htmlize uses <font color="...">...</font> to colorize HTML,
;; which is not standard-compliant, but works better in older
;; browsers. `css' mode is the default.
;; You can also use htmlize from your Emacs Lisp code. When called
;; non-interactively, `htmlize-buffer' and `htmlize-region' will
;; return the resulting HTML buffer, but will not change current
;; buffer or move the point.
;; I tried to make the package elisp-compatible with multiple Emacsen,
;; specifically aiming for XEmacs 19.14+ and GNU Emacs 19.34+. Please
;; let me know if it doesn't work on some of those, and I'll try to
;; fix it. I relied heavily on the presence of CL extensions,
;; especially for cross-emacs compatibility; please don't try to
;; remove that particular dependency. When byte-compiling under GNU
;; Emacs, you're likely to get some warnings; just ignore them.
;; The latest version should be available at:
;;
;; <http://fly.srk.fer.hr/~hniksic/emacs/htmlize.el>
;;
;; You can find a sample of htmlize's output (possibly generated with
;; an older version) at:
;;
;; <http://fly.srk.fer.hr/~hniksic/emacs/htmlize.el.html>
;; Thanks go to the multitudes of people who have sent reports and
;; contributed comments, suggestions, and fixes. They include Ron
;; Gut, Bob Weiner, Toni Drabik, Peter Breton, Thomas Vogels, Juri
;; Linkov, Maciek Pasternacki, and many others.
;; User quotes: "You sir, are a sick, sick, _sick_ person. :)"
;; -- Bill Perry, author of Emacs/W3
;;; Code:
(require 'cl)
(eval-when-compile
(if (string-match "XEmacs" emacs-version)
(byte-compiler-options
(warnings (- unresolved))))
(defvar font-lock-auto-fontify)
(defvar font-lock-support-mode)
(defvar global-font-lock-mode)
(when (and (eq emacs-major-version 19)
(not (string-match "XEmacs" emacs-version)))
;; Older versions of GNU Emacs fail to autoload cl-extra even when
;; `cl' is loaded.
(load "cl-extra")))
(defconst htmlize-version "1.37")
;; Incantations to make custom stuff work without customize, e.g. on
;; XEmacs 19.14 or GNU Emacs 19.34.
(eval-and-compile
(condition-case ()
(require 'custom)
(error nil))
(if (and (featurep 'custom) (fboundp 'custom-declare-variable))
nil ; we've got what we needed
;; No custom or obsolete custom, define surrogates. Define all
;; three macros, so we don't hose another library that expects
;; e.g. `defface' to work after (fboundp 'defcustom) succeeds.
(defmacro defgroup (&rest ignored) nil)
(defmacro defcustom (var value doc &rest ignored)
`(defvar ,var ,value ,doc))
(defmacro defface (face value doc &rest stuff)
`(make-face ,face))))
(defgroup htmlize nil
"Convert buffer text and faces to HTML."
:group 'hypermedia)
(defcustom htmlize-head-tags ""
"*Additional tags to insert within HEAD of the generated document."
:type 'string
:group 'htmlize)
(defcustom htmlize-output-type 'css
"*Output type of generated HTML, one of `css', `inline-css', or `font'.
When set to `css' (the default), htmlize will generate a style sheet
with description of faces, and use it in the HTML document, specifying
the faces in the actual text with <span class=\"FACE\">.
When set to `inline-css', the style will be generated as above, but
placed directly in the STYLE attribute of the span ELEMENT: <span
style=\"STYLE\">. This makes it easier to paste the resulting HTML to
other documents.
When set to `font', the properties will be set using layout tags
<font>, <b>, <i>, <u>, and <strike>.
`css' output is normally preferred, but `font' is still useful for
supporting old, pre-CSS browsers, and both `inline-css' and `font' for
easier embedding of colorized text in foreign HTML documents (no style
sheet to carry around)."
:type '(choice (const css) (const inline-css) (const font))
:group 'htmlize)
(defcustom htmlize-generate-hyperlinks t
"*Non-nil means generate the hyperlinks for URLs and mail addresses.
This is on by default; set it to nil if you don't want htmlize to
insert hyperlinks in the resulting HTML. (In which case you can still
do your own hyperlinkification from htmlize-after-hook.)"
:type 'boolean
:group 'htmlize)
(defcustom htmlize-hyperlink-style "
a {
color: inherit;
background-color: inherit;
font: inherit;
text-decoration: inherit;
}
a:hover {
text-decoration: underline;
}
"
"*The CSS style used for hyperlinks when in CSS mode."
:type 'string
:group 'htmlize)
(defcustom htmlize-replace-form-feeds t
"*Non-nil means replace form feeds in source code with HTML separators.
Form feeds are the ^L characters at line beginnings that are sometimes
used to separate sections of source code. If this variable is set to
`t', form feed characters are replaced with the <hr> separator. If this
is a string, it specifies the replacement to use. Note that <pre> is
temporarily closed before the separator is inserted, so the default
replacement is effectively \"</pre><hr /><pre>\". If you specify
another replacement, don't forget to close and reopen the <pre> if you
want the output to remain valid HTML.
If you need more elaborate processing, set this to nil and use
htmlize-after-hook."
:type 'boolean
:group 'htmlize)
(defcustom htmlize-html-charset nil
"*The charset declared by the resulting HTML documents.
When non-nil, causes htmlize to insert the following in the HEAD section
of the generated HTML:
<meta http-equiv=\"Content-Type\" content=\"text/html; charset=CHARSET\">
where CHARSET is the value you've set for htmlize-html-charset. Valid
charsets are defined by MIME and include strings like \"iso-8859-1\",
\"iso-8859-15\", \"utf-8\", etc.
If you are using non-Latin-1 charsets, you might need to set this for
your documents to render correctly. Also, the W3C validator requires
submitted HTML documents to declare a charset. So if you care about
validation, you can use this to prevent the validator from bitching.
Needless to say, if you set this, you should actually make sure that
the buffer is in the encoding you're claiming it is in. (Under Mule
that is done by ensuring the correct \"file coding system\" for the
buffer.) If you don't understand what that means, this option is
probably not for you."
:type '(choice (const :tag "Unset" nil)
string)
:group 'htmlize)
(defcustom htmlize-convert-nonascii-to-entities (featurep 'mule)
"*Whether non-ASCII characters should be converted to HTML entities.
When this is non-nil, characters with codes in the 128-255 range will be
considered Latin 1 and rewritten as \"&#CODE;\". Characters with codes
above 255 will be converted to \"&#UCS;\", where UCS denotes the Unicode
code point of the character. If the code point cannot be determined,
the character will be copied unchanged, as would be the case if the
option were nil.
When the option is nil, the non-ASCII characters are copied to HTML
without modification. In that case, the web server and/or the browser
must be set to understand the encoding that was used when saving the
buffer. (You might also want to specify it by setting
`htmlize-html-charset'.)
Note that in an HTML entity \"&#CODE;\", CODE is always a UCS code point,
which has nothing to do with the charset the page is in. For example,
\"©\" *always* refers to the copyright symbol, regardless of charset
specified by the META tag or the charset sent by the HTTP server. In
other words, \"©\" is exactly equivalent to \"©\".
By default, entity conversion is turned on for Mule-enabled Emacsen and
turned off otherwise. This is because Mule knows the charset of
non-ASCII characters in the buffer. A non-Mule Emacs cannot tell
whether a character with code 0xA9 represents Latin 1 copyright symbol,
Latin 2 \"S with caron\", or something else altogether. Setting this to
t without Mule means asserting that 128-255 characters always mean Latin
1.
For most people htmlize will work fine with this option left at the
default setting; don't change it unless you know what you're doing."
:type 'sexp
:group 'htmlize)
(defcustom htmlize-ignore-face-size 'absolute
"*Whether face size should be ignored when generating HTML.
If this is nil, face sizes are used. If set to t, sizes are ignored
If set to `absolute', only absolute size specifications are ignored.
Please note that font sizes only work with CSS-based output types."
:type '(choice (const :tag "Don't ignore" nil)
(const :tag "Ignore all" t)
(const :tag "Ignore absolute" absolute))
:group 'htmlize)
(defcustom htmlize-css-name-prefix ""
"*The prefix used for CSS names.
The CSS names that htmlize generates from face names are often too
generic for CSS files; for example, `font-lock-type-face' is transformed
to `type'. Use this variable to add a prefix to the generated names.
The string \"htmlize-\" is an example of a reasonable prefix."
:type 'string
:group 'htmlize)
(defcustom htmlize-use-rgb-txt t
"*Whether `rgb.txt' should be used to convert color names to RGB.
This conversion means determining, for instance, that the color
\"IndianRed\" corresponds to the (205, 92, 92) RGB triple. `rgb.txt'
is the X color database that maps hundreds of color names to such RGB
triples. When this variable is non-nil, `htmlize' uses `rgb.txt' to
look up color names.
If this variable is nil, htmlize queries Emacs for RGB components of
colors using `color-instance-rgb-components' and `x-color-values'.
This can yield incorrect results on non-true-color displays.
If the `rgb.txt' file is not found (which will be the case if you're
running Emacs on non-X11 systems), this option is ignored."
:type 'boolean
:group 'htmlize)
(defcustom htmlize-html-major-mode nil
"The mode the newly created HTML buffer will be put in.
Set this to nil if you prefer the default (fundamental) mode."
:type '(radio (const :tag "No mode (fundamental)" nil)
(function-item html-mode)
(function :tag "User-defined major mode"))
:group 'htmlize)
(defvar htmlize-before-hook nil
"Hook run before htmlizing a buffer.
The hook functions are run in the source buffer (not the resulting HTML
buffer).")
(defvar htmlize-after-hook nil
"Hook run after htmlizing a buffer.
Unlike `htmlize-before-hook', these functions are run in the generated
HTML buffer. You may use them to modify the outlook of the final HTML
output.")
(defvar htmlize-file-hook nil
"Hook run by `htmlize-file' after htmlizing a file, but before saving it.")
(defvar htmlize-buffer-places)
;;; Some cross-Emacs compatibility.
;; I try to conditionalize on features rather than Emacs version, but
;; in some cases checking against the version *is* necessary.
(defconst htmlize-running-xemacs (string-match "XEmacs" emacs-version))
(eval-and-compile
;; save-current-buffer, with-current-buffer, and with-temp-buffer
;; are not available in 19.34 and in older XEmacsen. Strictly
;; speaking, we should stick to our own namespace and define and use
;; htmlize-save-current-buffer, etc. But non-standard special forms
;; are a pain because they're not properly fontified or indented and
;; because they look weird and ugly. So I'll just go ahead and
;; define the real ones if they're not available. If someone
;; convinces me that this breaks something, I'll switch to the
;; "htmlize-" namespace.
(unless (fboundp 'save-current-buffer)
(defmacro save-current-buffer (&rest forms)
`(let ((__scb_current (current-buffer)))
(unwind-protect
(progn ,@forms)
(set-buffer __scb_current)))))
(unless (fboundp 'with-current-buffer)
(defmacro with-current-buffer (buffer &rest forms)
`(save-current-buffer (set-buffer ,buffer) ,@forms)))
(unless (fboundp 'with-temp-buffer)
(defmacro with-temp-buffer (&rest forms)
(let ((temp-buffer (gensym "tb-")))
`(let ((,temp-buffer
(get-buffer-create (generate-new-buffer-name " *temp*"))))
(unwind-protect
(with-current-buffer ,temp-buffer
,@forms)
(and (buffer-live-p ,temp-buffer)
(kill-buffer ,temp-buffer))))))))
;; We need a function that efficiently finds the next change of a
;; property (usually `face'), preferably regardless of whether the
;; change occurred because of a text property or an extent/overlay.
;; As it turns out, it is not easy to do that compatibly.
;;
;; Under XEmacs, `next-single-property-change' does that. Under GNU
;; Emacs beginning with version 21, `next-single-char-property-change'
;; is available and does the same. GNU Emacs 20 had
;; `next-char-property-change', which we can use. GNU Emacs 19 didn't
;; provide any means for simultaneously examining overlays and text
;; properties, so when using Emacs 19.34, we punt and fall back to
;; `next-single-property-change', thus ignoring overlays altogether.
(cond
(htmlize-running-xemacs
;; XEmacs: good.
(defun htmlize-next-change (pos prop &optional limit)
(next-single-property-change pos prop nil (or limit (point-max)))))
((fboundp 'next-single-char-property-change)
;; GNU Emacs 21: good.
(defun htmlize-next-change (pos prop &optional limit)
(next-single-char-property-change pos prop nil limit)))
((fboundp 'next-char-property-change)
;; GNU Emacs 20: bad, but fixable.
(defun htmlize-next-change (pos prop &optional limit)
(let ((done nil)
(current-value (get-char-property pos prop))
newpos next-value)
;; Loop over positions returned by next-char-property-change
;; until the value of PROP changes or we've hit EOB.
(while (not done)
(setq newpos (next-char-property-change pos limit)
next-value (get-char-property newpos prop))
(cond ((eq newpos pos)
;; Possibly at EOB? Whatever, just don't infloop.
(setq done t))
((eq next-value current-value)
;; PROP hasn't changed -- keep looping.
)
(t
(setq done t)))
(setq pos newpos))
pos)))
(t
;; GNU Emacs 19.34: hopeless, cannot properly support overlays.
(defun htmlize-next-change (pos prop &optional limit)
(unless limit
(setq limit (point-max)))
(let ((res (next-single-property-change pos prop)))
(if (or (null res)
(> res limit))
limit
res)))))
;;; Transformation of buffer text: HTML escapes, untabification, etc.
(defvar htmlize-basic-character-table
;; Map characters in the 0-127 range to either one-character strings
;; or to numeric entities.
(let ((table (make-vector 128 ?\0)))
;; Map characters in the 32-126 range to themselves, others to
;; &#CODE entities;
(dotimes (i 128)
(setf (aref table i) (if (and (>= i 32) (<= i 126))
(char-to-string i)
(format "&#%d;" i))))
;; Set exceptions manually.
(setf
;; Don't escape newline, carriage return, and TAB.
(aref table ?\n) "\n"
(aref table ?\r) "\r"
(aref table ?\t) "\t"
;; Escape &, <, and >.
(aref table ?&) "&"
(aref table ?<) "<"
(aref table ?>) ">"
;; Not escaping '"' buys us a measurable speedup. It's only
;; necessary to quote it for strings used in attribute values,
;; which htmlize doesn't do.
;(aref table ?\") """
)
table))
;; A cache of HTML representation of non-ASCII characters. Depending
;; on availability of `encode-char' and the setting of
;; `htmlize-convert-nonascii-to-entities', this maps non-ASCII
;; characters to either "&#<code>;" or "<char>" (mapconcat's mapper
;; must always return strings). It's only filled as characters are
;; encountered, so that in a buffer with e.g. French text, it will
;; only ever contain French accented characters as keys. It's cleared
;; on each entry to htmlize-buffer-1 to allow modifications of
;; `htmlize-convert-nonascii-to-entities' to take effect.
(defvar htmlize-extended-character-cache (make-hash-table :test 'eq))
(defun htmlize-protect-string (string)
"HTML-protect string, escaping HTML metacharacters and I18N chars."
;; Only protecting strings that actually contain unsafe or non-ASCII
;; chars removes a lot of unnecessary funcalls and consing.
(if (not (string-match "[^\r\n\t -%'-;=?-~]" string))
string
(mapconcat (lambda (char)
(cond
((< char 128)
;; ASCII: use htmlize-basic-character-table.
(aref htmlize-basic-character-table char))
((gethash char htmlize-extended-character-cache)
;; We've already seen this char; return the cached
;; string.
)
((not htmlize-convert-nonascii-to-entities)
;; If conversion to entities is not desired, always
;; copy the char literally.
(setf (gethash char htmlize-extended-character-cache)
(char-to-string char)))
((< char 256)
;; Latin 1: no need to call encode-char.
(setf (gethash char htmlize-extended-character-cache)
(format "&#%d;" char)))
((and (fboundp 'encode-char)
;; Must check if encode-char works for CHAR;
;; it fails for Arabic and possibly elsewhere.
(encode-char char 'ucs))
(setf (gethash char htmlize-extended-character-cache)
(format "&#%d;" (encode-char char 'ucs))))
(t
;; encode-char doesn't work for this char. Copy it
;; unchanged and hope for the best.
(setf (gethash char htmlize-extended-character-cache)
(char-to-string char)))))
string "")))
(defconst htmlize-ellipsis "...")
(put-text-property 0 (length htmlize-ellipsis) 'htmlize-ellipsis t htmlize-ellipsis)
(defun htmlize-buffer-substring-no-invisible (beg end)
;; Like buffer-substring-no-properties, but don't copy invisible
;; parts of the region. Where buffer-substring-no-properties
;; mandates an ellipsis to be shown, htmlize-ellipsis is inserted.
(let ((pos beg)
visible-list invisible show next-change)
;; Iterate over the changes in the `invisible' property and filter
;; out the portions where it's non-nil, i.e. where the text is
;; invisible.
(while (< pos end)
(setq invisible (get-char-property pos 'invisible)
next-change (htmlize-next-change pos 'invisible end))
(if (not (listp buffer-invisibility-spec))
;; If buffer-invisibility-spec is not a list, then all
;; characters with non-nil `invisible' property are visible.
(setq show (not invisible))
;; Otherwise, the value of a non-nil `invisible' property can be:
;; 1. a symbol -- make the text invisible if it matches
;; buffer-invisibility-spec.
;; 2. a list of symbols -- make the text invisible if
;; any symbol in the list matches
;; buffer-invisibility-spec.
;; If the match of buffer-invisibility-spec has a non-nil
;; CDR, replace the invisible text with an ellipsis.
(let (match)
(if (symbolp invisible)
(setq match (member* invisible buffer-invisibility-spec
:key (lambda (i)
(if (symbolp i) i (car i)))))
(setq match (block nil
(dolist (elem invisible)
(let ((m (member*
elem buffer-invisibility-spec
:key (lambda (i)
(if (symbolp i) i (car i))))))
(when m (return m))))
nil)))
(setq show (cond ((null match) t)
((and (cdr-safe (car match))
;; Conflate successive ellipses.
(not (eq show htmlize-ellipsis)))
htmlize-ellipsis)
(t nil)))))
(cond ((eq show t)
(push (buffer-substring-no-properties pos next-change) visible-list))
((stringp show)
(push show visible-list)))
(setq pos next-change))
(if (= (length visible-list) 1)
;; If VISIBLE-LIST consists of only one element, return it
;; without concatenation. This avoids additional consing in
;; regions without any invisible text.
(car visible-list)
(apply #'concat (nreverse visible-list)))))
(defun htmlize-trim-ellipsis (text)
;; Remove htmlize-ellipses ("...") from the beginning of TEXT if it
;; starts with it. It checks for the special property of the
;; ellipsis so it doesn't work on ordinary text that begins with
;; "...".
(if (get-text-property 0 'htmlize-ellipsis text)
(substring text (length htmlize-ellipsis))
text))
(defconst htmlize-tab-spaces
;; A table of strings with spaces. (aref htmlize-tab-spaces 5) is
;; like (make-string 5 ?\ ), except it doesn't cons.
(let ((v (make-vector 32 nil)))
(dotimes (i (length v))
(setf (aref v i) (make-string i ?\ )))
v))
(defun htmlize-untabify (text start-column)
"Untabify TEXT, assuming it starts at START-COLUMN."
(let ((column start-column)
(last-match 0)
(chunk-start 0)
chunks match-pos tab-size)
(while (string-match "[\t\n]" text last-match)
(setq match-pos (match-beginning 0))
(cond ((eq (aref text match-pos) ?\t)
;; Encountered a tab: create a chunk of text followed by
;; the expanded tab.
(push (substring text chunk-start match-pos) chunks)
;; Increase COLUMN by the length of the text we've
;; skipped since last tab or newline. (Encountering
;; newline resets it.)
(incf column (- match-pos last-match))
;; Calculate tab size based on tab-width and COLUMN.
(setq tab-size (- tab-width (% column tab-width)))
;; Expand the tab.
(push (aref htmlize-tab-spaces tab-size) chunks)
(incf column tab-size)
(setq chunk-start (1+ match-pos)))
(t
;; Reset COLUMN at beginning of line.
(setq column 0)))
(setq last-match (1+ match-pos)))
;; If no chunks have been allocated, it means there have been no
;; tabs to expand. Return TEXT unmodified.
(if (null chunks)
text
(when (< chunk-start (length text))
;; Push the remaining chunk.
(push (substring text chunk-start) chunks))
;; Generate the output from the available chunks.
(apply #'concat (nreverse chunks)))))
(defun htmlize-despam-address (string)
"Replace every occurrence of '@' in STRING with @.
`htmlize-make-hyperlinks' uses this to spam-protect mailto links
without modifying their meaning."
;; Suggested by Ville Skytta.
(while (string-match "@" string)
(setq string (replace-match "@" nil t string)))
string)
(defun htmlize-make-hyperlinks ()
"Make hyperlinks in HTML."
;; Function originally submitted by Ville Skytta. Rewritten by
;; Hrvoje Niksic, then modified by Ville Skytta and Hrvoje Niksic.
(goto-char (point-min))
(while (re-search-forward
"<\\(\\(mailto:\\)?\\([-=+_.a-zA-Z0-9]+@[-_.a-zA-Z0-9]+\\)\\)>"
nil t)
(let ((address (match-string 3))
(link-text (match-string 1)))
(delete-region (match-beginning 0) (match-end 0))
(insert "<<a href=\"mailto:"
(htmlize-despam-address address)
"\">"
(htmlize-despam-address link-text)
"</a>>")))
(goto-char (point-min))
(while (re-search-forward "<\\(\\(URL:\\)?\\([a-zA-Z]+://[^;]+\\)\\)>"
nil t)
(let ((url (match-string 3))
(link-text (match-string 1)))
(delete-region (match-beginning 0) (match-end 0))
(insert "<<a href=\"" url "\">" link-text "</a>>"))))
;; Tests for htmlize-make-hyperlinks:
;; <mailto:[email protected]>
;; <http://fly.srk.fer.hr>
;; <URL:http://www.xemacs.org>
;; <http://www.mail-archive.com/[email protected]/>
;; <[email protected]>
;; <xalan-dev-sc.10148567319.hacuhiucknfgmpfnjcpg-john=doe.com@xml.apache.org>
(defun htmlize-defang-local-variables ()
;; Juri Linkov reports that an HTML-ized "Local variables" can lead
;; visiting the HTML to fail with "Local variables list is not
;; properly terminated". He suggested changing the phrase to
;; syntactically equivalent HTML that Emacs doesn't recognize.
(goto-char (point-min))
(while (search-forward "Local Variables:" nil t)
(replace-match "Local Variables:" nil t)))
;;; Color handling.
(if (fboundp 'locate-file)
(defalias 'htmlize-locate-file 'locate-file)
(defun htmlize-locate-file (file path)
(dolist (dir path nil)
(when (file-exists-p (expand-file-name file dir))
(return (expand-file-name file dir))))))
(defvar htmlize-x-library-search-path
'("/usr/X11R6/lib/X11/"
"/usr/X11R5/lib/X11/"
"/usr/lib/X11R6/X11/"
"/usr/lib/X11R5/X11/"
"/usr/local/X11R6/lib/X11/"
"/usr/local/X11R5/lib/X11/"
"/usr/local/lib/X11R6/X11/"
"/usr/local/lib/X11R5/X11/"
"/usr/X11/lib/X11/"
"/usr/lib/X11/"
"/usr/local/lib/X11/"
"/usr/X386/lib/X11/"
"/usr/x386/lib/X11/"
"/usr/XFree86/lib/X11/"
"/usr/unsupported/lib/X11/"
"/usr/athena/lib/X11/"
"/usr/local/x11r5/lib/X11/"
"/usr/lpp/Xamples/lib/X11/"
"/usr/openwin/lib/X11/"
"/usr/openwin/share/lib/X11/"))
(defun htmlize-get-color-rgb-hash (&optional rgb-file)
"Return a hash table mapping X color names to RGB values.
The keys in the hash table are X11 color names, and the values are the
#rrggbb RGB specifications, extracted from `rgb.txt'.
If RGB-FILE is nil, the function will try hard to find a suitable file
in the system directories.
If no rgb.txt file is found, return nil."
(let ((rgb-file (or rgb-file (htmlize-locate-file
"rgb.txt"
htmlize-x-library-search-path)))
(hash nil))
(when rgb-file
(with-temp-buffer
(insert-file-contents rgb-file)
(setq hash (make-hash-table :test 'equal))
(while (not (eobp))
(cond ((looking-at "^\\s-*\\([!#]\\|$\\)")
;; Skip comments and empty lines.
)
((looking-at
"[ \t]*\\([0-9]+\\)[ \t]+\\([0-9]+\\)[ \t]+\\([0-9]+\\)[ \t]+\\(.*\\)")
(setf (gethash (downcase (match-string 4)) hash)
(format "#%02x%02x%02x"
(string-to-number (match-string 1))
(string-to-number (match-string 2))
(string-to-number (match-string 3)))))
(t
(error
"Unrecognized line in %s: %s"
rgb-file
(buffer-substring (point) (progn (end-of-line) (point))))))
(forward-line 1))))
hash))
;; Compile the RGB map when loaded. On systems where rgb.txt is
;; missing, the value of the variable will be nil, and rgb.txt will
;; not be used.
(defvar htmlize-color-rgb-hash (htmlize-get-color-rgb-hash))
;;; Face handling.
(defun htmlize-face-specifies-property (face prop)
;; Return t if face specifies PROP, as opposed to it being inherited
;; from the default face. The problem with e.g.
;; `face-foreground-instance' is that it returns an instance for
;; EVERY face because every face inherits from the default face.
;; However, we'd like htmlize-face-{fore,back}ground to return nil
;; when called with a face that doesn't specify its own foreground
;; or background.
(or (eq face 'default)
(assq 'global (specifier-spec-list (face-property face prop)))))
(defun htmlize-face-color-internal (face fg)
;; Used only under GNU Emacs. Return the color of FACE, but don't
;; return "unspecified-fg" or "unspecified-bg". If the face is
;; `default' and the color is unspecified, look up the color in
;; frame parameters.
(let* ((function (if fg #'face-foreground #'face-background))
color)
(if (>= emacs-major-version 22)
;; For GNU Emacs 22+ set INHERIT to get the inherited values.
(setq color (funcall function face nil t))
(setq color (funcall function face))
;; For GNU Emacs 21 (which has `face-attribute'): if the color
;; is nil, recursively check for the face's parent.
(when (and (null color)
(fboundp 'face-attribute)
(face-attribute face :inherit)
(not (eq (face-attribute face :inherit) 'unspecified)))
(setq color (htmlize-face-color-internal
(face-attribute face :inherit) fg))))
(when (and (eq face 'default) (null color))
(setq color (cdr (assq (if fg 'foreground-color 'background-color)
(frame-parameters)))))
(when (or (eq color 'unspecified)
(equal color "unspecified-fg")
(equal color "unspecified-bg"))
(setq color nil))
(when (and (eq face 'default)
(null color))
;; Assuming black on white doesn't seem right, but I can't think
;; of anything better to do.
(setq color (if fg "black" "white")))
color))
(defun htmlize-face-foreground (face)
;; Return the name of the foreground color of FACE. If FACE does
;; not specify a foreground color, return nil.
(cond (htmlize-running-xemacs
;; XEmacs.
(and (htmlize-face-specifies-property face 'foreground)
(color-instance-name (face-foreground-instance face))))
(t
;; GNU Emacs.
(htmlize-face-color-internal face t))))
(defun htmlize-face-background (face)
;; Return the name of the background color of FACE. If FACE does
;; not specify a background color, return nil.
(cond (htmlize-running-xemacs
;; XEmacs.
(and (htmlize-face-specifies-property face 'background)
(color-instance-name (face-background-instance face))))
(t
;; GNU Emacs.
(htmlize-face-color-internal face nil))))
;; Convert COLOR to the #RRGGBB string. If COLOR is already in that
;; format, it's left unchanged.
(defun htmlize-color-to-rgb (color)
(let ((rgb-string nil))
(cond ((null color)
;; Ignore nil COLOR because it means that the face is not
;; specifying any color. Hence (htmlize-color-to-rgb nil)
;; returns nil.
)
((string-match "\\`#" color)
;; The color is already in #rrggbb format.
(setq rgb-string color))
((and htmlize-use-rgb-txt
htmlize-color-rgb-hash)
;; Use of rgb.txt is requested, and it's available on the
;; system. Use it.
(setq rgb-string (gethash (downcase color) htmlize-color-rgb-hash)))
(t
;; We're getting the RGB components from Emacs.
(let ((rgb
;; Here I cannot conditionalize on (fboundp ...)
;; because ps-print under some versions of GNU Emacs
;; defines its own dummy version of
;; `color-instance-rgb-components'.
(if htmlize-running-xemacs
(mapcar (lambda (arg)
(/ arg 256))
(color-instance-rgb-components
(make-color-instance color)))
(mapcar (lambda (arg)
(/ arg 256))
(x-color-values color)))))
(when rgb
(setq rgb-string (apply #'format "#%02x%02x%02x" rgb))))))
;; If RGB-STRING is still nil, it means the color cannot be found,
;; for whatever reason. In that case just punt and return COLOR.
;; Most browsers support a decent set of color names anyway.
(or rgb-string color)))
;; We store the face properties we care about into an
;; `htmlize-fstruct' type. That way we only have to analyze face
;; properties, which can be time consuming, once per each face. The
;; mapping between Emacs faces and htmlize-fstructs is established by
;; htmlize-make-face-map. The name "fstruct" refers to variables of
;; type `htmlize-fstruct', while the term "face" is reserved for Emacs
;; faces.
(defstruct htmlize-fstruct
foreground ; foreground color, #rrggbb
background ; background color, #rrggbb
size ; size
boldp ; whether face is bold
italicp ; whether face is italic
underlinep ; whether face is underlined
overlinep ; whether face is overlined
strikep ; whether face is struck through
css-name ; CSS name of face
)
(defun htmlize-face-emacs21-attr (fstruct attr value)
;; For ATTR and VALUE, set the equivalent value in FSTRUCT.
(case attr
(:foreground
(setf (htmlize-fstruct-foreground fstruct) (htmlize-color-to-rgb value)))
(:background
(setf (htmlize-fstruct-background fstruct) (htmlize-color-to-rgb value)))
(:height
(setf (htmlize-fstruct-size fstruct) value))
(:weight
(when (string-match (symbol-name value) "bold")
(setf (htmlize-fstruct-boldp fstruct) t)))
(:slant
(setf (htmlize-fstruct-italicp fstruct) (or (eq value 'italic)
(eq value 'oblique))))
(:bold
(setf (htmlize-fstruct-boldp fstruct) value))
(:italic
(setf (htmlize-fstruct-italicp fstruct) value))
(:underline
(setf (htmlize-fstruct-underlinep fstruct) value))
(:overline
(setf (htmlize-fstruct-overlinep fstruct) value))
(:strike-through
(setf (htmlize-fstruct-strikep fstruct) value))))
(defun htmlize-face-size (face)
;; The size (height) of FACE, taking inheritance into account.
;; Only works in Emacs 21 and later.
(let ((size-list
(loop
for f = face then (face-attribute f :inherit)
until (or (not f) (eq f 'unspecified))
for h = (face-attribute f :height)
collect (if (eq h 'unspecified) nil h))))
(reduce 'htmlize-merge-size (cons nil size-list))))
(defun htmlize-face-to-fstruct (face)
"Convert Emacs face FACE to fstruct."
(let ((fstruct (make-htmlize-fstruct
:foreground (htmlize-color-to-rgb
(htmlize-face-foreground face))
:background (htmlize-color-to-rgb
(htmlize-face-background face)))))
(cond (htmlize-running-xemacs
;; XEmacs doesn't provide a way to detect whether a face is
;; bold or italic, so we need to examine the font instance.
;; #### This probably doesn't work under MS Windows and/or
;; GTK devices. I'll need help with those.
(let* ((font-instance (face-font-instance face))
(props (font-instance-properties font-instance)))
(when (equalp (cdr (assq 'WEIGHT_NAME props)) "bold")
(setf (htmlize-fstruct-boldp fstruct) t))
(when (or (equalp (cdr (assq 'SLANT props)) "i")
(equalp (cdr (assq 'SLANT props)) "o"))
(setf (htmlize-fstruct-italicp fstruct) t))
(setf (htmlize-fstruct-strikep fstruct)
(face-strikethru-p face))
(setf (htmlize-fstruct-underlinep fstruct)
(face-underline-p face))))
((fboundp 'face-attribute)
;; GNU Emacs 21 and further.
(dolist (attr '(:weight :slant :underline :overline :strike-through))
(let ((value (if (>= emacs-major-version 22)
;; Use the INHERIT arg in GNU Emacs 22.
(face-attribute face attr nil t)
;; Otherwise, fake it.
(let ((face face))
(while (and (eq (face-attribute face attr)
'unspecified)
(not (eq (face-attribute face :inherit)
'unspecified)))
(setq face (face-attribute face :inherit)))
(face-attribute face attr)))))
(when (and value (not (eq value 'unspecified)))
(htmlize-face-emacs21-attr fstruct attr value))))
(let ((size (htmlize-face-size face)))
(unless (eql size 1.0) ; ignore non-spec
(setf (htmlize-fstruct-size fstruct) size))))
(t
;; Older GNU Emacs. Some of these functions are only
;; available under Emacs 20+, hence the guards.
(when (fboundp 'face-bold-p)
(setf (htmlize-fstruct-boldp fstruct) (face-bold-p face)))
(when (fboundp 'face-italic-p)
(setf (htmlize-fstruct-italicp fstruct) (face-italic-p face)))
(setf (htmlize-fstruct-underlinep fstruct)
(face-underline-p face))))
;; Generate the css-name property. Emacs places no restrictions
;; on the names of symbols that represent faces -- any characters
;; may be in the name, even ^@. We try hard to beat the face name
;; into shape, both esthetically and according to CSS1 specs.
(setf (htmlize-fstruct-css-name fstruct)
(let ((name (downcase (symbol-name face))))
(when (string-match "\\`font-lock-" name)
;; Change font-lock-FOO-face to FOO.
(setq name (replace-match "" t t name)))
(when (string-match "-face\\'" name)
;; Drop the redundant "-face" suffix.
(setq name (replace-match "" t t name)))
(while (string-match "[^-a-zA-Z0-9]" name)
;; Drop the non-alphanumerics.
(setq name (replace-match "X" t t name)))
(when (string-match "\\`[-0-9]" name)
;; CSS identifiers may not start with a digit.
(setq name (concat "X" name)))
;; After these transformations, the face could come
;; out empty.
(when (equal name "")
(setq name "face"))
;; Apply the prefix.
(setq name (concat htmlize-css-name-prefix name))
name))
fstruct))
(defmacro htmlize-copy-attr-if-set (attr-list dest source)
;; Expand the code of the type
;; (and (htmlize-fstruct-ATTR source)
;; (setf (htmlize-fstruct-ATTR dest) (htmlize-fstruct-ATTR source)))
;; for the given list of boolean attributes.
(cons 'progn
(loop for attr in attr-list
for attr-sym = (intern (format "htmlize-fstruct-%s" attr))
collect `(and (,attr-sym ,source)
(setf (,attr-sym ,dest) (,attr-sym ,source))))))
(defun htmlize-merge-size (merged next)
;; Calculate the size of the merge of MERGED and NEXT.
(cond ((null merged) next)
((integerp next) next)
((null next) merged)
((floatp merged) (* merged next))
((integerp merged) (round (* merged next)))))
(defun htmlize-merge-two-faces (merged next)
(htmlize-copy-attr-if-set
(foreground background boldp italicp underlinep overlinep strikep)
merged next)
(setf (htmlize-fstruct-size merged)
(htmlize-merge-size (htmlize-fstruct-size merged)
(htmlize-fstruct-size next)))
merged)
(defun htmlize-merge-faces (fstruct-list)
(cond ((null fstruct-list)
;; Nothing to do, return a dummy face.
(make-htmlize-fstruct))
((null (cdr fstruct-list))
;; Optimize for the common case of a single face, simply
;; return it.
(car fstruct-list))
(t
(reduce #'htmlize-merge-two-faces
(cons (make-htmlize-fstruct) fstruct-list)))))
;; GNU Emacs 20+ supports attribute lists in `face' properties. For
;; example, you can use `(:foreground "red" :weight bold)' as an
;; overlay's "face", or you can even use a list of such lists, etc.
;; We call those "attrlists".
;;
;; htmlize supports attrlist by converting them to fstructs, the same
;; as with regular faces.