xref: /vim-8.2.3635/runtime/syntax/2html.vim (revision 071d4279)
1" Vim syntax support file
2" Maintainer: Bram Moolenaar <[email protected]>
3" Last Change: 2004 May 31
4"	       (modified by David Ne\v{c}as (Yeti) <[email protected]>)
5"	       (XHTML support by Panagiotis Issaris <[email protected]>)
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
241while s:lnum <= s:end
242
243  " Get the current line
244  let s:line = getline(s:lnum)
245  let s:len = strlen(s:line)
246  let s:new = ""
247
248  if s:numblines
249    let s:new = '<span class="lnr">' . strpart('        ', 0, strlen(line("$")) - strlen(s:lnum)) . s:lnum . '</span>  '
250  endif
251
252  " Loop over each character in the line
253  let s:col = 1
254  while s:col <= s:len
255    let s:startcol = s:col " The start column for processing text
256    let s:id = synID(s:lnum, s:col, 1)
257    let s:col = s:col + 1
258    " Speed loop (it's small - that's the trick)
259    " Go along till we find a change in synID
260    while s:col <= s:len && s:id == synID(s:lnum, s:col, 1) | let s:col = s:col + 1 | endwhile
261
262    " Output the text with the same synID, with class set to {s:id_name}
263    let s:id = synIDtrans(s:id)
264    let s:id_name = synIDattr(s:id, "name", s:whatterm)
265    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>'
266    " Add the class to class list if it's not there yet
267    if stridx(s:idlist, "," . s:id . ",") == -1
268      let s:idlist = s:idlist . s:id . ","
269    endif
270
271    if s:col > s:len
272      break
273    endif
274  endwhile
275
276  " Expand tabs
277  let s:pad=0
278  let s:start = 0
279  let s:idx = stridx(s:line, "\t")
280  while s:idx >= 0
281    let s:i = &ts - ((s:start + s:pad + s:idx) % &ts)
282    let s:new = substitute(s:new, '\t', strpart(s:expandedtab, 0, s:i), '')
283    let s:pad = s:pad + s:i - 1
284    let s:start = s:start + s:idx + 1
285    let s:idx = stridx(strpart(s:line, s:start), "\t")
286  endwhile
287
288  if exists("html_no_pre")
289    if exists("use_xhtml")
290      let s:new = substitute(s:new, '  ', '\&#x20;\&#x20;', 'g') . '<br/>'
291    else
292      let s:new = substitute(s:new, '  ', '\&nbsp;\&nbsp;', 'g') . '<br>'
293    endif
294  endif
295  exe s:newwin . "wincmd w"
296  exe "normal! a" . strtrans(s:new) . "\n\e"
297  exe s:orgwin . "wincmd w"
298  let s:lnum = s:lnum + 1
299  +
300endwhile
301" Finish with the last line
302exe s:newwin . "wincmd w"
303if exists("html_no_pre")
304  exe "normal! a\n</body>\n</html>\e"
305else
306  exe "normal! a</pre>\n</body>\n</html>\e"
307endif
308
309
310" Now, when we finally know which, we define the colors and styles
311if exists("html_use_css")
312  1;/<style type="text/+1
313endif
314
315" Find out the background and foreground color.
316let s:fgc = s:HtmlColor(synIDattr(hlID("Normal"), "fg#", s:whatterm))
317let s:bgc = s:HtmlColor(synIDattr(hlID("Normal"), "bg#", s:whatterm))
318if s:fgc == ""
319  let s:fgc = ( &background == "dark" ? "#ffffff" : "#000000" )
320endif
321if s:bgc == ""
322  let s:bgc = ( &background == "dark" ? "#000000" : "#ffffff" )
323endif
324
325" Normal/global attributes
326" For Netscape 4, set <body> attributes too, though, strictly speaking, it's
327" incorrect.
328if exists("html_use_css")
329  if exists("html_no_pre")
330    execute "normal! A\nbody { color: " . s:fgc . "; background-color: " . s:bgc . "; font-family: Courier, monospace; }\e"
331  else
332    execute "normal! A\npre { color: " . s:fgc . "; background-color: " . s:bgc . "; }\e"
333    yank
334    put
335    execute "normal! ^cwbody\e"
336  endif
337else
338  if exists("html_no_pre")
339    execute '%s:<body>:<body ' . 'bgcolor="' . s:bgc . '" text="' . s:fgc . '" style="font-family\: Courier, monospace;">'
340  else
341    execute '%s:<body>:<body ' . 'bgcolor="' . s:bgc . '" text="' . s:fgc . '">'
342  endif
343endif
344
345" Line numbering attributes
346if s:numblines
347  if exists("html_use_css")
348    execute "normal! A\n.lnr { " . s:CSS1(hlID("LineNr")) . "}\e"
349  else
350    execute '%s+<span class="lnr">\([^<]*\)</span>+' . s:HtmlOpening(hlID("LineNr")) . '\1' . s:HtmlClosing(hlID("LineNr")) . '+g'
351  endif
352endif
353
354" Gather attributes for all other classes
355let s:idlist = strpart(s:idlist, 1)
356while s:idlist != ""
357  let s:attr = ""
358  let s:col = stridx(s:idlist, ",")
359  let s:id = strpart(s:idlist, 0, s:col)
360  let s:idlist = strpart(s:idlist, s:col + 1)
361  let s:attr = s:CSS1(s:id)
362  let s:id_name = synIDattr(s:id, "name", s:whatterm)
363  " If the class has some attributes, export the style, otherwise DELETE all
364  " its occurences to make the HTML shorter
365  if s:attr != ""
366    if exists("html_use_css")
367      execute "normal! A\n." . s:id_name . " { " . s:attr . "}"
368    else
369      execute '%s+<span class="' . s:id_name . '">\([^<]*\)</span>+' . s:HtmlOpening(s:id) . '\1' . s:HtmlClosing(s:id) . '+g'
370    endif
371  else
372    execute '%s+<span class="' . s:id_name . '">\([^<]*\)</span>+\1+g'
373    if exists("html_use_css")
374      1;/<style type="text/+1
375    endif
376  endif
377endwhile
378
379" Add hyperlinks
380%s+\(http://\S\{-}\)\(\([.,;:}]\=\(\s\|$\)\)\|[\\"'<>]\|&gt;\|&lt;\)+<A HREF="\1">\1</A>\2+ge
381
382" The DTD
383if exists("html_use_css")
384  exe "normal! gg0i<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\" \"http://www.w3.org/TR/html4/strict.dtd\">\n\e"
385endif
386
387" Cleanup
388%s:\s\+$::e
389
390" Restore old settings
391let &report = s:old_report
392let &title = s:old_title
393let &icon = s:old_icon
394let &paste = s:old_paste
395let &magic = s:old_magic
396let @/ = s:old_search
397exe s:orgwin . "wincmd w"
398let &l:et = s:old_et
399exe s:newwin . "wincmd w"
400
401" Save a little bit of memory (worth doing?)
402unlet s:old_et s:old_paste s:old_icon s:old_report s:old_title s:old_search
403unlet s:whatterm s:idlist s:lnum s:end s:fgc s:bgc s:old_magic
404unlet! s:col s:id s:attr s:len s:line s:new s:did_retab s:numblines
405unlet s:orgwin s:newwin s:orgbufnr
406delfunc s:HtmlColor
407delfunc s:CSS1
408if !exists("html_use_css")
409  delfunc s:HtmlOpening
410  delfunc s:HtmlClosing
411endif
412