xref: /vim-8.2.3635/runtime/syntax/2html.vim (revision c1e37901)
1" Vim syntax support file
2" Maintainer: Bram Moolenaar <[email protected]>
3" Last Change: 2006 Apr 18
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 HTML valid characters enclosed in a span of class style_name with
107" unprintable characters expanded and double spaces replaced as necessary.
108function! s:HtmlFormat(text, style_name)
109  " Replace unprintable characters
110  let formatted = strtrans(a:text)
111
112  " Replace the reserved html characters
113  let formatted = substitute(substitute(substitute(substitute(substitute(formatted, '&', '\&amp;', 'g'), '<', '\&lt;', 'g'), '>', '\&gt;', 'g'), '"', '\&quot;', 'g'), "\x0c", '<hr class="PAGE-BREAK">', 'g')
114
115  " Replace double spaces
116  if ' ' != s:HtmlSpace
117    let formatted = substitute(formatted, '  ', s:HtmlSpace . s:HtmlSpace, 'g')
118  endif
119
120  " Enclose in a span of class style_name
121  let formatted = '<span class="' . a:style_name . '">' . formatted . '</span>'
122
123  " Add the class to class list if it's not there yet
124  let s:id = hlID(a:style_name)
125  if stridx(s:idlist, "," . s:id . ",") == -1
126    let s:idlist = s:idlist . s:id . ","
127  endif
128
129  return formatted
130endfun
131
132" Return CSS style describing given highlight id (can be empty)
133function! s:CSS1(id)
134  let a = ""
135  if synIDattr(a:id, "inverse")
136    " For inverse, we always must set both colors (and exchange them)
137    let x = s:HtmlColor(synIDattr(a:id, "bg#", s:whatterm))
138    let a = a . "color: " . ( x != "" ? x : s:bgc ) . "; "
139    let x = s:HtmlColor(synIDattr(a:id, "fg#", s:whatterm))
140    let a = a . "background-color: " . ( x != "" ? x : s:fgc ) . "; "
141  else
142    let x = s:HtmlColor(synIDattr(a:id, "fg#", s:whatterm))
143    if x != "" | let a = a . "color: " . x . "; " | endif
144    let x = s:HtmlColor(synIDattr(a:id, "bg#", s:whatterm))
145    if x != "" | let a = a . "background-color: " . x . "; " | endif
146  endif
147  if synIDattr(a:id, "bold") | let a = a . "font-weight: bold; " | endif
148  if synIDattr(a:id, "italic") | let a = a . "font-style: italic; " | endif
149  if synIDattr(a:id, "underline") | let a = a . "text-decoration: underline; " | endif
150  return a
151endfun
152
153" Figure out proper MIME charset from the 'encoding' option.
154if exists("html_use_encoding")
155  let s:html_encoding = html_use_encoding
156else
157  let s:vim_encoding = &encoding
158  if s:vim_encoding =~ '^8bit\|^2byte'
159    let s:vim_encoding = substitute(s:vim_encoding, '^8bit-\|^2byte-', '', '')
160  endif
161  if s:vim_encoding == 'latin1'
162    let s:html_encoding = 'iso-8859-1'
163  elseif s:vim_encoding =~ "^cp12"
164    let s:html_encoding = substitute(s:vim_encoding, 'cp', 'windows-', '')
165  elseif s:vim_encoding == 'sjis'
166    let s:html_encoding = 'Shift_JIS'
167  elseif s:vim_encoding == 'big5'
168    let s:html_encoding = "Big5"
169  elseif s:vim_encoding == 'euc-cn'
170    let s:html_encoding = 'GB_2312-80'
171  elseif s:vim_encoding == 'euc-tw'
172    let s:html_encoding = ""
173  elseif s:vim_encoding =~ '^euc\|^iso\|^koi'
174    let s:html_encoding = substitute(s:vim_encoding, '.*', '\U\0', '')
175  elseif s:vim_encoding == 'cp949'
176    let s:html_encoding = 'KS_C_5601-1987'
177  elseif s:vim_encoding == 'cp936'
178    let s:html_encoding = 'GBK'
179  elseif s:vim_encoding =~ '^ucs\|^utf'
180    let s:html_encoding = 'UTF-8'
181  else
182    let s:html_encoding = ""
183  endif
184endif
185
186
187" Set some options to make it work faster.
188" Don't report changes for :substitute, there will be many of them.
189let s:old_title = &title
190let s:old_icon = &icon
191let s:old_et = &l:et
192let s:old_report = &report
193let s:old_search = @/
194set notitle noicon
195setlocal et
196set report=1000000
197
198" Split window to create a buffer with the HTML file.
199let s:orgbufnr = winbufnr(0)
200if expand("%") == ""
201  new Untitled.html
202else
203  new %.html
204endif
205let s:newwin = winnr()
206let s:orgwin = bufwinnr(s:orgbufnr)
207
208set modifiable
209%d
210let s:old_paste = &paste
211set paste
212let s:old_magic = &magic
213set magic
214
215if exists("use_xhtml")
216  if s:html_encoding != ""
217    exe "normal!  a<?xml version=\"1.0\" encoding=\"" . s:html_encoding . "\"?>\n\e"
218  else
219    exe "normal! a<?xml version=\"1.0\"?>\n\e"
220  endif
221  let s:tag_close = '/>'
222else
223  let s:tag_close = '>'
224endif
225
226let s:HtmlSpace = ' '
227let s:LeadingSpace = ' '
228let s:HtmlEndline = ''
229if exists("html_no_pre")
230  let s:HtmlEndline = '<br' . s:tag_close
231  if exists("use_xhtml")
232    let s:LeadingSpace = '&#x20;'
233  else
234    let s:LeadingSpace = '&nbsp;'
235  endif
236  let s:HtmlSpace = '\' . s:LeadingSpace
237endif
238
239" HTML header, with the title and generator ;-). Left free space for the CSS,
240" to be filled at the end.
241exe "normal! a<html>\n\e"
242exe "normal! a<head>\n<title>" . expand("%:p:~") . "</title>\n\e"
243exe "normal! a<meta name=\"Generator\" content=\"Vim/" . v:version/100 . "." . v:version %100 . '"' . s:tag_close . "\n\e"
244if s:html_encoding != ""
245  exe "normal! a<meta http-equiv=\"content-type\" content=\"text/html; charset=" . s:html_encoding . '"' . s:tag_close . "\n\e"
246endif
247if exists("html_use_css")
248  exe "normal! a<style type=\"text/css\">\n<!--\n-->\n</style>\n\e"
249endif
250if exists("html_no_pre")
251  if exists("use_xhtml")
252    exe "normal! a</head>\n<body>\n<p>\n\e"
253  else
254    exe "normal! a</head>\n<body>\n\e"
255  endif
256else
257  if exists("use_xhtml")
258    exe "normal! a</head>\n<body>\n<p>\n<pre>\n\e"
259  else
260    exe "normal! a</head>\n<body>\n<pre>\n\e"
261  endif
262endif
263
264exe s:orgwin . "wincmd w"
265
266" List of all id's
267let s:idlist = ","
268
269" Loop over all lines in the original text.
270" Use html_start_line and html_end_line if they are set.
271if exists("html_start_line")
272  let s:lnum = html_start_line
273  if s:lnum < 1 || s:lnum > line("$")
274    let s:lnum = 1
275  endif
276else
277  let s:lnum = 1
278endif
279if exists("html_end_line")
280  let s:end = html_end_line
281  if s:end < s:lnum || s:end > line("$")
282    let s:end = line("$")
283  endif
284else
285  let s:end = line("$")
286endif
287
288if has('folding') && !exists('html_ignore_folding')
289  let s:foldfillchar = &fillchars[matchend(&fillchars, 'fold:')]
290  if s:foldfillchar == ''
291    let s:foldfillchar = '-'
292  endif
293endif
294let s:difffillchar = &fillchars[matchend(&fillchars, 'diff:')]
295if s:difffillchar == ''
296  let s:difffillchar = '-'
297endif
298
299
300while s:lnum <= s:end
301
302  " If there are filler lines for diff mode, show these above the line.
303  let s:filler = diff_filler(s:lnum)
304  if s:filler > 0
305    let s:n = s:filler
306    while s:n > 0
307      if s:numblines
308        " Indent if line numbering is on
309        let s:new = repeat(s:LeadingSpace, strlen(s:end) + 1) . repeat(s:difffillchar, 3)
310      else
311        let s:new = repeat(s:difffillchar, 3)
312      endif
313
314      if s:n > 2 && s:n < s:filler && !exists("html_whole_filler")
315	let s:new = s:new . " " . s:filler . " inserted lines "
316	let s:n = 2
317      endif
318
319      if !exists("html_no_pre")
320        " HTML line wrapping is off--go ahead and fill to the margin
321        let s:new = s:new . repeat(s:difffillchar, &columns - strlen(s:new))
322      endif
323
324      let s:new = s:HtmlFormat(s:new, "DiffDelete")
325      exe s:newwin . "wincmd w"
326      exe "normal! a" . s:new . s:HtmlEndline . "\n\e"
327      exe s:orgwin . "wincmd w"
328
329      let s:n = s:n - 1
330    endwhile
331    unlet s:n
332  endif
333  unlet s:filler
334
335  " Start the line with the line number.
336  if s:numblines
337    let s:new = repeat(' ', strlen(s:end) - strlen(s:lnum)) . s:lnum . ' '
338  else
339    let s:new = ""
340  endif
341
342  if has('folding') && !exists('html_ignore_folding') && foldclosed(s:lnum) > -1
343    "
344    " This is the beginning of a folded block
345    "
346    let s:new = s:new . foldtextresult(s:lnum)
347    if !exists("html_no_pre")
348      " HTML line wrapping is off--go ahead and fill to the margin
349      let s:new = s:new . repeat(s:foldfillchar, &columns - strlen(s:new))
350    endif
351
352    let s:new = s:HtmlFormat(s:new, "Folded")
353
354    " Skip to the end of the fold
355    let s:lnum = foldclosedend(s:lnum)
356
357  else
358    "
359    " A line that is not folded.
360    "
361    let s:line = getline(s:lnum)
362
363    let s:len = strlen(s:line)
364
365    if s:numblines
366      let s:new = '<span class="lnr">' . s:new . '</span>'
367    endif
368
369    " Get the diff attribute, if any.
370    let s:diffattr = diff_hlID(s:lnum, 1)
371
372    " Loop over each character in the line
373    let s:col = 1
374    while s:col <= s:len || (s:col == 1 && s:diffattr)
375      let s:startcol = s:col " The start column for processing text
376      if s:diffattr
377	let s:id = diff_hlID(s:lnum, s:col)
378	let s:col = s:col + 1
379	" Speed loop (it's small - that's the trick)
380	" Go along till we find a change in hlID
381	while s:col <= s:len && s:id == diff_hlID(s:lnum, s:col) | let s:col = s:col + 1 | endwhile
382        if s:len < &columns && !exists("html_no_pre")
383	  " Add spaces at the end to mark the changed line.
384          let s:line = s:line . repeat(' ', &columns - s:len)
385          let s:len = &columns
386        endif
387      else
388	let s:id = synID(s:lnum, s:col, 1)
389	let s:col = s:col + 1
390	" Speed loop (it's small - that's the trick)
391	" Go along till we find a change in synID
392	while s:col <= s:len && s:id == synID(s:lnum, s:col, 1) | let s:col = s:col + 1 | endwhile
393      endif
394
395      " Expand tabs
396      let s:expandedtab = strpart(s:line, s:startcol - 1, s:col - s:startcol)
397      let idx = stridx(s:expandedtab, "\t")
398      while idx >= 0
399        let i = &ts - ((idx + s:startcol - 1) % &ts)
400        let s:expandedtab = substitute(s:expandedtab, '\t', repeat(' ', i), '')
401        let idx = stridx(s:expandedtab, "\t")
402      endwhile
403
404      " Output the text with the same synID, with class set to {s:id_name}
405      let s:id = synIDtrans(s:id)
406      let s:id_name = synIDattr(s:id, "name", s:whatterm)
407      let s:new = s:new . s:HtmlFormat(s:expandedtab,  s:id_name)
408    endwhile
409  endif
410
411  exe s:newwin . "wincmd w"
412  exe "normal! a" . s:new . s:HtmlEndline . "\n\e"
413  exe s:orgwin . "wincmd w"
414  let s:lnum = s:lnum + 1
415endwhile
416" Finish with the last line
417exe s:newwin . "wincmd w"
418if exists("html_no_pre")
419  if exists("use_xhtml")
420    exe "normal! a</p>\n</body>\n</html>\e"
421  else
422    exe "normal! a\n</body>\n</html>\e"
423  endif
424else
425  if exists("use_xhtml")
426    exe "normal! a</pre>\n</p>\n</body>\n</html>\e"
427  else
428    exe "normal! a</pre>\n</body>\n</html>\e"
429  endif
430endif
431
432
433" Now, when we finally know which, we define the colors and styles
434if exists("html_use_css")
435  1;/<style type="text/+1
436endif
437
438" Find out the background and foreground color.
439let s:fgc = s:HtmlColor(synIDattr(hlID("Normal"), "fg#", s:whatterm))
440let s:bgc = s:HtmlColor(synIDattr(hlID("Normal"), "bg#", s:whatterm))
441if s:fgc == ""
442  let s:fgc = ( &background == "dark" ? "#ffffff" : "#000000" )
443endif
444if s:bgc == ""
445  let s:bgc = ( &background == "dark" ? "#000000" : "#ffffff" )
446endif
447
448" Normal/global attributes
449" For Netscape 4, set <body> attributes too, though, strictly speaking, it's
450" incorrect.
451if exists("html_use_css")
452  if exists("html_no_pre")
453    execute "normal! A\nbody { color: " . s:fgc . "; background-color: " . s:bgc . "; font-family: Courier, monospace; }\e"
454  else
455    execute "normal! A\npre { color: " . s:fgc . "; background-color: " . s:bgc . "; }\e"
456    yank
457    put
458    execute "normal! ^cwbody\e"
459  endif
460else
461  if exists("html_no_pre")
462    execute '%s:<body>:<body ' . 'bgcolor="' . s:bgc . '" text="' . s:fgc . '" style="font-family\: Courier, monospace;">'
463  else
464    execute '%s:<body>:<body ' . 'bgcolor="' . s:bgc . '" text="' . s:fgc . '">'
465  endif
466endif
467
468" Line numbering attributes
469if s:numblines
470  if exists("html_use_css")
471    execute "normal! A\n.lnr { " . s:CSS1(hlID("LineNr")) . "}\e"
472  else
473    execute '%s+<span class="lnr">\([^<]*\)</span>+' . s:HtmlOpening(hlID("LineNr")) . '\1' . s:HtmlClosing(hlID("LineNr")) . '+g'
474  endif
475endif
476
477" Gather attributes for all other classes
478let s:idlist = strpart(s:idlist, 1)
479while s:idlist != ""
480  let s:attr = ""
481  let s:col = stridx(s:idlist, ",")
482  let s:id = strpart(s:idlist, 0, s:col)
483  let s:idlist = strpart(s:idlist, s:col + 1)
484  let s:attr = s:CSS1(s:id)
485  let s:id_name = synIDattr(s:id, "name", s:whatterm)
486  " If the class has some attributes, export the style, otherwise DELETE all
487  " its occurences to make the HTML shorter
488  if s:attr != ""
489    if exists("html_use_css")
490      execute "normal! A\n." . s:id_name . " { " . s:attr . "}"
491    else
492      execute '%s+<span class="' . s:id_name . '">\([^<]*\)</span>+' . s:HtmlOpening(s:id) . '\1' . s:HtmlClosing(s:id) . '+g'
493    endif
494  else
495    execute '%s+<span class="' . s:id_name . '">\([^<]*\)</span>+\1+g'
496    if exists("html_use_css")
497      1;/<style type="text/+1
498    endif
499  endif
500endwhile
501
502" Add hyperlinks
503%s+\(https\=://\S\{-}\)\(\([.,;:}]\=\(\s\|$\)\)\|[\\"'<>]\|&gt;\|&lt;\|&quot;\)+<a href="\1">\1</a>\2+ge
504
505" The DTD
506if exists("html_use_css")
507  if exists("use_xhtml")
508    exe "normal! gg$a\n<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\e"
509  else
510    exe "normal! gg0i<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\" \"http://www.w3.org/TR/html4/strict.dtd\">\n\e"
511  endif
512endif
513
514if exists("use_xhtml")
515  exe "normal! gg/<html/e\na xmlns=\"http://www.w3.org/1999/xhtml\"\e"
516endif
517
518" Cleanup
519%s:\s\+$::e
520
521" Restore old settings
522let &report = s:old_report
523let &title = s:old_title
524let &icon = s:old_icon
525let &paste = s:old_paste
526let &magic = s:old_magic
527let @/ = s:old_search
528exe s:orgwin . "wincmd w"
529let &l:et = s:old_et
530exe s:newwin . "wincmd w"
531
532" Save a little bit of memory (worth doing?)
533unlet s:old_et s:old_paste s:old_icon s:old_report s:old_title s:old_search
534unlet s:whatterm s:idlist s:lnum s:end s:fgc s:bgc s:old_magic
535unlet! s:col s:id s:attr s:len s:line s:new s:expandedtab s:numblines
536unlet s:orgwin s:newwin s:orgbufnr
537if !v:profiling
538  delfunc s:HtmlColor
539  delfunc s:HtmlFormat
540  delfunc s:CSS1
541  if !exists("html_use_css")
542    delfunc s:HtmlOpening
543    delfunc s:HtmlClosing
544  endif
545endif
546silent! unlet s:diffattr s:difffillchar s:foldfillchar s:HtmlSpace s:LeadingSpace s:HtmlEndline
547