-
Notifications
You must be signed in to change notification settings - Fork 43
/
htmlize.el.html
2032 lines (1899 loc) · 154 KB
/
htmlize.el.html
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
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN">
<!-- Created by htmlize-1.45 in css mode. -->
<html>
<head>
<title>htmlize.el</title>
<style type="text/css">
<!--
body {
color: #839496;
background-color: #002b36;
}
.builtin {
/* font-lock-builtin-face */
color: #859900;
}
.comment {
/* font-lock-comment-face */
color: #586e75;
font-style: italic;
}
.comment-delimiter {
/* font-lock-comment-delimiter-face */
color: #586e75;
font-style: italic;
}
.constant {
/* font-lock-constant-face */
color: #2aa198;
}
.doc {
/* font-lock-doc-face */
color: #586e75;
font-style: italic;
}
.function-name {
/* font-lock-function-name-face */
color: #268bd2;
}
.keyword {
/* font-lock-keyword-face */
color: #859900;
}
.negation-char {
/* font-lock-negation-char-face */
color: #dc322f;
}
.regexp-grouping-backslash {
/* font-lock-regexp-grouping-backslash */
color: #b58900;
}
.regexp-grouping-construct {
/* font-lock-regexp-grouping-construct */
color: #cb4b16;
}
.string {
/* font-lock-string-face */
color: #2aa198;
}
.type {
/* font-lock-type-face */
color: #b58900;
}
.variable-name {
/* font-lock-variable-name-face */
color: #268bd2;
}
.warning {
/* font-lock-warning-face */
color: #dc322f;
font-weight: bold;
}
a {
color: inherit;
background-color: inherit;
font: inherit;
text-decoration: inherit;
}
a:hover {
text-decoration: underline;
}
-->
</style>
</head>
<body>
<pre>
<span class="comment-delimiter">;;; </span><span class="comment">htmlize.el --- Convert buffer text and decorations to HTML.
</span>
<span class="comment-delimiter">;; </span><span class="comment">Copyright (C) 1997-2003,2005,2006,2009,2011,2012 Hrvoje Niksic
</span>
<span class="comment-delimiter">;; </span><span class="comment">Author: Hrvoje Niksic <a href="mailto:hniksic%40xemacs.org"><[email protected]></a>
</span><span class="comment-delimiter">;; </span><span class="comment">Keywords: hypermedia, extensions
</span><span class="comment-delimiter">;; </span><span class="comment">Version: 1.45
</span>
<span class="comment-delimiter">;; </span><span class="comment">This program is free software; you can redistribute it and/or modify
</span><span class="comment-delimiter">;; </span><span class="comment">it under the terms of the GNU General Public License as published by
</span><span class="comment-delimiter">;; </span><span class="comment">the Free Software Foundation; either version 2, or (at your option)
</span><span class="comment-delimiter">;; </span><span class="comment">any later version.
</span>
<span class="comment-delimiter">;; </span><span class="comment">This program is distributed in the hope that it will be useful,
</span><span class="comment-delimiter">;; </span><span class="comment">but WITHOUT ANY WARRANTY; without even the implied warranty of
</span><span class="comment-delimiter">;; </span><span class="comment">MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
</span><span class="comment-delimiter">;; </span><span class="comment">GNU General Public License for more details.
</span>
<span class="comment-delimiter">;; </span><span class="comment">You should have received a copy of the GNU General Public License
</span><span class="comment-delimiter">;; </span><span class="comment">along with this program; see the file COPYING. If not, write to the
</span><span class="comment-delimiter">;; </span><span class="comment">Free Software Foundation, Inc., 59 Temple Place - Suite 330,
</span><span class="comment-delimiter">;; </span><span class="comment">Boston, MA 02111-1307, USA.
</span>
<span class="comment-delimiter">;;; </span><span class="comment">Commentary:
</span>
<span class="comment-delimiter">;; </span><span class="comment">This package converts the buffer text and the associated
</span><span class="comment-delimiter">;; </span><span class="comment">decorations to HTML. Mail to <a href="mailto:hniksic%40xemacs.org"><[email protected]></a> to discuss
</span><span class="comment-delimiter">;; </span><span class="comment">features and additions. All suggestions are more than welcome.
</span>
<span class="comment-delimiter">;; </span><span class="comment">To use it, just switch to the buffer you want HTML-ized and type
</span><span class="comment-delimiter">;; </span><span class="comment">`M-x htmlize-buffer'. You will be switched to a new buffer that
</span><span class="comment-delimiter">;; </span><span class="comment">contains the resulting HTML code. You can edit and inspect this
</span><span class="comment-delimiter">;; </span><span class="comment">buffer, or you can just save it with C-x C-w. `M-x htmlize-file'
</span><span class="comment-delimiter">;; </span><span class="comment">will find a file, fontify it, and save the HTML version in
</span><span class="comment-delimiter">;; </span><span class="comment">FILE.html, without any additional intervention. `M-x
</span><span class="comment-delimiter">;; </span><span class="comment">htmlize-many-files' allows you to htmlize any number of files in
</span><span class="comment-delimiter">;; </span><span class="comment">the same manner. `M-x htmlize-many-files-dired' does the same for
</span><span class="comment-delimiter">;; </span><span class="comment">files marked in a dired buffer.
</span>
<span class="comment-delimiter">;; </span><span class="comment">htmlize supports three types of HTML output, selected by setting
</span><span class="comment-delimiter">;; </span><span class="comment">`</span><span class="comment"><span class="constant">htmlize-output-type</span></span><span class="comment">': `</span><span class="comment"><span class="constant">css</span></span><span class="comment">', `</span><span class="comment"><span class="constant">inline-css</span></span><span class="comment">', and `</span><span class="comment"><span class="constant">font</span></span><span class="comment">'. In `</span><span class="comment"><span class="constant">css</span></span><span class="comment">'
</span><span class="comment-delimiter">;; </span><span class="comment">mode, htmlize uses cascading style sheets to specify colors; it
</span><span class="comment-delimiter">;; </span><span class="comment">generates classes that correspond to Emacs faces and uses <span
</span><span class="comment-delimiter">;; </span><span class="comment">class=FACE>...</span> to color parts of text. In this mode, the
</span><span class="comment-delimiter">;; </span><span class="comment">produced HTML is valid under the 4.01 strict DTD, as confirmed by
</span><span class="comment-delimiter">;; </span><span class="comment">the W3C validator. `</span><span class="comment"><span class="constant">inline-css</span></span><span class="comment">' is like `</span><span class="comment"><span class="constant">css</span></span><span class="comment">', except the CSS is
</span><span class="comment-delimiter">;; </span><span class="comment">put directly in the STYLE attribute of the SPAN element, making it
</span><span class="comment-delimiter">;; </span><span class="comment">possible to paste the generated HTML into existing HTML documents.
</span><span class="comment-delimiter">;; </span><span class="comment">In `</span><span class="comment"><span class="constant">font</span></span><span class="comment">' mode, htmlize uses <font color="...">...</font> to
</span><span class="comment-delimiter">;; </span><span class="comment">colorize HTML, which is not standard-compliant, but works better in
</span><span class="comment-delimiter">;; </span><span class="comment">older browsers. `</span><span class="comment"><span class="constant">css</span></span><span class="comment">' mode is the default.
</span>
<span class="comment-delimiter">;; </span><span class="comment">You can also use htmlize from your Emacs Lisp code. When called
</span><span class="comment-delimiter">;; </span><span class="comment">non-interactively, `</span><span class="comment"><span class="constant">htmlize-buffer</span></span><span class="comment">' and `</span><span class="comment"><span class="constant">htmlize-region</span></span><span class="comment">' will
</span><span class="comment-delimiter">;; </span><span class="comment">return the resulting HTML buffer, but will not change current
</span><span class="comment-delimiter">;; </span><span class="comment">buffer or move the point. htmlize will do its best to work on
</span><span class="comment-delimiter">;; </span><span class="comment">non-windowing Emacs sessions but the result will be limited to
</span><span class="comment-delimiter">;; </span><span class="comment">colors supported by the terminal.
</span>
<span class="comment-delimiter">;; </span><span class="comment">htmlize aims for compatibility with Emacsen version 21 and later.
</span><span class="comment-delimiter">;; </span><span class="comment">Please let me know if it doesn't work on the version of XEmacs or
</span><span class="comment-delimiter">;; </span><span class="comment">GNU Emacs that you are using. The package relies on the presence
</span><span class="comment-delimiter">;; </span><span class="comment">of CL extensions, especially for cross-emacs compatibility; please
</span><span class="comment-delimiter">;; </span><span class="comment">don't try to remove that dependency. I see no practical problems
</span><span class="comment-delimiter">;; </span><span class="comment">with using the full power of the CL extensions, except that one
</span><span class="comment-delimiter">;; </span><span class="comment">might learn to like them too much.
</span>
<span class="comment-delimiter">;; </span><span class="comment">The latest version is available as a git repository at:
</span><span class="comment-delimiter">;;</span><span class="comment">
</span><span class="comment-delimiter">;; </span><span class="comment"><a href="http://fly.srk.fer.hr/~hniksic/emacs/htmlize.git"><http://fly.srk.fer.hr/~hniksic/emacs/htmlize.git></a>
</span><span class="comment-delimiter">;;</span><span class="comment">
</span><span class="comment-delimiter">;; </span><span class="comment">The snapshot of the latest release can be obtained at:
</span><span class="comment-delimiter">;;</span><span class="comment">
</span><span class="comment-delimiter">;; </span><span class="comment"><a href="http://fly.srk.fer.hr/~hniksic/emacs/htmlize.el.cgi"><http://fly.srk.fer.hr/~hniksic/emacs/htmlize.el.cgi></a>
</span><span class="comment-delimiter">;;</span><span class="comment">
</span><span class="comment-delimiter">;; </span><span class="comment">You can find a sample of htmlize's output (possibly generated with
</span><span class="comment-delimiter">;; </span><span class="comment">an older version) at:
</span><span class="comment-delimiter">;;</span><span class="comment">
</span><span class="comment-delimiter">;; </span><span class="comment"><a href="http://fly.srk.fer.hr/~hniksic/emacs/htmlize.el.html"><http://fly.srk.fer.hr/~hniksic/emacs/htmlize.el.html></a>
</span>
<span class="comment-delimiter">;; </span><span class="comment">Thanks go to the many people who have sent reports and contributed
</span><span class="comment-delimiter">;; </span><span class="comment">comments, suggestions, and fixes. They include Ron Gut, Bob
</span><span class="comment-delimiter">;; </span><span class="comment">Weiner, Toni Drabik, Peter Breton, Ville Skytta, Thomas Vogels,
</span><span class="comment-delimiter">;; </span><span class="comment">Juri Linkov, Maciek Pasternacki, and many others.
</span>
<span class="comment-delimiter">;; </span><span class="comment">User quotes: "You sir, are a sick, sick, _sick_ person. :)"
</span><span class="comment-delimiter">;; </span><span class="comment">-- Bill Perry, author of Emacs/W3
<hr /></span>
<span class="comment-delimiter">;;; </span><span class="comment">Code:
</span>
(<span class="keyword">require</span> '<span class="constant">cl</span>)
(<span class="keyword">eval-when-compile</span>
(<span class="keyword">defvar</span> <span class="variable-name">unresolved</span>)
(<span class="keyword">if</span> (string-match <span class="string">"XEmacs"</span> emacs-version)
(byte-compiler-options
(warnings (- unresolved))))
(<span class="keyword">defvar</span> <span class="variable-name">font-lock-auto-fontify</span>)
(<span class="keyword">defvar</span> <span class="variable-name">font-lock-support-mode</span>)
(<span class="keyword">defvar</span> <span class="variable-name">global-font-lock-mode</span>))
(<span class="keyword">defconst</span> <span class="variable-name">htmlize-version</span> <span class="string">"1.45"</span>)
(<span class="keyword">defgroup</span> <span class="type">htmlize</span> nil
<span class="doc">"Convert buffer text and faces to HTML."</span>
<span class="builtin">:group</span> 'hypermedia)
(<span class="keyword">defcustom</span> <span class="variable-name">htmlize-head-tags</span> <span class="string">""</span>
<span class="doc">"Additional tags to insert within HEAD of the generated document."</span>
<span class="builtin">:type</span> 'string
<span class="builtin">:group</span> 'htmlize)
(<span class="keyword">defcustom</span> <span class="variable-name">htmlize-output-type</span> 'css
<span class="doc">"Output type of generated HTML, one of `</span><span class="doc"><span class="constant">css</span></span><span class="doc">', `</span><span class="doc"><span class="constant">inline-css</span></span><span class="doc">', or `</span><span class="doc"><span class="constant">font</span></span><span class="doc">'.
When set to `</span><span class="doc"><span class="constant">css</span></span><span class="doc">' (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 `</span><span class="doc"><span class="constant">inline-css</span></span><span class="doc">', 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 `</span><span class="doc"><span class="constant">font</span></span><span class="doc">', the properties will be set using layout tags
<font>, <b>, <i>, <u>, and <strike>.
`</span><span class="doc"><span class="constant">css</span></span><span class="doc">' output is normally preferred, but `</span><span class="doc"><span class="constant">font</span></span><span class="doc">' is still useful for
supporting old, pre-CSS browsers, and both `</span><span class="doc"><span class="constant">inline-css</span></span><span class="doc">' and `</span><span class="doc"><span class="constant">font</span></span><span class="doc">' for
easier embedding of colorized text in foreign HTML documents (no style
sheet to carry around)."</span>
<span class="builtin">:type</span> '(choice (const css) (const inline-css) (const font))
<span class="builtin">:group</span> 'htmlize)
(<span class="keyword">defcustom</span> <span class="variable-name">htmlize-use-images</span> t
<span class="doc">"Whether htmlize generates `</span><span class="doc"><span class="constant">img</span></span><span class="doc">' for images attached to buffer contents."</span>
<span class="builtin">:type</span> 'boolean
<span class="builtin">:group</span> 'htmlize)
(<span class="keyword">defcustom</span> <span class="variable-name">htmlize-force-inline-images</span> nil
<span class="doc">"Non-nil means generate all images inline using data URLs.
Normally htmlize converts image descriptors with :file properties to
relative URIs, and those with :data properties to data URIs. With this
flag set, the images specified as a file name are loaded into memory and
embedded in the HTML as data URIs."</span>
<span class="builtin">:type</span> 'boolean
<span class="builtin">:group</span> 'htmlize)
(<span class="keyword">defcustom</span> <span class="variable-name">htmlize-max-alt-text</span> 100
<span class="doc">"Maximum size of text to use as ALT text in images.
Normally when htmlize encounters text covered by the `</span><span class="doc"><span class="constant">display</span></span><span class="doc">' property
that specifies an image, it generates an `</span><span class="doc"><span class="constant">alt</span></span><span class="doc">' attribute containing the
original text. If the text is larger than `</span><span class="doc"><span class="constant">htmlize-max-alt-text</span></span><span class="doc">' characters,
this will not be done."</span>)
(<span class="keyword">defcustom</span> <span class="variable-name">htmlize-transform-image</span> 'htmlize-default-transform-image
<span class="doc">"Function called to modify the image descriptor.
The function is called with the image descriptor found in the buffer and
the text the image is supposed to replace. It should return a (possibly
different) image descriptor property list or a replacement string to use
instead of of the original buffer text.
Returning nil is the same as returning the original text."</span>
<span class="builtin">:type</span> 'boolean
<span class="builtin">:group</span> 'htmlize)
(<span class="keyword">defcustom</span> <span class="variable-name">htmlize-generate-hyperlinks</span> t
<span class="doc">"Non-nil means auto-generate the links from URLs and mail addresses in buffer.
This is on by default; set it to nil if you don't want htmlize to
autogenerate such links. Note that this option only turns off automatic
search for contents that looks like URLs and converting them to links.
It has no effect on whether htmlize respects the `</span><span class="doc"><span class="constant">htmlize-link</span></span><span class="doc">' property."</span>
<span class="builtin">:type</span> 'boolean
<span class="builtin">:group</span> 'htmlize)
(<span class="keyword">defcustom</span> <span class="variable-name">htmlize-hyperlink-style</span> <span class="string">"
a {
color: inherit;
background-color: inherit;
font: inherit;
text-decoration: inherit;
}
a:hover {
text-decoration: underline;
}
"</span>
<span class="doc">"The CSS style used for hyperlinks when in CSS mode."</span>
<span class="builtin">:type</span> 'string
<span class="builtin">:group</span> 'htmlize)
(<span class="keyword">defcustom</span> <span class="variable-name">htmlize-replace-form-feeds</span> t
<span class="doc">"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."</span>
<span class="builtin">:type</span> 'boolean
<span class="builtin">:group</span> 'htmlize)
(<span class="keyword">defcustom</span> <span class="variable-name">htmlize-html-charset</span> nil
<span class="doc">"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. (This is
normally achieved by using the correct file coding system for the
buffer.) If you don't understand what that means, you should probably
leave this option in its default setting."</span>
<span class="builtin">:type</span> '(choice (const <span class="builtin">:tag</span> <span class="string">"Unset"</span> nil)
string)
<span class="builtin">:group</span> 'htmlize)
(<span class="keyword">defcustom</span> <span class="variable-name">htmlize-convert-nonascii-to-entities</span> t
<span class="doc">"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
`</span><span class="doc"><span class="constant">htmlize-html-charset</span></span><span class="doc">'.)
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,
\"&#169;\" *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, \"&#169;\" is exactly equivalent to \"&copy;\".
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."</span>
<span class="builtin">:type</span> 'sexp
<span class="builtin">:group</span> 'htmlize)
(<span class="keyword">defcustom</span> <span class="variable-name">htmlize-ignore-face-size</span> 'absolute
<span class="doc">"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 `</span><span class="doc"><span class="constant">absolute</span></span><span class="doc">', only absolute size specifications are ignored.
Please note that font sizes only work with CSS-based output types."</span>
<span class="builtin">:type</span> '(choice (const <span class="builtin">:tag</span> <span class="string">"Don't ignore"</span> nil)
(const <span class="builtin">:tag</span> <span class="string">"Ignore all"</span> t)
(const <span class="builtin">:tag</span> <span class="string">"Ignore absolute"</span> absolute))
<span class="builtin">:group</span> 'htmlize)
(<span class="keyword">defcustom</span> <span class="variable-name">htmlize-css-name-prefix</span> <span class="string">""</span>
<span class="doc">"The prefix used for CSS names.
The CSS names that htmlize generates from face names are often too
generic for CSS files; for example, `</span><span class="doc"><span class="constant">font-lock-type-face</span></span><span class="doc">' is transformed
to `</span><span class="doc"><span class="constant">type</span></span><span class="doc">'. Use this variable to add a prefix to the generated names.
The string \"htmlize-\" is an example of a reasonable prefix."</span>
<span class="builtin">:type</span> 'string
<span class="builtin">:group</span> 'htmlize)
(<span class="keyword">defcustom</span> <span class="variable-name">htmlize-use-rgb-txt</span> t
<span class="doc">"Whether `</span><span class="doc"><span class="constant">rgb.txt</span></span><span class="doc">' 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. `</span><span class="doc"><span class="constant">rgb.txt</span></span><span class="doc">'
is the X color database that maps hundreds of color names to such RGB
triples. When this variable is non-nil, `</span><span class="doc"><span class="constant">htmlize</span></span><span class="doc">' uses `</span><span class="doc"><span class="constant">rgb.txt</span></span><span class="doc">' to
look up color names.
If this variable is nil, htmlize queries Emacs for RGB components of
colors using `</span><span class="doc"><span class="constant">color-instance-rgb-components</span></span><span class="doc">' and `</span><span class="doc"><span class="constant">color-values</span></span><span class="doc">'.
This can yield incorrect results on non-true-color displays.
If the `</span><span class="doc"><span class="constant">rgb.txt</span></span><span class="doc">' file is not found (which will be the case if you're
running Emacs on non-X11 systems), this option is ignored."</span>
<span class="builtin">:type</span> 'boolean
<span class="builtin">:group</span> 'htmlize)
(<span class="keyword">defcustom</span> <span class="variable-name">htmlize-html-major-mode</span> nil
<span class="doc">"The mode the newly created HTML buffer will be put in.
Set this to nil if you prefer the default (fundamental) mode."</span>
<span class="builtin">:type</span> '(radio (const <span class="builtin">:tag</span> <span class="string">"No mode (fundamental)"</span> nil)
(function-item html-mode)
(function <span class="builtin">:tag</span> <span class="string">"User-defined major mode"</span>))
<span class="builtin">:group</span> 'htmlize)
(<span class="keyword">defvar</span> <span class="variable-name">htmlize-before-hook</span> nil
<span class="doc">"Hook run before htmlizing a buffer.
The hook functions are run in the source buffer (not the resulting HTML
buffer)."</span>)
(<span class="keyword">defvar</span> <span class="variable-name">htmlize-after-hook</span> nil
<span class="doc">"Hook run after htmlizing a buffer.
Unlike `</span><span class="doc"><span class="constant">htmlize-before-hook</span></span><span class="doc">', these functions are run in the generated
HTML buffer. You may use them to modify the outlook of the final HTML
output."</span>)
(<span class="keyword">defvar</span> <span class="variable-name">htmlize-file-hook</span> nil
<span class="doc">"Hook run by `</span><span class="doc"><span class="constant">htmlize-file</span></span><span class="doc">' after htmlizing a file, but before saving it."</span>)
(<span class="keyword">defvar</span> <span class="variable-name">htmlize-buffer-places</span>)
<hr />
<span class="comment-delimiter">;;; </span><span class="comment">Some cross-Emacs compatibility.
</span>
<span class="comment-delimiter">;; </span><span class="comment">I try to conditionalize on features rather than Emacs version, but
</span><span class="comment-delimiter">;; </span><span class="comment">in some cases checking against the version *is* necessary.
</span>(<span class="keyword">defconst</span> <span class="variable-name">htmlize-running-xemacs</span> (string-match <span class="string">"XEmacs"</span> emacs-version))
<span class="comment-delimiter">;; </span><span class="comment">We need a function that efficiently finds the next change of a
</span><span class="comment-delimiter">;; </span><span class="comment">property regardless of whether the change occurred because of a
</span><span class="comment-delimiter">;; </span><span class="comment">text property or an extent/overlay.
</span>(<span class="keyword">cond</span>
(htmlize-running-xemacs
(<span class="keyword">defun</span> <span class="function-name">htmlize-next-change</span> (pos prop <span class="type">&optional</span> limit)
(<span class="keyword">if</span> prop
(next-single-char-property-change pos prop nil (or limit (point-max)))
(next-property-change pos nil (or limit (point-max)))))
(<span class="keyword">defun</span> <span class="function-name">htmlize-next-face-change</span> (pos <span class="type">&optional</span> limit)
(htmlize-next-change pos 'face limit)))
((fboundp 'next-single-char-property-change)
<span class="comment-delimiter">;; </span><span class="comment">GNU Emacs 21+
</span> (<span class="keyword">defun</span> <span class="function-name">htmlize-next-change</span> (pos prop <span class="type">&optional</span> limit)
(<span class="keyword">if</span> prop
(next-single-char-property-change pos prop nil limit)
(next-char-property-change pos limit)))
(<span class="keyword">defun</span> <span class="function-name">htmlize-overlay-faces-at</span> (pos)
(delq nil (mapcar (<span class="keyword">lambda</span> (o) (overlay-get o 'face)) (overlays-at pos))))
(<span class="keyword">defun</span> <span class="function-name">htmlize-next-face-change</span> (pos <span class="type">&optional</span> limit)
<span class="comment-delimiter">;; </span><span class="comment">(htmlize-next-change pos 'face limit) would skip over entire
</span> <span class="comment-delimiter">;; </span><span class="comment">overlays that specify the `</span><span class="comment"><span class="constant">face</span></span><span class="comment">' property, even when they
</span> <span class="comment-delimiter">;; </span><span class="comment">contain smaller text properties that also specify `</span><span class="comment"><span class="constant">face</span></span><span class="comment">'.
</span> <span class="comment-delimiter">;; </span><span class="comment">Emacs display engine merges those faces, and so must we.
</span> (or limit
(setq limit (point-max)))
(<span class="keyword">let</span> ((next-prop (next-single-property-change pos 'face nil limit))
(overlay-faces (htmlize-overlay-faces-at pos)))
(<span class="keyword">while</span> (<span class="keyword">progn</span>
(setq pos (next-overlay-change pos))
(and (< pos next-prop)
(equal overlay-faces (htmlize-overlay-faces-at pos)))))
(setq pos (min pos next-prop))
<span class="comment-delimiter">;; </span><span class="comment">Additionally, we include the entire region that specifies the
</span> <span class="comment-delimiter">;; </span><span class="comment">`</span><span class="comment"><span class="constant">display</span></span><span class="comment">' property.
</span> (<span class="keyword">when</span> (get-char-property pos 'display)
(setq pos (next-single-char-property-change pos 'display nil limit)))
pos)))
(t
(<span class="warning">error</span> <span class="string">"htmlize requires next-single-property-change or \
next-single-char-property-change"</span>)))
(<span class="keyword">defmacro</span> <span class="function-name">htmlize-lexlet</span> (<span class="type">&rest</span> letforms)
(<span class="keyword">declare</span> (indent 1) (debug let))
(<span class="keyword">if</span> (and (boundp 'lexical-binding)
lexical-binding)
`(<span class="keyword">let</span> ,@letforms)
<span class="comment-delimiter">;; </span><span class="comment">cl extensions have a macro implementing lexical let
</span> `(<span class="keyword">lexical-let</span> ,@letforms)))
<span class="comment-delimiter">;; </span><span class="comment">Simple overlay emulation for XEmacs
</span>
(<span class="keyword">cond</span>
(htmlize-running-xemacs
(<span class="keyword">defalias</span> '<span class="function-name">htmlize-make-overlay</span> 'make-extent)
(<span class="keyword">defalias</span> '<span class="function-name">htmlize-overlay-put</span> 'set-extent-property)
(<span class="keyword">defalias</span> '<span class="function-name">htmlize-overlay-get</span> 'extent-property)
(<span class="keyword">defun</span> <span class="function-name">htmlize-overlays-in</span> (beg end) (extent-list nil beg end))
(<span class="keyword">defalias</span> '<span class="function-name">htmlize-delete-overlay</span> 'detach-extent))
(t
(<span class="keyword">defalias</span> '<span class="function-name">htmlize-make-overlay</span> 'make-overlay)
(<span class="keyword">defalias</span> '<span class="function-name">htmlize-overlay-put</span> 'overlay-put)
(<span class="keyword">defalias</span> '<span class="function-name">htmlize-overlay-get</span> 'overlay-get)
(<span class="keyword">defalias</span> '<span class="function-name">htmlize-overlays-in</span> 'overlays-in)
(<span class="keyword">defalias</span> '<span class="function-name">htmlize-delete-overlay</span> 'delete-overlay)))
<hr />
<span class="comment-delimiter">;;; </span><span class="comment">Transformation of buffer text: HTML escapes, untabification, etc.
</span>
(<span class="keyword">defvar</span> <span class="variable-name">htmlize-basic-character-table</span>
<span class="comment-delimiter">;; </span><span class="comment">Map characters in the 0-127 range to either one-character strings
</span> <span class="comment-delimiter">;; </span><span class="comment">or to numeric entities.
</span> (<span class="keyword">let</span> ((table (make-vector 128 ?\0)))
<span class="comment-delimiter">;; </span><span class="comment">Map characters in the 32-126 range to themselves, others to
</span> <span class="comment-delimiter">;; </span><span class="comment">&#CODE entities;
</span> (<span class="keyword">dotimes</span> (i 128)
(setf (aref table i) (<span class="keyword">if</span> (and (>= i 32) (<= i 126))
(char-to-string i)
(format <span class="string">"&#%d;"</span> i))))
<span class="comment-delimiter">;; </span><span class="comment">Set exceptions manually.
</span> (setf
<span class="comment-delimiter">;; </span><span class="comment">Don't escape newline, carriage return, and TAB.
</span> (aref table ?\n) <span class="string">"\n"</span>
(aref table ?\r) <span class="string">"\r"</span>
(aref table ?\t) <span class="string">"\t"</span>
<span class="comment-delimiter">;; </span><span class="comment">Escape &, <, and >.
</span> (aref table ?&) <span class="string">"&amp;"</span>
(aref table ?<) <span class="string">"&lt;"</span>
(aref table ?>) <span class="string">"&gt;"</span>
<span class="comment-delimiter">;; </span><span class="comment">Not escaping '"' buys us a measurable speedup. It's only
</span> <span class="comment-delimiter">;; </span><span class="comment">necessary to quote it for strings used in attribute values,
</span> <span class="comment-delimiter">;; </span><span class="comment">which htmlize doesn't typically do.
</span> <span class="comment-delimiter">;</span><span class="comment">(aref table ?\") "&quot;"
</span> )
table))
<span class="comment-delimiter">;; </span><span class="comment">A cache of HTML representation of non-ASCII characters. Depending
</span><span class="comment-delimiter">;; </span><span class="comment">on the setting of `</span><span class="comment"><span class="constant">htmlize-convert-nonascii-to-entities</span></span><span class="comment">', this maps
</span><span class="comment-delimiter">;; </span><span class="comment">non-ASCII characters to either "&#<code>;" or "<char>" (mapconcat's
</span><span class="comment-delimiter">;; </span><span class="comment">mapper must always return strings). It's only filled as characters
</span><span class="comment-delimiter">;; </span><span class="comment">are encountered, so that in a buffer with e.g. French text, it will
</span><span class="comment-delimiter">;; </span><span class="comment">only ever contain French accented characters as keys. It's cleared
</span><span class="comment-delimiter">;; </span><span class="comment">on each entry to htmlize-buffer-1 to allow modifications of
</span><span class="comment-delimiter">;; </span><span class="comment">`</span><span class="comment"><span class="constant">htmlize-convert-nonascii-to-entities</span></span><span class="comment">' to take effect.
</span>(<span class="keyword">defvar</span> <span class="variable-name">htmlize-extended-character-cache</span> (make-hash-table <span class="builtin">:test</span> 'eq))
(<span class="keyword">defun</span> <span class="function-name">htmlize-protect-string</span> (string)
<span class="doc">"HTML-protect string, escaping HTML metacharacters and I18N chars."</span>
<span class="comment-delimiter">;; </span><span class="comment">Only protecting strings that actually contain unsafe or non-ASCII
</span> <span class="comment-delimiter">;; </span><span class="comment">chars removes a lot of unnecessary funcalls and consing.
</span> (<span class="keyword">if</span> (not (string-match <span class="string">"[</span><span class="string"><span class="negation-char">^</span></span><span class="string">\r\n\t -%'-;=?-~]"</span> string))
string
(mapconcat (<span class="keyword">lambda</span> (char)
(<span class="keyword">cond</span>
((< char 128)
<span class="comment-delimiter">;; </span><span class="comment">ASCII: use htmlize-basic-character-table.
</span> (aref htmlize-basic-character-table char))
((gethash char htmlize-extended-character-cache)
<span class="comment-delimiter">;; </span><span class="comment">We've already seen this char; return the cached
</span> <span class="comment-delimiter">;; </span><span class="comment">string.
</span> )
((not htmlize-convert-nonascii-to-entities)
<span class="comment-delimiter">;; </span><span class="comment">If conversion to entities is not desired, always
</span> <span class="comment-delimiter">;; </span><span class="comment">copy the char literally.
</span> (setf (gethash char htmlize-extended-character-cache)
(char-to-string char)))
((< char 256)
<span class="comment-delimiter">;; </span><span class="comment">Latin 1: no need to call encode-char.
</span> (setf (gethash char htmlize-extended-character-cache)
(format <span class="string">"&#%d;"</span> char)))
((encode-char char 'ucs)
<span class="comment-delimiter">;; </span><span class="comment">Must check if encode-char works for CHAR;
</span> <span class="comment-delimiter">;; </span><span class="comment">it fails for Arabic and possibly elsewhere.
</span> (setf (gethash char htmlize-extended-character-cache)
(format <span class="string">"&#%d;"</span> (encode-char char 'ucs))))
(t
<span class="comment-delimiter">;; </span><span class="comment">encode-char doesn't work for this char. Copy it
</span> <span class="comment-delimiter">;; </span><span class="comment">unchanged and hope for the best.
</span> (setf (gethash char htmlize-extended-character-cache)
(char-to-string char)))))
string <span class="string">""</span>)))
(<span class="keyword">defun</span> <span class="function-name">htmlize-attr-escape</span> (string)
<span class="comment-delimiter">;; </span><span class="comment">Like htmlize-protect-string, but also escapes double-quoted
</span> <span class="comment-delimiter">;; </span><span class="comment">strings to make it usable in attribute values.
</span> (setq string (htmlize-protect-string string))
(<span class="keyword">if</span> (not (string-match <span class="string">"\""</span> string))
string
(mapconcat (<span class="keyword">lambda</span> (char)
(<span class="keyword">if</span> (eql char ?\")
<span class="string">"&quot;"</span>
(char-to-string char)))
string <span class="string">""</span>)))
(<span class="keyword">defsubst</span> <span class="function-name">htmlize-concat</span> (list)
(<span class="keyword">if</span> (and (consp list) (null (cdr list)))
<span class="comment-delimiter">;; </span><span class="comment">Don't create a new string in the common case where the list only
</span> <span class="comment-delimiter">;; </span><span class="comment">consists of one element.
</span> (car list)
(apply #'concat list)))
(<span class="keyword">defun</span> <span class="function-name">htmlize-format-link</span> (linkprops text)
(<span class="keyword">let</span> ((uri (<span class="keyword">if</span> (stringp linkprops)
linkprops
(plist-get linkprops <span class="builtin">:uri</span>)))
(escaped-text (htmlize-protect-string text)))
(<span class="keyword">if</span> uri
(format <span class="string">"<a href=\"%s\">%s</a>"</span> (htmlize-attr-escape uri) escaped-text)
escaped-text)))
(<span class="keyword">defun</span> <span class="function-name">htmlize-escape-or-link</span> (string)
<span class="comment-delimiter">;; </span><span class="comment">Escape STRING and/or add hyperlinks. STRING comes from a
</span> <span class="comment-delimiter">;; </span><span class="comment">`</span><span class="comment"><span class="constant">display</span></span><span class="comment">' property.
</span> (<span class="keyword">let</span> ((pos 0) (end (length string)) outlist)
(<span class="keyword">while</span> (< pos end)
(<span class="keyword">let*</span> ((link (get-char-property pos 'htmlize-link string))
(next-link-change (next-single-property-change
pos 'htmlize-link string end))
(chunk (substring string pos next-link-change)))
(push
(<span class="keyword">cond</span> (link
(htmlize-format-link link chunk))
((get-char-property 0 'htmlize-literal chunk)
chunk)
(t
(htmlize-protect-string chunk)))
outlist)
(setq pos next-link-change)))
(htmlize-concat (nreverse outlist))))
(<span class="keyword">defun</span> <span class="function-name">htmlize-display-prop-to-html</span> (display text)
(<span class="keyword">let</span> (desc)
(<span class="keyword">cond</span> ((stringp display)
<span class="comment-delimiter">;; </span><span class="comment">Emacs ignores recursive display properties.
</span> (htmlize-escape-or-link display))
((not (eq (car-safe display) 'image))
(htmlize-protect-string text))
((null (setq desc (funcall htmlize-transform-image
(cdr display) text)))
(htmlize-escape-or-link text))
((stringp desc)
(htmlize-escape-or-link desc))
(t
(htmlize-generate-image desc text)))))
(<span class="keyword">defun</span> <span class="function-name">htmlize-string-to-html</span> (string)
<span class="comment-delimiter">;; </span><span class="comment">Convert the string to HTML, including images attached as
</span> <span class="comment-delimiter">;; </span><span class="comment">`</span><span class="comment"><span class="constant">display</span></span><span class="comment">' property and links as `</span><span class="comment"><span class="constant">htmlize-link</span></span><span class="comment">' property. In a
</span> <span class="comment-delimiter">;; </span><span class="comment">string without images or links, this is equivalent to
</span> <span class="comment-delimiter">;; </span><span class="comment">`</span><span class="comment"><span class="constant">htmlize-protect-string</span></span><span class="comment">'.
</span> (<span class="keyword">let</span> ((pos 0) (end (length string)) outlist)
(<span class="keyword">while</span> (< pos end)
(<span class="keyword">let*</span> ((display (get-char-property pos 'display string))
(next-display-change (next-single-property-change
pos 'display string end))
(chunk (substring string pos next-display-change)))
(push
(<span class="keyword">if</span> display
(htmlize-display-prop-to-html display chunk)
(htmlize-escape-or-link chunk))
outlist)
(setq pos next-display-change)))
(htmlize-concat (nreverse outlist))))
(<span class="keyword">defun</span> <span class="function-name">htmlize-default-transform-image</span> (imgprops _text)
<span class="doc">"Default transformation of image descriptor to something usable in HTML.
If `</span><span class="doc"><span class="constant">htmlize-use-images</span></span><span class="doc">' is nil, the function always returns nil, meaning
use original text. Otherwise, it tries to find the image for images that
specify a file name. If `</span><span class="doc"><span class="constant">htmlize-force-inline-images</span></span><span class="doc">' is non-nil, it also
converts the :file attribute to :data and returns the modified property
list."</span>
(<span class="keyword">when</span> htmlize-use-images
(<span class="keyword">when</span> (plist-get imgprops <span class="builtin">:file</span>)
(<span class="keyword">let</span> ((location (plist-get (cdr (find-image (list imgprops))) <span class="builtin">:file</span>)))
(<span class="keyword">when</span> location
(setq imgprops (plist-put (copy-list imgprops) <span class="builtin">:file</span> location)))))
(<span class="keyword">if</span> htmlize-force-inline-images
(<span class="keyword">let</span> ((location (plist-get imgprops <span class="builtin">:file</span>))
data)
(<span class="keyword">when</span> location
(<span class="keyword">with-temp-buffer</span>
(<span class="keyword">condition-case</span> nil
(<span class="keyword">progn</span>
(insert-file-contents-literally location)
(setq data (buffer-string)))
(<span class="warning">error</span> nil))))
<span class="comment-delimiter">;; </span><span class="comment">if successful, return the new plist, otherwise return
</span> <span class="comment-delimiter">;; </span><span class="comment">nil, which will use the original text
</span> (and data
(plist-put (plist-put imgprops <span class="builtin">:file</span> nil)
<span class="builtin">:data</span> data)))
imgprops)))
(<span class="keyword">defun</span> <span class="function-name">htmlize-alt-text</span> (_imgprops origtext)
(and (/= (length origtext) 0)
(<= (length origtext) htmlize-max-alt-text)
(not (string-match <span class="string">"[\0-\x1f]"</span> origtext))
origtext))
(<span class="keyword">defun</span> <span class="function-name">htmlize-generate-image</span> (imgprops origtext)
(<span class="keyword">let*</span> ((alt-text (htmlize-alt-text imgprops origtext))
(alt-attr (<span class="keyword">if</span> alt-text
(format <span class="string">" alt=\"%s\""</span> (htmlize-attr-escape alt-text))
<span class="string">""</span>)))
(<span class="keyword">cond</span> ((plist-get imgprops <span class="builtin">:file</span>)
<span class="comment-delimiter">;; </span><span class="comment">Try to find the image in image-load-path
</span> (<span class="keyword">let*</span> ((found-props (cdr (find-image (list imgprops))))
(file (or (plist-get found-props <span class="builtin">:file</span>)
(plist-get imgprops <span class="builtin">:file</span>))))
(format <span class="string">"<img src=\"%s\"%s />"</span>
(htmlize-attr-escape (file-relative-name file))
alt-attr)))
((plist-get imgprops <span class="builtin">:data</span>)
(format <span class="string">"<img src=\"data:image/%s;base64,%s\"%s />"</span>
(or (plist-get imgprops <span class="builtin">:type</span>) <span class="string">""</span>)
(base64-encode-string (plist-get imgprops <span class="builtin">:data</span>))
alt-attr)))))
(<span class="keyword">defconst</span> <span class="variable-name">htmlize-ellipsis</span> <span class="string">"..."</span>)
(put-text-property 0 (length htmlize-ellipsis) 'htmlize-ellipsis t htmlize-ellipsis)
(<span class="keyword">defun</span> <span class="function-name">htmlize-match-inv-spec</span> (inv)
(member* inv buffer-invisibility-spec
<span class="builtin">:key</span> (<span class="keyword">lambda</span> (i)
(<span class="keyword">if</span> (symbolp i) i (car i)))))
(<span class="keyword">defun</span> <span class="function-name">htmlize-decode-invisibility-spec</span> (invisible)
<span class="comment-delimiter">;; </span><span class="comment">Return t, nil, or `</span><span class="comment"><span class="constant">ellipsis</span></span><span class="comment">', depending on how invisible text should be inserted.
</span>
(<span class="keyword">if</span> (not (listp buffer-invisibility-spec))
<span class="comment-delimiter">;; </span><span class="comment">If buffer-invisibility-spec is not a list, then all
</span> <span class="comment-delimiter">;; </span><span class="comment">characters with non-nil `</span><span class="comment"><span class="constant">invisible</span></span><span class="comment">' property are visible.
</span> (not invisible)
<span class="comment-delimiter">;; </span><span class="comment">Otherwise, the value of a non-nil `</span><span class="comment"><span class="constant">invisible</span></span><span class="comment">' property can be:
</span> <span class="comment-delimiter">;; </span><span class="comment">1. a symbol -- make the text invisible if it matches
</span> <span class="comment-delimiter">;; </span><span class="comment">buffer-invisibility-spec.
</span> <span class="comment-delimiter">;; </span><span class="comment">2. a list of symbols -- make the text invisible if
</span> <span class="comment-delimiter">;; </span><span class="comment">any symbol in the list matches
</span> <span class="comment-delimiter">;; </span><span class="comment">buffer-invisibility-spec.
</span> <span class="comment-delimiter">;; </span><span class="comment">If the match of buffer-invisibility-spec has a non-nil
</span> <span class="comment-delimiter">;; </span><span class="comment">CDR, replace the invisible text with an ellipsis.
</span> (<span class="keyword">let</span> ((match (<span class="keyword">if</span> (symbolp invisible)
(htmlize-match-inv-spec invisible)
(some #'htmlize-match-inv-spec invisible))))
(<span class="keyword">cond</span> ((null match) t)
((cdr-safe (car match)) 'ellipsis)
(t nil)))))
(<span class="keyword">defun</span> <span class="function-name">htmlize-add-before-after-strings</span> (beg end text)
<span class="comment-delimiter">;; </span><span class="comment">Find overlays specifying before-string and after-string in [beg,
</span> <span class="comment-delimiter">;; </span><span class="comment">pos). If any are found, splice them into TEXT and return the new
</span> <span class="comment-delimiter">;; </span><span class="comment">text.
</span> (<span class="keyword">let</span> (additions)
(<span class="keyword">dolist</span> (overlay (overlays-in beg end))
(<span class="keyword">let</span> ((before (overlay-get overlay 'before-string))
(after (overlay-get overlay 'after-string)))
(<span class="keyword">when</span> after
(push (cons (- (overlay-end overlay) beg)
after)
additions))
(<span class="keyword">when</span> before
(push (cons (- (overlay-start overlay) beg)
before)
additions))))
(<span class="keyword">if</span> additions
(<span class="keyword">let</span> ((textlist nil)
(strpos 0))
(<span class="keyword">dolist</span> (add (stable-sort additions #'< <span class="builtin">:key</span> #'car))
(<span class="keyword">let</span> ((addpos (car add))
(addtext (cdr add)))
(push (substring text strpos addpos) textlist)
(push addtext textlist)
(setq strpos addpos)))
(push (substring text strpos) textlist)
(apply #'concat (nreverse textlist)))
text)))
(<span class="keyword">defun</span> <span class="function-name">htmlize-copy-prop</span> (prop beg end string)
<span class="comment-delimiter">;; </span><span class="comment">Copy the specified property from the specified region of the
</span> <span class="comment-delimiter">;; </span><span class="comment">buffer to the target string. We cannot rely on Emacs to copy the
</span> <span class="comment-delimiter">;; </span><span class="comment">property because we want to handle properties coming from both
</span> <span class="comment-delimiter">;; </span><span class="comment">text properties and overlays.
</span> (<span class="keyword">let</span> ((pos beg))
(<span class="keyword">while</span> (< pos end)
(<span class="keyword">let</span> ((value (get-char-property pos prop))
(next-change (htmlize-next-change pos prop end)))
(<span class="keyword">when</span> value
(put-text-property (- pos beg) (- next-change beg)
prop value string))
(setq pos next-change)))))
(<span class="keyword">defun</span> <span class="function-name">htmlize-get-text-with-display</span> (beg end)
<span class="comment-delimiter">;; </span><span class="comment">Like buffer-substring-no-properties, except it copies the
</span> <span class="comment-delimiter">;; </span><span class="comment">`</span><span class="comment"><span class="constant">display</span></span><span class="comment">' property from the buffer, if found.
</span> (<span class="keyword">let</span> ((text (buffer-substring-no-properties beg end)))
(htmlize-copy-prop 'display beg end text)
(htmlize-copy-prop 'htmlize-link beg end text)
(<span class="keyword">unless</span> htmlize-running-xemacs
(setq text (htmlize-add-before-after-strings beg end text)))
text))
(<span class="keyword">defun</span> <span class="function-name">htmlize-buffer-substring-no-invisible</span> (beg end)
<span class="comment-delimiter">;; </span><span class="comment">Like buffer-substring-no-properties, but don't copy invisible
</span> <span class="comment-delimiter">;; </span><span class="comment">parts of the region. Where buffer-substring-no-properties
</span> <span class="comment-delimiter">;; </span><span class="comment">mandates an ellipsis to be shown, htmlize-ellipsis is inserted.
</span> (<span class="keyword">let</span> ((pos beg)
visible-list invisible show last-show next-change)
<span class="comment-delimiter">;; </span><span class="comment">Iterate over the changes in the `</span><span class="comment"><span class="constant">invisible</span></span><span class="comment">' property and filter
</span> <span class="comment-delimiter">;; </span><span class="comment">out the portions where it's non-nil, i.e. where the text is
</span> <span class="comment-delimiter">;; </span><span class="comment">invisible.
</span> (<span class="keyword">while</span> (< pos end)
(setq invisible (get-char-property pos 'invisible)
next-change (htmlize-next-change pos 'invisible end)
show (htmlize-decode-invisibility-spec invisible))
(<span class="keyword">cond</span> ((eq show t)
(push (htmlize-get-text-with-display pos next-change)
visible-list))
((and (eq show 'ellipsis)
(not (eq last-show 'ellipsis))
<span class="comment-delimiter">;; </span><span class="comment">Conflate successive ellipses.
</span> (push htmlize-ellipsis visible-list))))
(setq pos next-change last-show show))
(htmlize-concat (nreverse visible-list))))
(<span class="keyword">defun</span> <span class="function-name">htmlize-trim-ellipsis</span> (text)
<span class="comment-delimiter">;; </span><span class="comment">Remove htmlize-ellipses ("...") from the beginning of TEXT if it
</span> <span class="comment-delimiter">;; </span><span class="comment">starts with it. It checks for the special property of the
</span> <span class="comment-delimiter">;; </span><span class="comment">ellipsis so it doesn't work on ordinary text that begins with
</span> <span class="comment-delimiter">;; </span><span class="comment">"...".
</span> (<span class="keyword">if</span> (get-text-property 0 'htmlize-ellipsis text)
(substring text (length htmlize-ellipsis))
text))
(<span class="keyword">defconst</span> <span class="variable-name">htmlize-tab-spaces</span>
<span class="comment-delimiter">;; </span><span class="comment">A table of strings with spaces. (aref htmlize-tab-spaces 5) is
</span> <span class="comment-delimiter">;; </span><span class="comment">like (make-string 5 ?\ ), except it doesn't cons.
</span> (<span class="keyword">let</span> ((v (make-vector 32 nil)))
(<span class="keyword">dotimes</span> (i (length v))
(setf (aref v i) (make-string i ?\ )))
v))
(<span class="keyword">defun</span> <span class="function-name">htmlize-untabify</span> (text start-column)
<span class="doc">"Untabify TEXT, assuming it starts at START-COLUMN."</span>
(<span class="keyword">let</span> ((column start-column)
(last-match 0)
(chunk-start 0)
chunks match-pos tab-size)
(<span class="keyword">while</span> (string-match <span class="string">"[\t\n]"</span> text last-match)
(setq match-pos (match-beginning 0))
(<span class="keyword">cond</span> ((eq (aref text match-pos) ?\t)
<span class="comment-delimiter">;; </span><span class="comment">Encountered a tab: create a chunk of text followed by
</span> <span class="comment-delimiter">;; </span><span class="comment">the expanded tab.
</span> (push (substring text chunk-start match-pos) chunks)
<span class="comment-delimiter">;; </span><span class="comment">Increase COLUMN by the length of the text we've
</span> <span class="comment-delimiter">;; </span><span class="comment">skipped since last tab or newline. (Encountering
</span> <span class="comment-delimiter">;; </span><span class="comment">newline resets it.)
</span> (incf column (- match-pos last-match))
<span class="comment-delimiter">;; </span><span class="comment">Calculate tab size based on tab-width and COLUMN.
</span> (setq tab-size (- tab-width (% column tab-width)))
<span class="comment-delimiter">;; </span><span class="comment">Expand the tab, carefully recreating the `</span><span class="comment"><span class="constant">display</span></span><span class="comment">'
</span> <span class="comment-delimiter">;; </span><span class="comment">property if one was on the TAB.
</span> (<span class="keyword">let</span> ((display (get-text-property match-pos 'display text))
(expanded-tab (aref htmlize-tab-spaces tab-size)))
(<span class="keyword">when</span> display
(put-text-property 0 tab-size 'display display expanded-tab))
(push expanded-tab chunks))
(incf column tab-size)
(setq chunk-start (1+ match-pos)))
(t
<span class="comment-delimiter">;; </span><span class="comment">Reset COLUMN at beginning of line.
</span> (setq column 0)))
(setq last-match (1+ match-pos)))
<span class="comment-delimiter">;; </span><span class="comment">If no chunks have been allocated, it means there have been no
</span> <span class="comment-delimiter">;; </span><span class="comment">tabs to expand. Return TEXT unmodified.
</span> (<span class="keyword">if</span> (null chunks)
text
(<span class="keyword">when</span> (< chunk-start (length text))
<span class="comment-delimiter">;; </span><span class="comment">Push the remaining chunk.
</span> (push (substring text chunk-start) chunks))
<span class="comment-delimiter">;; </span><span class="comment">Generate the output from the available chunks.
</span> (htmlize-concat (nreverse chunks)))))
(<span class="keyword">defun</span> <span class="function-name">htmlize-extract-text</span> (beg end trailing-ellipsis)
<span class="comment-delimiter">;; </span><span class="comment">Extract buffer text, sans the invisible parts. Then
</span> <span class="comment-delimiter">;; </span><span class="comment">untabify it and escape the HTML metacharacters.
</span> (<span class="keyword">let</span> ((text (htmlize-buffer-substring-no-invisible beg end)))
(<span class="keyword">when</span> trailing-ellipsis
(setq text (htmlize-trim-ellipsis text)))
<span class="comment-delimiter">;; </span><span class="comment">If TEXT ends up empty, don't change trailing-ellipsis.
</span> (<span class="keyword">when</span> (> (length text) 0)
(setq trailing-ellipsis
(get-text-property (1- (length text))
'htmlize-ellipsis text)))
(setq text (htmlize-untabify text (current-column)))
(setq text (htmlize-string-to-html text))
(values text trailing-ellipsis)))
(<span class="keyword">defun</span> <span class="function-name">htmlize-despam-address</span> (string)
<span class="doc">"Replace every occurrence of '@' in STRING with %40.
This is used to protect mailto links without modifying their meaning."</span>
<span class="comment-delimiter">;; </span><span class="comment">Suggested by Ville Skytta.
</span> (<span class="keyword">while</span> (string-match <span class="string">"@"</span> string)
(setq string (replace-match <span class="string">"%40"</span> nil t string)))
string)
(<span class="keyword">defun</span> <span class="function-name">htmlize-make-tmp-overlay</span> (beg end props)
(<span class="keyword">let</span> ((overlay (htmlize-make-overlay beg end)))
(htmlize-overlay-put overlay 'htmlize-tmp-overlay t)
(<span class="keyword">while</span> props
(htmlize-overlay-put overlay (pop props) (pop props)))
overlay))
(<span class="keyword">defun</span> <span class="function-name">htmlize-delete-tmp-overlays</span> ()
(<span class="keyword">dolist</span> (overlay (htmlize-overlays-in (point-min) (point-max)))
(<span class="keyword">when</span> (htmlize-overlay-get overlay 'htmlize-tmp-overlay)
(htmlize-delete-overlay overlay))))
(<span class="keyword">defun</span> <span class="function-name">htmlize-make-link-overlay</span> (beg end uri)
(htmlize-make-tmp-overlay beg end `(htmlize-link (<span class="builtin">:uri</span> ,uri))))
(<span class="keyword">defun</span> <span class="function-name">htmlize-create-auto-links</span> ()
<span class="doc">"Add `</span><span class="doc"><span class="constant">htmlize-link</span></span><span class="doc">' property to all mailto links in the buffer."</span>
(<span class="keyword">save-excursion</span>
(goto-char (point-min))
(<span class="keyword">while</span> (re-search-forward
<span class="string">"<</span><span class="string"><span class="regexp-grouping-backslash">\\</span></span><span class="string"><span class="regexp-grouping-construct">(</span></span><span class="string"><span class="regexp-grouping-backslash">\\</span></span><span class="string"><span class="regexp-grouping-construct">(</span></span><span class="string">mailto:</span><span class="string"><span class="regexp-grouping-backslash">\\</span></span><span class="string"><span class="regexp-grouping-construct">)</span></span><span class="string">?</span><span class="string"><span class="regexp-grouping-backslash">\\</span></span><span class="string"><span class="regexp-grouping-construct">(</span></span><span class="string">[-=+_.a-zA-Z0-9]+@[-_.a-zA-Z0-9]+</span><span class="string"><span class="regexp-grouping-backslash">\\</span></span><span class="string"><span class="regexp-grouping-construct">)</span></span><span class="string"><span class="regexp-grouping-backslash">\\</span></span><span class="string"><span class="regexp-grouping-construct">)</span></span><span class="string">>"</span>
nil t)
(<span class="keyword">let*</span> ((address (match-string 3))
(beg (match-beginning 0)) (end (match-end 0))
(uri (concat <span class="string">"mailto:"</span> (htmlize-despam-address address))))
(htmlize-make-link-overlay beg end uri)))
(goto-char (point-min))
(<span class="keyword">while</span> (re-search-forward <span class="string">"<</span><span class="string"><span class="regexp-grouping-backslash">\\</span></span><span class="string"><span class="regexp-grouping-construct">(</span></span><span class="string"><span class="regexp-grouping-backslash">\\</span></span><span class="string"><span class="regexp-grouping-construct">(</span></span><span class="string">URL:</span><span class="string"><span class="regexp-grouping-backslash">\\</span></span><span class="string"><span class="regexp-grouping-construct">)</span></span><span class="string">?</span><span class="string"><span class="regexp-grouping-backslash">\\</span></span><span class="string"><span class="regexp-grouping-construct">(</span></span><span class="string">[a-zA-Z]+://[</span><span class="string"><span class="negation-char">^</span></span><span class="string">;]+</span><span class="string"><span class="regexp-grouping-backslash">\\</span></span><span class="string"><span class="regexp-grouping-construct">)</span></span><span class="string"><span class="regexp-grouping-backslash">\\</span></span><span class="string"><span class="regexp-grouping-construct">)</span></span><span class="string">>"</span>
nil t)
(htmlize-make-link-overlay
(match-beginning 0) (match-end 0) (match-string 3)))))
<span class="comment-delimiter">;; </span><span class="comment">Tests for htmlize-create-auto-links:
</span>
<span class="comment-delimiter">;; </span><span class="comment"><a href="mailto:hniksic%40xemacs.org"><mailto:[email protected]></a>
</span><span class="comment-delimiter">;; </span><span class="comment"><a href="http://fly.srk.fer.hr"><http://fly.srk.fer.hr></a>
</span><span class="comment-delimiter">;; </span><span class="comment"><a href="http://www.xemacs.org"><URL:http://www.xemacs.org></a>
</span><span class="comment-delimiter">;; </span><span class="comment"><a href="http://www.mail-archive.com/[email protected]/"><http://www.mail-archive.com/[email protected]/></a>
</span><span class="comment-delimiter">;; </span><span class="comment"><a href="mailto:hniksic%40xemacs.org"><[email protected]></a>
</span><span class="comment-delimiter">;; </span><span class="comment"><a href="mailto:xalan-dev-sc.10148567319.hacuhiucknfgmpfnjcpg-john=doe.com%40xml.apache.org"><xalan-dev-sc.10148567319.hacuhiucknfgmpfnjcpg-john=doe.com@xml.apache.org></a>
</span>
(<span class="keyword">defun</span> <span class="function-name">htmlize-shadow-form-feeds</span> ()
(<span class="keyword">let</span> ((s <span class="string">"\n<hr />"</span>))
(put-text-property 0 (length s) 'htmlize-literal t s)
(<span class="keyword">let</span> ((disp `(display ,s)))
(<span class="keyword">while</span> (re-search-forward <span class="string">"\n\^L"</span> nil t)
(htmlize-make-tmp-overlay (match-beginning 0) (match-end 0) disp)))))
(<span class="keyword">defun</span> <span class="function-name">htmlize-defang-local-variables</span> ()
<span class="comment-delimiter">;; </span><span class="comment">Juri Linkov reports that an HTML-ized "Local variables" can lead
</span> <span class="comment-delimiter">;; </span><span class="comment">visiting the HTML to fail with "Local variables list is not
</span> <span class="comment-delimiter">;; </span><span class="comment">properly terminated". He suggested changing the phrase to
</span> <span class="comment-delimiter">;; </span><span class="comment">syntactically equivalent HTML that Emacs doesn't recognize.
</span> (goto-char (point-min))
(<span class="keyword">while</span> (search-forward <span class="string">"Local Variables:"</span> nil t)
(replace-match <span class="string">"Local Variables&#58;"</span> nil t)))
<hr />
<span class="comment-delimiter">;;; </span><span class="comment">Color handling.
</span>
(<span class="keyword">defvar</span> <span class="variable-name">htmlize-x-library-search-path</span>
`(,data-directory
<span class="string">"/etc/X11/rgb.txt"</span>
<span class="string">"/usr/share/X11/rgb.txt"</span>
<span class="comment-delimiter">;; </span><span class="comment">the remainder of this list really belongs in a museum
</span> <span class="string">"/usr/X11R6/lib/X11/"</span>
<span class="string">"/usr/X11R5/lib/X11/"</span>
<span class="string">"/usr/lib/X11R6/X11/"</span>
<span class="string">"/usr/lib/X11R5/X11/"</span>
<span class="string">"/usr/local/X11R6/lib/X11/"</span>
<span class="string">"/usr/local/X11R5/lib/X11/"</span>
<span class="string">"/usr/local/lib/X11R6/X11/"</span>
<span class="string">"/usr/local/lib/X11R5/X11/"</span>
<span class="string">"/usr/X11/lib/X11/"</span>
<span class="string">"/usr/lib/X11/"</span>
<span class="string">"/usr/local/lib/X11/"</span>
<span class="string">"/usr/X386/lib/X11/"</span>
<span class="string">"/usr/x386/lib/X11/"</span>
<span class="string">"/usr/XFree86/lib/X11/"</span>
<span class="string">"/usr/unsupported/lib/X11/"</span>
<span class="string">"/usr/athena/lib/X11/"</span>
<span class="string">"/usr/local/x11r5/lib/X11/"</span>
<span class="string">"/usr/lpp/Xamples/lib/X11/"</span>
<span class="string">"/usr/openwin/lib/X11/"</span>
<span class="string">"/usr/openwin/share/lib/X11/"</span>))
(<span class="keyword">defun</span> <span class="function-name">htmlize-get-color-rgb-hash</span> (<span class="type">&optional</span> rgb-file)
<span class="doc">"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 `</span><span class="doc"><span class="constant">rgb.txt</span></span><span class="doc">'.
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."</span>
(<span class="keyword">let</span> ((rgb-file (or rgb-file (locate-file
<span class="string">"rgb.txt"</span>
htmlize-x-library-search-path)))
(hash nil))
(<span class="keyword">when</span> rgb-file
(<span class="keyword">with-temp-buffer</span>
(insert-file-contents rgb-file)
(setq hash (make-hash-table <span class="builtin">:test</span> 'equal))
(<span class="keyword">while</span> (not (eobp))
(<span class="keyword">cond</span> ((looking-at <span class="string">"^\\s-*</span><span class="string"><span class="regexp-grouping-backslash">\\</span></span><span class="string"><span class="regexp-grouping-construct">(</span></span><span class="string">[!#]</span><span class="string"><span class="regexp-grouping-backslash">\\</span></span><span class="string"><span class="regexp-grouping-construct">|</span></span><span class="string">$</span><span class="string"><span class="regexp-grouping-backslash">\\</span></span><span class="string"><span class="regexp-grouping-construct">)</span></span><span class="string">"</span>)
<span class="comment-delimiter">;; </span><span class="comment">Skip comments and empty lines.
</span> )
((looking-at
<span class="string">"[ \t]*</span><span class="string"><span class="regexp-grouping-backslash">\\</span></span><span class="string"><span class="regexp-grouping-construct">(</span></span><span class="string">[0-9]+</span><span class="string"><span class="regexp-grouping-backslash">\\</span></span><span class="string"><span class="regexp-grouping-construct">)</span></span><span class="string">[ \t]+</span><span class="string"><span class="regexp-grouping-backslash">\\</span></span><span class="string"><span class="regexp-grouping-construct">(</span></span><span class="string">[0-9]+</span><span class="string"><span class="regexp-grouping-backslash">\\</span></span><span class="string"><span class="regexp-grouping-construct">)</span></span><span class="string">[ \t]+</span><span class="string"><span class="regexp-grouping-backslash">\\</span></span><span class="string"><span class="regexp-grouping-construct">(</span></span><span class="string">[0-9]+</span><span class="string"><span class="regexp-grouping-backslash">\\</span></span><span class="string"><span class="regexp-grouping-construct">)</span></span><span class="string">[ \t]+</span><span class="string"><span class="regexp-grouping-backslash">\\</span></span><span class="string"><span class="regexp-grouping-construct">(</span></span><span class="string">.*</span><span class="string"><span class="regexp-grouping-backslash">\\</span></span><span class="string"><span class="regexp-grouping-construct">)</span></span><span class="string">"</span>)
(setf (gethash (downcase (match-string 4)) hash)
(format <span class="string">"#%02x%02x%02x"</span>
(string-to-number (match-string 1))
(string-to-number (match-string 2))
(string-to-number (match-string 3)))))
(t
(<span class="warning">error</span>
<span class="string">"Unrecognized line in %s: %s"</span>
rgb-file
(buffer-substring (point) (<span class="keyword">progn</span> (end-of-line) (point))))))
(forward-line 1))))