blob: c189b188f399b7762df5bf15f149b9e6f7128990 [file] [log] [blame]
Bram Moolenaarb02cbe32010-07-11 22:38:52 +02001" Vim autoload file for the tohtml plugin.
Bram Moolenaar349b2fb2010-07-16 20:35:36 +02002" Maintainer: Ben Fritz <fritzophrenic@gmail.com>
Bram Moolenaarbebca9d2010-08-07 15:47:30 +02003" Last Change: 2010 Aug 06
Bram Moolenaarb02cbe32010-07-11 22:38:52 +02004"
Bram Moolenaar349b2fb2010-07-16 20:35:36 +02005" Additional contributors:
6"
Bram Moolenaar7c86f4c2010-07-18 14:07:22 +02007" Original by Bram Moolenaar <Bram@vim.org>
8" Diff2HTML() added by Christian Brabandt <cb@256bit.org>
Bram Moolenaar349b2fb2010-07-16 20:35:36 +02009"
Bram Moolenaar7c86f4c2010-07-18 14:07:22 +020010" See Mercurial change logs for more!
11
Bram Moolenaar349b2fb2010-07-16 20:35:36 +020012" this file uses line continuations
13let s:cpo_sav = &cpo
14set cpo-=C
Bram Moolenaarb02cbe32010-07-11 22:38:52 +020015
16func! tohtml#Convert2HTML(line1, line2)
Bram Moolenaar076e8b22010-08-05 21:54:00 +020017 let s:settings = tohtml#GetUserSettings()
Bram Moolenaar349b2fb2010-07-16 20:35:36 +020018
Bram Moolenaar076e8b22010-08-05 21:54:00 +020019 if !&diff || s:settings.diff_one_file
Bram Moolenaarb02cbe32010-07-11 22:38:52 +020020 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 = []
Bram Moolenaar349b2fb2010-07-16 20:35:36 +020031 windo | if &diff | call add(win_list, winbufnr(0)) | endif
Bram Moolenaar076e8b22010-08-05 21:54:00 +020032 let s:settings.whole_filler = 1
Bram Moolenaar349b2fb2010-07-16 20:35:36 +020033 let g:html_diff_win_num = 0
Bram Moolenaarb02cbe32010-07-11 22:38:52 +020034 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('$')
Bram Moolenaar349b2fb2010-07-16 20:35:36 +020038 let g:html_diff_win_num += 1
Bram Moolenaarb02cbe32010-07-11 22:38:52 +020039 runtime syntax/2html.vim
40 call add(buf_list, bufnr('%'))
41 endfor
Bram Moolenaar349b2fb2010-07-16 20:35:36 +020042 unlet g:html_diff_win_num
Bram Moolenaarb02cbe32010-07-11 22:38:52 +020043 call tohtml#Diff2HTML(win_list, buf_list)
44 endif
45
46 unlet g:html_start_line
47 unlet g:html_end_line
Bram Moolenaar076e8b22010-08-05 21:54:00 +020048 unlet s:settings
Bram Moolenaarb02cbe32010-07-11 22:38:52 +020049endfunc
50
51func! tohtml#Diff2HTML(win_list, buf_list)
Bram Moolenaarbebca9d2010-08-07 15:47:30 +020052 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 ? "" : '-->']
Bram Moolenaar8ada2cc2010-07-29 20:43:36 +020065 let body_line = ''
66
Bram Moolenaarb02cbe32010-07-11 22:38:52 +020067 let html = []
Bram Moolenaarbebca9d2010-08-07 15:47:30 +020068 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
Bram Moolenaarb02cbe32010-07-11 22:38:52 +020082 call add(html, '<head>')
Bram Moolenaarbebca9d2010-08-07 15:47:30 +020083
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
Bram Moolenaarb02cbe32010-07-11 22:38:52 +020090 call add(html, '<title>diff</title>')
Bram Moolenaarbebca9d2010-08-07 15:47:30 +020091 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)
Bram Moolenaar349b2fb2010-07-16 20:35:36 +020096
Bram Moolenaarb02cbe32010-07-11 22:38:52 +020097 call add(html, '</head>')
Bram Moolenaar8ada2cc2010-07-29 20:43:36 +020098 let body_line_num = len(html)
Bram Moolenaarb02cbe32010-07-11 22:38:52 +020099 call add(html, '<body>')
100 call add(html, '<table border="1" width="100%">')
Bram Moolenaar349b2fb2010-07-16 20:35:36 +0200101
Bram Moolenaarb02cbe32010-07-11 22:38:52 +0200102 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
Bram Moolenaar8ada2cc2010-07-29 20:43:36 +0200108 let diff_style_start = 0
109 let insert_index = 0
110
Bram Moolenaarb02cbe32010-07-11 22:38:52 +0200111 for buf in a:buf_list
112 let temp = []
113 exe bufwinnr(buf) . 'wincmd w'
114
Bram Moolenaar349b2fb2010-07-16 20:35:36 +0200115 " 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
Bram Moolenaar8ada2cc2010-07-29 20:43:36 +0200119 " 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
Bram Moolenaarb02cbe32010-07-11 22:38:52 +0200131 " 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
Bram Moolenaar8ada2cc2010-07-29 20:43:36 +0200137 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
Bram Moolenaarb02cbe32010-07-11 22:38:52 +0200153 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
Bram Moolenaar349b2fb2010-07-16 20:35:36 +0200163 " TODO: restore using grabbed lines if undolevel is 1?
Bram Moolenaarb02cbe32010-07-11 22:38:52 +0200164 normal 2u
Bram Moolenaarbebca9d2010-08-07 15:47:30 +0200165 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
Bram Moolenaarb02cbe32010-07-11 22:38:52 +0200172 let html += temp
Bram Moolenaar349b2fb2010-07-16 20:35:36 +0200173 call add(html, '</div></td>')
Bram Moolenaarb02cbe32010-07-11 22:38:52 +0200174
175 " Close this buffer
Bram Moolenaar349b2fb2010-07-16 20:35:36 +0200176 " TODO: the comment above says we're going to allow saving the file
177 " later...but here we discard it?
Bram Moolenaarb02cbe32010-07-11 22:38:52 +0200178 quit!
179 endfor
180
Bram Moolenaar8ada2cc2010-07-29 20:43:36 +0200181 let html[body_line_num] = body_line
182
Bram Moolenaarb02cbe32010-07-11 22:38:52 +0200183 call add(html, '</tr>')
184 call add(html, '</table>')
Bram Moolenaar8ada2cc2010-07-29 20:43:36 +0200185 call add(html, s:body_end_line)
Bram Moolenaarb02cbe32010-07-11 22:38:52 +0200186 call add(html, '</html>')
187
188 let i = 1
Bram Moolenaarbebca9d2010-08-07 15:47:30 +0200189 let name = "Diff" . (s:settings.use_xhtml ? ".xhtml" : ".html")
Bram Moolenaar349b2fb2010-07-16 20:35:36 +0200190 " Find an unused file name if current file name is already in use
Bram Moolenaarb02cbe32010-07-11 22:38:52 +0200191 while filereadable(name)
Bram Moolenaarbebca9d2010-08-07 15:47:30 +0200192 let name = substitute(name, '\d*\.x\?html$', '', '') . i . '.' . fnamemodify(copy(name), ":t:e")
Bram Moolenaarb02cbe32010-07-11 22:38:52 +0200193 let i += 1
Bram Moolenaar349b2fb2010-07-16 20:35:36 +0200194 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
Bram Moolenaarb02cbe32010-07-11 22:38:52 +0200201 call append(0, html)
Bram Moolenaar349b2fb2010-07-16 20:35:36 +0200202
Bram Moolenaarb02cbe32010-07-11 22:38:52 +0200203 if len(style) > 0
204 1
Bram Moolenaar349b2fb2010-07-16 20:35:36 +0200205 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.
Bram Moolenaar076e8b22010-08-05 21:54:00 +0200209 if s:settings.dynamic_folds
Bram Moolenaar349b2fb2010-07-16 20:35:36 +0200210 call append(style_start, [
Bram Moolenaar7c86f4c2010-07-18 14:07:22 +0200211 \ "<script type='text/javascript'>",
Bram Moolenaarbebca9d2010-08-07 15:47:30 +0200212 \ s:settings.use_xhtml ? '//<![CDATA[' : " <!--",
Bram Moolenaar7c86f4c2010-07-18 14:07:22 +0200213 \ " 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 \ " }",
Bram Moolenaarbebca9d2010-08-07 15:47:30 +0200229 \ s:settings.use_xhtml ? '//]]>' : " -->",
Bram Moolenaar7c86f4c2010-07-18 14:07:22 +0200230 \ "</script>"
231 \ ])
Bram Moolenaar349b2fb2010-07-16 20:35:36 +0200232 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.
Bram Moolenaar076e8b22010-08-05 21:54:00 +0200239 if s:settings.use_css
Bram Moolenaarbebca9d2010-08-07 15:47:30 +0200240 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>'
Bram Moolenaar7c86f4c2010-07-18 14:07:22 +0200250 \ ])
Bram Moolenaar349b2fb2010-07-16 20:35:36 +0200251 endif
Bram Moolenaarb02cbe32010-07-11 22:38:52 +0200252 endif
253endfunc
Bram Moolenaar349b2fb2010-07-16 20:35:36 +0200254
Bram Moolenaar076e8b22010-08-05 21:54:00 +0200255" 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
Bram Moolenaar349b2fb2010-07-16 20:35:36 +0200262 endif
Bram Moolenaar349b2fb2010-07-16 20:35:36 +0200263endfunc
264
Bram Moolenaar076e8b22010-08-05 21:54:00 +0200265" 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
Bram Moolenaar349b2fb2010-07-16 20:35:36 +0200272 else
Bram Moolenaar076e8b22010-08-05 21:54:00 +0200273 " 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
Bram Moolenaar076e8b22010-08-05 21:54:00 +0200298 " override those settings that need it
299
Bram Moolenaarbebca9d2010-08-07 15:47:30 +0200300 " hover opening implies dynamic folding
301 if user_settings.hover_unfold
302 let user_settings.dynamic_folds = 1
303 endif
304
Bram Moolenaar076e8b22010-08-05 21:54:00 +0200305 " 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
Bram Moolenaar076e8b22010-08-05 21:54:00 +0200311 " 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
Bram Moolenaarbebca9d2010-08-07 15:47:30 +0200327 " 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
Bram Moolenaar076e8b22010-08-05 21:54:00 +0200362 return user_settings
Bram Moolenaar349b2fb2010-07-16 20:35:36 +0200363 endif
364endfunc
365
366let &cpo = s:cpo_sav
367unlet s:cpo_sav
368
369" Make sure any patches will probably use consistent indent
Bram Moolenaar7c86f4c2010-07-18 14:07:22 +0200370" vim: ts=8 sw=2 sts=2 noet