1" Vim autoload file for the tohtml plugin. 2" Maintainer: Ben Fritz <fritzophrenic@gmail.com> 3" Last Change: 2010 Aug 12 4" 5" Additional contributors: 6" 7" Original by Bram Moolenaar <Bram@vim.org> 8" Diff2HTML() added by Christian Brabandt <cb@256bit.org> 9" 10" See Mercurial change logs for more! 11 12" this file uses line continuations 13let s:cpo_sav = &cpo 14set cpo-=C 15 16func! tohtml#Convert2HTML(line1, line2) 17 let s:settings = tohtml#GetUserSettings() 18 19 if !&diff || s:settings.diff_one_file 20 if a:line2 >= a:line1 21 let g:html_start_line = a:line1 22 let g:html_end_line = a:line2 23 else 24 let g:html_start_line = a:line2 25 let g:html_end_line = a:line1 26 endif 27 runtime syntax/2html.vim 28 else 29 let win_list = [] 30 let buf_list = [] 31 windo | if &diff | call add(win_list, winbufnr(0)) | endif 32 let s:settings.whole_filler = 1 33 let g:html_diff_win_num = 0 34 for window in win_list 35 exe ":" . bufwinnr(window) . "wincmd w" 36 let g:html_start_line = 1 37 let g:html_end_line = line('$') 38 let g:html_diff_win_num += 1 39 runtime syntax/2html.vim 40 call add(buf_list, bufnr('%')) 41 endfor 42 unlet g:html_diff_win_num 43 call tohtml#Diff2HTML(win_list, buf_list) 44 endif 45 46 unlet g:html_start_line 47 unlet g:html_end_line 48 unlet s:settings 49endfunc 50 51func! tohtml#Diff2HTML(win_list, buf_list) 52 let xml_line = "" 53 let tag_close = '>' 54 55 let s:old_paste = &paste 56 set paste 57 let s:old_magic = &magic 58 set magic 59 60 if s:settings.use_xhtml 61 if s:settings.encoding != "" 62 let xml_line = "<?xml version=\"1.0\" encoding=\"" . s:settings.encoding . "\"?>" 63 else 64 let xml_line = "<?xml version=\"1.0\"?>" 65 endif 66 let tag_close = ' />' 67 endif 68 69 let style = [s:settings.use_xhtml ? "" : '-->'] 70 let body_line = '' 71 72 let html = [] 73 if s:settings.use_xhtml 74 call add(html, xml_line) 75 endif 76 if s:settings.use_xhtml 77 call add(html, "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">") 78 call add(html, '<html xmlns="http://www.w3.org/1999/xhtml">') 79 elseif s:settings.use_css && !s:settings.no_pre 80 call add(html, "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\" \"http://www.w3.org/TR/html4/strict.dtd\">") 81 call add(html, '<html>') 82 else 83 call add(html, '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"') 84 call add(html, ' "http://www.w3.org/TR/html4/loose.dtd">') 85 call add(html, '<html>') 86 endif 87 call add(html, '<head>') 88 89 " include encoding as close to the top as possible, but only if not already 90 " contained in XML information (to avoid haggling over content type) 91 if s:settings.encoding != "" && !s:settings.use_xhtml 92 call add(html, "<meta http-equiv=\"content-type\" content=\"text/html; charset=" . s:settings.encoding . '"' . tag_close) 93 endif 94 95 call add(html, '<title>diff</title>') 96 call add(html, '<meta name="Generator" content="Vim/'.v:version/100.'.'.v:version%100.'"'.tag_close) 97 call add(html, '<meta name="plugin-version" content="'.g:loaded_2html_plugin.'"'.tag_close) 98 call add(html, '<meta name="settings" content="'. 99 \ join(filter(keys(s:settings),'s:settings[v:val]'),','). 100 \ '"'.tag_close) 101 102 call add(html, '</head>') 103 let body_line_num = len(html) 104 call add(html, '<body>') 105 call add(html, '<table border="1" width="100%">') 106 107 call add(html, '<tr>') 108 for buf in a:win_list 109 call add(html, '<th>'.bufname(buf).'</th>') 110 endfor 111 call add(html, '</tr><tr>') 112 113 let diff_style_start = 0 114 let insert_index = 0 115 116 for buf in a:buf_list 117 let temp = [] 118 exe bufwinnr(buf) . 'wincmd w' 119 120 " If text is folded because of user foldmethod settings, etc. we don't want 121 " to act on everything in a fold by mistake. 122 setlocal nofoldenable 123 124 " When not using CSS or when using xhtml, the <body> line can be important. 125 " Assume it will be the same for all buffers and grab it from the first 126 " buffer. Similarly, need to grab the body end line as well. 127 if body_line == '' 128 1 129 call search('<body') 130 let body_line = getline('.') 131 $ 132 call search('</body>', 'b') 133 let s:body_end_line = getline('.') 134 endif 135 136 " Grab the style information. Some of this will be duplicated... 137 1 138 let style_start = search('^<style type="text/css">') 139 1 140 let style_end = search('^</style>') 141 if style_start > 0 && style_end > 0 142 let buf_styles = getline(style_start + 1, style_end - 1) 143 for a_style in buf_styles 144 if index(style, a_style) == -1 145 if diff_style_start == 0 146 if a_style =~ '\<Diff\(Change\|Text\|Add\|Delete\)' 147 let diff_style_start = len(style)-1 148 endif 149 endif 150 call insert(style, a_style, insert_index) 151 let insert_index += 1 152 endif 153 endfor 154 endif 155 156 if diff_style_start != 0 157 let insert_index = diff_style_start 158 endif 159 160 " Delete those parts that are not needed so 161 " we can include the rest into the resulting table 162 1,/^<body/d_ 163 $ 164 ?</body>?,$d_ 165 let temp = getline(1,'$') 166 " undo deletion of start and end part 167 " so we can later save the file as valid html 168 " TODO: restore using grabbed lines if undolevel is 1? 169 normal 2u 170 if s:settings.use_css 171 call add(html, '<td valign="top"><div>') 172 elseif s:settings.use_xhtml 173 call add(html, '<td nowrap="nowrap" valign="top"><div>') 174 else 175 call add(html, '<td nowrap valign="top"><div>') 176 endif 177 let html += temp 178 call add(html, '</div></td>') 179 180 " Close this buffer 181 " TODO: the comment above says we're going to allow saving the file 182 " later...but here we discard it? 183 quit! 184 endfor 185 186 let html[body_line_num] = body_line 187 188 call add(html, '</tr>') 189 call add(html, '</table>') 190 call add(html, s:body_end_line) 191 call add(html, '</html>') 192 193 let i = 1 194 let name = "Diff" . (s:settings.use_xhtml ? ".xhtml" : ".html") 195 " Find an unused file name if current file name is already in use 196 while filereadable(name) 197 let name = substitute(name, '\d*\.x\?html$', '', '') . i . '.' . fnamemodify(copy(name), ":t:e") 198 let i += 1 199 endwhile 200 exe "topleft new " . name 201 setlocal modifiable 202 203 " just in case some user autocmd creates content in the new buffer, make sure 204 " it is empty before proceeding 205 %d 206 call append(0, html) 207 208 if len(style) > 0 209 1 210 let style_start = search('^</head>')-1 211 212 " Insert javascript to toggle matching folds open and closed in all windows, 213 " if dynamic folding is active. 214 if s:settings.dynamic_folds 215 call append(style_start, [ 216 \ "<script type='text/javascript'>", 217 \ s:settings.use_xhtml ? '//<![CDATA[' : " <!--", 218 \ " function toggleFold(objID)", 219 \ " {", 220 \ " for (win_num = 1; win_num <= ".len(a:buf_list)."; win_num++)", 221 \ " {", 222 \ " var fold;", 223 \ ' fold = document.getElementById("win"+win_num+objID);', 224 \ " if(fold.className == 'closed-fold')", 225 \ " {", 226 \ " fold.className = 'open-fold';", 227 \ " }", 228 \ " else if (fold.className == 'open-fold')", 229 \ " {", 230 \ " fold.className = 'closed-fold';", 231 \ " }", 232 \ " }", 233 \ " }", 234 \ s:settings.use_xhtml ? '//]]>' : " -->", 235 \ "</script>" 236 \ ]) 237 endif 238 239 " Insert styles from all the generated html documents and additional styles 240 " for the table-based layout of the side-by-side diff. The diff should take 241 " up the full browser window (but not more), and be static in size, 242 " horizontally scrollable when the lines are too long. Otherwise, the diff 243 " is pretty useless for really long lines. 244 if s:settings.use_css 245 call append(style_start, 246 \ ['<style type="text/css">']+ 247 \ style+ 248 \ [ s:settings.use_xhtml ? '' : '<!--', 249 \ 'table { table-layout: fixed; }', 250 \ 'html, body, table, tbody { width: 100%; margin: 0; padding: 0; }', 251 \ 'th, td { width: '.printf("%.1f",100.0/len(a:win_list)).'%; }', 252 \ 'td div { overflow: auto; }', 253 \ s:settings.use_xhtml ? '' : '-->', 254 \ '</style>' 255 \ ]) 256 endif 257 endif 258 259 let &paste = s:old_paste 260 let &magic = s:old_magic 261endfunc 262 263" Gets a single user option and sets it in the passed-in Dict, or gives it the 264" default value if the option doesn't actually exist. 265func! tohtml#GetOption(settings, option, default) 266 if exists('g:html_'.a:option) 267 let a:settings[a:option] = g:html_{a:option} 268 else 269 let a:settings[a:option] = a:default 270 endif 271endfunc 272 273" returns a Dict containing the values of all user options for 2html, including 274" default values for those not given an explicit value by the user. Discards the 275" html_ prefix of the option for nicer looking code. 276func! tohtml#GetUserSettings() 277 if exists('s:settings') 278 " just restore the known options if we've already retrieved them 279 return s:settings 280 else 281 " otherwise figure out which options are set 282 let user_settings = {} 283 284 " Define the correct option if the old option name exists and we haven't 285 " already defined the correct one. Maybe I'll put out a warnig message about 286 " this sometime and remove the old option entirely at some even later time, 287 " but for now just silently accept the old option. 288 if exists('g:use_xhtml') && !exists("g:html_use_xhtml") 289 let g:html_use_xhtml = g:use_xhtml 290 endif 291 292 " get current option settings with appropriate defaults 293 call tohtml#GetOption(user_settings, 'no_progress', !has("statusline") ) 294 call tohtml#GetOption(user_settings, 'diff_one_file', 0 ) 295 call tohtml#GetOption(user_settings, 'number_lines', &number ) 296 call tohtml#GetOption(user_settings, 'use_css', 1 ) 297 call tohtml#GetOption(user_settings, 'ignore_conceal', 0 ) 298 call tohtml#GetOption(user_settings, 'ignore_folding', 0 ) 299 call tohtml#GetOption(user_settings, 'dynamic_folds', 0 ) 300 call tohtml#GetOption(user_settings, 'no_foldcolumn', 0 ) 301 call tohtml#GetOption(user_settings, 'hover_unfold', 0 ) 302 call tohtml#GetOption(user_settings, 'no_pre', 0 ) 303 call tohtml#GetOption(user_settings, 'whole_filler', 0 ) 304 call tohtml#GetOption(user_settings, 'use_xhtml', 0 ) 305 306 " override those settings that need it 307 308 " hover opening implies dynamic folding 309 if user_settings.hover_unfold 310 let user_settings.dynamic_folds = 1 311 endif 312 313 " ignore folding overrides dynamic folding 314 if user_settings.ignore_folding && user_settings.dynamic_folds 315 let user_settings.dynamic_folds = 0 316 let user_settings.hover_unfold = 0 317 endif 318 319 " dynamic folding with no foldcolumn implies hover opens 320 if user_settings.dynamic_folds && user_settings.no_foldcolumn 321 let user_settings.hover_unfold = 1 322 endif 323 324 " dynamic folding implies css 325 if user_settings.dynamic_folds 326 let user_settings.use_css = 1 327 endif 328 329 " if we're not using CSS we cannot use a pre section because <font> tags 330 " aren't allowed inside a <pre> block 331 if !user_settings.use_css 332 let user_settings.no_pre = 1 333 endif 334 335 " Figure out proper MIME charset from the 'encoding' option. 336 if exists("g:html_use_encoding") 337 let user_settings.encoding = g:html_use_encoding 338 else 339 let vim_encoding = &encoding 340 if vim_encoding =~ '^8bit\|^2byte' 341 let vim_encoding = substitute(vim_encoding, '^8bit-\|^2byte-', '', '') 342 endif 343 if vim_encoding == 'latin1' 344 let user_settings.encoding = 'iso-8859-1' 345 elseif vim_encoding =~ "^cp12" 346 let user_settings.encoding = substitute(vim_encoding, 'cp', 'windows-', '') 347 elseif vim_encoding == 'sjis' || vim_encoding == 'cp932' 348 let user_settings.encoding = 'Shift_JIS' 349 elseif vim_encoding == 'big5' || vim_encoding == 'cp950' 350 let user_settings.encoding = "Big5" 351 elseif vim_encoding == 'euc-cn' 352 let user_settings.encoding = 'GB_2312-80' 353 elseif vim_encoding == 'euc-tw' 354 let user_settings.encoding = "" 355 elseif vim_encoding =~ '^euc\|^iso\|^koi' 356 let user_settings.encoding = substitute(vim_encoding, '.*', '\U\0', '') 357 elseif vim_encoding == 'cp949' 358 let user_settings.encoding = 'KS_C_5601-1987' 359 elseif vim_encoding == 'cp936' 360 let user_settings.encoding = 'GBK' 361 elseif vim_encoding =~ '^ucs\|^utf' 362 let user_settings.encoding = 'UTF-8' 363 else 364 let user_settings.encoding = "" 365 endif 366 endif 367 368 " TODO: font 369 370 return user_settings 371 endif 372endfunc 373 374let &cpo = s:cpo_sav 375unlet s:cpo_sav 376 377" Make sure any patches will probably use consistent indent 378" vim: ts=8 sw=2 sts=2 noet 379