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