blob: 2f6baf355cd3090bd9efe3baf90634fe05fdb9aa [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 Moolenaar076e8b22010-08-05 21:54:00 +02003" Last Change: 2010 Aug 02
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 Moolenaar349b2fb2010-07-16 20:35:36 +020052 " TODO: add logic for xhtml
Bram Moolenaar8ada2cc2010-07-29 20:43:36 +020053 let style = ['-->']
54 let body_line = ''
55
Bram Moolenaarb02cbe32010-07-11 22:38:52 +020056 let html = []
57 call add(html, '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"')
58 call add(html, ' "http://www.w3.org/TR/html4/loose.dtd">')
59 call add(html, '<html>')
60 call add(html, '<head>')
61 call add(html, '<title>diff</title>')
Bram Moolenaar349b2fb2010-07-16 20:35:36 +020062 call add(html, '<meta name="Generator" content="Vim/'.v:version/100.'.'.v:version%100.'">')
Bram Moolenaar7510fe72010-07-25 12:46:44 +020063 call add(html, '<meta name="plugin-version" content="'.g:loaded_2html_plugin.'">')
Bram Moolenaar349b2fb2010-07-16 20:35:36 +020064 " TODO: copy or move encoding logic from 2html.vim so generated markup can
65 " validate without warnings about encoding
66
Bram Moolenaarb02cbe32010-07-11 22:38:52 +020067 call add(html, '</head>')
Bram Moolenaar8ada2cc2010-07-29 20:43:36 +020068 let body_line_num = len(html)
Bram Moolenaarb02cbe32010-07-11 22:38:52 +020069 call add(html, '<body>')
70 call add(html, '<table border="1" width="100%">')
Bram Moolenaar349b2fb2010-07-16 20:35:36 +020071
Bram Moolenaarb02cbe32010-07-11 22:38:52 +020072 call add(html, '<tr>')
73 for buf in a:win_list
74 call add(html, '<th>'.bufname(buf).'</th>')
75 endfor
76 call add(html, '</tr><tr>')
77
Bram Moolenaar8ada2cc2010-07-29 20:43:36 +020078 let diff_style_start = 0
79 let insert_index = 0
80
Bram Moolenaarb02cbe32010-07-11 22:38:52 +020081 for buf in a:buf_list
82 let temp = []
83 exe bufwinnr(buf) . 'wincmd w'
84
Bram Moolenaar349b2fb2010-07-16 20:35:36 +020085 " If text is folded because of user foldmethod settings, etc. we don't want
86 " to act on everything in a fold by mistake.
87 setlocal nofoldenable
88
Bram Moolenaar8ada2cc2010-07-29 20:43:36 +020089 " When not using CSS or when using xhtml, the <body> line can be important.
90 " Assume it will be the same for all buffers and grab it from the first
91 " buffer. Similarly, need to grab the body end line as well.
92 if body_line == ''
93 1
94 call search('<body')
95 let body_line = getline('.')
96 $
97 call search('</body>', 'b')
98 let s:body_end_line = getline('.')
99 endif
100
Bram Moolenaarb02cbe32010-07-11 22:38:52 +0200101 " Grab the style information. Some of this will be duplicated...
102 1
103 let style_start = search('^<style type="text/css">')
104 1
105 let style_end = search('^</style>')
106 if style_start > 0 && style_end > 0
Bram Moolenaar8ada2cc2010-07-29 20:43:36 +0200107 let buf_styles = getline(style_start + 1, style_end - 1)
108 for a_style in buf_styles
109 if index(style, a_style) == -1
110 if diff_style_start == 0
111 if a_style =~ '\<Diff\(Change\|Text\|Add\|Delete\)'
112 let diff_style_start = len(style)-1
113 endif
114 endif
115 call insert(style, a_style, insert_index)
116 let insert_index += 1
117 endif
118 endfor
119 endif
120
121 if diff_style_start != 0
122 let insert_index = diff_style_start
Bram Moolenaarb02cbe32010-07-11 22:38:52 +0200123 endif
124
125 " Delete those parts that are not needed so
126 " we can include the rest into the resulting table
127 1,/^<body/d_
128 $
129 ?</body>?,$d_
130 let temp = getline(1,'$')
131 " undo deletion of start and end part
132 " so we can later save the file as valid html
Bram Moolenaar349b2fb2010-07-16 20:35:36 +0200133 " TODO: restore using grabbed lines if undolevel is 1?
Bram Moolenaarb02cbe32010-07-11 22:38:52 +0200134 normal 2u
Bram Moolenaar349b2fb2010-07-16 20:35:36 +0200135 call add(html, '<td nowrap valign="top"><div>')
Bram Moolenaarb02cbe32010-07-11 22:38:52 +0200136 let html += temp
Bram Moolenaar349b2fb2010-07-16 20:35:36 +0200137 call add(html, '</div></td>')
Bram Moolenaarb02cbe32010-07-11 22:38:52 +0200138
139 " Close this buffer
Bram Moolenaar349b2fb2010-07-16 20:35:36 +0200140 " TODO: the comment above says we're going to allow saving the file
141 " later...but here we discard it?
Bram Moolenaarb02cbe32010-07-11 22:38:52 +0200142 quit!
143 endfor
144
Bram Moolenaar8ada2cc2010-07-29 20:43:36 +0200145 let html[body_line_num] = body_line
146
Bram Moolenaarb02cbe32010-07-11 22:38:52 +0200147 call add(html, '</tr>')
148 call add(html, '</table>')
Bram Moolenaar8ada2cc2010-07-29 20:43:36 +0200149 call add(html, s:body_end_line)
Bram Moolenaarb02cbe32010-07-11 22:38:52 +0200150 call add(html, '</html>')
151
152 let i = 1
153 let name = "Diff" . ".html"
Bram Moolenaar349b2fb2010-07-16 20:35:36 +0200154 " Find an unused file name if current file name is already in use
Bram Moolenaarb02cbe32010-07-11 22:38:52 +0200155 while filereadable(name)
156 let name = substitute(name, '\d*\.html$', '', '') . i . ".html"
157 let i += 1
Bram Moolenaar349b2fb2010-07-16 20:35:36 +0200158 endwhile
159 exe "topleft new " . name
160 setlocal modifiable
161
162 " just in case some user autocmd creates content in the new buffer, make sure
163 " it is empty before proceeding
164 %d
Bram Moolenaarb02cbe32010-07-11 22:38:52 +0200165 call append(0, html)
Bram Moolenaar349b2fb2010-07-16 20:35:36 +0200166
Bram Moolenaarb02cbe32010-07-11 22:38:52 +0200167 if len(style) > 0
168 1
Bram Moolenaar349b2fb2010-07-16 20:35:36 +0200169 let style_start = search('^</head>')-1
170
171 " Insert javascript to toggle matching folds open and closed in all windows,
172 " if dynamic folding is active.
Bram Moolenaar076e8b22010-08-05 21:54:00 +0200173 if s:settings.dynamic_folds
Bram Moolenaar349b2fb2010-07-16 20:35:36 +0200174 call append(style_start, [
Bram Moolenaar7c86f4c2010-07-18 14:07:22 +0200175 \ "<script type='text/javascript'>",
176 \ " <!--",
177 \ " function toggleFold(objID)",
178 \ " {",
179 \ " for (win_num = 1; win_num <= ".len(a:buf_list)."; win_num++)",
180 \ " {",
181 \ " var fold;",
182 \ ' fold = document.getElementById("win"+win_num+objID);',
183 \ " if(fold.className == 'closed-fold')",
184 \ " {",
185 \ " fold.className = 'open-fold';",
186 \ " }",
187 \ " else if (fold.className == 'open-fold')",
188 \ " {",
189 \ " fold.className = 'closed-fold';",
190 \ " }",
191 \ " }",
192 \ " }",
193 \ " -->",
194 \ "</script>"
195 \ ])
Bram Moolenaar349b2fb2010-07-16 20:35:36 +0200196 endif
197
198 " Insert styles from all the generated html documents and additional styles
199 " for the table-based layout of the side-by-side diff. The diff should take
200 " up the full browser window (but not more), and be static in size,
201 " horizontally scrollable when the lines are too long. Otherwise, the diff
202 " is pretty useless for really long lines.
Bram Moolenaar076e8b22010-08-05 21:54:00 +0200203 if s:settings.use_css
Bram Moolenaar349b2fb2010-07-16 20:35:36 +0200204 call append(style_start, [
Bram Moolenaar7c86f4c2010-07-18 14:07:22 +0200205 \ '<style type="text/css">']+
206 \ style+[
207 \ '<!--',
208 \ 'table { table-layout: fixed; }',
209 \ 'html, body, table, tbody { width: 100%; margin: 0; padding: 0; }',
210 \ 'th, td { width: '.printf("%.1f",100.0/len(a:win_list)).'%; }',
211 \ 'td div { overflow: auto; }',
212 \ '-->',
213 \ '</style>'
214 \ ])
Bram Moolenaar349b2fb2010-07-16 20:35:36 +0200215 endif
Bram Moolenaarb02cbe32010-07-11 22:38:52 +0200216 endif
217endfunc
Bram Moolenaar349b2fb2010-07-16 20:35:36 +0200218
Bram Moolenaar076e8b22010-08-05 21:54:00 +0200219" Gets a single user option and sets it in the passed-in Dict, or gives it the
220" default value if the option doesn't actually exist.
221func! tohtml#GetOption(settings, option, default)
222 if exists('g:html_'.a:option)
223 let a:settings[a:option] = g:html_{a:option}
224 else
225 let a:settings[a:option] = a:default
Bram Moolenaar349b2fb2010-07-16 20:35:36 +0200226 endif
Bram Moolenaar349b2fb2010-07-16 20:35:36 +0200227endfunc
228
Bram Moolenaar076e8b22010-08-05 21:54:00 +0200229" returns a Dict containing the values of all user options for 2html, including
230" default values for those not given an explicit value by the user. Discards the
231" html_ prefix of the option for nicer looking code.
232func! tohtml#GetUserSettings()
233 if exists('s:settings')
234 " just restore the known options if we've already retrieved them
235 return s:settings
Bram Moolenaar349b2fb2010-07-16 20:35:36 +0200236 else
Bram Moolenaar076e8b22010-08-05 21:54:00 +0200237 " otherwise figure out which options are set
238 let user_settings = {}
239
240 " Define the correct option if the old option name exists and we haven't
241 " already defined the correct one. Maybe I'll put out a warnig message about
242 " this sometime and remove the old option entirely at some even later time,
243 " but for now just silently accept the old option.
244 if exists('g:use_xhtml') && !exists("g:html_use_xhtml")
245 let g:html_use_xhtml = g:use_xhtml
246 endif
247
248 " get current option settings with appropriate defaults
249 call tohtml#GetOption(user_settings, 'no_progress', !has("statusline") )
250 call tohtml#GetOption(user_settings, 'diff_one_file', 0 )
251 call tohtml#GetOption(user_settings, 'number_lines', &number )
252 call tohtml#GetOption(user_settings, 'use_css', 1 )
253 call tohtml#GetOption(user_settings, 'ignore_conceal', 0 )
254 call tohtml#GetOption(user_settings, 'ignore_folding', 0 )
255 call tohtml#GetOption(user_settings, 'dynamic_folds', 0 )
256 call tohtml#GetOption(user_settings, 'no_foldcolumn', 0 )
257 call tohtml#GetOption(user_settings, 'hover_unfold', 0 )
258 call tohtml#GetOption(user_settings, 'no_pre', 0 )
259 call tohtml#GetOption(user_settings, 'whole_filler', 0 )
260 call tohtml#GetOption(user_settings, 'use_xhtml', 0 )
261
262 " TODO: encoding? font? These are string options that require more parsing.
263
264 " override those settings that need it
265
266 " ignore folding overrides dynamic folding
267 if user_settings.ignore_folding && user_settings.dynamic_folds
268 let user_settings.dynamic_folds = 0
269 let user_settings.hover_unfold = 0
270 endif
271
272 " hover opening implies dynamic folding
273 if user_settings.hover_unfold
274 let user_settings.dynamic_folds = 1
275 endif
276
277 " dynamic folding with no foldcolumn implies hover opens
278 if user_settings.dynamic_folds && user_settings.no_foldcolumn
279 let user_settings.hover_unfold = 1
280 endif
281
282 " dynamic folding implies css
283 if user_settings.dynamic_folds
284 let user_settings.use_css = 1
285 endif
286
287 " if we're not using CSS we cannot use a pre section because <font> tags
288 " aren't allowed inside a <pre> block
289 if !user_settings.use_css
290 let user_settings.no_pre = 1
291 endif
292
293 return user_settings
Bram Moolenaar349b2fb2010-07-16 20:35:36 +0200294 endif
295endfunc
296
297let &cpo = s:cpo_sav
298unlet s:cpo_sav
299
300" Make sure any patches will probably use consistent indent
Bram Moolenaar7c86f4c2010-07-18 14:07:22 +0200301" vim: ts=8 sw=2 sts=2 noet