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