blob: ea1e6e7bbc58d3a82acb17c6d1513b1b3691ab84 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001" Vim syntax support file
2" Maintainer: Bram Moolenaar <Bram@vim.org>
Bram Moolenaar47136d72004-10-12 20:02:24 +00003" Last Change: 2004 Oct 12
Bram Moolenaar071d4272004-06-13 20:20:40 +00004" (modified by David Ne\v{c}as (Yeti) <yeti@physics.muni.cz>)
5" (XHTML support by Panagiotis Issaris <takis@lumumba.luc.ac.be>)
6
7" Transform a file into HTML, using the current syntax highlighting.
8
9" Number lines when explicitely requested or when `number' is set
10if exists("html_number_lines")
11 let s:numblines = html_number_lines
12else
13 let s:numblines = &number
14endif
15
16" When not in gui we can only guess the colors.
17if has("gui_running")
18 let s:whatterm = "gui"
19else
20 let s:whatterm = "cterm"
21 if &t_Co == 8
22 let s:cterm_color0 = "#808080"
23 let s:cterm_color1 = "#ff6060"
24 let s:cterm_color2 = "#00ff00"
25 let s:cterm_color3 = "#ffff00"
26 let s:cterm_color4 = "#8080ff"
27 let s:cterm_color5 = "#ff40ff"
28 let s:cterm_color6 = "#00ffff"
29 let s:cterm_color7 = "#ffffff"
30 else
31 let s:cterm_color0 = "#000000"
32 let s:cterm_color1 = "#c00000"
33 let s:cterm_color2 = "#008000"
34 let s:cterm_color3 = "#804000"
35 let s:cterm_color4 = "#0000c0"
36 let s:cterm_color5 = "#c000c0"
37 let s:cterm_color6 = "#008080"
38 let s:cterm_color7 = "#c0c0c0"
39 let s:cterm_color8 = "#808080"
40 let s:cterm_color9 = "#ff6060"
41 let s:cterm_color10 = "#00ff00"
42 let s:cterm_color11 = "#ffff00"
43 let s:cterm_color12 = "#8080ff"
44 let s:cterm_color13 = "#ff40ff"
45 let s:cterm_color14 = "#00ffff"
46 let s:cterm_color15 = "#ffffff"
47 endif
48endif
49
50" Return good color specification: in GUI no transformation is done, in
51" terminal return RGB values of known colors and empty string on unknown
52if s:whatterm == "gui"
53 function! s:HtmlColor(color)
54 return a:color
55 endfun
56else
57 function! s:HtmlColor(color)
58 if exists("s:cterm_color" . a:color)
59 execute "return s:cterm_color" . a:color
60 else
61 return ""
62 endif
63 endfun
64endif
65
66if !exists("html_use_css")
67 " Return opening HTML tag for given highlight id
68 function! s:HtmlOpening(id)
69 let a = ""
70 if synIDattr(a:id, "inverse")
71 " For inverse, we always must set both colors (and exchange them)
72 let x = s:HtmlColor(synIDattr(a:id, "fg#", s:whatterm))
73 let a = a . '<span style="background-color: ' . ( x != "" ? x : s:fgc ) . '">'
74 let x = s:HtmlColor(synIDattr(a:id, "bg#", s:whatterm))
75 let a = a . '<font color="' . ( x != "" ? x : s:bgc ) . '">'
76 else
77 let x = s:HtmlColor(synIDattr(a:id, "bg#", s:whatterm))
78 if x != "" | let a = a . '<span style="background-color: ' . x . '">' | endif
79 let x = s:HtmlColor(synIDattr(a:id, "fg#", s:whatterm))
80 if x != "" | let a = a . '<font color="' . x . '">' | endif
81 endif
82 if synIDattr(a:id, "bold") | let a = a . "<b>" | endif
83 if synIDattr(a:id, "italic") | let a = a . "<i>" | endif
84 if synIDattr(a:id, "underline") | let a = a . "<u>" | endif
85 return a
86 endfun
87
88 " Return closing HTML tag for given highlight id
89 function s:HtmlClosing(id)
90 let a = ""
91 if synIDattr(a:id, "underline") | let a = a . "</u>" | endif
92 if synIDattr(a:id, "italic") | let a = a . "</i>" | endif
93 if synIDattr(a:id, "bold") | let a = a . "</b>" | endif
94 if synIDattr(a:id, "inverse")
95 let a = a . '</font></span>'
96 else
97 let x = s:HtmlColor(synIDattr(a:id, "fg#", s:whatterm))
98 if x != "" | let a = a . '</font>' | endif
99 let x = s:HtmlColor(synIDattr(a:id, "bg#", s:whatterm))
100 if x != "" | let a = a . '</span>' | endif
101 endif
102 return a
103 endfun
104endif
105
106" Return CSS style describing given highlight id (can be empty)
107function! s:CSS1(id)
108 let a = ""
109 if synIDattr(a:id, "inverse")
110 " For inverse, we always must set both colors (and exchange them)
111 let x = s:HtmlColor(synIDattr(a:id, "bg#", s:whatterm))
112 let a = a . "color: " . ( x != "" ? x : s:bgc ) . "; "
113 let x = s:HtmlColor(synIDattr(a:id, "fg#", s:whatterm))
114 let a = a . "background-color: " . ( x != "" ? x : s:fgc ) . "; "
115 else
116 let x = s:HtmlColor(synIDattr(a:id, "fg#", s:whatterm))
117 if x != "" | let a = a . "color: " . x . "; " | endif
118 let x = s:HtmlColor(synIDattr(a:id, "bg#", s:whatterm))
119 if x != "" | let a = a . "background-color: " . x . "; " | endif
120 endif
121 if synIDattr(a:id, "bold") | let a = a . "font-weight: bold; " | endif
122 if synIDattr(a:id, "italic") | let a = a . "font-style: italic; " | endif
123 if synIDattr(a:id, "underline") | let a = a . "text-decoration: underline; " | endif
124 return a
125endfun
126
127" Figure out proper MIME charset from the 'encoding' option.
128if exists("html_use_encoding")
129 let s:html_encoding = html_use_encoding
130else
131 let s:vim_encoding = &encoding
132 if s:vim_encoding =~ '^8bit\|^2byte'
133 let s:vim_encoding = substitute(s:vim_encoding, '^8bit-\|^2byte-', '', '')
134 endif
135 if s:vim_encoding == 'latin1'
136 let s:html_encoding = 'iso-8859-1'
137 elseif s:vim_encoding =~ "^cp12"
138 let s:html_encoding = substitute(s:vim_encoding, 'cp', 'windows-', '')
139 elseif s:vim_encoding == 'sjis'
140 let s:html_encoding = 'Shift_JIS'
141 elseif s:vim_encoding == 'euc-cn'
142 let s:html_encoding = 'GB_2312-80'
143 elseif s:vim_encoding == 'euc-tw'
144 let s:html_encoding = ""
145 elseif s:vim_encoding =~ '^euc\|^iso\|^koi'
146 let s:html_encoding = substitute(s:vim_encoding, '.*', '\U\0', '')
147 elseif s:vim_encoding == 'cp949'
148 let s:html_encoding = 'KS_C_5601-1987'
149 elseif s:vim_encoding == 'cp936'
150 let s:html_encoding = 'GBK'
151 elseif s:vim_encoding =~ '^ucs\|^utf'
152 let s:html_encoding = 'UTF-8'
153 else
154 let s:html_encoding = ""
155 endif
156endif
157
158
159" Set some options to make it work faster.
160" Expand tabs in original buffer to get 'tabstop' correctly used.
161" Don't report changes for :substitute, there will be many of them.
162let s:old_title = &title
163let s:old_icon = &icon
164let s:old_et = &l:et
165let s:old_report = &report
166let s:old_search = @/
167set notitle noicon
168setlocal et
169set report=1000000
170
171" Split window to create a buffer with the HTML file.
172let s:orgbufnr = winbufnr(0)
173if expand("%") == ""
174 new Untitled.html
175else
176 new %.html
177endif
178let s:newwin = winnr()
179let s:orgwin = bufwinnr(s:orgbufnr)
180
181set modifiable
182%d
183let s:old_paste = &paste
184set paste
185let s:old_magic = &magic
186set magic
187
188if exists("use_xhtml")
189 exe "normal! a<?xml version=\"1.0\"?>\n\e"
190 let tag_close = '/>'
191else
192 let tag_close = '>'
193endif
194
195" HTML header, with the title and generator ;-). Left free space for the CSS,
196" to be filled at the end.
197exe "normal! a<html>\n<head>\n<title>\e"
198exe "normal! a" . expand("%:p:~") . "</title>\n\e"
199exe "normal! a<meta name=\"Generator\" content=\"Vim/" . v:version/100 . "." . v:version %100 . '"' . tag_close . "\n\e"
200if s:html_encoding != ""
201 exe "normal! a<meta http-equiv=\"content-type\" content=\"text/html; charset=" . s:html_encoding . '"' . tag_close . "\n\e"
202endif
203if exists("html_use_css")
204 exe "normal! a<style type=\"text/css\">\n<!--\n-->\n</style>\n\e"
205endif
206if exists("html_no_pre")
207 exe "normal! a</head>\n<body>\n\e"
208else
209 exe "normal! a</head>\n<body>\n<pre>\n\e"
210endif
211
212exe s:orgwin . "wincmd w"
213
214" List of all id's
215let s:idlist = ","
216
217let s:expandedtab = ' '
218while strlen(s:expandedtab) < &ts
219 let s:expandedtab = s:expandedtab . ' '
220endwhile
221
222" Loop over all lines in the original text.
223" Use html_start_line and html_end_line if they are set.
224if exists("html_start_line")
225 let s:lnum = html_start_line
226 if s:lnum < 1 || s:lnum > line("$")
227 let s:lnum = 1
228 endif
229else
230 let s:lnum = 1
231endif
232if exists("html_end_line")
233 let s:end = html_end_line
234 if s:end < s:lnum || s:end > line("$")
235 let s:end = line("$")
236 endif
237else
238 let s:end = line("$")
239endif
240
Bram Moolenaar7b0294c2004-10-11 10:16:09 +0000241" Closed folds are kept in the HTML. Prepare the closed fold template text.
242if has('folding')
243 let s:c = &fillchars[matchend(&fillchars, 'fold:')]
244 if s:c == ''
245 let s:c = '-'
246 endif
247 let s:htmlfoldtext = '+' . s:c
248 while strlen(s:htmlfoldtext) < &columns
249 let s:htmlfoldtext = s:htmlfoldtext . s:c
250 endwhile
251 unlet s:c
252endif
253
Bram Moolenaar47136d72004-10-12 20:02:24 +0000254" For diff filler lines
255if has('diff')
256 if s:numblines
257 let s:fillerline = strpart(' ', 0, strlen(line("$"))) . ' '
258 else
259 let s:fillerline = ''
260 endif
261 let s:fillchar = &fillchars[matchend(&fillchars, 'diff:')]
262 if s:fillchar == ''
263 let s:fillchar = '-'
264 endif
265 while strlen(s:fillerline) < &columns
266 let s:fillerline = s:fillerline . s:fillchar
267 endwhile
268endif
Bram Moolenaar7b0294c2004-10-11 10:16:09 +0000269
Bram Moolenaar071d4272004-06-13 20:20:40 +0000270while s:lnum <= s:end
271
Bram Moolenaar47136d72004-10-12 20:02:24 +0000272 " If there are filler lines for diff mode, show these above the line.
273 let s:filler = diff_filler(s:lnum)
274 if s:filler > 0
275 let s:n = s:filler
276 while s:n > 0
277 if s:n > 2 && s:n < s:filler && !exists("html_whole_filler")
278 let s:new = strpart(s:fillerline, 0, 3) . " " . s:filler . " inserted lines "
279 let s:new = s:new . strpart(s:fillerline, strlen(s:new))
280 let s:n = 2
281 else
282 let s:new = s:fillerline
283 endif
284 let s:id_name = "DiffDelete"
285 let s:id = hlID(s:id_name)
286 let s:new = '<span class="' . s:id_name . '">' . s:new . '</span>'
287 " Add the class to class list if it's not there yet
288 if stridx(s:idlist, "," . s:id . ",") == -1
289 let s:idlist = s:idlist . s:id . ","
290 endif
291
292 exe s:newwin . "wincmd w"
293 exe "normal! a" . strtrans(s:new) . "\n\e"
294 exe s:orgwin . "wincmd w"
295 let s:n = s:n - 1
296 endwhile
297 unlet s:n
298 endif
299 unlet s:filler
300
301 " Start the line with the line number.
302 if s:numblines
303 let s:new = strpart(' ', 0, strlen(line("$")) - strlen(s:lnum)) . s:lnum . ' '
304 else
305 let s:new = ""
306 endif
307
Bram Moolenaar071d4272004-06-13 20:20:40 +0000308 " Get the current line
309 let s:line = getline(s:lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000310
Bram Moolenaar7b0294c2004-10-11 10:16:09 +0000311 if has('folding') && foldclosed(s:lnum) > -1
312 "
313 " This is the beginning of a folded block
314 "
Bram Moolenaar7b0294c2004-10-11 10:16:09 +0000315 let s:line = foldtextresult(s:lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000316
Bram Moolenaar7b0294c2004-10-11 10:16:09 +0000317 let s:new = s:new . s:line
318 if !exists("html_no_pre")
319 let s:new = s:new . strpart(s:htmlfoldtext, strlen(s:new))
320 endif
321
322 " Replace the reserved html characters
323 let s:new = substitute(substitute(substitute(substitute(substitute(s:new, '&', '\&amp;', 'g'), '<', '\&lt;', 'g'), '>', '\&gt;', 'g'), '"', '\&quot;', 'g'), "\x0c", '<hr class="PAGE-BREAK">', 'g')
324
325 let s:id_name = "Folded"
326 let s:id = hlID(s:id_name)
327 let s:new = '<span class="' . s:id_name . '">' . s:new . '</span>'
Bram Moolenaar071d4272004-06-13 20:20:40 +0000328 " Add the class to class list if it's not there yet
329 if stridx(s:idlist, "," . s:id . ",") == -1
330 let s:idlist = s:idlist . s:id . ","
331 endif
332
Bram Moolenaar7b0294c2004-10-11 10:16:09 +0000333 " Skip to the end of the fold
334 let s:lnum = foldclosedend(s:lnum)
335
336 else
337 "
338 " A line that is not folded.
339 "
340 let s:len = strlen(s:line)
341
342 if s:numblines
Bram Moolenaar47136d72004-10-12 20:02:24 +0000343 let s:new = '<span class="lnr">' . s:new . '</span> '
Bram Moolenaar071d4272004-06-13 20:20:40 +0000344 endif
Bram Moolenaar7b0294c2004-10-11 10:16:09 +0000345
Bram Moolenaar47136d72004-10-12 20:02:24 +0000346 " Get the diff attribute, if any.
347 let s:diffattr = diff_hlID(s:lnum, 1)
348
Bram Moolenaar7b0294c2004-10-11 10:16:09 +0000349 " Loop over each character in the line
350 let s:col = 1
351 while s:col <= s:len
352 let s:startcol = s:col " The start column for processing text
Bram Moolenaar47136d72004-10-12 20:02:24 +0000353 if s:diffattr
354 let s:id = diff_hlID(s:lnum, s:col)
355 let s:col = s:col + 1
356 " Speed loop (it's small - that's the trick)
357 " Go along till we find a change in hlID
358 while s:col <= s:len && s:id == diff_hlID(s:lnum, s:col) | let s:col = s:col + 1 | endwhile
359 while s:len < &columns
360 " Add spaces at the end to mark the changed line.
361 let s:line = s:line . ' '
362 let s:len = s:len + 1
363 endwhile
364 else
365 let s:id = synID(s:lnum, s:col, 1)
366 let s:col = s:col + 1
367 " Speed loop (it's small - that's the trick)
368 " Go along till we find a change in synID
369 while s:col <= s:len && s:id == synID(s:lnum, s:col, 1) | let s:col = s:col + 1 | endwhile
370 endif
Bram Moolenaar7b0294c2004-10-11 10:16:09 +0000371
372 " Output the text with the same synID, with class set to {s:id_name}
373 let s:id = synIDtrans(s:id)
374 let s:id_name = synIDattr(s:id, "name", s:whatterm)
375 let s:new = s:new . '<span class="' . s:id_name . '">' . substitute(substitute(substitute(substitute(substitute(strpart(s:line, s:startcol - 1, s:col - s:startcol), '&', '\&amp;', 'g'), '<', '\&lt;', 'g'), '>', '\&gt;', 'g'), '"', '\&quot;', 'g'), "\x0c", '<hr class="PAGE-BREAK">', 'g') . '</span>'
376 " Add the class to class list if it's not there yet
377 if stridx(s:idlist, "," . s:id . ",") == -1
378 let s:idlist = s:idlist . s:id . ","
379 endif
380
381 if s:col > s:len
382 break
383 endif
384 endwhile
385 endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000386
387 " Expand tabs
388 let s:pad=0
389 let s:start = 0
390 let s:idx = stridx(s:line, "\t")
391 while s:idx >= 0
392 let s:i = &ts - ((s:start + s:pad + s:idx) % &ts)
393 let s:new = substitute(s:new, '\t', strpart(s:expandedtab, 0, s:i), '')
394 let s:pad = s:pad + s:i - 1
395 let s:start = s:start + s:idx + 1
396 let s:idx = stridx(strpart(s:line, s:start), "\t")
397 endwhile
398
399 if exists("html_no_pre")
400 if exists("use_xhtml")
401 let s:new = substitute(s:new, ' ', '\&#x20;\&#x20;', 'g') . '<br/>'
402 else
403 let s:new = substitute(s:new, ' ', '\&nbsp;\&nbsp;', 'g') . '<br>'
404 endif
405 endif
406 exe s:newwin . "wincmd w"
407 exe "normal! a" . strtrans(s:new) . "\n\e"
408 exe s:orgwin . "wincmd w"
409 let s:lnum = s:lnum + 1
410 +
411endwhile
412" Finish with the last line
413exe s:newwin . "wincmd w"
414if exists("html_no_pre")
415 exe "normal! a\n</body>\n</html>\e"
416else
417 exe "normal! a</pre>\n</body>\n</html>\e"
418endif
419
420
421" Now, when we finally know which, we define the colors and styles
422if exists("html_use_css")
423 1;/<style type="text/+1
424endif
425
426" Find out the background and foreground color.
427let s:fgc = s:HtmlColor(synIDattr(hlID("Normal"), "fg#", s:whatterm))
428let s:bgc = s:HtmlColor(synIDattr(hlID("Normal"), "bg#", s:whatterm))
429if s:fgc == ""
430 let s:fgc = ( &background == "dark" ? "#ffffff" : "#000000" )
431endif
432if s:bgc == ""
433 let s:bgc = ( &background == "dark" ? "#000000" : "#ffffff" )
434endif
435
436" Normal/global attributes
437" For Netscape 4, set <body> attributes too, though, strictly speaking, it's
438" incorrect.
439if exists("html_use_css")
440 if exists("html_no_pre")
441 execute "normal! A\nbody { color: " . s:fgc . "; background-color: " . s:bgc . "; font-family: Courier, monospace; }\e"
442 else
443 execute "normal! A\npre { color: " . s:fgc . "; background-color: " . s:bgc . "; }\e"
444 yank
445 put
446 execute "normal! ^cwbody\e"
447 endif
448else
449 if exists("html_no_pre")
450 execute '%s:<body>:<body ' . 'bgcolor="' . s:bgc . '" text="' . s:fgc . '" style="font-family\: Courier, monospace;">'
451 else
452 execute '%s:<body>:<body ' . 'bgcolor="' . s:bgc . '" text="' . s:fgc . '">'
453 endif
454endif
455
456" Line numbering attributes
457if s:numblines
458 if exists("html_use_css")
459 execute "normal! A\n.lnr { " . s:CSS1(hlID("LineNr")) . "}\e"
460 else
461 execute '%s+<span class="lnr">\([^<]*\)</span>+' . s:HtmlOpening(hlID("LineNr")) . '\1' . s:HtmlClosing(hlID("LineNr")) . '+g'
462 endif
463endif
464
465" Gather attributes for all other classes
466let s:idlist = strpart(s:idlist, 1)
467while s:idlist != ""
468 let s:attr = ""
469 let s:col = stridx(s:idlist, ",")
470 let s:id = strpart(s:idlist, 0, s:col)
471 let s:idlist = strpart(s:idlist, s:col + 1)
472 let s:attr = s:CSS1(s:id)
473 let s:id_name = synIDattr(s:id, "name", s:whatterm)
474 " If the class has some attributes, export the style, otherwise DELETE all
475 " its occurences to make the HTML shorter
476 if s:attr != ""
477 if exists("html_use_css")
478 execute "normal! A\n." . s:id_name . " { " . s:attr . "}"
479 else
480 execute '%s+<span class="' . s:id_name . '">\([^<]*\)</span>+' . s:HtmlOpening(s:id) . '\1' . s:HtmlClosing(s:id) . '+g'
481 endif
482 else
483 execute '%s+<span class="' . s:id_name . '">\([^<]*\)</span>+\1+g'
484 if exists("html_use_css")
485 1;/<style type="text/+1
486 endif
487 endif
488endwhile
489
490" Add hyperlinks
491%s+\(http://\S\{-}\)\(\([.,;:}]\=\(\s\|$\)\)\|[\\"'<>]\|&gt;\|&lt;\)+<A HREF="\1">\1</A>\2+ge
492
493" The DTD
494if exists("html_use_css")
495 exe "normal! gg0i<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\" \"http://www.w3.org/TR/html4/strict.dtd\">\n\e"
496endif
497
498" Cleanup
499%s:\s\+$::e
500
501" Restore old settings
502let &report = s:old_report
503let &title = s:old_title
504let &icon = s:old_icon
505let &paste = s:old_paste
506let &magic = s:old_magic
507let @/ = s:old_search
508exe s:orgwin . "wincmd w"
509let &l:et = s:old_et
510exe s:newwin . "wincmd w"
511
512" Save a little bit of memory (worth doing?)
513unlet s:old_et s:old_paste s:old_icon s:old_report s:old_title s:old_search
514unlet s:whatterm s:idlist s:lnum s:end s:fgc s:bgc s:old_magic
515unlet! s:col s:id s:attr s:len s:line s:new s:did_retab s:numblines
516unlet s:orgwin s:newwin s:orgbufnr
517delfunc s:HtmlColor
518delfunc s:CSS1
519if !exists("html_use_css")
520 delfunc s:HtmlOpening
521 delfunc s:HtmlClosing
522endif
Bram Moolenaar47136d72004-10-12 20:02:24 +0000523silent! unlet s:htmlfoldtext s:fillerline s:diffattr