tohtml.vim (29495B)
1 vim9script 2 # Vim autoload file for the tohtml plugin. 3 # Maintainer: Ben Fritz <fritzophrenic@gmail.com> 4 # Last Change: 2026-05-22 5 # 6 # Additional contributors: 7 # 8 # Original by Bram Moolenaar <Bram@vim.org> 9 # Diff2HTML() added by Christian Brabandt <cb@256bit.org> 10 # Converted to Vim9 by Mao-Yining Vim PR #19915 11 # 12 # See git change logs for more! 13 14 # Automatically find charsets from all encodings supported natively by Vim. With 15 # the 8bit- and 2byte- prefixes, Vim can actually support more encodings than 16 # this. Let the user specify these however since they won't be supported on 17 # every system. 18 # 19 # Note, not all of Vim's supported encodings have a charset to use. 20 # 21 # Names in this list are from: 22 # http://www.iana.org/assignments/character-sets 23 # export const encoding_to_charset: {{{ 24 export const encoding_to_charset = { 25 'latin1': 'ISO-8859-1', 26 'iso-8859-2': 'ISO-8859-2', 27 'iso-8859-3': 'ISO-8859-3', 28 'iso-8859-4': 'ISO-8859-4', 29 'iso-8859-5': 'ISO-8859-5', 30 'iso-8859-6': 'ISO-8859-6', 31 'iso-8859-7': 'ISO-8859-7', 32 'iso-8859-8': 'ISO-8859-8', 33 'iso-8859-9': 'ISO-8859-9', 34 'iso-8859-10': '', 35 'iso-8859-13': 'ISO-8859-13', 36 'iso-8859-14': '', 37 'iso-8859-15': 'ISO-8859-15', 38 'koi8-r': 'KOI8-R', 39 'koi8-u': 'KOI8-U', 40 'macroman': 'macintosh', 41 'cp437': '', 42 'cp775': '', 43 'cp850': '', 44 'cp852': '', 45 'cp855': '', 46 'cp857': '', 47 'cp860': '', 48 'cp861': '', 49 'cp862': '', 50 'cp863': '', 51 'cp865': '', 52 'cp866': 'IBM866', 53 'cp869': '', 54 'cp874': '', 55 'cp1250': 'windows-1250', 56 'cp1251': 'windows-1251', 57 'cp1253': 'windows-1253', 58 'cp1254': 'windows-1254', 59 'cp1255': 'windows-1255', 60 'cp1256': 'windows-1256', 61 'cp1257': 'windows-1257', 62 'cp1258': 'windows-1258', 63 'euc-jp': 'EUC-JP', 64 'sjis': 'Shift_JIS', 65 'cp932': 'Shift_JIS', 66 'cp949': '', 67 'euc-kr': 'EUC-KR', 68 'cp936': 'GBK', 69 'euc-cn': 'GB2312', 70 'big5': 'Big5', 71 'cp950': 'Big5', 72 'utf-8': 'UTF-8', 73 'ucs-2': 'UTF-8', 74 'ucs-2le': 'UTF-8', 75 'utf-16': 'UTF-8', 76 'utf-16le': 'UTF-8', 77 'ucs-4': 'UTF-8', 78 'ucs-4le': 'UTF-8', 79 } 80 81 # Notes: 82 # 1. All UCS/UTF are converted to UTF-8 because it is much better supported 83 # 2. Any blank spaces are there because Vim supports it but at least one major 84 # web browser does not according to http://wiki.whatwg.org/wiki/Web_Encodings. 85 # }}} 86 87 # Only automatically find encodings supported natively by Vim, let the user 88 # specify the encoding if it's not natively supported. This function is only 89 # used when the user specifies the charset, they better know what they are 90 # doing! 91 # 92 # Names in this list are from: 93 # http://www.iana.org/assignments/character-sets 94 # export const charset_to_encoding: {{{ 95 export const charset_to_encoding = { 96 'iso_8859-1:1987': 'latin1', 97 'iso-ir-100': 'latin1', 98 'iso_8859-1': 'latin1', 99 'iso-8859-1': 'latin1', 100 'latin1': 'latin1', 101 'l1': 'latin1', 102 'ibm819': 'latin1', 103 'cp819': 'latin1', 104 'csisolatin1': 'latin1', 105 'iso_8859-2:1987': 'iso-8859-2', 106 'iso-ir-101': 'iso-8859-2', 107 'iso_8859-2': 'iso-8859-2', 108 'iso-8859-2': 'iso-8859-2', 109 'latin2': 'iso-8859-2', 110 'l2': 'iso-8859-2', 111 'csisolatin2': 'iso-8859-2', 112 'iso_8859-3:1988': 'iso-8859-3', 113 'iso-ir-109': 'iso-8859-3', 114 'iso_8859-3': 'iso-8859-3', 115 'iso-8859-3': 'iso-8859-3', 116 'latin3': 'iso-8859-3', 117 'l3': 'iso-8859-3', 118 'csisolatin3': 'iso-8859-3', 119 'iso_8859-4:1988': 'iso-8859-4', 120 'iso-ir-110': 'iso-8859-4', 121 'iso_8859-4': 'iso-8859-4', 122 'iso-8859-4': 'iso-8859-4', 123 'latin4': 'iso-8859-4', 124 'l4': 'iso-8859-4', 125 'csisolatin4': 'iso-8859-4', 126 'iso_8859-5:1988': 'iso-8859-5', 127 'iso-ir-144': 'iso-8859-5', 128 'iso_8859-5': 'iso-8859-5', 129 'iso-8859-5': 'iso-8859-5', 130 'cyrillic': 'iso-8859-5', 131 'csisolatincyrillic': 'iso-8859-5', 132 'iso_8859-6:1987': 'iso-8859-6', 133 'iso-ir-127': 'iso-8859-6', 134 'iso_8859-6': 'iso-8859-6', 135 'iso-8859-6': 'iso-8859-6', 136 'ecma-114': 'iso-8859-6', 137 'asmo-708': 'iso-8859-6', 138 'arabic': 'iso-8859-6', 139 'csisolatinarabic': 'iso-8859-6', 140 'iso_8859-7:1987': 'iso-8859-7', 141 'iso-ir-126': 'iso-8859-7', 142 'iso_8859-7': 'iso-8859-7', 143 'iso-8859-7': 'iso-8859-7', 144 'elot_928': 'iso-8859-7', 145 'ecma-118': 'iso-8859-7', 146 'greek': 'iso-8859-7', 147 'greek8': 'iso-8859-7', 148 'csisolatingreek': 'iso-8859-7', 149 'iso_8859-8:1988': 'iso-8859-8', 150 'iso-ir-138': 'iso-8859-8', 151 'iso_8859-8': 'iso-8859-8', 152 'iso-8859-8': 'iso-8859-8', 153 'hebrew': 'iso-8859-8', 154 'csisolatinhebrew': 'iso-8859-8', 155 'iso_8859-9:1989': 'iso-8859-9', 156 'iso-ir-148': 'iso-8859-9', 157 'iso_8859-9': 'iso-8859-9', 158 'iso-8859-9': 'iso-8859-9', 159 'latin5': 'iso-8859-9', 160 'l5': 'iso-8859-9', 161 'csisolatin5': 'iso-8859-9', 162 'iso-8859-10': 'iso-8859-10', 163 'iso-ir-157': 'iso-8859-10', 164 'l6': 'iso-8859-10', 165 'iso_8859-10:1992': 'iso-8859-10', 166 'csisolatin6': 'iso-8859-10', 167 'latin6': 'iso-8859-10', 168 'iso-8859-13': 'iso-8859-13', 169 'iso-8859-14': 'iso-8859-14', 170 'iso-ir-199': 'iso-8859-14', 171 'iso_8859-14:1998': 'iso-8859-14', 172 'iso_8859-14': 'iso-8859-14', 173 'latin8': 'iso-8859-14', 174 'iso-celtic': 'iso-8859-14', 175 'l8': 'iso-8859-14', 176 'iso-8859-15': 'iso-8859-15', 177 'iso_8859-15': 'iso-8859-15', 178 'latin-9': 'iso-8859-15', 179 'koi8-r': 'koi8-r', 180 'cskoi8r': 'koi8-r', 181 'koi8-u': 'koi8-u', 182 'macintosh': 'macroman', 183 'mac': 'macroman', 184 'csmacintosh': 'macroman', 185 'ibm437': 'cp437', 186 'cp437': 'cp437', 187 '437': 'cp437', 188 'cspc8codepage437': 'cp437', 189 'ibm775': 'cp775', 190 'cp775': 'cp775', 191 'cspc775baltic': 'cp775', 192 'ibm850': 'cp850', 193 'cp850': 'cp850', 194 '850': 'cp850', 195 'cspc850multilingual': 'cp850', 196 'ibm852': 'cp852', 197 'cp852': 'cp852', 198 '852': 'cp852', 199 'cspcp852': 'cp852', 200 'ibm855': 'cp855', 201 'cp855': 'cp855', 202 '855': 'cp855', 203 'csibm855': 'cp855', 204 'ibm857': 'cp857', 205 'cp857': 'cp857', 206 '857': 'cp857', 207 'csibm857': 'cp857', 208 'ibm860': 'cp860', 209 'cp860': 'cp860', 210 '860': 'cp860', 211 'csibm860': 'cp860', 212 'ibm861': 'cp861', 213 'cp861': 'cp861', 214 '861': 'cp861', 215 'cp-is': 'cp861', 216 'csibm861': 'cp861', 217 'ibm862': 'cp862', 218 'cp862': 'cp862', 219 '862': 'cp862', 220 'cspc862latinhebrew': 'cp862', 221 'ibm863': 'cp863', 222 'cp863': 'cp863', 223 '863': 'cp863', 224 'csibm863': 'cp863', 225 'ibm865': 'cp865', 226 'cp865': 'cp865', 227 '865': 'cp865', 228 'csibm865': 'cp865', 229 'ibm866': 'cp866', 230 'cp866': 'cp866', 231 '866': 'cp866', 232 'csibm866': 'cp866', 233 'ibm869': 'cp869', 234 'cp869': 'cp869', 235 '869': 'cp869', 236 'cp-gr': 'cp869', 237 'csibm869': 'cp869', 238 'windows-1250': 'cp1250', 239 'windows-1251': 'cp1251', 240 'windows-1253': 'cp1253', 241 'windows-1254': 'cp1254', 242 'windows-1255': 'cp1255', 243 'windows-1256': 'cp1256', 244 'windows-1257': 'cp1257', 245 'windows-1258': 'cp1258', 246 'extended_unix_code_packed_format_for_japanese': 'euc-jp', 247 'cseucpkdfmtjapanese': 'euc-jp', 248 'euc-jp': 'euc-jp', 249 'shift_jis': 'sjis', 250 'ms_kanji': 'sjis', 251 'sjis': 'sjis', 252 'csshiftjis': 'sjis', 253 'ibm-thai': 'cp874', 254 'csibmthai': 'cp874', 255 'ks_c_5601-1987': 'cp949', 256 'iso-ir-149': 'cp949', 257 'ks_c_5601-1989': 'cp949', 258 'ksc_5601': 'cp949', 259 'korean': 'cp949', 260 'csksc56011987': 'cp949', 261 'euc-kr': 'euc-kr', 262 'cseuckr': 'euc-kr', 263 'gbk': 'cp936', 264 'cp936': 'cp936', 265 'ms936': 'cp936', 266 'windows-936': 'cp936', 267 'gb_2312-80': 'euc-cn', 268 'iso-ir-58': 'euc-cn', 269 'chinese': 'euc-cn', 270 'csiso58gb231280': 'euc-cn', 271 'big5': 'big5', 272 'csbig5': 'big5', 273 'utf-8': 'utf-8', 274 'iso-10646-ucs-2': 'ucs-2', 275 'csunicode': 'ucs-2', 276 'utf-16': 'utf-16', 277 'utf-16be': 'utf-16', 278 'utf-16le': 'utf-16le', 279 'utf-32': 'ucs-4', 280 'utf-32be': 'ucs-4', 281 'utf-32le': 'ucs-4le', 282 'iso-10646-ucs-4': 'ucs-4', 283 'csucs4': 'ucs-4' 284 } 285 #}}} 286 287 var settings: dict<any> 288 289 export def Convert2HTML(line1: number, line2: number) #{{{ 290 settings = GetUserSettings() 291 292 if !&diff || settings.diff_one_file #{{{ 293 if line2 >= line1 294 g:html_start_line = line1 295 g:html_end_line = line2 296 else 297 g:html_start_line = line2 298 g:html_end_line = line1 299 endif 300 runtime syntax/2html.vim #}}} 301 else #{{{ 302 var win_list = range(1, winnr('$')) 303 ->filter((_, w) => getwinvar(w, '&diff')) 304 ->mapnew((_, w) => winbufnr(w)) 305 var buf_list: list<number> 306 settings.whole_filler = 1 307 g:html_diff_win_num = 0 308 for window in win_list 309 # switch to the next buffer to convert 310 win_gotoid(bufwinid(window)) 311 312 # figure out whether current charset and encoding will work, if not 313 # default to UTF-8 314 if !exists('g:html_use_encoding') && 315 (((&l:fileencoding == '' || (&l:buftype != '' && &l:buftype !=? 'help')) 316 && &encoding !=? settings.vim_encoding) 317 || &l:fileencoding != '' && &l:fileencoding !=? settings.vim_encoding) 318 echohl WarningMsg 319 echomsg "TOhtml: mismatched file encodings in Diff buffers, using UTF-8" 320 echohl None 321 settings.vim_encoding = 'utf-8' 322 settings.encoding = 'UTF-8' 323 endif 324 325 # set up for diff-mode conversion 326 g:html_start_line = 1 327 g:html_end_line = line('$') 328 g:html_diff_win_num += 1 329 330 # convert this file 331 runtime syntax/2html.vim 332 333 # remember the HTML buffer for later combination 334 add(buf_list, bufnr('%')) 335 endfor 336 unlet g:html_diff_win_num 337 Diff2HTML(win_list, buf_list) 338 endif #}}} 339 unlet g:html_start_line 340 unlet g:html_end_line 341 settings = null_dict 342 enddef #}}} 343 344 export def Diff2HTML(win_list: list<number>, buf_list: list<number>) #{{{ 345 var xml_line = '' 346 var tag_close = '>' 347 348 var old_paste = &paste 349 set paste 350 var old_magic = &magic 351 set magic 352 353 var html = [] 354 var style: list<string> 355 var body_line: string 356 var body_end_line: string 357 var body_line_num: number 358 var html5 = 0 359 if !settings.no_doc 360 if settings.use_xhtml 361 if settings.encoding != '' 362 xml_line = "<?xml version=\"1.0\" encoding=\"" .. settings.encoding .. "\"?>" 363 else 364 xml_line = "<?xml version=\"1.0\"?>" 365 endif 366 tag_close = ' />' 367 endif 368 369 style = [settings.use_xhtml ? '' : '-->'] 370 371 if settings.use_xhtml 372 add(html, xml_line) 373 endif 374 if settings.use_xhtml 375 add(html, "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">") 376 add(html, '<html xmlns="http://www.w3.org/1999/xhtml">') 377 elseif settings.use_css && !settings.no_pre 378 add(html, "<!DOCTYPE html>") 379 add(html, '<html>') 380 html5 = 1 381 else 382 add(html, '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"') 383 add(html, ' "http://www.w3.org/TR/html4/loose.dtd">') 384 add(html, '<html>') 385 endif 386 add(html, '<head>') 387 388 # include encoding as close to the top as possible, but only if not already 389 # contained in XML information 390 if settings.encoding != '' && !settings.use_xhtml 391 if html5 392 add(html, '<meta charset="' .. settings.encoding .. '"' .. tag_close) 393 else 394 add(html, "<meta http-equiv=\"content-type\" content=\"text/html; charset=" .. settings.encoding .. '"' .. tag_close) 395 endif 396 endif 397 398 add(html, '<title>diff</title>') 399 add(html, $'<meta name="Generator" content="Vim/{v:version / 100}.{v:version % 100}"{tag_close}') 400 add(html, $'<meta name="plugin-version" content="{g:loaded_2html_plugin}"{tag_close}') 401 add(html, '<meta name="settings" content="' .. 402 join(filter(keys(settings), (_, k) => { 403 var v = settings[k] 404 if type(v) == v:t_number 405 return v != 0 406 elseif type(v) == v:t_bool 407 return !empty(v) 408 else 409 return false 410 endif 411 }), ',') .. 412 ',prevent_copy=' .. settings.prevent_copy .. 413 ',use_input_for_pc=' .. settings.use_input_for_pc .. 414 '"' .. tag_close) 415 add(html, '<meta name="colorscheme" content="' .. 416 (exists('g:colors_name') 417 ? g:colors_name 418 : 'none') .. '"' .. tag_close) 419 420 add(html, '</head>') 421 body_line_num = len(html) 422 add(html, '<body' .. (settings.line_ids ? ' onload="JumpToLine();"' : '') .. '>') 423 endif 424 add(html, "<table " .. (settings.use_css ? "" : "border='1' width='100%' ") .. "id='vimCodeElement" .. settings.id_suffix .. "'>") 425 426 add(html, '<tr>') 427 for buf in win_list 428 add(html, '<th>' .. bufname(buf) .. '</th>') 429 endfor 430 add(html, '</tr><tr>') 431 432 var diff_style_start = 0 433 var insert_index = 0 434 435 for buf in buf_list 436 var temp = [] 437 win_gotoid(bufwinid(buf)) 438 439 # If text is folded because of user foldmethod settings, etc. we don't want 440 # to act on everything in a fold by mistake. 441 setlocal nofoldenable 442 443 # When not using CSS or when using xhtml, the <body> line can be important. 444 # Assume it will be the same for all buffers and grab it from the first 445 # buffer. Similarly, need to grab the body end line as well. 446 if !settings.no_doc 447 if body_line == '' 448 :1 449 search('<body') 450 body_line = getline('.') 451 :$ 452 search('</body>', 'b') 453 body_end_line = getline('.') 454 endif 455 456 # Grab the style information. Some of this will be duplicated so only insert 457 # it if it's not already there. {{{ 458 :1 459 var style_start = search('^<style\( type="text/css"\)\?>') 460 :1 461 var style_end = search('^</style>') 462 if style_start > 0 && style_end > 0 463 var buf_styles = getline(style_start + 1, style_end - 1) 464 for a_style in buf_styles 465 if index(style, a_style) == -1 466 if diff_style_start == 0 467 if a_style =~ '\<Diff\(Change\|Text\|Add\|Delete\)' 468 diff_style_start = len(style) - 1 469 endif 470 endif 471 insert(style, a_style, insert_index) 472 insert_index += 1 473 endif 474 endfor 475 endif # }}} 476 477 # everything new will get added before the diff styles so diff highlight 478 # properly overrides normal highlight 479 if diff_style_start != 0 480 insert_index = diff_style_start 481 endif 482 483 # Delete those parts that are not needed so we can include the rest into the 484 # resulting table. 485 :1,/^<body.*\%(\n<!--.*-->\_s\+.*id='oneCharWidth'.*\_s\+.*id='oneInputWidth'.*\_s\+.*id='oneEmWidth'\)\?\zs/d _ 486 :$ 487 :?</body>?,$d _ 488 elseif !settings.no_modeline 489 # remove modeline from source files if it is included and we haven't deleted 490 # due to removing html footer already 491 :$d 492 endif 493 temp = getline(1, '$') 494 # clean out id on the main content container because we already set it on 495 # the table 496 temp[0] = substitute(temp[0], " id='vimCodeElement[^']*'", "", "") 497 # undo deletion of start and end part 498 # so we can later save the file as valid html 499 # TODO: restore using grabbed lines if undolevel is 1? 500 if !settings.no_doc 501 normal! 2u 502 elseif !settings.no_modeline 503 normal! u 504 endif 505 if settings.use_css 506 add(html, '<td><div>') 507 elseif settings.use_xhtml 508 add(html, '<td nowrap="nowrap" valign="top"><div>') 509 else 510 add(html, '<td nowrap valign="top"><div>') 511 endif 512 html += temp 513 add(html, '</div></td>') 514 515 # Close this buffer 516 # TODO: the comment above says we're going to allow saving the file 517 # later...but here we discard it? 518 quit! 519 endfor 520 521 if !settings.no_doc 522 html[body_line_num] = body_line 523 endif 524 525 add(html, '</tr>') 526 add(html, '</table>') 527 if !settings.no_doc 528 add(html, body_end_line) 529 add(html, '</html>') 530 endif 531 532 # The generated HTML is admittedly ugly and takes a LONG time to fold. 533 # Make sure the user doesn't do syntax folding when loading a generated file, 534 # using a modeline. 535 if !settings.no_modeline 536 add(html, '<!-- vim: set foldmethod=manual : -->') 537 endif 538 539 var i = 1 540 var name = "Diff" .. (settings.use_xhtml ? ".xhtml" : ".html") 541 # Find an unused file name if current file name is already in use 542 while filereadable(name) 543 name = substitute(name, '\d*\.x\?html$', '', '') .. i .. '.' .. fnamemodify(copy(name), ":t:e") 544 i += 1 545 endwhile 546 547 var ei_sav = &eventignore 548 set eventignore+=FileType 549 execute "topleft new" name 550 &eventignore = ei_sav 551 552 setlocal modifiable 553 554 # just in case some user autocmd creates content in the new buffer, make sure 555 # it is empty before proceeding 556 deletebufline(bufnr(), 1, '$') 557 558 # set the fileencoding to match the charset we'll be using 559 &l:fileencoding = settings.vim_encoding 560 561 # According to http://www.w3.org/TR/html4/charset.html#doc-char-set, the byte 562 # order mark is highly recommend on the web when using multibyte encodings. But, 563 # it is not a good idea to include it on UTF-8 files. Otherwise, let Vim 564 # determine when it is actually inserted. 565 if settings.vim_encoding == 'utf-8' 566 setlocal nobomb 567 else 568 setlocal bomb 569 endif 570 571 append(0, html) 572 573 if !settings.no_doc 574 if len(style) > 0 575 :1 576 var style_start = search('^</head>') - 1 577 578 # add required javascript in reverse order so we can just call append again 579 # and again without adjusting {{{ 580 581 var uses_script = settings.dynamic_folds || settings.line_ids 582 583 # insert script closing tag if needed 584 if uses_script 585 append(style_start, [ 586 '', 587 settings.use_xhtml ? '//]]>' : '-->', 588 "</script>" 589 ]) 590 endif 591 592 # insert javascript to get IDs from line numbers, and to open a fold before 593 # jumping to any lines contained therein 594 if settings.line_ids 595 append(style_start, [ 596 " /* Always jump to new location even if the line was hidden inside a fold, or", 597 " * we corrected the raw number to a line ID.", 598 " */", 599 " if (lineElem) {", 600 " lineElem.scrollIntoView(true);", 601 " }", 602 " return true;", 603 "}", 604 "if ('onhashchange' in window) {", 605 " window.onhashchange = JumpToLine;", 606 "}" 607 ]) 608 609 if settings.dynamic_folds 610 append(style_start, [ 611 "", 612 " /* navigate upwards in the DOM tree to open all folds containing the line */", 613 " var node = lineElem;", 614 " while (node && node.id != 'vimCodeElement" .. settings.id_suffix .. "')", 615 " {", 616 " if (node.className == 'closed-fold')", 617 " {", 618 " /* toggle open the fold ID (remove window ID) */", 619 " toggleFold(node.id.substr(4));", 620 " }", 621 " node = node.parentNode;", 622 " }", 623 ]) 624 endif 625 endif 626 627 if settings.line_ids 628 append(style_start, [ 629 "", 630 "/* function to open any folds containing a jumped-to line before jumping to it */", 631 "function JumpToLine()", 632 "{", 633 " var lineNum;", 634 " lineNum = window.location.hash;", 635 " lineNum = lineNum.substr(1); /* strip off '#' */", 636 "", 637 " if (lineNum.indexOf('L') == -1) {", 638 " lineNum = 'L'+lineNum;", 639 " }", 640 " if (lineNum.indexOf('W') == -1) {", 641 " lineNum = 'W1'+lineNum;", 642 " }", 643 " var lineElem = document.getElementById(lineNum);" 644 ]) 645 endif 646 647 # Insert javascript to toggle matching folds open and closed in all windows, 648 # if dynamic folding is active. 649 if settings.dynamic_folds 650 append(style_start, [ 651 " function toggleFold(objID)", 652 " {", 653 " for (win_num = 1; win_num <= " .. len(buf_list) .. "; win_num++)", 654 " {", 655 " var fold;", 656 ' fold = document.getElementById("win"+win_num+objID);', 657 " if(fold.className == 'closed-fold')", 658 " {", 659 " fold.className = 'open-fold';", 660 " }", 661 " else if (fold.className == 'open-fold')", 662 " {", 663 " fold.className = 'closed-fold';", 664 " }", 665 " }", 666 " }", 667 ]) 668 endif 669 670 if uses_script 671 # insert script tag if needed 672 append(style_start, [ 673 "<script" .. (html5 ? "" : " type='text/javascript'") .. ">", 674 settings.use_xhtml ? '//<![CDATA[' : "<!--"]) 675 endif 676 677 # Insert styles from all the generated html documents and additional styles 678 # for the table-based layout of the side-by-side diff. The diff should take 679 # up the full browser window (but not more), and be static in size, 680 # horizontally scrollable when the lines are too long. Otherwise, the diff 681 # is pretty useless for really long lines. {{{ 682 if settings.use_css 683 append(style_start, 684 ['<style' .. (html5 ? '' : ' type="text/css"') .. '>'] + 685 style + 686 [ settings.use_xhtml ? '' : '<!--', 687 'table { table-layout: fixed; }', 688 'html, body, table, tbody { width: 100%; margin: 0; padding: 0; }', 689 'table, td, th { border: 1px solid; }', 690 'td { vertical-align: top; }', 691 'th, td { width: ' .. printf("%.1f", 100.0 / len(win_list)) .. '%; }', 692 'td div { overflow: auto; }', 693 settings.use_xhtml ? '' : '-->', 694 '</style>' 695 ]) 696 endif #}}} 697 endif 698 endif 699 700 &paste = old_paste 701 &magic = old_magic 702 enddef #}}} 703 704 # Gets a single user option and sets it in the passed-in Dict, or gives it the 705 # default value if the option doesn't actually exist. 706 export def GetOption(_settings: dict<any>, option: string, default: any) #{{{ 707 _settings[option] = get(g:, $'html_{option}', default) 708 enddef #}}} 709 710 # returns a Dict containing the values of all user options for 2html, including 711 # default values for those not given an explicit value by the user. Discards the 712 # html_ prefix of the option for nicer looking code. 713 export def GetUserSettings(): dict<any> #{{{ 714 if !empty(settings) 715 # just restore the known options if we've already retrieved them 716 return settings 717 else 718 # otherwise figure out which options are set 719 var user_settings = {} 720 721 # Define the correct option if the old option name exists and we haven't 722 # already defined the correct one. 723 if exists('g:use_xhtml') && !exists("g:html_use_xhtml") 724 echohl WarningMsg 725 echomsg "Warning: g:use_xhtml is deprecated, use g:html_use_xhtml" 726 echohl None 727 g:html_use_xhtml = g:use_xhtml 728 endif 729 730 # get current option settings with appropriate defaults {{{ 731 GetOption(user_settings, 'no_progress', !has("statusline") ) 732 GetOption(user_settings, 'diff_one_file', 0 ) 733 GetOption(user_settings, 'number_lines', &number ) 734 GetOption(user_settings, 'pre_wrap', &wrap ) 735 GetOption(user_settings, 'use_css', 1 ) 736 GetOption(user_settings, 'ignore_conceal', 0 ) 737 GetOption(user_settings, 'ignore_folding', 0 ) 738 GetOption(user_settings, 'dynamic_folds', 0 ) 739 GetOption(user_settings, 'no_foldcolumn', user_settings.ignore_folding) 740 GetOption(user_settings, 'hover_unfold', 0 ) 741 GetOption(user_settings, 'no_pre', 0 ) 742 GetOption(user_settings, 'no_doc', 0 ) 743 GetOption(user_settings, 'no_links', 0 ) 744 GetOption(user_settings, 'no_modeline', 0 ) 745 GetOption(user_settings, 'no_invalid', 0 ) 746 GetOption(user_settings, 'whole_filler', 0 ) 747 GetOption(user_settings, 'use_xhtml', 0 ) 748 GetOption(user_settings, 'line_ids', user_settings.number_lines ) 749 GetOption(user_settings, 'use_input_for_pc', 'none') 750 # }}} 751 752 # override those settings that need it {{{ 753 754 # hover opening implies dynamic folding 755 if user_settings.hover_unfold 756 user_settings.dynamic_folds = 1 757 endif 758 759 # ignore folding overrides dynamic folding 760 if user_settings.ignore_folding && user_settings.dynamic_folds 761 user_settings.dynamic_folds = 0 762 user_settings.hover_unfold = 0 763 endif 764 765 # dynamic folding with no foldcolumn implies hover opens 766 if user_settings.dynamic_folds && user_settings.no_foldcolumn 767 user_settings.hover_unfold = 1 768 endif 769 770 # dynamic folding implies css 771 if user_settings.dynamic_folds 772 user_settings.use_css = 1 773 else 774 user_settings.no_foldcolumn = 1 # won't do anything but for consistency and for the test suite 775 endif 776 777 # if we're not using CSS we cannot use a pre section because <font> tags 778 # aren't allowed inside a <pre> block 779 if !user_settings.use_css 780 user_settings.no_pre = 1 781 endif 782 783 # pre_wrap doesn't do anything if not using pre or not using CSS 784 if user_settings.no_pre || !user_settings.use_css 785 user_settings.pre_wrap = 0 786 endif 787 #}}} 788 789 # set up expand_tabs option after all the overrides so we know the 790 # appropriate defaults #{{{ 791 if user_settings.no_pre == 0 792 GetOption(user_settings, 793 'expand_tabs', 794 &expandtab || &ts != 8 || &vts != '' || user_settings.number_lines || 795 (user_settings.dynamic_folds && !user_settings.no_foldcolumn)) 796 else 797 user_settings.expand_tabs = 1 798 endif 799 # }}} 800 801 # textual options 802 if exists("g:html_use_encoding") #{{{ 803 # user specified the desired MIME charset, figure out proper 804 # 'fileencoding' from it or warn the user if we cannot 805 user_settings.encoding = g:html_use_encoding 806 user_settings.vim_encoding = EncodingFromCharset(g:html_use_encoding) 807 if user_settings.vim_encoding == '' 808 echohl WarningMsg 809 echomsg "TOhtml: file encoding for" g:html_use_encoding "unknown, please set 'fileencoding'" 810 echohl None 811 endif 812 else 813 # Figure out proper MIME charset from 'fileencoding' if possible 814 if &l:fileencoding != '' 815 # If the buffer is not a "normal" type, the 'fileencoding' value may not 816 # be trusted; since the buffer should not be written the fileencoding is 817 # not intended to be used. 818 if &l:buftype == '' || &l:buftype ==? 'help' 819 user_settings.vim_encoding = &l:fileencoding 820 CharsetFromEncoding(user_settings) 821 else 822 user_settings.encoding = '' # trigger detection using &encoding 823 endif 824 endif 825 826 # else from 'encoding' if possible 827 if &l:fileencoding == '' || user_settings.encoding == '' 828 user_settings.vim_encoding = &encoding 829 CharsetFromEncoding(user_settings) 830 endif 831 832 # else default to UTF-8 and warn user 833 if user_settings.encoding == '' 834 user_settings.vim_encoding = 'utf-8' 835 user_settings.encoding = 'UTF-8' 836 echohl WarningMsg 837 echomsg "TOhtml: couldn't determine MIME charset, using UTF-8" 838 echohl None 839 endif 840 endif #}}} 841 842 # Default to making nothing uncopyable, because we default to 843 # not-standards way of doing things, and also because Microsoft Word and 844 # others paste the <input> elements anyway. 845 # 846 # html_prevent_copy only has an effect when using CSS. 847 # 848 # All options: 849 # f - fold column 850 # n - line numbers (also within fold text) 851 # t - fold text 852 # d - diff filler 853 # c - concealed text (reserved future) 854 # l - listchars (reserved possible future) 855 # s - signs (reserved possible future) 856 # 857 # Normal text is always selectable. 858 user_settings.prevent_copy = "" 859 if user_settings.use_css 860 if exists("g:html_prevent_copy") 861 if user_settings.dynamic_folds && !user_settings.no_foldcolumn && g:html_prevent_copy =~# 'f' 862 user_settings.prevent_copy ..= 'f' 863 endif 864 if user_settings.number_lines && g:html_prevent_copy =~# 'n' 865 user_settings.prevent_copy ..= 'n' 866 endif 867 if &diff && g:html_prevent_copy =~# 'd' 868 user_settings.prevent_copy ..= 'd' 869 endif 870 if !user_settings.ignore_folding && g:html_prevent_copy =~# 't' 871 user_settings.prevent_copy ..= 't' 872 endif 873 else 874 user_settings.prevent_copy = "" 875 endif 876 endif 877 if empty(user_settings.prevent_copy) 878 user_settings.no_invalid = 0 879 endif 880 881 # enforce valid values for use_input_for_pc 882 if user_settings.use_input_for_pc !~# 'fallback\|none\|all' 883 user_settings.use_input_for_pc = 'none' 884 echohl WarningMsg 885 echomsg '2html: "' .. g:html_use_input_for_pc .. '" is not valid for g:html_use_input_for_pc' 886 echomsg '2html: defaulting to "' .. user_settings.use_input_for_pc .. '"' 887 echohl None 888 sleep 3 889 endif 890 891 if exists('g:html_id_expr') 892 user_settings.id_suffix = eval(g:html_id_expr) 893 if user_settings.id_suffix !~ '^[-_:.A-Za-z0-9]*$' 894 echohl WarningMsg 895 echomsg '2html: g:html_id_expr evaluated to invalid string for HTML id attributes' 896 echomsg '2html: Omitting user-specified suffix' 897 echohl None 898 sleep 3 899 user_settings.id_suffix = "" 900 endif 901 else 902 user_settings.id_suffix = "" 903 endif 904 905 # TODO: font 906 907 return user_settings 908 endif 909 enddef #}}} 910 911 # get the proper HTML charset name from a Vim encoding option. 912 export def CharsetFromEncoding(_settings: dict<any>) #{{{ 913 var vim_encoding = _settings.vim_encoding 914 if exists('g:html_charset_override') && has_key(g:html_charset_override, vim_encoding) 915 _settings.encoding = g:html_charset_override[vim_encoding] 916 else 917 if vim_encoding =~ '^8bit\|^2byte' 918 # 8bit- and 2byte- prefixes are to indicate encodings available on the 919 # system that Vim will convert with iconv(), look up just the encoding name, 920 # not Vim's prefix. 921 vim_encoding = substitute(vim_encoding, '^8bit-\|^2byte-', '', '') 922 endif 923 if has_key(encoding_to_charset, vim_encoding) 924 _settings.encoding = encoding_to_charset[vim_encoding] 925 else 926 _settings.encoding = "" 927 endif 928 endif 929 if _settings.encoding != "" 930 var vim_encoding2 = EncodingFromCharset(_settings.encoding) 931 if vim_encoding2 != "" 932 # if the Vim encoding to HTML encoding conversion is set up (by default or 933 # by the user) to convert to a different encoding, we need to also change 934 # the Vim encoding of the new buffer 935 _settings.vim_encoding = vim_encoding2 936 endif 937 endif 938 enddef #}}} 939 940 # Get the proper Vim encoding option setting from an HTML charset name. 941 export def EncodingFromCharset(encoding: string): string #{{{ 942 if exists('g:html_encoding_override') && has_key(g:html_encoding_override, encoding) 943 return g:html_encoding_override[encoding] 944 elseif has_key(charset_to_encoding, tolower(encoding)) 945 return charset_to_encoding[tolower(encoding)] 946 else 947 return "" 948 endif 949 enddef #}}} 950 951 # Make sure any patches will probably use consistent indent 952 # vim: ts=8 sw=2 sts=2 noet fdm=marker