1" Vim syntax support file 2" Maintainer: Bram Moolenaar <[email protected]> 3" Last Change: 2005 Dec 04 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, '&', '\&', 'g'), '<', '\<', 'g'), '>', '\>', 'g'), '"', '\"', '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:HtmlEndline = '' 228if exists("html_no_pre") 229 let s:HtmlEndline = '<br' . s:tag_close 230 if exists("use_xhtml") 231 let s:HtmlSpace = '\ ' 232 else 233 let s:HtmlSpace = '\ ' 234 endif 235endif 236 237" HTML header, with the title and generator ;-). Left free space for the CSS, 238" to be filled at the end. 239exe "normal! a<html>\n\e" 240exe "normal! a<head>\n<title>" . expand("%:p:~") . "</title>\n\e" 241exe "normal! a<meta name=\"Generator\" content=\"Vim/" . v:version/100 . "." . v:version %100 . '"' . s:tag_close . "\n\e" 242if s:html_encoding != "" 243 exe "normal! a<meta http-equiv=\"content-type\" content=\"text/html; charset=" . s:html_encoding . '"' . s:tag_close . "\n\e" 244endif 245if exists("html_use_css") 246 exe "normal! a<style type=\"text/css\">\n<!--\n-->\n</style>\n\e" 247endif 248if exists("html_no_pre") 249 exe "normal! a</head>\n<body>\n\e" 250else 251 exe "normal! a</head>\n<body>\n<pre>\n\e" 252endif 253 254exe s:orgwin . "wincmd w" 255 256" List of all id's 257let s:idlist = "," 258 259" Loop over all lines in the original text. 260" Use html_start_line and html_end_line if they are set. 261if exists("html_start_line") 262 let s:lnum = html_start_line 263 if s:lnum < 1 || s:lnum > line("$") 264 let s:lnum = 1 265 endif 266else 267 let s:lnum = 1 268endif 269if exists("html_end_line") 270 let s:end = html_end_line 271 if s:end < s:lnum || s:end > line("$") 272 let s:end = line("$") 273 endif 274else 275 let s:end = line("$") 276endif 277 278if has('folding') && !exists('html_ignore_folding') 279 let s:foldfillchar = &fillchars[matchend(&fillchars, 'fold:')] 280 if s:foldfillchar == '' 281 let s:foldfillchar = '-' 282 endif 283endif 284let s:difffillchar = &fillchars[matchend(&fillchars, 'diff:')] 285if s:difffillchar == '' 286 let s:difffillchar = '-' 287endif 288 289 290while s:lnum <= s:end 291 292 " If there are filler lines for diff mode, show these above the line. 293 let s:filler = diff_filler(s:lnum) 294 if s:filler > 0 295 let s:n = s:filler 296 while s:n > 0 297 if s:numblines 298 " Indent if line numbering is on 299 let s:new = repeat(' ', strlen(s:end) + 1) . repeat(s:difffillchar, 3) 300 else 301 let s:new = repeat(s:difffillchar, 3) 302 endif 303 304 if s:n > 2 && s:n < s:filler && !exists("html_whole_filler") 305 let s:new = s:new . " " . s:filler . " inserted lines " 306 let s:n = 2 307 endif 308 309 if !exists("html_no_pre") 310 " HTML line wrapping is off--go ahead and fill to the margin 311 let s:new = s:new . repeat(s:difffillchar, &columns - strlen(s:new)) 312 endif 313 314 let s:new = s:HtmlFormat(s:new, "DiffDelete") 315 exe s:newwin . "wincmd w" 316 exe "normal! a" . s:new . s:HtmlEndline . "\n\e" 317 exe s:orgwin . "wincmd w" 318 319 let s:n = s:n - 1 320 endwhile 321 unlet s:n 322 endif 323 unlet s:filler 324 325 " Start the line with the line number. 326 if s:numblines 327 let s:new = repeat(' ', strlen(s:end) - strlen(s:lnum)) . s:lnum . ' ' 328 else 329 let s:new = "" 330 endif 331 332 if has('folding') && !exists('html_ignore_folding') && foldclosed(s:lnum) > -1 333 " 334 " This is the beginning of a folded block 335 " 336 let s:new = s:new . foldtextresult(s:lnum) 337 if !exists("html_no_pre") 338 " HTML line wrapping is off--go ahead and fill to the margin 339 let s:new = s:new . repeat(s:foldfillchar, &columns - strlen(s:new)) 340 endif 341 342 let s:new = s:HtmlFormat(s:new, "Folded") 343 344 " Skip to the end of the fold 345 let s:lnum = foldclosedend(s:lnum) 346 347 else 348 " 349 " A line that is not folded. 350 " 351 let s:line = getline(s:lnum) 352 353 let s:len = strlen(s:line) 354 355 if s:numblines 356 let s:new = '<span class="lnr">' . s:new . '</span>' 357 endif 358 359 " Get the diff attribute, if any. 360 let s:diffattr = diff_hlID(s:lnum, 1) 361 362 " Loop over each character in the line 363 let s:col = 1 364 while s:col <= s:len || (s:col == 1 && s:diffattr) 365 let s:startcol = s:col " The start column for processing text 366 if s:diffattr 367 let s:id = diff_hlID(s:lnum, s:col) 368 let s:col = s:col + 1 369 " Speed loop (it's small - that's the trick) 370 " Go along till we find a change in hlID 371 while s:col <= s:len && s:id == diff_hlID(s:lnum, s:col) | let s:col = s:col + 1 | endwhile 372 if s:len < &columns && !exists("html_no_pre") 373 " Add spaces at the end to mark the changed line. 374 let s:line = s:line . repeat(' ', &columns - s:len) 375 let s:len = &columns 376 endif 377 else 378 let s:id = synID(s:lnum, s:col, 1) 379 let s:col = s:col + 1 380 " Speed loop (it's small - that's the trick) 381 " Go along till we find a change in synID 382 while s:col <= s:len && s:id == synID(s:lnum, s:col, 1) | let s:col = s:col + 1 | endwhile 383 endif 384 385 " Expand tabs 386 let s:expandedtab = strpart(s:line, s:startcol - 1, s:col - s:startcol) 387 let idx = stridx(s:expandedtab, "\t") 388 while idx >= 0 389 let i = &ts - ((idx + s:startcol - 1) % &ts) 390 let s:expandedtab = substitute(s:expandedtab, '\t', repeat(' ', i), '') 391 let idx = stridx(s:expandedtab, "\t") 392 endwhile 393 394 " Output the text with the same synID, with class set to {s:id_name} 395 let s:id = synIDtrans(s:id) 396 let s:id_name = synIDattr(s:id, "name", s:whatterm) 397 let s:new = s:new . s:HtmlFormat(s:expandedtab, s:id_name) 398 endwhile 399 endif 400 401 exe s:newwin . "wincmd w" 402 exe "normal! a" . s:new . s:HtmlEndline . "\n\e" 403 exe s:orgwin . "wincmd w" 404 let s:lnum = s:lnum + 1 405endwhile 406" Finish with the last line 407exe s:newwin . "wincmd w" 408if exists("html_no_pre") 409 exe "normal! a\n</body>\n</html>\e" 410else 411 exe "normal! a</pre>\n</body>\n</html>\e" 412endif 413 414 415" Now, when we finally know which, we define the colors and styles 416if exists("html_use_css") 417 1;/<style type="text/+1 418endif 419 420" Find out the background and foreground color. 421let s:fgc = s:HtmlColor(synIDattr(hlID("Normal"), "fg#", s:whatterm)) 422let s:bgc = s:HtmlColor(synIDattr(hlID("Normal"), "bg#", s:whatterm)) 423if s:fgc == "" 424 let s:fgc = ( &background == "dark" ? "#ffffff" : "#000000" ) 425endif 426if s:bgc == "" 427 let s:bgc = ( &background == "dark" ? "#000000" : "#ffffff" ) 428endif 429 430" Normal/global attributes 431" For Netscape 4, set <body> attributes too, though, strictly speaking, it's 432" incorrect. 433if exists("html_use_css") 434 if exists("html_no_pre") 435 execute "normal! A\nbody { color: " . s:fgc . "; background-color: " . s:bgc . "; font-family: Courier, monospace; }\e" 436 else 437 execute "normal! A\npre { color: " . s:fgc . "; background-color: " . s:bgc . "; }\e" 438 yank 439 put 440 execute "normal! ^cwbody\e" 441 endif 442else 443 if exists("html_no_pre") 444 execute '%s:<body>:<body ' . 'bgcolor="' . s:bgc . '" text="' . s:fgc . '" style="font-family\: Courier, monospace;">' 445 else 446 execute '%s:<body>:<body ' . 'bgcolor="' . s:bgc . '" text="' . s:fgc . '">' 447 endif 448endif 449 450" Line numbering attributes 451if s:numblines 452 if exists("html_use_css") 453 execute "normal! A\n.lnr { " . s:CSS1(hlID("LineNr")) . "}\e" 454 else 455 execute '%s+<span class="lnr">\([^<]*\)</span>+' . s:HtmlOpening(hlID("LineNr")) . '\1' . s:HtmlClosing(hlID("LineNr")) . '+g' 456 endif 457endif 458 459" Gather attributes for all other classes 460let s:idlist = strpart(s:idlist, 1) 461while s:idlist != "" 462 let s:attr = "" 463 let s:col = stridx(s:idlist, ",") 464 let s:id = strpart(s:idlist, 0, s:col) 465 let s:idlist = strpart(s:idlist, s:col + 1) 466 let s:attr = s:CSS1(s:id) 467 let s:id_name = synIDattr(s:id, "name", s:whatterm) 468 " If the class has some attributes, export the style, otherwise DELETE all 469 " its occurences to make the HTML shorter 470 if s:attr != "" 471 if exists("html_use_css") 472 execute "normal! A\n." . s:id_name . " { " . s:attr . "}" 473 else 474 execute '%s+<span class="' . s:id_name . '">\([^<]*\)</span>+' . s:HtmlOpening(s:id) . '\1' . s:HtmlClosing(s:id) . '+g' 475 endif 476 else 477 execute '%s+<span class="' . s:id_name . '">\([^<]*\)</span>+\1+g' 478 if exists("html_use_css") 479 1;/<style type="text/+1 480 endif 481 endif 482endwhile 483 484" Add hyperlinks 485%s+\(https\=://\S\{-}\)\(\([.,;:}]\=\(\s\|$\)\)\|[\\"'<>]\|>\|<\|"\)+<a href="\1">\1</a>\2+ge 486 487" The DTD 488if exists("html_use_css") 489 if exists("use_xhtml") 490 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" 491 else 492 exe "normal! gg0i<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\" \"http://www.w3.org/TR/html4/strict.dtd\">\n\e" 493 endif 494endif 495 496if exists("use_xhtml") 497 exe "normal! gg/<html/e\na xmlns=\"http://www.w3.org/1999/xhtml\"\e" 498endif 499 500" Cleanup 501%s:\s\+$::e 502 503" Restore old settings 504let &report = s:old_report 505let &title = s:old_title 506let &icon = s:old_icon 507let &paste = s:old_paste 508let &magic = s:old_magic 509let @/ = s:old_search 510exe s:orgwin . "wincmd w" 511let &l:et = s:old_et 512exe s:newwin . "wincmd w" 513 514" Save a little bit of memory (worth doing?) 515unlet s:old_et s:old_paste s:old_icon s:old_report s:old_title s:old_search 516unlet s:whatterm s:idlist s:lnum s:end s:fgc s:bgc s:old_magic 517unlet! s:col s:id s:attr s:len s:line s:new s:expandedtab s:numblines 518unlet s:orgwin s:newwin s:orgbufnr 519if !v:profiling 520 delfunc s:HtmlColor 521 delfunc s:HtmlFormat 522 delfunc s:CSS1 523 if !exists("html_use_css") 524 delfunc s:HtmlOpening 525 delfunc s:HtmlClosing 526 endif 527endif 528silent! unlet s:diffattr s:difffillchar s:foldfillchar s:HtmlSpace s:HtmlEndline 529