1 " Vim syntax support file
2 " Maintainer: Bram Moolenaar <Bram@vim.org>
3 " Last Change: 2001 May 10
5 " Transform a file into HTML, using the current syntax highlighting.
7 " function to produce text with HTML codes for attributes and colors
9 " a:attr contains 'U' for underline, 'I' for italic and 'B' for bold
10 " a:fg foregound color name
11 " a:bg background color name
14 " the big return statement concatenates:
15 " - the code to start underline/italic/bold, substituting each 'U', 'I' or 'B'
16 " by the same character inside <>
17 " - the code to start the background color
18 " - the code to start the foreground color
19 " - the text, where each '&', '<', '>' and '"' is translated for their special
20 " meaning. A CTRL-L is translated into a page break
21 " - the code to end the foreground color
22 " - the code to end the background color
23 " - the code to end underline/italic/bold, substituting each 'U', 'I' or 'B'
24 " by the same character inside </>, in reverse order
25 function! HTMLPutText(attr, bg, fg, txt)
26 let bgs = "" " code for background color start
27 let bge = "" " code for background color end
29 let bgs = '<SPAN style="background-color: ' . a:bg . '">'
32 let fgs = "" " code for foreground color start
33 let fge = "" " code for foreground color end
35 let fgs = '<FONT color=' . a:fg . ">"
38 return substitute(a:attr, '.', '<&>', 'g') . bgs . fgs . substitute(substitute(substitute(substitute(substitute(a:txt, '&', '\&', 'g'), '<', '\<', 'g'), '>', '\>', 'g'), '"', '\"', 'g'), "\x0c", '<HR class=PAGE-BREAK>', 'g') . fge . bge . substitute(a:attr[2] . a:attr[1] . a:attr[0], '.', '</&>', 'g')
43 let cterm_color0 = "#808080"
44 let cterm_color1 = "#ff6060"
45 let cterm_color2 = "#00ff00"
46 let cterm_color3 = "#ffff00"
47 let cterm_color4 = "#8080ff"
48 let cterm_color5 = "#ff40ff"
49 let cterm_color6 = "#00ffff"
50 let cterm_color7 = "#ffffff"
52 let cterm_color0 = "#000000"
53 let cterm_color1 = "#c00000"
54 let cterm_color2 = "#008000"
55 let cterm_color3 = "#804000"
56 let cterm_color4 = "#0000c0"
57 let cterm_color5 = "#c000c0"
58 let cterm_color6 = "#008080"
59 let cterm_color7 = "#c0c0c0"
60 let cterm_color8 = "#808080"
61 let cterm_color9 = "#ff6060"
62 let cterm_color10 = "#00ff00"
63 let cterm_color11 = "#ffff00"
64 let cterm_color12 = "#8080ff"
65 let cterm_color13 = "#ff40ff"
66 let cterm_color14 = "#00ffff"
67 let cterm_color15 = "#ffffff"
70 function! HTMLColor(c)
71 if exists("g:cterm_color" . a:c)
72 execute "return g:cterm_color" . a:c
78 " Set some options to make it work faster.
79 " Expand tabs in original buffer to get 'tabstop' correctly used.
80 let old_title = &title
82 let old_paste = &paste
84 set notitle noicon paste et
86 " Split window to create a buffer with the HTML file.
94 " Find out the background and foreground color.
96 let bg = synIDattr(highlightID("Normal"), "bg#", "gui")
97 let fg = synIDattr(highlightID("Normal"), "fg#", "gui")
99 let bg = HTMLColor(synIDattr(highlightID("Normal"), "bg", "cterm"))
100 let fg = HTMLColor(synIDattr(highlightID("Normal"), "fg", "cterm"))
103 if &background == "dark"
116 " Insert HTML header, with the background color. Add the foreground color
117 " only when it is defined.
118 exe "normal a<HTML>\n<HEAD>\n<TITLE>".expand("%:t")."</TITLE>\n</HEAD>\n<BODY BGcolor=".bg."\e"
120 exe "normal a TEXT=".fg."\e"
122 exe "normal a>\n<PRE>\n\e"
126 " Some 'constants' for ease of addressing with []
131 " Loop over all lines in the original text
136 " Get the current line, with tabs expanded to spaces when needed
137 let line = getline(lnum)
138 if match(line, "\t") >= 0
141 let line = getline(lnum)
145 let len = strlen(line)
148 if exists("html_number_color")
149 let new = '<FONT COLOR=' . html_number_color . '>' . strpart(' ', 0, strlen(line("$")) - strlen(lnum)) . lnum . '</FONT> '
152 " Loop over each character in the line
155 let startcol = col " The start column for processing text
156 let id = synID(lnum, col, 1)
158 " Speed loop (it's small - that's the trick)
159 " Go along till we find a change in synID
160 while col <= len && id == synID(lnum, col, 1) | let col = col + 1 | endwhile
162 " output the text with the same synID, with all its attributes
163 " The first part turns attributes into [U][I][B]
164 let id = synIDtrans(id)
165 if has("gui_running")
166 let new = new . HTMLPutText(uline[synIDattr(id, "underline", "gui") - 1] . itl[synIDattr(id, "italic", "gui") - 1] . bld[synIDattr(id, "bold", "gui") - 1], synIDattr(id, "bg#", "gui"), synIDattr(id, "fg#", "gui"), strpart(line, startcol - 1, col - startcol))
168 let new = new . HTMLPutText(uline[synIDattr(id, "underline", "cterm") - 1] . itl[synIDattr(id, "italic", "cterm") - 1] . bld[synIDattr(id, "bold", "cterm") - 1], HTMLColor(synIDattr(id, "bg", "cterm")), HTMLColor(synIDattr(id, "fg", "cterm")), strpart(line, startcol - 1, col - startcol))
178 exe "normal \<C-W>pa" . strtrans(new) . "\n\e\<C-W>p"
182 " Finish with the last line
183 exe "normal \<C-W>pa</PRE>\n</BODY>\n</HTML>\e"
185 let &title = old_title
187 let &paste = old_paste
192 " In case they didn't get used
195 unlet uline bld itl lnum end col startcol line len new id
196 unlet old_title old_icon old_paste old_et did_retab bg fg
197 unlet cterm_color0 cterm_color1 cterm_color2 cterm_color3
198 unlet cterm_color4 cterm_color5 cterm_color6 cterm_color7
200 unlet cterm_color8 cterm_color9 cterm_color10 cterm_color11
201 unlet cterm_color12 cterm_color13 cterm_color14 cterm_color15