robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
aa1d8a2702
master
1--- @brief 2---<pre>help 3---:[range]TOhtml {file} *:TOhtml* 4---Converts the buffer shown in the current window to HTML, opens the generated 5---HTML in a new split window, and saves its contents to {file}. If {file} is not 6---given, a temporary file (created by |tempname()|) is used. 7---</pre> 8--- 9--- The plugin is not loaded by default; use `:packadd` to activate it: 10--- ``` 11--- :packadd nvim.tohtml 12--- ``` 13 14-- This HTML conversion script is different from Vim's. If you want to use Vim's TOhtml converter, 15-- download the related files from the Vim repo: 16-- - https://github.com/vim/vim/blob/master/runtime/syntax/2html.vim 17-- - https://github.com/vim/vim/blob/master/runtime/autoload/tohtml.vim 18-- - https://github.com/vim/vim/blob/master/runtime/plugin/tohtml.vim 19-- 20-- Main differences between this and the Vim version: 21-- - No "ignore some visual thing" settings (just set the right Vim option) 22-- - No support for legacy web engines 23-- - No support for legacy encoding (supports only UTF-8) 24-- - No interactive webpage 25-- - No specifying the internal HTML (no XHTML, no use_css=false) 26-- - No multiwindow diffs 27-- - No ranges 28-- 29-- Remarks: 30-- - Not all visuals are supported, so it may differ. 31 32--- @class (private) vim.tohtml.state.global 33--- @field background string 34--- @field foreground string 35--- @field title string|false 36--- @field font string 37--- @field highlights_name table<integer,string> 38--- @field conf vim.tohtml.opt 39 40--- @class (private) vim.tohtml.state:vim.tohtml.state.global 41--- @field style vim.tohtml.styletable 42--- @field tabstop string|false 43--- @field opt vim.wo 44--- @field winid integer 45--- @field bufnr integer 46--- @field width integer 47--- @field start integer 48--- @field end_ integer 49 50--- @class (private) vim.tohtml.styletable 51--- @field [integer] vim.tohtml.line (integer: (1-index, exclusive)) 52 53--- @class (private) vim.tohtml.line 54--- @field virt_lines {[integer]:[string,integer][]} 55--- @field pre_text [string, integer?][] 56--- @field hide? boolean 57--- @field [integer] vim.tohtml.cell? (integer: (1-index, exclusive)) 58 59--- @class (private) vim.tohtml.cell 60--- @field [1] integer[] start 61--- @field [2] integer[] close 62--- @field [3] any[][] virt_text 63--- @field [4] any[][] overlay_text 64 65--- @type string[] 66local notifications ={} 67 68---@param msg string 69local function notify ( msg ) 70if # notifications == 0 then 71vim . schedule ( function () 72if # notifications > 1 then 73vim . notify (( 'TOhtml: %s (+ %d more warnings)' ): format ( notifications [ 1 ], # notifications - 1 )) 74elseif # notifications == 1 then 75vim . notify ( 'TOhtml: ' .. notifications [ 1 ]) 76end 77notifications ={} 78end ) 79end 80table . insert ( notifications , msg ) 81end 82 83local HIDE_ID =- 1 84-- stylua: ignore start 85local cterm_8_to_hex ={ 86[ 0 ] ="#808080" , "#ff6060" , "#00ff00" , "#ffff00" , 87"#8080ff" , "#ff40ff" , "#00ffff" , "#ffffff" , 88} 89local cterm_16_to_hex ={ 90[ 0 ] ="#000000" , "#c00000" , "#008000" , "#804000" , 91"#0000c0" , "#c000c0" , "#008080" , "#c0c0c0" , 92"#808080" , "#ff6060" , "#00ff00" , "#ffff00" , 93"#8080ff" , "#ff40ff" , "#00ffff" , "#ffffff" , 94} 95local cterm_88_to_hex ={ 96[ 0 ] ="#000000" , "#c00000" , "#008000" , "#804000" , 97"#0000c0" , "#c000c0" , "#008080" , "#c0c0c0" , 98"#808080" , "#ff6060" , "#00ff00" , "#ffff00" , 99"#8080ff" , "#ff40ff" , "#00ffff" , "#ffffff" , 100"#000000" , "#00008b" , "#0000cd" , "#0000ff" , 101"#008b00" , "#008b8b" , "#008bcd" , "#008bff" , 102"#00cd00" , "#00cd8b" , "#00cdcd" , "#00cdff" , 103"#00ff00" , "#00ff8b" , "#00ffcd" , "#00ffff" , 104"#8b0000" , "#8b008b" , "#8b00cd" , "#8b00ff" , 105"#8b8b00" , "#8b8b8b" , "#8b8bcd" , "#8b8bff" , 106"#8bcd00" , "#8bcd8b" , "#8bcdcd" , "#8bcdff" , 107"#8bff00" , "#8bff8b" , "#8bffcd" , "#8bffff" , 108"#cd0000" , "#cd008b" , "#cd00cd" , "#cd00ff" , 109"#cd8b00" , "#cd8b8b" , "#cd8bcd" , "#cd8bff" , 110"#cdcd00" , "#cdcd8b" , "#cdcdcd" , "#cdcdff" , 111"#cdff00" , "#cdff8b" , "#cdffcd" , "#cdffff" , 112"#ff0000" , "#ff008b" , "#ff00cd" , "#ff00ff" , 113"#ff8b00" , "#ff8b8b" , "#ff8bcd" , "#ff8bff" , 114"#ffcd00" , "#ffcd8b" , "#ffcdcd" , "#ffcdff" , 115"#ffff00" , "#ffff8b" , "#ffffcd" , "#ffffff" , 116"#2e2e2e" , "#5c5c5c" , "#737373" , "#8b8b8b" , 117"#a2a2a2" , "#b9b9b9" , "#d0d0d0" , "#e7e7e7" , 118} 119local cterm_256_to_hex ={ 120[ 0 ] ="#000000" , "#c00000" , "#008000" , "#804000" , 121"#0000c0" , "#c000c0" , "#008080" , "#c0c0c0" , 122"#808080" , "#ff6060" , "#00ff00" , "#ffff00" , 123"#8080ff" , "#ff40ff" , "#00ffff" , "#ffffff" , 124"#000000" , "#00005f" , "#000087" , "#0000af" , 125"#0000d7" , "#0000ff" , "#005f00" , "#005f5f" , 126"#005f87" , "#005faf" , "#005fd7" , "#005fff" , 127"#008700" , "#00875f" , "#008787" , "#0087af" , 128"#0087d7" , "#0087ff" , "#00af00" , "#00af5f" , 129"#00af87" , "#00afaf" , "#00afd7" , "#00afff" , 130"#00d700" , "#00d75f" , "#00d787" , "#00d7af" , 131"#00d7d7" , "#00d7ff" , "#00ff00" , "#00ff5f" , 132"#00ff87" , "#00ffaf" , "#00ffd7" , "#00ffff" , 133"#5f0000" , "#5f005f" , "#5f0087" , "#5f00af" , 134"#5f00d7" , "#5f00ff" , "#5f5f00" , "#5f5f5f" , 135"#5f5f87" , "#5f5faf" , "#5f5fd7" , "#5f5fff" , 136"#5f8700" , "#5f875f" , "#5f8787" , "#5f87af" , 137"#5f87d7" , "#5f87ff" , "#5faf00" , "#5faf5f" , 138"#5faf87" , "#5fafaf" , "#5fafd7" , "#5fafff" , 139"#5fd700" , "#5fd75f" , "#5fd787" , "#5fd7af" , 140"#5fd7d7" , "#5fd7ff" , "#5fff00" , "#5fff5f" , 141"#5fff87" , "#5fffaf" , "#5fffd7" , "#5fffff" , 142"#870000" , "#87005f" , "#870087" , "#8700af" , 143"#8700d7" , "#8700ff" , "#875f00" , "#875f5f" , 144"#875f87" , "#875faf" , "#875fd7" , "#875fff" , 145"#878700" , "#87875f" , "#878787" , "#8787af" , 146"#8787d7" , "#8787ff" , "#87af00" , "#87af5f" , 147"#87af87" , "#87afaf" , "#87afd7" , "#87afff" , 148"#87d700" , "#87d75f" , "#87d787" , "#87d7af" , 149"#87d7d7" , "#87d7ff" , "#87ff00" , "#87ff5f" , 150"#87ff87" , "#87ffaf" , "#87ffd7" , "#87ffff" , 151"#af0000" , "#af005f" , "#af0087" , "#af00af" , 152"#af00d7" , "#af00ff" , "#af5f00" , "#af5f5f" , 153"#af5f87" , "#af5faf" , "#af5fd7" , "#af5fff" , 154"#af8700" , "#af875f" , "#af8787" , "#af87af" , 155"#af87d7" , "#af87ff" , "#afaf00" , "#afaf5f" , 156"#afaf87" , "#afafaf" , "#afafd7" , "#afafff" , 157"#afd700" , "#afd75f" , "#afd787" , "#afd7af" , 158"#afd7d7" , "#afd7ff" , "#afff00" , "#afff5f" , 159"#afff87" , "#afffaf" , "#afffd7" , "#afffff" , 160"#d70000" , "#d7005f" , "#d70087" , "#d700af" , 161"#d700d7" , "#d700ff" , "#d75f00" , "#d75f5f" , 162"#d75f87" , "#d75faf" , "#d75fd7" , "#d75fff" , 163"#d78700" , "#d7875f" , "#d78787" , "#d787af" , 164"#d787d7" , "#d787ff" , "#d7af00" , "#d7af5f" , 165"#d7af87" , "#d7afaf" , "#d7afd7" , "#d7afff" , 166"#d7d700" , "#d7d75f" , "#d7d787" , "#d7d7af" , 167"#d7d7d7" , "#d7d7ff" , "#d7ff00" , "#d7ff5f" , 168"#d7ff87" , "#d7ffaf" , "#d7ffd7" , "#d7ffff" , 169"#ff0000" , "#ff005f" , "#ff0087" , "#ff00af" , 170"#ff00d7" , "#ff00ff" , "#ff5f00" , "#ff5f5f" , 171"#ff5f87" , "#ff5faf" , "#ff5fd7" , "#ff5fff" , 172"#ff8700" , "#ff875f" , "#ff8787" , "#ff87af" , 173"#ff87d7" , "#ff87ff" , "#ffaf00" , "#ffaf5f" , 174"#ffaf87" , "#ffafaf" , "#ffafd7" , "#ffafff" , 175"#ffd700" , "#ffd75f" , "#ffd787" , "#ffd7af" , 176"#ffd7d7" , "#ffd7ff" , "#ffff00" , "#ffff5f" , 177"#ffff87" , "#ffffaf" , "#ffffd7" , "#ffffff" , 178"#080808" , "#121212" , "#1c1c1c" , "#262626" , 179"#303030" , "#3a3a3a" , "#444444" , "#4e4e4e" , 180"#585858" , "#626262" , "#6c6c6c" , "#767676" , 181"#808080" , "#8a8a8a" , "#949494" , "#9e9e9e" , 182"#a8a8a8" , "#b2b2b2" , "#bcbcbc" , "#c6c6c6" , 183"#d0d0d0" , "#dadada" , "#e4e4e4" , "#eeeeee" , 184} 185-- stylua: ignore end 186 187--- @type table<integer,string> 188local cterm_color_cache ={} 189--- @type string? 190local background_color_cache =nil 191--- @type string? 192local foreground_color_cache =nil 193 194local len =vim . api . nvim_strwidth 195 196--- @see https://invisible-island.net/xterm/ctlseqs/ctlseqs.html#h3-Operating-System-Commands 197--- @param color "background"|"foreground"|integer 198--- @return string? 199local function try_query_terminal_color ( color ) 200local parameter =( color == 'foreground' and 10 ) or ( color == 'background' and 11 ) or 4 201local payload =type ( color ) == 'number' and ( '\027]%s;%s;?\027\\' ): format ( parameter , color ) 202or ( '\027]%s;?\027\\' ): format ( parameter ) 203 204--- @type string? 205local hex =nil 206vim . tty . request ( payload , { timeout =100 } , function ( resp ) 207local r , g , b =resp : match ( '\027%]%d+;%d*;?rgb:(%w%w)%w%w/(%w%w)%w%w/(%w%w)%w%w' ) ---@type string?, string?, string? 208if r and g and b then 209hex ='#' .. r .. g .. b 210return true 211end 212end ) 213vim . wait ( 100 , function () 214return hex ~= nil 215end ) 216return hex 217end 218 219--- @param colorstr string 220--- @return string 221local function cterm_to_hex ( colorstr ) 222if colorstr : sub ( 1 , 1 ) == '#' then 223return colorstr 224end 225local color =vim . _assert_integer ( colorstr ) 226assert ( 0 <= color and color <= 255 ) 227if cterm_color_cache [ color ] then 228return cterm_color_cache [ color ] 229end 230local hex =try_query_terminal_color ( color ) 231if hex then 232cterm_color_cache [ color ] =hex 233else 234notify ( "Couldn't get terminal colors, using fallback" ) 235local t_Co =vim . _assert_integer ( vim . api . nvim_eval ( '&t_Co' )) 236if t_Co <= 8 then 237cterm_color_cache =cterm_8_to_hex 238elseif t_Co == 88 then 239cterm_color_cache =cterm_88_to_hex 240elseif t_Co == 256 then 241cterm_color_cache =cterm_256_to_hex 242else 243cterm_color_cache =cterm_16_to_hex 244end 245end 246-- EmmyLua retains the failed lookup narrowing after the cache is updated. 247---@diagnostic disable-next-line: return-type-mismatch 248return cterm_color_cache [ color ] 249end 250 251--- @return string 252local function get_background_color () 253local bg =vim . fn . synIDattr ( vim . fn . hlID ( 'Normal' ), 'bg#' ) 254if bg ~= '' then 255return cterm_to_hex ( bg ) 256end 257if background_color_cache then 258return background_color_cache 259end 260local hex =try_query_terminal_color ( 'background' ) 261if not hex or not hex : match ( '#%x%x%x%x%x%x' ) then 262notify ( "Couldn't get terminal background colors, using fallback" ) 263hex =vim . o . background == 'light' and '#ffffff' or '#000000' 264end 265background_color_cache =hex 266return hex 267end 268 269--- @return string 270local function get_foreground_color () 271local fg =vim . fn . synIDattr ( vim . fn . hlID ( 'Normal' ), 'fg#' ) 272if fg ~= '' then 273return cterm_to_hex ( fg ) 274end 275if foreground_color_cache then 276return foreground_color_cache 277end 278local hex =try_query_terminal_color ( 'foreground' ) 279if not hex or not hex : match ( '#%x%x%x%x%x%x' ) then 280notify ( "Couldn't get terminal foreground colors, using fallback" ) 281hex =vim . o . background == 'light' and '#000000' or '#ffffff' 282end 283foreground_color_cache =hex 284return hex 285end 286 287--- @param style_line vim.tohtml.line 288--- @param col integer (1-index) 289--- @param field integer 290--- @param val any 291local function _style_line_insert ( style_line , col , field , val ) 292if style_line [ col ] == nil then 293style_line [ col ] ={ {} , {} , {} , {} } 294end 295table . insert ( assert ( style_line [ col ][ field ]), val ) 296end 297 298--- @param style_line vim.tohtml.line 299--- @param col integer (1-index) 300--- @param val any[] 301local function style_line_insert_overlay_char ( style_line , col , val ) 302_style_line_insert ( style_line , col , 4 , val ) 303end 304 305--- @param style_line vim.tohtml.line 306--- @param col integer (1-index) 307--- @param val any[] 308local function style_line_insert_virt_text ( style_line , col , val ) 309_style_line_insert ( style_line , col , 3 , val ) 310end 311 312--- @param state vim.tohtml.state 313--- @param hl string|integer|string[]|integer[]? 314--- @return nil|integer 315local function register_hl ( state , hl ) 316if type ( hl ) == 'table' then 317hl =hl [ # hl ] --- @type string|integer 318end 319if type ( hl ) == 'nil' then 320return 321elseif type ( hl ) == 'string' then 322hl =vim . fn . hlID ( hl ) 323assert ( hl ~= 0 ) 324end 325hl =vim . fn . synIDtrans ( hl ) 326if not state . highlights_name [ hl ] then 327local name =vim . fn . synIDattr ( hl , 'name' ) 328assert ( name ~= '' ) 329state . highlights_name [ hl ] =name 330end 331return hl 332end 333 334--- @param state vim.tohtml.state 335--- @param start_row integer (1-index) 336--- @param start_col integer (1-index) 337--- @param end_row integer (1-index) 338--- @param end_col integer (1-index) 339--- @param conceal_text string 340--- @param hl_group string|integer? 341local function styletable_insert_conceal ( 342state , 343start_row , 344start_col , 345end_row , 346end_col , 347conceal_text , 348hl_group 349) 350assert ( state . opt . conceallevel > 0 ) 351local styletable =state . style 352if start_col == end_col and start_row == end_row then 353return 354end 355if state . opt . conceallevel == 1 and conceal_text == '' then 356local listchars =vim . opt_local . listchars : get () --[[@as table<string,string>]] 357conceal_text =listchars . conceal or ' ' 358end 359local hlid =register_hl ( state , hl_group ) 360if vim . wo [ state . winid ]. conceallevel ~= 3 then 361_style_line_insert ( styletable [ start_row ], start_col , 3 , { conceal_text , hlid } ) 362end 363_style_line_insert ( styletable [ start_row ], start_col , 1 , HIDE_ID ) 364_style_line_insert ( styletable [ end_row ], end_col , 2 , HIDE_ID ) 365end 366 367--- @param state vim.tohtml.state 368--- @param start_row integer (1-index) 369--- @param start_col integer (1-index) 370--- @param end_row integer (1-index) 371--- @param end_col integer (1-index) 372--- @param hl_group string|integer|nil 373local function styletable_insert_range ( state , start_row , start_col , end_row , end_col , hl_group ) 374if start_col == end_col and start_row == end_row or not hl_group then 375return 376end 377local styletable =state . style 378_style_line_insert ( styletable [ start_row ], start_col , 1 , hl_group ) 379_style_line_insert ( styletable [ end_row ], end_col , 2 , hl_group ) 380end 381 382--- @param bufnr integer 383--- @return vim.tohtml.styletable 384local function generate_styletable ( bufnr ) 385--- @type vim.tohtml.styletable 386local styletable ={} 387for row =1 , vim . api . nvim_buf_line_count ( bufnr ) + 1 do 388styletable [ row ] ={ virt_lines ={} , pre_text ={} } 389end 390return styletable 391end 392 393--- @param state vim.tohtml.state 394local function styletable_syntax ( state ) 395for row =state . start , state . end_ do 396local prev_id =0 397local prev_col --- @type integer? 398for col =1 , # vim . fn . getline ( row ) + 1 do 399local hlid =vim . fn . synID ( row , col , 1 ) 400hlid =hlid == 0 and 0 or assert ( register_hl ( state , hlid )) 401if hlid ~= prev_id then 402if prev_id ~= 0 then 403styletable_insert_range ( state , row , assert ( prev_col ), row , col , prev_id ) 404end 405prev_col =col 406prev_id =hlid 407end 408end 409end 410end 411 412--- @param state vim.tohtml.state 413local function styletable_diff ( state ) 414local styletable =state . style 415for row =state . start , state . end_ do 416local style_line =styletable [ row ] 417local filler =vim . fn . diff_filler ( row ) 418if filler ~= 0 then 419local fillchars =vim . opt_local . fillchars : get () --[[@as table<string,string>]] 420local fill =fillchars . diff or '-' 421table . insert ( 422style_line . virt_lines , 423{ { fill : rep ( state . width ), register_hl ( state , 'DiffDelete' ) } } 424) 425end 426if row == state . end_ + 1 then 427break 428end 429local prev_id =0 430local prev_col --- @type integer? 431for col =1 , # vim . fn . getline ( row ) do 432local hlid =vim . fn . diff_hlID ( row , col ) 433hlid =hlid == 0 and 0 or assert ( register_hl ( state , hlid )) 434if hlid ~= prev_id then 435if prev_id ~= 0 then 436styletable_insert_range ( state , row , assert ( prev_col ), row , col , prev_id ) 437end 438prev_col =col 439prev_id =hlid 440end 441end 442if prev_id ~= 0 then 443styletable_insert_range ( state , row , assert ( prev_col ), row , # vim . fn . getline ( row ) + 1 , prev_id ) 444end 445end 446end 447 448--- @param state vim.tohtml.state 449local function styletable_treesitter ( state ) 450local bufnr =state . bufnr 451local buf_highlighter =vim . treesitter . highlighter . active [ bufnr ] 452if not buf_highlighter then 453return 454end 455buf_highlighter . tree : parse ( true ) 456buf_highlighter . tree : for_each_tree ( function ( tstree , tree ) 457--- @cast tree vim.treesitter.LanguageTree 458if not tstree then 459return 460end 461local root =tstree : root () 462local q =buf_highlighter : get_query ( tree : lang ()) 463--- @type vim.treesitter.Query? 464local query =q : query () 465if not query then 466return 467end 468for capture , node , metadata in 469query : iter_captures ( root , buf_highlighter . bufnr , state . start - 1 , state . end_ ) 470do 471local srow , scol , erow , ecol =node : range () 472local c =query . captures [ capture ] 473if c ~= nil then 474local hlid =register_hl ( state , '@' .. c .. '.' .. tree : lang ()) 475if metadata . conceal and state . opt . conceallevel ~= 0 then 476local conceal =metadata . conceal == true and '' or metadata . conceal --[[@as string]] 477styletable_insert_conceal ( state , srow + 1 , scol + 1 , erow + 1 , ecol + 1 , conceal ) 478end 479styletable_insert_range ( state , srow + 1 , scol + 1 , erow + 1 , ecol + 1 , hlid ) 480end 481end 482end ) 483end 484 485--- @param state vim.tohtml.state 486--- @param extmark [integer, integer, integer, vim.api.keyset.extmark_details] 487--- @param namespaces table<integer,string> 488local function _styletable_extmarks_highlight ( state , extmark , namespaces ) 489if not extmark [ 4 ]. hl_group then 490return 491end 492---TODO(altermo) LSP semantic tokens (and some other extmarks) are only 493---generated in visible lines, and not in the whole buffer. 494if ( namespaces [ extmark [ 4 ]. ns_id ] or '' ): find ( 'nvim.lsp.semantic_tokens' ) then 495notify ( 'lsp semantic tokens are not supported, HTML may be incorrect' ) 496return 497end 498local srow , scol , erow , ecol = 499extmark [ 2 ], extmark [ 3 ], extmark [ 4 ]. end_row or extmark [ 2 ], extmark [ 4 ]. end_col or extmark [ 3 ] 500if scol == ecol and srow == erow then 501return 502end 503local hlid =register_hl ( state , extmark [ 4 ]. hl_group ) 504styletable_insert_range ( state , srow + 1 , scol + 1 , erow + 1 , ecol + 1 , hlid ) 505end 506 507--- @param state vim.tohtml.state 508--- @param extmark [integer, integer, integer, vim.api.keyset.set_extmark|any] 509--- @param namespaces table<integer,string> 510local function _styletable_extmarks_virt_text ( state , extmark , namespaces ) 511if not extmark [ 4 ]. virt_text then 512return 513end 514---TODO(altermo) LSP semantic tokens (and some other extmarks) are only 515---generated in visible lines, and not in the whole buffer. 516if ( namespaces [ extmark [ 4 ]. ns_id ] or '' ): find ( 'nvim.lsp.inlayhint' ) then 517notify ( 'lsp inlay hints are not supported, HTML may be incorrect' ) 518return 519end 520local styletable =state . style 521--- @type integer,integer 522local row , col =extmark [ 2 ], extmark [ 3 ] 523if 524row < vim . api . nvim_buf_line_count ( state . bufnr ) 525and ( 526extmark [ 4 ]. virt_text_pos == 'inline' 527or extmark [ 4 ]. virt_text_pos == 'eol' 528or extmark [ 4 ]. virt_text_pos == 'overlay' 529) 530then 531if extmark [ 4 ]. virt_text_pos == 'eol' then 532style_line_insert_virt_text ( styletable [ row + 1 ], # vim . fn . getline ( row + 1 ) + 1 , { ' ' } ) 533end 534local virt_text_len =0 535for _ , i in 536ipairs ( extmark [ 4 ]. virt_text --[[@as (string[][])]] ) 537do 538local hlid =register_hl ( state , i [ 2 ]) 539if extmark [ 4 ]. virt_text_pos == 'eol' then 540style_line_insert_virt_text ( 541styletable [ row + 1 ], 542# vim . fn . getline ( row + 1 ) + 1 , 543{ i [ 1 ], hlid } 544) 545else 546style_line_insert_virt_text ( styletable [ row + 1 ], col + 1 , { i [ 1 ], hlid } ) 547end 548virt_text_len =virt_text_len + len ( assert ( i [ 1 ])) 549end 550if extmark [ 4 ]. virt_text_pos == 'overlay' then 551styletable_insert_range ( state , row + 1 , col + 1 , row + 1 , col + virt_text_len + 1 , HIDE_ID ) 552end 553end 554local not_supported ={ 555virt_text_pos ='right_align' , 556hl_mode ='blend' , 557hl_group ='combine' , 558} 559for opt , val in pairs ( not_supported ) do 560if extmark [ 4 ][ opt ] == val then 561notify (( 'extmark.%s="%s" is not supported, HTML may be incorrect' ): format ( opt , val )) 562end 563end 564end 565 566--- @param state vim.tohtml.state 567--- @param extmark [integer, integer, integer, vim.api.keyset.set_extmark|any] 568local function _styletable_extmarks_virt_lines ( state , extmark ) 569---TODO(altermo) if the fold start is equal to virt_line start then the fold hides the virt_line 570if not extmark [ 4 ]. virt_lines then 571return 572end 573--- @type integer 574local row =extmark [ 2 ] + ( extmark [ 4 ]. virt_lines_above and 1 or 2 ) 575for _ , line in 576ipairs ( extmark [ 4 ]. virt_lines --[[@as (string[][][])]] ) 577do 578local virt_line ={} 579for _ , i in ipairs ( line ) do 580local hlid =register_hl ( state , i [ 2 ]) 581table . insert ( virt_line , { i [ 1 ], hlid } ) 582end 583table . insert ( state . style [ row ]. virt_lines , virt_line ) 584end 585end 586 587--- @param state vim.tohtml.state 588--- @param extmark [integer, integer, integer, vim.api.keyset.extmark_details] 589local function _styletable_extmarks_conceal ( state , extmark ) 590if not extmark [ 4 ]. conceal or state . opt . conceallevel == 0 then 591return 592end 593local srow , scol , erow , ecol = 594extmark [ 2 ], extmark [ 3 ], extmark [ 4 ]. end_row or extmark [ 2 ], extmark [ 4 ]. end_col or extmark [ 3 ] 595styletable_insert_conceal ( 596state , 597srow + 1 , 598scol + 1 , 599erow + 1 , 600ecol + 1 , 601extmark [ 4 ]. conceal , 602extmark [ 4 ]. hl_group or 'Conceal' 603) 604end 605 606--- @param state vim.tohtml.state 607local function styletable_extmarks ( state ) 608--TODO(altermo) extmarks may have col/row which is outside of the buffer, which could cause an error 609local bufnr =state . bufnr 610local extmarks =vim . api . nvim_buf_get_extmarks ( bufnr , - 1 , 0 , - 1 , { details =true } ) 611--- @cast extmarks [integer,integer,integer,vim.api.keyset.extmark_details][] 612 613local namespaces ={} --- @type table<integer, string> 614for ns , ns_id in pairs ( vim . api . nvim_get_namespaces ()) do 615namespaces [ ns_id ] =ns 616end 617for _ , v in ipairs ( extmarks ) do 618_styletable_extmarks_highlight ( state , v , namespaces ) 619end 620for _ , v in ipairs ( extmarks ) do 621_styletable_extmarks_conceal ( state , v ) 622end 623for _ , v in ipairs ( extmarks ) do 624_styletable_extmarks_virt_text ( state , v , namespaces ) 625end 626for _ , v in ipairs ( extmarks ) do 627_styletable_extmarks_virt_lines ( state , v ) 628end 629end 630 631--- @param state vim.tohtml.state 632local function styletable_folds ( state ) 633local styletable =state . style 634local has_folded =false 635for row =state . start , state . end_ do 636if vim . fn . foldclosed ( row ) > 0 then 637has_folded =true 638styletable [ row ]. hide =true 639end 640if vim . fn . foldclosed ( row ) == row then 641local hlid =register_hl ( state , 'Folded' ) 642---TODO(altermo): Is there a way to get highlighted foldtext? 643local foldtext =vim . fn . foldtextresult ( row ) 644local fillchars =vim . opt . fillchars : get () --[[@as table<string,string>]] 645foldtext =foldtext .. ( fillchars . fold or '·' ): rep ( state . width - # foldtext ) 646table . insert ( styletable [ row ]. virt_lines , { { foldtext , hlid } } ) 647end 648end 649local foldtext =vim . o . foldtext 650local foldtext_fn =type ( foldtext ) == 'function' and foldtext 651or function () 652return vim . api . nvim_eval ( foldtext ) 653end 654if has_folded and type (( { pcall ( foldtext_fn ) } )[ 2 ]) == 'table' then 655notify ( 'foldtext returning a table with highlights is not supported, HTML may be incorrect' ) 656end 657end 658 659--- @param state vim.tohtml.state 660local function styletable_conceal ( state ) 661local bufnr =state . bufnr 662vim . _with ( { buf =bufnr } , function () 663for row =state . start , state . end_ do 664--- @type table<integer,[integer,integer,string]> 665local conceals ={} 666local line_len_exclusive =# vim . fn . getline ( row ) + 1 667for col =1 , line_len_exclusive do 668--- @type integer,string,integer 669local is_concealed , conceal , hlid =unpack ( vim . fn . synconcealed ( row , col ) --[[@as table]] ) 670if is_concealed ~= 0 then 671if not conceals [ hlid ] then 672conceals [ hlid ] ={ col , math . min ( col + 1 , line_len_exclusive ), conceal } 673else 674conceals [ hlid ][ 2 ] =math . min ( col + 1 , line_len_exclusive ) 675end 676end 677end 678for _ , v in pairs ( conceals ) do 679styletable_insert_conceal ( state , row , v [ 1 ], row , v [ 2 ], v [ 3 ], 'Conceal' ) 680end 681end 682end ) 683end 684 685--- @param state vim.tohtml.state 686local function styletable_match ( state ) 687for _ , match in ipairs ( vim . fn . getmatches ( state . winid )) do 688local hlid =register_hl ( state , match . group ) 689--- @param srow integer 690--- @param scol integer 691--- @param erow integer 692--- @param ecol integer 693local function range ( srow , scol , erow , ecol ) 694if match . group == 'Conceal' and state . opt . conceallevel ~= 0 then 695styletable_insert_conceal ( state , srow , scol , erow , ecol , match . conceal or '' , hlid ) 696else 697styletable_insert_range ( state , srow , scol , erow , ecol , hlid ) 698end 699end 700if match . pos1 then 701for key , v in 702pairs ( match --[[@as table<string,[integer,integer,integer]>]] ) 703do 704if key : match ( '^pos(%d+)$' ) then 705if # v == 1 then 706range ( v [ 1 ], 1 , v [ 1 ], # vim . fn . getline ( v [ 1 ]) + 1 ) 707else 708range ( v [ 1 ], v [ 2 ], v [ 1 ], v [ 3 ] + v [ 2 ]) 709end 710end 711end 712else 713for _ , v in 714ipairs ( vim . fn . matchbufline ( state . bufnr , assert ( match . pattern ), 1 , '$' ) --[[@as (table[])]] ) 715do 716range ( v . lnum , v . byteidx + 1 , v . lnum , v . byteidx + 1 + # v . text ) 717end 718end 719end 720end 721 722--- Requires state.conf.number_lines to be set to true 723--- @param state vim.tohtml.state 724local function styletable_statuscolumn ( state ) 725if not state . conf . number_lines then 726return 727end 728local statuscolumn =state . opt . statuscolumn 729 730if statuscolumn == '' then 731if state . opt . relativenumber then 732if state . opt . number then 733statuscolumn ='%C%s%{%v:lnum!=line(".")?"%=".v:relnum." ":v:lnum%}' 734else 735statuscolumn ='%C%s%{%"%=".v:relnum." "%}' 736end 737else 738statuscolumn ='%C%s%{%"%=".v:lnum." "%}' 739end 740end 741local minwidth =0 742 743local signcolumn =state . opt . signcolumn 744if state . opt . number or state . opt . relativenumber then 745minwidth =minwidth + state . opt . numberwidth 746if signcolumn == 'number' then 747signcolumn ='no' 748end 749end 750if signcolumn == 'number' then 751signcolumn ='auto' 752end 753if signcolumn ~= 'no' then 754local max =vim . _tointeger ( signcolumn : match ( '^%w-:(%d)' )) or 1 755if signcolumn : match ( '^auto' ) then 756--- @type table<integer,integer> 757local signcount ={} 758for _ , extmark in 759ipairs ( vim . api . nvim_buf_get_extmarks ( state . bufnr , - 1 , 0 , - 1 , { details =true } )) 760do 761--- @cast extmark [integer, integer, integer, vim.api.keyset.extmark_details] 762if extmark [ 4 ]. sign_text then 763signcount [ extmark [ 2 ]] =( signcount [ extmark [ 2 ]] or 0 ) + 1 764end 765end 766local maxsigns =0 767for _ , v in pairs ( signcount ) do 768if v > maxsigns then 769maxsigns =v 770end 771end 772minwidth =minwidth + math . min ( maxsigns , max ) * 2 773else 774minwidth =minwidth + max * 2 775end 776end 777 778local foldcolumn =state . opt . foldcolumn 779if foldcolumn ~= '0' then 780if foldcolumn : match ( '^auto' ) then 781local max =vim . _tointeger ( foldcolumn : match ( '^%w-:(%d)' )) or 1 782local maxfold =0 783vim . _with ( { buf =state . bufnr } , function () 784for row =state . start , state . end_ do 785local foldlevel =vim . fn . foldlevel ( row ) 786if foldlevel > maxfold then 787maxfold =foldlevel 788end 789end 790end ) 791minwidth =minwidth + math . min ( maxfold , max ) 792else 793minwidth =minwidth + vim . _assert_integer ( foldcolumn ) 794end 795end 796 797--- @type table<integer,vim.api.keyset.eval_statusline_ret> 798local statuses ={} 799for row =state . start , state . end_ do 800local status =vim . api . nvim_eval_statusline ( 801statuscolumn , 802{ winid =state . winid , use_statuscol_lnum =row , highlights =true } 803) 804local width =len ( status . str ) 805if width > minwidth then 806minwidth =width 807end 808table . insert ( statuses , status ) 809end 810for row , status in pairs ( statuses ) do 811local str =status . str 812local hls =status . highlights 813for k , v in ipairs ( hls ) do 814local hlsk1 =hls [ k + 1 ] 815local text =str : sub ( v . start + 1 , hlsk1 and hlsk1 . start or nil ) 816if k == # hls then 817text =text .. ( ' ' ): rep ( minwidth - len ( str )) 818end 819if text ~= '' then 820local hlid =register_hl ( state , v . group ) 821local virt_text ={ text , hlid } 822table . insert ( state . style [ row ]. pre_text , virt_text ) 823end 824end 825end 826end 827 828--- @param state vim.tohtml.state 829local function styletable_listchars ( state ) 830if not state . opt . list then 831return 832end 833--- @param str string 834--- @param i integer 835--- @param j? integer 836--- @return string 837local function utf8_sub ( str , i , j ) 838return vim . fn . strcharpart ( str , i - 1 , j and j - i + 1 or nil ) 839end 840local listchars =vim . opt_local . listchars : get () --[[@as table<string,string>]] 841local ids =setmetatable ( {} , { 842__index =function ( t , k ) 843rawset ( t , k , register_hl ( state , k )) 844return rawget ( t , k ) 845end , 846} ) 847 848if listchars . eol then 849for row =state . start , state . end_ do 850local style_line =state . style [ row ] 851style_line_insert_overlay_char ( 852style_line , 853# vim . fn . getline ( row ) + 1 , 854{ listchars . eol , ids . NonText } 855) 856end 857end 858 859if listchars . tab and state . tabstop then 860for _ , match in 861ipairs ( vim . fn . matchbufline ( state . bufnr , '\t' , 1 , '$' ) --[[@as (table[])]] ) 862do 863local vcol =vim . fn . virtcol ( { match . lnum , match . byteidx } , false , state . winid ) --[[@as integer]] 864local tablen =# state . tabstop - ( vcol % # state . tabstop ) 865local text --- @type string 866if len ( listchars . tab ) == 3 then 867if tablen == 1 then 868text =utf8_sub ( listchars . tab , 3 , 3 ) 869else 870text =utf8_sub ( listchars . tab , 1 , 1 ) 871.. utf8_sub ( listchars . tab , 2 , 2 ): rep ( tablen - 2 ) 872.. utf8_sub ( listchars . tab , 3 , 3 ) 873end 874else 875text =utf8_sub ( listchars . tab , 1 , 1 ) .. utf8_sub ( listchars . tab , 2 , 2 ): rep ( tablen - 1 ) 876end 877style_line_insert_overlay_char ( 878state . style [ match . lnum ], 879match . byteidx + 1 , 880{ text , ids . Whitespace } 881) 882end 883end 884 885if listchars . space then 886for _ , match in 887ipairs ( vim . fn . matchbufline ( state . bufnr , ' ' , 1 , '$' ) --[[@as (table[])]] ) 888do 889style_line_insert_overlay_char ( 890state . style [ match . lnum ], 891match . byteidx + 1 , 892{ listchars . space , ids . Whitespace } 893) 894end 895end 896 897if listchars . multispace then 898for _ , match in 899ipairs ( vim . fn . matchbufline ( state . bufnr , [[ \+]] , 1 , '$' ) --[[@as (table[])]] ) 900do 901local text =utf8_sub ( listchars . multispace : rep ( len ( match . text )), 1 , len ( match . text )) 902for i =1 , len ( text ) do 903style_line_insert_overlay_char ( 904state . style [ match . lnum ], 905match . byteidx + i , 906{ utf8_sub ( text , i , i ), ids . Whitespace } 907) 908end 909end 910end 911 912if listchars . lead or listchars . leadmultispace then 913for _ , match in 914ipairs ( vim . fn . matchbufline ( state . bufnr , [[^ \+]] , 1 , '$' ) --[[@as (table[])]] ) 915do 916local text ='' 917if len ( match . text ) == 1 or not listchars . leadmultispace then 918if listchars . lead then 919text =listchars . lead : rep ( len ( match . text )) 920end 921elseif listchars . leadmultispace then 922text =utf8_sub ( listchars . leadmultispace : rep ( len ( match . text )), 1 , len ( match . text )) 923end 924for i =1 , len ( text ) do 925style_line_insert_overlay_char ( 926state . style [ match . lnum ], 927match . byteidx + i , 928{ utf8_sub ( text , i , i ), ids . Whitespace } 929) 930end 931end 932end 933 934if listchars . trail then 935for _ , match in 936ipairs ( vim . fn . matchbufline ( state . bufnr , [[ \+$]] , 1 , '$' ) --[[@as (table[])]] ) 937do 938local text =listchars . trail : rep ( len ( match . text )) 939for i =1 , len ( text ) do 940style_line_insert_overlay_char ( 941state . style [ match . lnum ], 942match . byteidx + i , 943{ utf8_sub ( text , i , i ), ids . Whitespace } 944) 945end 946end 947end 948 949if listchars . nbsp then 950for _ , match in 951ipairs ( 952vim . fn . matchbufline ( state . bufnr , '\226\128\175\\|\194\160' , 1 , '$' ) --[[@as (table[])]] 953) 954do 955style_line_insert_overlay_char ( 956state . style [ match . lnum ], 957match . byteidx + 1 , 958{ listchars . nbsp , ids . Whitespace } 959) 960for i =2 , # match . text do 961style_line_insert_overlay_char ( 962state . style [ match . lnum ], 963match . byteidx + i , 964{ '' , ids . Whitespace } 965) 966end 967end 968end 969end 970 971--- @param name string 972--- @return string 973local function highlight_name_to_class_name ( name ) 974return ( name : gsub ( '%.' , '-' ): gsub ( '@' , '-' )) 975end 976 977--- @param name string 978--- @return string 979local function name_to_tag ( name ) 980return '<span class="' .. highlight_name_to_class_name ( name ) .. '">' 981end 982 983--- @param _ string 984--- @return string 985local function name_to_closetag ( _ ) 986return '</span>' 987end 988 989--- @param str string 990--- @param tabstop string|false? 991--- @return string 992local function html_escape ( str , tabstop ) 993str =str : gsub ( '&' , '&' ): gsub ( '<' , '<' ): gsub ( '>' , '>' ): gsub ( '"' , '"' ) 994if tabstop then 995--- @type string 996str =str : gsub ( '\t' , tabstop ) 997end 998return str 999end 1000 1001--- @param out string[] 1002--- @param state vim.tohtml.state.global 1003local function extend_style ( out , state ) 1004table . insert ( out , '<style>' ) 1005table . insert ( out , ( '* {font-family: %s}' ): format ( state . font )) 1006table . insert ( 1007out , 1008( 'body {background-color: %s; color: %s}' ): format ( state . background , state . foreground ) 1009) 1010for hlid , name in pairs ( state . highlights_name ) do 1011--TODO(altermo) use local namespace (instead of global 0) 1012local fg =vim . fn . synIDattr ( hlid , 'fg#' ) 1013local bg =vim . fn . synIDattr ( hlid , 'bg#' ) 1014local sp =vim . fn . synIDattr ( hlid , 'sp#' ) 1015local decor_line ={} 1016if vim . fn . synIDattr ( hlid , 'underline' ) ~= '' then 1017table . insert ( decor_line , 'underline' ) 1018end 1019if vim . fn . synIDattr ( hlid , 'strikethrough' ) ~= '' then 1020table . insert ( decor_line , 'line-through' ) 1021end 1022if vim . fn . synIDattr ( hlid , 'undercurl' ) ~= '' then 1023table . insert ( decor_line , 'underline' ) 1024end 1025local c ={ 1026color =fg ~= '' and cterm_to_hex ( fg ) or nil , 1027[ 'background-color' ] =bg ~= '' and cterm_to_hex ( bg ) or nil , 1028[ 'font-style' ] =vim . fn . synIDattr ( hlid , 'italic' ) ~= '' and 'italic' or nil , 1029[ 'font-weight' ] =vim . fn . synIDattr ( hlid , 'bold' ) ~= '' and 'bold' or nil , 1030[ 'text-decoration-line' ] =not vim . tbl_isempty ( decor_line ) and table . concat ( decor_line , ' ' ) 1031or nil , 1032-- TODO(ribru17): fallback to displayed text color if sp not set 1033[ 'text-decoration-color' ] =sp ~= '' and cterm_to_hex ( sp ) or nil , 1034--TODO(altermo) if strikethrough and undercurl then the strikethrough becomes wavy 1035[ 'text-decoration-style' ] =vim . fn . synIDattr ( hlid , 'undercurl' ) ~= '' and 'wavy' or nil , 1036} 1037local attrs ={} 1038for attr , val in pairs ( c ) do 1039table . insert ( attrs , attr .. ': ' .. val ) 1040end 1041table . insert ( 1042out , 1043'.' .. highlight_name_to_class_name ( name ) .. ' {' .. table . concat ( attrs , '; ' ) .. '}' 1044) 1045end 1046table . insert ( out , '</style>' ) 1047end 1048 1049--- @param out string[] 1050--- @param state vim.tohtml.state.global 1051local function extend_head ( out , state ) 1052table . insert ( out , '<head>' ) 1053table . insert ( out , '<meta charset="UTF-8">' ) 1054if state . title ~= false then 1055table . insert ( out , ( '<title>%s</title>' ): format ( state . title )) 1056end 1057local colorscheme =vim . api . nvim_exec2 ( 'colorscheme' , { output =true } ). output 1058table . insert ( 1059out , 1060( '<meta name="colorscheme" content="%s"></meta>' ): format ( html_escape ( colorscheme )) 1061) 1062extend_style ( out , state ) 1063table . insert ( out , '</head>' ) 1064end 1065 1066--- @param out string[] 1067--- @param state vim.tohtml.state 1068--- @param row integer 1069local function _extend_virt_lines ( out , state , row ) 1070local style_line =state . style [ row ] 1071for _ , virt_line in ipairs ( style_line . virt_lines ) do 1072local virt_s ='' 1073for _ , v in ipairs ( virt_line ) do 1074if v [ 2 ] then 1075virt_s =virt_s .. ( name_to_tag ( state . highlights_name [ v [ 2 ]])) 1076end 1077virt_s =virt_s .. v [ 1 ] 1078if v [ 2 ] then 1079--- @type string 1080virt_s =virt_s .. ( name_to_closetag ( state . highlights_name [ v [ 2 ]])) 1081end 1082end 1083table . insert ( out , virt_s ) 1084end 1085end 1086 1087--- @param state vim.tohtml.state 1088--- @param row integer 1089--- @return string 1090local function _pre_text_to_html ( state , row ) 1091local style_line =state . style [ row ] 1092local s ='' 1093for _ , pre_text in ipairs ( style_line . pre_text ) do 1094if pre_text [ 2 ] then 1095s =s .. ( name_to_tag ( state . highlights_name [ pre_text [ 2 ]])) 1096end 1097s =s .. ( html_escape ( pre_text [ 1 ], state . tabstop )) 1098if pre_text [ 2 ] then 1099--- @type string 1100s =s .. ( name_to_closetag ( state . highlights_name [ pre_text [ 2 ]])) 1101end 1102end 1103return s 1104end 1105 1106--- @param state vim.tohtml.state 1107--- @param char table 1108--- @return string 1109local function _char_to_html ( state , char ) 1110local s ='' 1111if char [ 2 ] then 1112s =s .. name_to_tag ( state . highlights_name [ char [ 2 ]]) 1113end 1114s =s .. html_escape ( char [ 1 ], state . tabstop ) 1115if char [ 2 ] then 1116s =s .. name_to_closetag ( state . highlights_name [ char [ 2 ]]) 1117end 1118return s 1119end 1120 1121--- @param state vim.tohtml.state 1122--- @param cell vim.tohtml.cell 1123--- @return string 1124local function _virt_text_to_html ( state , cell ) 1125local s ='' 1126for _ , v in ipairs ( cell [ 3 ]) do 1127if v [ 2 ] then 1128s =s .. ( name_to_tag ( state . highlights_name [ v [ 2 ]])) 1129end 1130--- @type string 1131s =s .. html_escape ( v [ 1 ], state . tabstop ) 1132if v [ 2 ] then 1133s =s .. name_to_closetag ( state . highlights_name [ v [ 2 ]]) 1134end 1135end 1136return s 1137end 1138 1139--- @param out string[] 1140--- @param state vim.tohtml.state 1141local function extend_pre ( out , state ) 1142local styletable =state . style 1143table . insert ( out , '<pre>' ) 1144local out_start =# out 1145local hide_count =0 1146--- @type integer[] 1147local stack ={} 1148 1149local before ='' 1150local after ='' 1151--- @param row integer 1152local function loop ( row ) 1153local inside =row <= state . end_ and row >= state . start 1154local style_line =styletable [ row ] 1155if style_line . hide and ( styletable [ row - 1 ] or {} ). hide then 1156return 1157end 1158if inside then 1159_extend_virt_lines ( out , state , row ) 1160end 1161--Possible improvement (altermo): 1162--Instead of looping over all the buffer characters per line, 1163--why not loop over all the style_line cells, 1164--and then calculating the amount of text. 1165if style_line . hide then 1166return 1167end 1168local line =vim . api . nvim_buf_get_lines ( state . bufnr , row - 1 , row , false )[ 1 ] or '' 1169local s ='' 1170if inside then 1171s =s .. _pre_text_to_html ( state , row ) 1172end 1173local true_line_len =# line + 1 1174for k in 1175pairs ( style_line --[[@as table<string,any>]] ) 1176do 1177if type ( k ) == 'number' and k > true_line_len then 1178true_line_len =k --[[@as integer]] 1179end 1180end 1181for col =1 , true_line_len do 1182local cell =style_line [ col ] 1183--- @type table? 1184local char 1185if cell then 1186for i =# cell [ 2 ], 1 , - 1 do 1187local hlid =cell [ 2 ][ i ] 1188if hlid < 0 then 1189if hlid == HIDE_ID then 1190hide_count =hide_count - 1 1191end 1192else 1193--- @type integer? 1194local index 1195for idx =# stack , 1 , - 1 do 1196s =s .. ( name_to_closetag ( state . highlights_name [ stack [ idx ]])) 1197if stack [ idx ] == hlid then 1198index =idx 1199break 1200end 1201end 1202assert ( index , 'a coles tag which has no corresponding open tag' ) 1203for idx =index + 1 , # stack do 1204s =s .. ( name_to_tag ( state . highlights_name [ stack [ idx ]])) 1205end 1206table . remove ( stack , index ) 1207end 1208end 1209 1210for _ , hlid in ipairs ( cell [ 1 ]) do 1211if hlid < 0 then 1212if hlid == HIDE_ID then 1213hide_count =hide_count + 1 1214end 1215else 1216table . insert ( stack , hlid ) 1217s =s .. ( name_to_tag ( state . highlights_name [ hlid ])) 1218end 1219end 1220 1221if cell [ 3 ] and inside then 1222s =s .. _virt_text_to_html ( state , cell ) 1223end 1224 1225char =cell [ 4 ][ # cell [ 4 ]] 1226end 1227 1228if col == true_line_len and not char then 1229break 1230end 1231 1232if hide_count == 0 and inside then 1233s =s 1234.. _char_to_html ( 1235state , 1236char 1237or { vim . api . nvim_buf_get_text ( state . bufnr , row - 1 , col - 1 , row - 1 , col , {} )[ 1 ] } 1238) 1239end 1240end 1241if row > state . end_ + 1 then 1242after =after .. s 1243elseif row < state . start then 1244before =s .. before 1245else 1246table . insert ( out , s ) 1247end 1248end 1249 1250for row =1 , vim . api . nvim_buf_line_count ( state . bufnr ) + 1 do 1251loop ( row ) 1252end 1253out [ out_start ] =out [ out_start ] .. before 1254out [ # out ] =out [ # out ] .. after 1255assert ( # stack == 0 , 'an open HTML tag was never closed' ) 1256table . insert ( out , '</pre>' ) 1257end 1258 1259--- @param out string[] 1260--- @param fn fun() 1261local function extend_body ( out , fn ) 1262table . insert ( out , '<body style="display: flex">' ) 1263fn () 1264table . insert ( out , '</body>' ) 1265end 1266 1267--- @param out string[] 1268--- @param fn fun() 1269local function extend_html ( out , fn ) 1270table . insert ( out , '<!DOCTYPE html>' ) 1271table . insert ( out , '<html>' ) 1272fn () 1273table . insert ( out , '</html>' ) 1274end 1275 1276--- @param winid integer 1277--- @param global_state vim.tohtml.state.global 1278--- @return vim.tohtml.state 1279local function global_state_to_state ( winid , global_state ) 1280local bufnr =vim . api . nvim_win_get_buf ( winid ) 1281local opt =global_state . conf 1282local width =opt . width or vim . bo [ bufnr ]. textwidth 1283if not width or width < 1 then 1284width =vim . api . nvim_win_get_width ( winid ) 1285end 1286local range =opt . range or { 1 , vim . api . nvim_buf_line_count ( bufnr ) } 1287local state =setmetatable ( { 1288winid =winid == 0 and vim . api . nvim_get_current_win () or winid , 1289opt =vim . wo [ winid ], 1290style =generate_styletable ( bufnr ), 1291bufnr =bufnr , 1292tabstop =( ' ' ): rep ( vim . bo [ bufnr ]. tabstop ), 1293width =width , 1294start =range [ 1 ], 1295end_ =range [ 2 ], 1296} , { __index =global_state } ) 1297return state --[[@as vim.tohtml.state]] 1298end 1299 1300--- @param opt vim.tohtml.opt 1301--- @param title? string 1302--- @return vim.tohtml.state.global 1303local function opt_to_global_state ( opt , title ) 1304local fonts ={} --- @type string[] 1305if opt . font then 1306fonts =( type ( opt . font ) == 'string' and { opt . font } or opt . font ) --[[@as string[] ]] 1307for i , v in pairs ( fonts ) do 1308fonts [ i ] =( '"%s"' ): format ( v ) 1309end 1310elseif vim . o . guifont : match ( '^[^:]+' ) then 1311-- Example: 1312-- Input: "Font,Escape\,comma, Ignore space after comma" 1313-- Output: { "Font","Escape,comma","Ignore space after comma" } 1314local prev ='' 1315for name in vim . gsplit ( assert ( vim . o . guifont : match ( '^[^:]+' )), ',' , { trimempty =true } ) do 1316if vim . endswith ( name , '\\' ) then 1317prev =prev .. vim . trim ( name : sub ( 1 , - 2 ) .. ',' ) 1318elseif vim . trim ( name ) ~= '' then 1319table . insert ( fonts , ( '"%s%s"' ): format ( prev , vim . trim ( name ))) 1320prev ='' 1321end 1322end 1323end 1324-- Generic family names (monospace here) must not be quoted 1325-- because the browser recognizes them as font families. 1326table . insert ( fonts , 'monospace' ) 1327--- @type vim.tohtml.state.global 1328local state ={ 1329background =get_background_color (), 1330foreground =get_foreground_color (), 1331title =opt . title or title or false , 1332font =table . concat ( fonts , ',' ), 1333highlights_name ={} , 1334conf =opt , 1335} 1336return state 1337end 1338 1339--- @type fun(state: vim.tohtml.state)[] 1340local styletable_funcs ={ 1341styletable_syntax , 1342styletable_diff , 1343styletable_treesitter , 1344styletable_match , 1345styletable_extmarks , 1346styletable_conceal , 1347styletable_listchars , 1348styletable_folds , 1349styletable_statuscolumn , 1350} 1351 1352--- @param state vim.tohtml.state 1353local function state_generate_style ( state ) 1354vim . _with ( { win =state . winid } , function () 1355for _ , fn in ipairs ( styletable_funcs ) do 1356fn ( state ) 1357end 1358end ) 1359end 1360 1361--- @param winid integer 1362--- @param opt? vim.tohtml.opt 1363--- @return string[] 1364local function win_to_html ( winid , opt ) 1365opt =opt or {} 1366local title =vim . api . nvim_buf_get_name ( vim . api . nvim_win_get_buf ( winid )) 1367 1368local global_state =opt_to_global_state ( opt , title ) 1369local state =global_state_to_state ( winid , global_state ) 1370state_generate_style ( state ) 1371 1372local html ={} 1373table . insert ( html , '<!-- vim: set nomodeline: -->' ) 1374extend_html ( html , function () 1375extend_head ( html , global_state ) 1376extend_body ( html , function () 1377extend_pre ( html , state ) 1378end ) 1379end ) 1380return html 1381end 1382 1383local M ={} 1384 1385--- @class vim.tohtml.opt 1386--- @inlinedoc 1387--- 1388--- Title tag to set in the generated HTML code. 1389--- (default: buffer name) 1390--- @field title? string|false 1391--- 1392--- Show line numbers. 1393--- (default: `false`) 1394--- @field number_lines? boolean 1395--- 1396--- Fonts to use. 1397--- (default: `guifont`) 1398--- @field font? string[]|string 1399--- 1400--- Width used for items which are either right aligned or repeat a character 1401--- infinitely. 1402--- (default: 'textwidth' if non-zero or window width otherwise) 1403--- @field width? integer 1404--- 1405--- Range of rows to use. 1406--- (default: entire buffer) 1407--- @field range? integer[] 1408 1409--- Converts the buffer shown in the window {winid} to HTML and returns the output as a list of string. 1410--- @param winid? integer Window to convert (defaults to current window) 1411--- @param opt? vim.tohtml.opt Optional parameters. 1412--- @return string[] 1413function M . tohtml ( winid , opt ) 1414return win_to_html ( winid or 0 , opt ) 1415end 1416 1417return M