xref: /vim-8.2.3635/runtime/syntax/2html.vim (revision b02cbe34)
1" Vim syntax support file
2" Maintainer: Bram Moolenaar <[email protected]>
3" Last Change: 2009 Jul 14
4"	       (modified by David Ne\v{c}as (Yeti) <[email protected]>)
5"	       (XHTML support by Panagiotis Issaris <[email protected]>)
6"	       (made w3 compliant by Edd Barrett <[email protected]>)
7"	       (added html_font. Edd Barrett <[email protected]>)
8"	       (dynamic folding by Ben Fritz <[email protected]>)
9
10" Transform a file into HTML, using the current syntax highlighting.
11
12" this file uses line continuations
13let s:cpo_sav = &cpo
14set cpo-=C
15
16" Number lines when explicitely requested or when `number' is set
17if exists("g:html_number_lines")
18  let s:numblines = html_number_lines
19else
20  let s:numblines = &number
21endif
22
23" Font
24if exists("g:html_font")
25  let s:htmlfont = html_font . ", monospace"
26else
27  let s:htmlfont = "monospace"
28endif
29
30" make copies of the user-defined settings that we may overrule
31if exists("g:html_dynamic_folds")
32  let s:html_dynamic_folds = 1
33endif
34if exists("g:html_hover_unfold")
35  let s:html_hover_unfold = 1
36endif
37if exists("g:html_use_css")
38  let s:html_use_css = 1
39endif
40
41" hover opening implies dynamic folding
42if exists("s:html_hover_unfold")
43  let s:html_dynamic_folds = 1
44endif
45
46" dynamic folding with no foldcolumn implies hover opens
47if exists("s:html_dynamic_folds") && exists("g:html_no_foldcolumn")
48  let s:html_hover_unfold = 1
49endif
50
51" ignore folding overrides dynamic folding
52if exists("g:html_ignore_folding") && exists("s:html_dynamic_folds")
53  unlet s:html_dynamic_folds
54endif
55
56" dynamic folding implies css
57if exists("s:html_dynamic_folds")
58  let s:html_use_css = 1
59endif
60
61" When not in gui we can only guess the colors.
62if has("gui_running")
63  let s:whatterm = "gui"
64else
65  let s:whatterm = "cterm"
66  if &t_Co == 8
67    let s:cterm_color = {0: "#808080", 1: "#ff6060", 2: "#00ff00", 3: "#ffff00", 4: "#8080ff", 5: "#ff40ff", 6: "#00ffff", 7: "#ffffff"}
68  else
69    let s:cterm_color = {0: "#000000", 1: "#c00000", 2: "#008000", 3: "#804000", 4: "#0000c0", 5: "#c000c0", 6: "#008080", 7: "#c0c0c0", 8: "#808080", 9: "#ff6060", 10: "#00ff00", 11: "#ffff00", 12: "#8080ff", 13: "#ff40ff", 14: "#00ffff", 15: "#ffffff"}
70
71    " Colors for 88 and 256 come from xterm.
72    if &t_Co == 88
73      call extend(s:cterm_color, {16: "#000000", 17: "#00008b", 18: "#0000cd", 19: "#0000ff", 20: "#008b00", 21: "#008b8b", 22: "#008bcd", 23: "#008bff", 24: "#00cd00", 25: "#00cd8b", 26: "#00cdcd", 27: "#00cdff", 28: "#00ff00", 29: "#00ff8b", 30: "#00ffcd", 31: "#00ffff", 32: "#8b0000", 33: "#8b008b", 34: "#8b00cd", 35: "#8b00ff", 36: "#8b8b00", 37: "#8b8b8b", 38: "#8b8bcd", 39: "#8b8bff", 40: "#8bcd00", 41: "#8bcd8b", 42: "#8bcdcd", 43: "#8bcdff", 44: "#8bff00", 45: "#8bff8b", 46: "#8bffcd", 47: "#8bffff", 48: "#cd0000", 49: "#cd008b", 50: "#cd00cd", 51: "#cd00ff", 52: "#cd8b00", 53: "#cd8b8b", 54: "#cd8bcd", 55: "#cd8bff", 56: "#cdcd00", 57: "#cdcd8b", 58: "#cdcdcd", 59: "#cdcdff", 60: "#cdff00", 61: "#cdff8b", 62: "#cdffcd", 63: "#cdffff", 64: "#ff0000"})
74      call extend(s:cterm_color, {65: "#ff008b", 66: "#ff00cd", 67: "#ff00ff", 68: "#ff8b00", 69: "#ff8b8b", 70: "#ff8bcd", 71: "#ff8bff", 72: "#ffcd00", 73: "#ffcd8b", 74: "#ffcdcd", 75: "#ffcdff", 76: "#ffff00", 77: "#ffff8b", 78: "#ffffcd", 79: "#ffffff", 80: "#2e2e2e", 81: "#5c5c5c", 82: "#737373", 83: "#8b8b8b", 84: "#a2a2a2", 85: "#b9b9b9", 86: "#d0d0d0", 87: "#e7e7e7"})
75    elseif &t_Co == 256
76      call extend(s:cterm_color, {16: "#000000", 17: "#00005f", 18: "#000087", 19: "#0000af", 20: "#0000d7", 21: "#0000ff", 22: "#005f00", 23: "#005f5f", 24: "#005f87", 25: "#005faf", 26: "#005fd7", 27: "#005fff", 28: "#008700", 29: "#00875f", 30: "#008787", 31: "#0087af", 32: "#0087d7", 33: "#0087ff", 34: "#00af00", 35: "#00af5f", 36: "#00af87", 37: "#00afaf", 38: "#00afd7", 39: "#00afff", 40: "#00d700", 41: "#00d75f", 42: "#00d787", 43: "#00d7af", 44: "#00d7d7", 45: "#00d7ff", 46: "#00ff00", 47: "#00ff5f", 48: "#00ff87", 49: "#00ffaf", 50: "#00ffd7", 51: "#00ffff", 52: "#5f0000", 53: "#5f005f", 54: "#5f0087", 55: "#5f00af", 56: "#5f00d7", 57: "#5f00ff", 58: "#5f5f00", 59: "#5f5f5f", 60: "#5f5f87", 61: "#5f5faf", 62: "#5f5fd7", 63: "#5f5fff", 64: "#5f8700"})
77      call extend(s:cterm_color, {65: "#5f875f", 66: "#5f8787", 67: "#5f87af", 68: "#5f87d7", 69: "#5f87ff", 70: "#5faf00", 71: "#5faf5f", 72: "#5faf87", 73: "#5fafaf", 74: "#5fafd7", 75: "#5fafff", 76: "#5fd700", 77: "#5fd75f", 78: "#5fd787", 79: "#5fd7af", 80: "#5fd7d7", 81: "#5fd7ff", 82: "#5fff00", 83: "#5fff5f", 84: "#5fff87", 85: "#5fffaf", 86: "#5fffd7", 87: "#5fffff", 88: "#870000", 89: "#87005f", 90: "#870087", 91: "#8700af", 92: "#8700d7", 93: "#8700ff", 94: "#875f00", 95: "#875f5f", 96: "#875f87", 97: "#875faf", 98: "#875fd7", 99: "#875fff", 100: "#878700", 101: "#87875f", 102: "#878787", 103: "#8787af", 104: "#8787d7", 105: "#8787ff", 106: "#87af00", 107: "#87af5f", 108: "#87af87", 109: "#87afaf", 110: "#87afd7", 111: "#87afff", 112: "#87d700"})
78      call extend(s:cterm_color, {113: "#87d75f", 114: "#87d787", 115: "#87d7af", 116: "#87d7d7", 117: "#87d7ff", 118: "#87ff00", 119: "#87ff5f", 120: "#87ff87", 121: "#87ffaf", 122: "#87ffd7", 123: "#87ffff", 124: "#af0000", 125: "#af005f", 126: "#af0087", 127: "#af00af", 128: "#af00d7", 129: "#af00ff", 130: "#af5f00", 131: "#af5f5f", 132: "#af5f87", 133: "#af5faf", 134: "#af5fd7", 135: "#af5fff", 136: "#af8700", 137: "#af875f", 138: "#af8787", 139: "#af87af", 140: "#af87d7", 141: "#af87ff", 142: "#afaf00", 143: "#afaf5f", 144: "#afaf87", 145: "#afafaf", 146: "#afafd7", 147: "#afafff", 148: "#afd700", 149: "#afd75f", 150: "#afd787", 151: "#afd7af", 152: "#afd7d7", 153: "#afd7ff", 154: "#afff00", 155: "#afff5f", 156: "#afff87", 157: "#afffaf", 158: "#afffd7"})
79      call extend(s:cterm_color, {159: "#afffff", 160: "#d70000", 161: "#d7005f", 162: "#d70087", 163: "#d700af", 164: "#d700d7", 165: "#d700ff", 166: "#d75f00", 167: "#d75f5f", 168: "#d75f87", 169: "#d75faf", 170: "#d75fd7", 171: "#d75fff", 172: "#d78700", 173: "#d7875f", 174: "#d78787", 175: "#d787af", 176: "#d787d7", 177: "#d787ff", 178: "#d7af00", 179: "#d7af5f", 180: "#d7af87", 181: "#d7afaf", 182: "#d7afd7", 183: "#d7afff", 184: "#d7d700", 185: "#d7d75f", 186: "#d7d787", 187: "#d7d7af", 188: "#d7d7d7", 189: "#d7d7ff", 190: "#d7ff00", 191: "#d7ff5f", 192: "#d7ff87", 193: "#d7ffaf", 194: "#d7ffd7", 195: "#d7ffff", 196: "#ff0000", 197: "#ff005f", 198: "#ff0087", 199: "#ff00af", 200: "#ff00d7", 201: "#ff00ff", 202: "#ff5f00", 203: "#ff5f5f", 204: "#ff5f87"})
80      call extend(s:cterm_color, {205: "#ff5faf", 206: "#ff5fd7", 207: "#ff5fff", 208: "#ff8700", 209: "#ff875f", 210: "#ff8787", 211: "#ff87af", 212: "#ff87d7", 213: "#ff87ff", 214: "#ffaf00", 215: "#ffaf5f", 216: "#ffaf87", 217: "#ffafaf", 218: "#ffafd7", 219: "#ffafff", 220: "#ffd700", 221: "#ffd75f", 222: "#ffd787", 223: "#ffd7af", 224: "#ffd7d7", 225: "#ffd7ff", 226: "#ffff00", 227: "#ffff5f", 228: "#ffff87", 229: "#ffffaf", 230: "#ffffd7", 231: "#ffffff", 232: "#080808", 233: "#121212", 234: "#1c1c1c", 235: "#262626", 236: "#303030", 237: "#3a3a3a", 238: "#444444", 239: "#4e4e4e", 240: "#585858", 241: "#626262", 242: "#6c6c6c", 243: "#767676", 244: "#808080", 245: "#8a8a8a", 246: "#949494", 247: "#9e9e9e", 248: "#a8a8a8", 249: "#b2b2b2", 250: "#bcbcbc", 251: "#c6c6c6", 252: "#d0d0d0", 253: "#dadada", 254: "#e4e4e4", 255: "#eeeeee"})
81    endif
82  endif
83endif
84
85" Return good color specification: in GUI no transformation is done, in
86" terminal return RGB values of known colors and empty string for unknown
87if s:whatterm == "gui"
88  function! s:HtmlColor(color)
89    return a:color
90  endfun
91else
92  function! s:HtmlColor(color)
93    if has_key(s:cterm_color, a:color)
94      return s:cterm_color[a:color]
95    else
96      return ""
97    endif
98  endfun
99endif
100
101if !exists("s:html_use_css")
102  " Return opening HTML tag for given highlight id
103  function! s:HtmlOpening(id)
104    let a = ""
105    if synIDattr(a:id, "inverse")
106      " For inverse, we always must set both colors (and exchange them)
107      let x = s:HtmlColor(synIDattr(a:id, "fg#", s:whatterm))
108      let a = a . '<span style="background-color: ' . ( x != "" ? x : s:fgc ) . '">'
109      let x = s:HtmlColor(synIDattr(a:id, "bg#", s:whatterm))
110      let a = a . '<font color="' . ( x != "" ? x : s:bgc ) . '">'
111    else
112      let x = s:HtmlColor(synIDattr(a:id, "bg#", s:whatterm))
113      if x != "" | let a = a . '<span style="background-color: ' . x . '">' | endif
114      let x = s:HtmlColor(synIDattr(a:id, "fg#", s:whatterm))
115      if x != "" | let a = a . '<font color="' . x . '">' | endif
116    endif
117    if synIDattr(a:id, "bold") | let a = a . "<b>" | endif
118    if synIDattr(a:id, "italic") | let a = a . "<i>" | endif
119    if synIDattr(a:id, "underline") | let a = a . "<u>" | endif
120    return a
121  endfun
122
123  " Return closing HTML tag for given highlight id
124  function s:HtmlClosing(id)
125    let a = ""
126    if synIDattr(a:id, "underline") | let a = a . "</u>" | endif
127    if synIDattr(a:id, "italic") | let a = a . "</i>" | endif
128    if synIDattr(a:id, "bold") | let a = a . "</b>" | endif
129    if synIDattr(a:id, "inverse")
130      let a = a . '</font></span>'
131    else
132      let x = s:HtmlColor(synIDattr(a:id, "fg#", s:whatterm))
133      if x != "" | let a = a . '</font>' | endif
134      let x = s:HtmlColor(synIDattr(a:id, "bg#", s:whatterm))
135      if x != "" | let a = a . '</span>' | endif
136    endif
137    return a
138  endfun
139endif
140
141" Return HTML valid characters enclosed in a span of class style_name with
142" unprintable characters expanded and double spaces replaced as necessary.
143function! s:HtmlFormat(text, style_name)
144  " Replace unprintable characters
145  let formatted = strtrans(a:text)
146
147  " Replace the reserved html characters
148  let formatted = substitute(substitute(substitute(substitute(substitute(formatted, '&', '\&amp;', 'g'), '<', '\&lt;', 'g'), '>', '\&gt;', 'g'), '"', '\&quot;', 'g'), "\x0c", '<hr class="PAGE-BREAK">', 'g')
149
150  " Replace double spaces and leading spaces
151  if ' ' != s:HtmlSpace
152    let formatted = substitute(formatted, '  ', s:HtmlSpace . s:HtmlSpace, 'g')
153    let formatted = substitute(formatted, '^ ', s:HtmlSpace, 'g')
154  endif
155
156  " Enclose in a span of class style_name
157  let formatted = '<span class="' . a:style_name . '">' . formatted . '</span>'
158
159  " Add the class to class list if it's not there yet
160  let s:id = hlID(a:style_name)
161  if stridx(s:idlist, "," . s:id . ",") == -1
162    let s:idlist = s:idlist . s:id . ","
163  endif
164
165  return formatted
166endfun
167
168" Return CSS style describing given highlight id (can be empty)
169function! s:CSS1(id)
170  let a = ""
171  if synIDattr(a:id, "inverse")
172    " For inverse, we always must set both colors (and exchange them)
173    let x = s:HtmlColor(synIDattr(a:id, "bg#", s:whatterm))
174    let a = a . "color: " . ( x != "" ? x : s:bgc ) . "; "
175    let x = s:HtmlColor(synIDattr(a:id, "fg#", s:whatterm))
176    let a = a . "background-color: " . ( x != "" ? x : s:fgc ) . "; "
177  else
178    let x = s:HtmlColor(synIDattr(a:id, "fg#", s:whatterm))
179    if x != "" | let a = a . "color: " . x . "; " | endif
180    let x = s:HtmlColor(synIDattr(a:id, "bg#", s:whatterm))
181    if x != "" | let a = a . "background-color: " . x . "; " | endif
182  endif
183  if synIDattr(a:id, "bold") | let a = a . "font-weight: bold; " | endif
184  if synIDattr(a:id, "italic") | let a = a . "font-style: italic; " | endif
185  if synIDattr(a:id, "underline") | let a = a . "text-decoration: underline; " | endif
186  return a
187endfun
188
189if exists("s:html_dynamic_folds")
190
191  " compares two folds as stored in our list of folds
192  " A fold is "less" than another if it starts at an earlier line number,
193  " or ends at a later line number, ties broken by fold level
194  function! s:FoldCompare(f1, f2)
195    if a:f1.firstline != a:f2.firstline
196      " put it before if it starts earlier
197      return a:f1.firstline - a:f2.firstline
198    elseif a:f1.lastline != a:f2.lastline
199      " put it before if it ends later
200      return a:f2.lastline - a:f1.lastline
201    else
202      " if folds begin and end on the same lines, put lowest fold level first
203      return a:f1.level - a:f2.level
204    endif
205  endfunction
206
207endif
208
209" Figure out proper MIME charset from the 'encoding' option.
210if exists("g:html_use_encoding")
211  let s:html_encoding = html_use_encoding
212else
213  let s:vim_encoding = &encoding
214  if s:vim_encoding =~ '^8bit\|^2byte'
215    let s:vim_encoding = substitute(s:vim_encoding, '^8bit-\|^2byte-', '', '')
216  endif
217  if s:vim_encoding == 'latin1'
218    let s:html_encoding = 'iso-8859-1'
219  elseif s:vim_encoding =~ "^cp12"
220    let s:html_encoding = substitute(s:vim_encoding, 'cp', 'windows-', '')
221  elseif s:vim_encoding == 'sjis' || s:vim_encoding == 'cp932'
222    let s:html_encoding = 'Shift_JIS'
223  elseif s:vim_encoding == 'big5' || s:vim_encoding == 'cp950'
224    let s:html_encoding = "Big5"
225  elseif s:vim_encoding == 'euc-cn'
226    let s:html_encoding = 'GB_2312-80'
227  elseif s:vim_encoding == 'euc-tw'
228    let s:html_encoding = ""
229  elseif s:vim_encoding =~ '^euc\|^iso\|^koi'
230    let s:html_encoding = substitute(s:vim_encoding, '.*', '\U\0', '')
231  elseif s:vim_encoding == 'cp949'
232    let s:html_encoding = 'KS_C_5601-1987'
233  elseif s:vim_encoding == 'cp936'
234    let s:html_encoding = 'GBK'
235  elseif s:vim_encoding =~ '^ucs\|^utf'
236    let s:html_encoding = 'UTF-8'
237  else
238    let s:html_encoding = ""
239  endif
240endif
241
242
243" Set some options to make it work faster.
244" Don't report changes for :substitute, there will be many of them.
245let s:old_title = &title
246let s:old_icon = &icon
247let s:old_et = &l:et
248let s:old_report = &report
249let s:old_search = @/
250set notitle noicon
251setlocal et
252set report=1000000
253
254" Split window to create a buffer with the HTML file.
255let s:orgbufnr = winbufnr(0)
256if expand("%") == ""
257  new Untitled.html
258else
259  new %.html
260endif
261let s:newwin = winnr()
262let s:orgwin = bufwinnr(s:orgbufnr)
263
264set modifiable
265%d
266let s:old_paste = &paste
267set paste
268let s:old_magic = &magic
269set magic
270
271if exists("g:use_xhtml")
272  if s:html_encoding != ""
273    exe "normal!  a<?xml version=\"1.0\" encoding=\"" . s:html_encoding . "\"?>\n\e"
274  else
275    exe "normal! a<?xml version=\"1.0\"?>\n\e"
276  endif
277  let s:tag_close = ' />'
278else
279  let s:tag_close = '>'
280endif
281
282" Cache html_no_pre in case we have to turn it on for non-css mode
283if exists("g:html_no_pre")
284  let s:old_html_no_pre = html_no_pre
285endif
286
287if !exists("s:html_use_css")
288  " Can't put font tags in <pre>
289  let html_no_pre=1
290endif
291
292let s:HtmlSpace = ' '
293let s:LeadingSpace = ' '
294let s:HtmlEndline = ''
295if exists("g:html_no_pre")
296  let s:HtmlEndline = '<br' . s:tag_close
297  let s:LeadingSpace = '&nbsp;'
298  let s:HtmlSpace = '\' . s:LeadingSpace
299endif
300
301" HTML header, with the title and generator ;-). Left free space for the CSS,
302" to be filled at the end.
303exe "normal! a<html>\n\e"
304exe "normal! a<head>\n<title>" . expand("%:p:~") . "</title>\n\e"
305exe "normal! a<meta name=\"Generator\" content=\"Vim/" . v:version/100 . "." . v:version %100 . '"' . s:tag_close . "\n\e"
306if s:html_encoding != ""
307  exe "normal! a<meta http-equiv=\"content-type\" content=\"text/html; charset=" . s:html_encoding . '"' . s:tag_close . "\n\e"
308endif
309
310if exists("s:html_use_css")
311  if exists("s:html_dynamic_folds")
312    if exists("s:html_hover_unfold")
313      " if we are doing hover_unfold, use css 2 with css 1 fallback for IE6
314      exe "normal! a".
315	  \ "<style type=\"text/css\">\n<!--\n".
316	  \ ".FoldColumn { text-decoration: none; white-space: pre; }\n\n".
317	  \ "body * { margin: 0; padding: 0; }\n".
318	  \ "\n".
319	  \ ".open-fold   > .Folded { display: none;  }\n".
320	  \ ".open-fold   > .fulltext { display: inline; }\n".
321	  \ ".closed-fold > .fulltext { display: none;  }\n".
322	  \ ".closed-fold > .Folded { display: inline; }\n".
323	  \ "\n".
324	  \ ".open-fold   > .toggle-open   { display: none;   }\n".
325	  \ ".open-fold   > .toggle-closed { display: inline; }\n".
326	  \ ".closed-fold > .toggle-open   { display: inline; }\n".
327	  \ ".closed-fold > .toggle-closed { display: none;   }\n"
328      exe "normal! a\n/* opening a fold while hovering won't be supported by IE6 and other\n".
329	  \ "similar browsers, but it should fail gracefully. */\n".
330	  \ ".closed-fold:hover > .fulltext { display: inline; }\n".
331	  \ ".closed-fold:hover > .toggle-filler { display: none; }\n".
332	  \ ".closed-fold:hover > .Folded { display: none; }\n"
333      exe "normal! a-->\n</style>\n"
334      exe "normal! a<!--[if lt IE 7]>".
335	  \ "<style type=\"text/css\">\n".
336	  \ ".open-fold   .Folded      { display: none; }\n".
337	  \ ".open-fold   .fulltext      { display: inline; }\n".
338	  \ ".open-fold   .toggle-open   { display: none; }\n".
339	  \ ".closed-fold .toggle-closed { display: inline; }\n".
340	  \ "\n".
341	  \ ".closed-fold .fulltext      { display: none; }\n".
342	  \ ".closed-fold .Folded      { display: inline; }\n".
343	  \ ".closed-fold .toggle-open   { display: inline; }\n".
344	  \ ".closed-fold .toggle-closed { display: none; }\n".
345	  \ "</style>\n".
346	  \ "<![endif]-->\n"
347    else
348      " if we aren't doing hover_unfold, use CSS 1 only
349      exe "normal! a<style type=\"text/css\">\n<!--\n".
350	    \ ".FoldColumn { text-decoration: none; white-space: pre; }\n\n".
351	    \ ".open-fold   .Folded      { display: none; }\n".
352	    \ ".open-fold   .fulltext      { display: inline; }\n".
353	    \ ".open-fold   .toggle-open   { display: none; }\n".
354	    \ ".closed-fold .toggle-closed { display: inline; }\n".
355	    \ "\n".
356	    \ ".closed-fold .fulltext      { display: none; }\n".
357	    \ ".closed-fold .Folded      { display: inline; }\n".
358	    \ ".closed-fold .toggle-open   { display: inline; }\n".
359	    \ ".closed-fold .toggle-closed { display: none; }\n".
360	    \ "-->\n</style>\n"
361    endif
362  else
363    " if we aren't doing any dynamic folding, no need for any special rules
364    exe "normal! a<style type=\"text/css\">\n<!--\n-->\n</style>\n\e"
365  endif
366endif
367
368" insert javascript to toggle folds open and closed
369if exists("s:html_dynamic_folds")
370  exe "normal! a\n".
371	\ "<script type='text/javascript'>\n".
372	\ "<!--\n".
373	\ "function toggleFold(objID)\n".
374	\ "{\n".
375	\ "  var fold;\n".
376	\ "  fold = document.getElementById(objID);\n".
377	\ "  if(fold.className == 'closed-fold')\n".
378	\ "  {\n".
379	\ "    fold.className = 'open-fold';\n".
380	\ "  }\n".
381	\ "  else if (fold.className == 'open-fold')\n".
382	\ "  {\n".
383	\ "    fold.className = 'closed-fold';\n".
384	\ "  }\n".
385	\ "}\n".
386	\ "-->\n".
387	\ "</script>\n\e"
388endif
389
390if exists("g:html_no_pre")
391  exe "normal! a</head>\n<body>\n\e"
392else
393  exe "normal! a</head>\n<body>\n<pre>\n\e"
394endif
395
396exe s:orgwin . "wincmd w"
397
398" List of all id's
399let s:idlist = ","
400
401" First do some preprocessing for dynamic folding. Do this for the entire file
402" so we don't accidentally start within a closed fold or something.
403let s:allfolds = []
404
405if exists("s:html_dynamic_folds")
406  let s:lnum = 1
407  let s:end = line('$')
408  " save the fold text and set it to the default so we can find fold levels
409  let s:foldtext_save = &foldtext
410  set foldtext&
411
412  " we will set the foldcolumn in the html to the greater of the maximum fold
413  " level and the current foldcolumn setting
414  let s:foldcolumn = &foldcolumn
415
416  " get all info needed to describe currently closed folds
417  while s:lnum < s:end
418    if foldclosed(s:lnum) == s:lnum
419      " default fold text has '+-' and then a number of dashes equal to fold
420      " level, so subtract 2 from index of first non-dash after the dashes
421      " in order to get the fold level of the current fold
422      let s:level = match(foldtextresult(s:lnum), '+-*\zs[^-]') - 2
423      if s:level+1 > s:foldcolumn
424	let s:foldcolumn = s:level+1
425      endif
426      " store fold info for later use
427      let s:newfold = {'firstline': s:lnum, 'lastline': foldclosedend(s:lnum), 'level': s:level,'type': "closed-fold"}
428      call add(s:allfolds, s:newfold)
429      " open the fold so we can find any contained folds
430      execute s:lnum."foldopen"
431    else
432      let s:lnum = s:lnum + 1
433    endif
434  endwhile
435
436  " close all folds to get info for originally open folds
437  silent! %foldclose!
438  let s:lnum = 1
439
440  " the originally open folds will be all folds we encounter that aren't
441  " already in the list of closed folds
442  while s:lnum < s:end
443    if foldclosed(s:lnum) == s:lnum
444      " default fold text has '+-' and then a number of dashes equal to fold
445      " level, so subtract 2 from index of first non-dash after the dashes
446      " in order to get the fold level of the current fold
447      let s:level = match(foldtextresult(s:lnum), '+-*\zs[^-]') - 2
448      if s:level+1 > s:foldcolumn
449	let s:foldcolumn = s:level+1
450      endif
451      let s:newfold = {'firstline': s:lnum, 'lastline': foldclosedend(s:lnum), 'level': s:level,'type': "closed-fold"}
452      " only add the fold if we don't already have it
453      if empty(s:allfolds) || index(s:allfolds, s:newfold) == -1
454	let s:newfold.type = "open-fold"
455	call add(s:allfolds, s:newfold)
456      endif
457      " open the fold so we can find any contained folds
458      execute s:lnum."foldopen"
459    else
460      let s:lnum = s:lnum + 1
461    endif
462  endwhile
463
464  " sort the folds so that we only ever need to look at the first item in the
465  " list of folds
466  call sort(s:allfolds, "s:FoldCompare")
467
468  let &foldtext = s:foldtext_save
469  unlet s:foldtext_save
470
471  " close all folds again so we can get the fold text as we go
472  silent! %foldclose!
473endif
474
475" Now loop over all lines in the original text to convert to html.
476" Use html_start_line and html_end_line if they are set.
477if exists("g:html_start_line")
478  let s:lnum = html_start_line
479  if s:lnum < 1 || s:lnum > line("$")
480    let s:lnum = 1
481  endif
482else
483  let s:lnum = 1
484endif
485if exists("g:html_end_line")
486  let s:end = html_end_line
487  if s:end < s:lnum || s:end > line("$")
488    let s:end = line("$")
489  endif
490else
491  let s:end = line("$")
492endif
493
494" stack to keep track of all the folds containing the current line
495let s:foldstack = []
496
497if s:numblines
498  let s:margin = strlen(s:end) + 1
499else
500  let s:margin = 0
501endif
502
503if has('folding') && !exists('g:html_ignore_folding')
504  let s:foldfillchar = &fillchars[matchend(&fillchars, 'fold:')]
505  if s:foldfillchar == ''
506    let s:foldfillchar = '-'
507  endif
508endif
509let s:difffillchar = &fillchars[matchend(&fillchars, 'diff:')]
510if s:difffillchar == ''
511  let s:difffillchar = '-'
512endif
513
514let s:foldId = 0
515
516while s:lnum <= s:end
517
518  " If there are filler lines for diff mode, show these above the line.
519  let s:filler = diff_filler(s:lnum)
520  if s:filler > 0
521    let s:n = s:filler
522    while s:n > 0
523      let s:new = repeat(s:difffillchar, 3)
524
525      if s:n > 2 && s:n < s:filler && !exists("g:html_whole_filler")
526	let s:new = s:new . " " . s:filler . " inserted lines "
527	let s:n = 2
528      endif
529
530      if !exists("g:html_no_pre")
531	" HTML line wrapping is off--go ahead and fill to the margin
532	let s:new = s:new . repeat(s:difffillchar, &columns - strlen(s:new) - s:margin)
533      else
534	let s:new = s:new . repeat(s:difffillchar, 3)
535      endif
536
537      let s:new = s:HtmlFormat(s:new, "DiffDelete")
538      if s:numblines
539	" Indent if line numbering is on; must be after escaping.
540	let s:new = repeat(s:LeadingSpace, s:margin) . s:new
541      endif
542      exe s:newwin . "wincmd w"
543      exe "normal! a" . s:new . s:HtmlEndline . "\n\e"
544      exe s:orgwin . "wincmd w"
545
546      let s:n = s:n - 1
547    endwhile
548    unlet s:n
549  endif
550  unlet s:filler
551
552  " Start the line with the line number.
553  if s:numblines
554    let s:numcol = repeat(' ', s:margin - 1 - strlen(s:lnum)) . s:lnum . ' '
555  else
556    let s:numcol = ""
557  endif
558
559  let s:new = ""
560
561  if has('folding') && !exists('g:html_ignore_folding') && foldclosed(s:lnum) > -1 && !exists('s:html_dynamic_folds')
562    "
563    " This is the beginning of a folded block (with no dynamic folding)
564    "
565    let s:new = s:numcol . foldtextresult(s:lnum)
566    if !exists("g:html_no_pre")
567      " HTML line wrapping is off--go ahead and fill to the margin
568      let s:new = s:new . repeat(s:foldfillchar, &columns - strlen(s:new))
569    endif
570
571    let s:new = s:HtmlFormat(s:new, "Folded")
572
573    " Skip to the end of the fold
574    let s:lnum = foldclosedend(s:lnum)
575
576  else
577    "
578    " A line that is not folded, or doing dynamic folding.
579    "
580    let s:line = getline(s:lnum)
581    let s:len = strlen(s:line)
582
583    if exists("s:html_dynamic_folds")
584      " First insert a closing for any open folds that end on this line
585      while !empty(s:foldstack) && get(s:foldstack,0).lastline == s:lnum-1
586	let s:new = s:new."</span></span>"
587	call remove(s:foldstack, 0)
588      endwhile
589
590      " Now insert an opening any new folds that start on this line
591      let s:firstfold = 1
592      while !empty(s:allfolds) && get(s:allfolds,0).firstline == s:lnum
593	let s:foldId = s:foldId + 1
594	let s:new = s:new . "<span id='fold".s:foldId."' class='".s:allfolds[0].type."'>"
595
596	" Unless disabled, add a fold column for the opening line of a fold.
597	"
598	" Note that dynamic folds require using css so we just use css to take
599	" care of the leading spaces rather than using &nbsp; in the case of
600	" html_no_pre to make it easier
601	if !exists("g:html_no_foldcolumn")
602	  " add fold column that can open the new fold
603	  if s:allfolds[0].level > 1 && s:firstfold
604	    let s:new = s:new . "<a class='toggle-open FoldColumn' href='javascript:toggleFold(\"fold".s:foldstack[0].id."\")'>"
605	    let s:new = s:new . repeat('|', s:allfolds[0].level - 1) . "</a>"
606	  endif
607	  let s:new = s:new . "<a class='toggle-open FoldColumn' href='javascript:toggleFold(\"fold".s:foldId."\")'>+</a>"
608	  let s:new = s:new . "<a class='toggle-open "
609	  " If this is not the last fold we're opening on this line, we need
610	  " to keep the filler spaces hidden if the fold is opened by mouse
611	  " hover. If it is the last fold to open in the line, we shouldn't hide
612	  " them, so don't apply the toggle-filler class.
613	  if get(s:allfolds, 1, {'firstline': 0}).firstline == s:lnum
614	    let s:new = s:new . "toggle-filler "
615	  endif
616	  let s:new = s:new . "FoldColumn' href='javascript:toggleFold(\"fold".s:foldId."\")'>"
617	  let s:new = s:new . repeat(" ", s:foldcolumn - s:allfolds[0].level) . "</a>"
618
619	  " add fold column that can close the new fold
620	  let s:new = s:new . "<a class='toggle-closed FoldColumn' href='javascript:toggleFold(\"fold".s:foldId."\")'>"
621	  if s:firstfold
622	    let s:new = s:new . repeat('|', s:allfolds[0].level - 1)
623	  endif
624	  let s:new = s:new . "-"
625	  " only add spaces if we aren't opening another fold on the same line
626	  if get(s:allfolds, 1, {'firstline': 0}).firstline != s:lnum
627	    let s:new = s:new . repeat(" ", s:foldcolumn - s:allfolds[0].level)
628	  endif
629	  let s:new = s:new . "</a>"
630	  let s:firstfold = 0
631	endif
632
633	" add fold text, moving the span ending to the next line so collapsing
634	" of folds works correctly
635	let s:new = s:new . substitute(s:HtmlFormat(s:numcol . foldtextresult(s:lnum), "Folded"), '</span>', s:HtmlEndline.'\r\0', '')
636	let s:new = s:new . "<span class='fulltext'>"
637
638	" open the fold now that we have the fold text to allow retrieval of
639	" fold text for subsequent folds
640	execute s:lnum."foldopen"
641	call insert(s:foldstack, remove(s:allfolds,0))
642	let s:foldstack[0].id = s:foldId
643      endwhile
644
645      " Unless disabled, add a fold column for other lines.
646      "
647      " Note that dynamic folds require using css so we just use css to take
648      " care of the leading spaces rather than using &nbsp; in the case of
649      " html_no_pre to make it easier
650      if !exists("g:html_no_foldcolumn")
651	if empty(s:foldstack)
652	  " add the empty foldcolumn for unfolded lines
653	  let s:new = s:new . s:HtmlFormat(repeat(' ', s:foldcolumn), "FoldColumn")
654	else
655	  " add the fold column for folds not on the opening line
656	  if get(s:foldstack, 0).firstline < s:lnum
657	    let s:new = s:new . "<a class='FoldColumn' href='javascript:toggleFold(\"fold".s:foldstack[0].id."\")'>"
658	    let s:new = s:new . repeat('|', s:foldstack[0].level)
659	    let s:new = s:new . repeat(' ', s:foldcolumn - s:foldstack[0].level) . "</a>"
660	  endif
661	endif
662      endif
663    endif
664
665    " Now continue with the unfolded line text
666    if s:numblines
667      let s:new = s:new . s:HtmlFormat(s:numcol, "lnr")
668    endif
669
670    " Get the diff attribute, if any.
671    let s:diffattr = diff_hlID(s:lnum, 1)
672
673    " Loop over each character in the line
674    let s:col = 1
675    while s:col <= s:len || (s:col == 1 && s:diffattr)
676      let s:startcol = s:col " The start column for processing text
677      if s:diffattr
678	let s:id = diff_hlID(s:lnum, s:col)
679	let s:col = s:col + 1
680	" Speed loop (it's small - that's the trick)
681	" Go along till we find a change in hlID
682	while s:col <= s:len && s:id == diff_hlID(s:lnum, s:col) | let s:col = s:col + 1 | endwhile
683	if s:len < &columns && !exists("g:html_no_pre")
684	  " Add spaces at the end to mark the changed line.
685	  let s:line = s:line . repeat(' ', &columns - virtcol([s:lnum, s:len]) - s:margin)
686	  let s:len = &columns
687	endif
688      else
689	let s:id = synID(s:lnum, s:col, 1)
690	let s:col = s:col + 1
691	" Speed loop (it's small - that's the trick)
692	" Go along till we find a change in synID
693	while s:col <= s:len && s:id == synID(s:lnum, s:col, 1) | let s:col = s:col + 1 | endwhile
694      endif
695
696      " Expand tabs
697      let s:expandedtab = strpart(s:line, s:startcol - 1, s:col - s:startcol)
698      let s:offset = 0
699      let s:idx = stridx(s:expandedtab, "\t")
700      while s:idx >= 0
701	if has("multi_byte_encoding")
702	  if s:startcol + s:idx == 1
703	    let s:i = &ts
704	  else
705	    if s:idx == 0
706	      let s:prevc = matchstr(s:line, '.\%' . (s:startcol + s:idx + s:offset) . 'c')
707	    else
708	      let s:prevc = matchstr(s:expandedtab, '.\%' . (s:idx + 1) . 'c')
709	    endif
710	    let s:vcol = virtcol([s:lnum, s:startcol + s:idx + s:offset - len(s:prevc)])
711	    let s:i = &ts - (s:vcol % &ts)
712	  endif
713	  let s:offset -= s:i - 1
714	else
715	  let s:i = &ts - ((s:idx + s:startcol - 1) % &ts)
716	endif
717	let s:expandedtab = substitute(s:expandedtab, '\t', repeat(' ', s:i), '')
718	let s:idx = stridx(s:expandedtab, "\t")
719      endwhile
720
721      " Output the text with the same synID, with class set to {s:id_name}
722      let s:id = synIDtrans(s:id)
723      let s:id_name = synIDattr(s:id, "name", s:whatterm)
724      let s:new = s:new . s:HtmlFormat(s:expandedtab,  s:id_name)
725    endwhile
726  endif
727
728  exe s:newwin . "wincmd w"
729  exe "normal! a" . s:new . s:HtmlEndline . "\n\e"
730  exe s:orgwin . "wincmd w"
731  let s:lnum = s:lnum + 1
732endwhile
733" Finish with the last line
734exe s:newwin . "wincmd w"
735
736if exists("s:html_dynamic_folds")
737  " finish off any open folds
738  while !empty(s:foldstack)
739    exe "normal! a</span></span>"
740    call remove(s:foldstack, 0)
741  endwhile
742
743  " add fold column to the style list if not already there
744  let s:id = hlID('FoldColumn')
745  if stridx(s:idlist, "," . s:id . ",") == -1
746    let s:idlist = s:idlist . s:id . ","
747  endif
748endif
749
750" Close off the font tag that encapsulates the whole <body>
751if !exists("s:html_use_css")
752  exe "normal! a</font>\e"
753endif
754
755if exists("g:html_no_pre")
756  exe "normal! a</body>\n</html>\e"
757else
758  exe "normal! a</pre>\n</body>\n</html>\e"
759endif
760
761
762" Now, when we finally know which, we define the colors and styles
763if exists("s:html_use_css")
764  1;/<style type="text/+1
765endif
766
767" Find out the background and foreground color.
768let s:fgc = s:HtmlColor(synIDattr(hlID("Normal"), "fg#", s:whatterm))
769let s:bgc = s:HtmlColor(synIDattr(hlID("Normal"), "bg#", s:whatterm))
770if s:fgc == ""
771  let s:fgc = ( &background == "dark" ? "#ffffff" : "#000000" )
772endif
773if s:bgc == ""
774  let s:bgc = ( &background == "dark" ? "#000000" : "#ffffff" )
775endif
776
777" Normal/global attributes
778" For Netscape 4, set <body> attributes too, though, strictly speaking, it's
779" incorrect.
780if exists("s:html_use_css")
781  if exists("g:html_no_pre")
782    execute "normal! A\nbody { color: " . s:fgc . "; background-color: " . s:bgc . "; font-family: ". s:htmlfont ."; }\e"
783  else
784    execute "normal! A\npre { font-family: ". s:htmlfont ."; color: " . s:fgc . "; background-color: " . s:bgc . "; }\e"
785    yank
786    put
787    execute "normal! ^cwbody\e"
788  endif
789else
790  execute '%s:<body>:<body bgcolor="' . s:bgc . '" text="' . s:fgc . '"><font face="'. s:htmlfont .'">'
791endif
792
793" Line numbering attributes
794if s:numblines
795  if exists("s:html_use_css")
796    execute "normal! A\n.lnr { " . s:CSS1(hlID("LineNr")) . "}\e"
797  else
798    execute '%s+^<span class="lnr">\([^<]*\)</span>+' . s:HtmlOpening(hlID("LineNr")) . '\1' . s:HtmlClosing(hlID("LineNr")) . '+g'
799  endif
800endif
801
802" Gather attributes for all other classes
803let s:idlist = strpart(s:idlist, 1)
804while s:idlist != ""
805  let s:attr = ""
806  let s:col = stridx(s:idlist, ",")
807  let s:id = strpart(s:idlist, 0, s:col)
808  let s:idlist = strpart(s:idlist, s:col + 1)
809  let s:attr = s:CSS1(s:id)
810  let s:id_name = synIDattr(s:id, "name", s:whatterm)
811  " If the class has some attributes, export the style, otherwise DELETE all
812  " its occurences to make the HTML shorter
813  if s:attr != ""
814    if exists("s:html_use_css")
815      execute "normal! A\n." . s:id_name . " { " . s:attr . "}"
816    else
817      execute '%s+<span class="' . s:id_name . '">\([^<]*\)</span>+' . s:HtmlOpening(s:id) . '\1' . s:HtmlClosing(s:id) . '+g'
818    endif
819  else
820    execute '%s+<span class="' . s:id_name . '">\([^<]*\)</span>+\1+ge'
821    if exists("s:html_use_css")
822      1;/<style type="text/+1
823    endif
824  endif
825endwhile
826
827" Add hyperlinks
828%s+\(https\=://\S\{-}\)\(\([.,;:}]\=\(\s\|$\)\)\|[\\"'<>]\|&gt;\|&lt;\|&quot;\)+<a href="\1">\1</a>\2+ge
829
830" The DTD
831if exists("g:use_xhtml")
832  exe "normal! gg$a\n<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\e"
833else
834  exe "normal! gg0i<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">\n\e"
835endif
836
837if exists("g:use_xhtml")
838  exe "normal! gg/<html/e\na xmlns=\"http://www.w3.org/1999/xhtml\"\e"
839endif
840
841" Cleanup
842%s:\s\+$::e
843
844" Restore old settings
845let &report = s:old_report
846let &title = s:old_title
847let &icon = s:old_icon
848let &paste = s:old_paste
849let &magic = s:old_magic
850let @/ = s:old_search
851exe s:orgwin . "wincmd w"
852let &l:et = s:old_et
853exe s:newwin . "wincmd w"
854
855" Reset old <pre> settings
856if exists("s:old_html_no_pre")
857  let html_no_pre = s:old_html_no_pre
858  unlet s:old_html_no_pre
859elseif exists("g:html_no_pre")
860  unlet html_no_pre
861endif
862
863" Save a little bit of memory (worth doing?)
864unlet s:htmlfont
865unlet s:old_et s:old_paste s:old_icon s:old_report s:old_title s:old_search
866unlet s:whatterm s:idlist s:lnum s:end s:margin s:fgc s:bgc s:old_magic
867unlet! s:col s:id s:attr s:len s:line s:new s:expandedtab s:numblines
868unlet! s:orgwin s:newwin s:orgbufnr s:idx s:i s:offset
869if !v:profiling
870  delfunc s:HtmlColor
871  delfunc s:HtmlFormat
872  delfunc s:CSS1
873  if !exists("s:html_use_css")
874    delfunc s:HtmlOpening
875    delfunc s:HtmlClosing
876  endif
877endif
878silent! unlet s:diffattr s:difffillchar s:foldfillchar s:HtmlSpace s:LeadingSpace s:HtmlEndline s:firstfold s:foldcolumn
879unlet s:foldstack s:allfolds s:foldId s:numcol
880
881if exists("s:html_dynamic_folds")
882  delfunc s:FoldCompare
883endif
884
885silent! unlet s:html_dynamic_folds s:html_hover_unfold s:html_use_css
886
887let &cpo = s:cpo_sav
888unlet s:cpo_sav
889
890" vim: noet sw=2 sts=2
891