Start anew
[msysgit.git] / share / vim / vim58 / syntax / 2html.vim
blob099a4554fc11ae7bd146f21c7c1fe1197ed5ce13
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
12 " a:txt   text
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
28         if a:bg != ""
29           let bgs = '<SPAN style="background-color: ' . a:bg . '">'
30           let bge = '</SPAN>'
31         endif
32         let fgs = ""    " code for foreground color start
33         let fge = ""    " code for foreground color end
34         if a:fg != ""
35           let fgs = '<FONT color=' . a:fg . ">"
36           let fge = '</FONT>'
37         endif
38         return substitute(a:attr, '.', '<&>', 'g') . bgs . fgs . substitute(substitute(substitute(substitute(substitute(a:txt, '&', '\&amp;', 'g'), '<', '\&lt;', 'g'), '>', '\&gt;', 'g'), '"', '\&quot;', 'g'), "\x0c", '<HR class=PAGE-BREAK>', 'g') . fge . bge . substitute(a:attr[2] . a:attr[1] . a:attr[0], '.', '</&>', 'g')
39 endfun
42 if &t_Co == 8
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"
51 else
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"
68 endif
70 function! HTMLColor(c)
71   if exists("g:cterm_color" . a:c)
72     execute "return g:cterm_color" . a:c
73   else
74     return ""
75   endif
76 endfun
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
81 let old_icon = &icon
82 let old_paste = &paste
83 let old_et = &et
84 set notitle noicon paste et
86 " Split window to create a buffer with the HTML file.
87 if expand("%") == ""
88   new Untitled.html
89 else
90   new %.html
91 endif
92 1,$d
93 set noet
94 " Find out the background and foreground color.
95 if has("gui_running")
96   let bg = synIDattr(highlightID("Normal"), "bg#", "gui")
97   let fg = synIDattr(highlightID("Normal"), "fg#", "gui")
98 else
99   let bg = HTMLColor(synIDattr(highlightID("Normal"), "bg", "cterm"))
100   let fg = HTMLColor(synIDattr(highlightID("Normal"), "fg", "cterm"))
101 endif
102 if bg == ""
103    if &background == "dark"
104      let bg = "#000000"
105      if fg == ""
106        let fg = "#FFFFFF"
107      endif
108    else
109      let bg = "#FFFFFF"
110      if fg == ""
111        let fg = "#000000"
112      endif
113    endif
114 endif
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"
119 if fg != ""
120   exe "normal a TEXT=".fg."\e"
121 endif
122 exe "normal a>\n<PRE>\n\e"
124 exe "normal \<C-W>p"
126 " Some 'constants' for ease of addressing with []
127 let uline = "U"
128 let bld = "B"
129 let itl = "I"
131 " Loop over all lines in the original text
132 let end = line("$")
133 let lnum = 1
134 while lnum <= end
136   " Get the current line, with tabs expanded to spaces when needed
137   let line = getline(lnum)
138   if match(line, "\t") >= 0
139     exe lnum . "retab!"
140     let did_retab = 1
141     let line = getline(lnum)
142   else
143     let did_retab = 0
144   endif
145   let len = strlen(line)
146   let new = ""
148   if exists("html_number_color")
149     let new = '<FONT COLOR=' . html_number_color . '>' . strpart('        ', 0, strlen(line("$")) - strlen(lnum)) . lnum . '</FONT>  '
150   endif
152   " Loop over each character in the line
153   let col = 1
154   while col <= len
155     let startcol = col " The start column for processing text
156     let id = synID(lnum, col, 1)
157     let col = 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))
167     else
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))
169     endif
170     if col > len
171       break
172     endif
173   endwhile
174   if did_retab
175     undo
176   endif
178   exe "normal \<C-W>pa" . strtrans(new) . "\n\e\<C-W>p"
179   let lnum = lnum + 1
180   +
181 endwhile
182 " Finish with the last line
183 exe "normal \<C-W>pa</PRE>\n</BODY>\n</HTML>\e"
185 let &title = old_title
186 let &icon = old_icon
187 let &paste = old_paste
188 exe "normal \<C-W>p"
189 let &et = old_et
190 exe "normal \<C-W>p"
192 " In case they didn't get used
193 let startcol = 0
194 let id = 0
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
199 if &t_Co != 8
200   unlet cterm_color8 cterm_color9 cterm_color10 cterm_color11
201   unlet cterm_color12 cterm_color13 cterm_color14 cterm_color15
202 endif
203 delfunc HTMLPutText
204 delfunc HTMLColor