blob: e929db325559e70990db8bb1c19b670b912f8b1f [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>
3" Last Change: 2010 Jul 15
Bram Moolenaarb02cbe32010-07-11 22:38:52 +02004"
Bram Moolenaar349b2fb2010-07-16 20:35:36 +02005" 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
Bram Moolenaarb02cbe32010-07-11 22:38:52 +020015
16func! tohtml#Convert2HTML(line1, line2)
Bram Moolenaar349b2fb2010-07-16 20:35:36 +020017 let old_vals = tohtml#OverrideUserSettings()
18
19 if !&diff || exists("g:html_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 Moolenaarb02cbe32010-07-11 22:38:52 +020032 let save_hwf = exists("g:html_whole_filler")
33 let g:html_whole_filler = 1
Bram Moolenaar349b2fb2010-07-16 20:35:36 +020034 let g:html_diff_win_num = 0
Bram Moolenaarb02cbe32010-07-11 22:38:52 +020035 for window in win_list
36 exe ":" . bufwinnr(window) . "wincmd w"
37 let g:html_start_line = 1
38 let g:html_end_line = line('$')
Bram Moolenaar349b2fb2010-07-16 20:35:36 +020039 let g:html_diff_win_num += 1
Bram Moolenaarb02cbe32010-07-11 22:38:52 +020040 runtime syntax/2html.vim
41 call add(buf_list, bufnr('%'))
Bram Moolenaar349b2fb2010-07-16 20:35:36 +020042 "exec '%s#<span id=''\zsfold\d\+\ze''#win'.win_num.'\0#ge'
Bram Moolenaarb02cbe32010-07-11 22:38:52 +020043 endfor
Bram Moolenaar349b2fb2010-07-16 20:35:36 +020044 unlet g:html_diff_win_num
Bram Moolenaarb02cbe32010-07-11 22:38:52 +020045 if !save_hwf
46 unlet g:html_whole_filler
47 endif
48 call tohtml#Diff2HTML(win_list, buf_list)
49 endif
50
Bram Moolenaar349b2fb2010-07-16 20:35:36 +020051 call tohtml#RestoreUserSettings(old_vals)
52
Bram Moolenaarb02cbe32010-07-11 22:38:52 +020053 unlet g:html_start_line
54 unlet g:html_end_line
55endfunc
56
57func! tohtml#Diff2HTML(win_list, buf_list)
Bram Moolenaar349b2fb2010-07-16 20:35:36 +020058 " TODO: add logic for xhtml
Bram Moolenaarb02cbe32010-07-11 22:38:52 +020059 let style = []
60 let html = []
61 call add(html, '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"')
62 call add(html, ' "http://www.w3.org/TR/html4/loose.dtd">')
63 call add(html, '<html>')
64 call add(html, '<head>')
65 call add(html, '<title>diff</title>')
Bram Moolenaar349b2fb2010-07-16 20:35:36 +020066 call add(html, '<meta name="Generator" content="Vim/'.v:version/100.'.'.v:version%100.'">')
67 " TODO: copy or move encoding logic from 2html.vim so generated markup can
68 " validate without warnings about encoding
69
Bram Moolenaarb02cbe32010-07-11 22:38:52 +020070 call add(html, '</head>')
71 call add(html, '<body>')
72 call add(html, '<table border="1" width="100%">')
Bram Moolenaar349b2fb2010-07-16 20:35:36 +020073
Bram Moolenaarb02cbe32010-07-11 22:38:52 +020074 call add(html, '<tr>')
75 for buf in a:win_list
76 call add(html, '<th>'.bufname(buf).'</th>')
77 endfor
78 call add(html, '</tr><tr>')
79
80 for buf in a:buf_list
81 let temp = []
82 exe bufwinnr(buf) . 'wincmd w'
83
Bram Moolenaar349b2fb2010-07-16 20:35:36 +020084 " If text is folded because of user foldmethod settings, etc. we don't want
85 " to act on everything in a fold by mistake.
86 setlocal nofoldenable
87
Bram Moolenaarb02cbe32010-07-11 22:38:52 +020088 " Grab the style information. Some of this will be duplicated...
89 1
90 let style_start = search('^<style type="text/css">')
91 1
92 let style_end = search('^</style>')
93 if style_start > 0 && style_end > 0
94 let style += getline(style_start + 1, style_end - 1)
95 endif
96
97 " Delete those parts that are not needed so
98 " we can include the rest into the resulting table
99 1,/^<body/d_
100 $
101 ?</body>?,$d_
102 let temp = getline(1,'$')
103 " undo deletion of start and end part
104 " so we can later save the file as valid html
Bram Moolenaar349b2fb2010-07-16 20:35:36 +0200105 " TODO: restore using grabbed lines if undolevel is 1?
Bram Moolenaarb02cbe32010-07-11 22:38:52 +0200106 normal 2u
Bram Moolenaar349b2fb2010-07-16 20:35:36 +0200107 call add(html, '<td nowrap valign="top"><div>')
Bram Moolenaarb02cbe32010-07-11 22:38:52 +0200108 let html += temp
Bram Moolenaar349b2fb2010-07-16 20:35:36 +0200109 call add(html, '</div></td>')
Bram Moolenaarb02cbe32010-07-11 22:38:52 +0200110
111 " Close this buffer
Bram Moolenaar349b2fb2010-07-16 20:35:36 +0200112 " TODO: the comment above says we're going to allow saving the file
113 " later...but here we discard it?
Bram Moolenaarb02cbe32010-07-11 22:38:52 +0200114 quit!
115 endfor
116
117 call add(html, '</tr>')
118 call add(html, '</table>')
119 call add(html, '</body>')
120 call add(html, '</html>')
121
122 let i = 1
123 let name = "Diff" . ".html"
Bram Moolenaar349b2fb2010-07-16 20:35:36 +0200124 " Find an unused file name if current file name is already in use
Bram Moolenaarb02cbe32010-07-11 22:38:52 +0200125 while filereadable(name)
126 let name = substitute(name, '\d*\.html$', '', '') . i . ".html"
127 let i += 1
Bram Moolenaar349b2fb2010-07-16 20:35:36 +0200128 endwhile
129 exe "topleft new " . name
130 setlocal modifiable
131
132 " just in case some user autocmd creates content in the new buffer, make sure
133 " it is empty before proceeding
134 %d
Bram Moolenaarb02cbe32010-07-11 22:38:52 +0200135 call append(0, html)
Bram Moolenaar349b2fb2010-07-16 20:35:36 +0200136
Bram Moolenaarb02cbe32010-07-11 22:38:52 +0200137 if len(style) > 0
138 1
Bram Moolenaar349b2fb2010-07-16 20:35:36 +0200139 let style_start = search('^</head>')-1
140
141 " Insert javascript to toggle matching folds open and closed in all windows,
142 " if dynamic folding is active.
143 if exists("g:html_dynamic_folds")
144 call append(style_start, [
145 \ "<script type='text/javascript'>",
146 \ " <!--",
147 \ " function toggleFold(objID)",
148 \ " {",
149 \ " for (win_num = 1; win_num <= 2; win_num++)",
150 \ " {",
151 \ " var fold;",
152 \ ' fold = document.getElementById("win"+win_num+objID);',
153 \ " if(fold.className == 'closed-fold')",
154 \ " {",
155 \ " fold.className = 'open-fold';",
156 \ " }",
157 \ " else if (fold.className == 'open-fold')",
158 \ " {",
159 \ " fold.className = 'closed-fold';",
160 \ " }",
161 \ " }",
162 \ " }",
163 \ " -->",
164 \ "</script>"
165 \ ])
166 endif
167
168 " Insert styles from all the generated html documents and additional styles
169 " for the table-based layout of the side-by-side diff. The diff should take
170 " up the full browser window (but not more), and be static in size,
171 " horizontally scrollable when the lines are too long. Otherwise, the diff
172 " is pretty useless for really long lines.
173 if exists("g:html_use_css")
174 call append(style_start, [
175 \ '<style type="text/css">']+
176 \ style+[
177 \ '<!--',
178 \ 'table { table-layout: fixed; }',
179 \ 'html, body, table, tbody { width: 100%; margin: 0; padding: 0; }',
180 \ 'th, td { width: '.printf("%.1f",100.0/len(a:win_list)).'%; }',
181 \ 'td div { overflow: auto; }',
182 \ '-->',
183 \ '</style>'
184 \ ])
185 endif
Bram Moolenaarb02cbe32010-07-11 22:38:52 +0200186 endif
187endfunc
Bram Moolenaar349b2fb2010-07-16 20:35:36 +0200188
189func! tohtml#OverrideUserSettings()
190 let old_settings = {}
191 " make copies of the user-defined settings that we may overrule
192 let old_settings.html_dynamic_folds = exists("g:html_dynamic_folds")
193 let old_settings.html_hover_unfold = exists("g:html_hover_unfold")
194 let old_settings.html_use_css = exists("g:html_use_css")
195
196 " hover opening implies dynamic folding
197 if exists("g:html_hover_unfold")
198 let g:html_dynamic_folds = 1
199 endif
200
201 " dynamic folding with no foldcolumn implies hover opens
202 if exists("g:html_dynamic_folds") && exists("g:html_no_foldcolumn")
203 let g:html_hover_unfold = 1
204 endif
205
206 " ignore folding overrides dynamic folding
207 if exists("g:html_ignore_folding") && exists("g:html_dynamic_folds")
208 unlet g:html_dynamic_folds
209 endif
210
211 " dynamic folding implies css
212 if exists("g:html_dynamic_folds")
213 let g:html_use_css = 1
214 endif
215
216 return old_settings
217endfunc
218
219func! tohtml#RestoreUserSettings(old_settings)
220 " restore any overridden user options
221 if a:old_settings.html_dynamic_folds
222 let g:html_dynamic_folds = 1
223 else
224 unlet! g:html_dynamic_folds
225 endif
226 if a:old_settings.html_hover_unfold
227 let g:html_hover_unfold = 1
228 else
229 unlet! g:html_hover_unfold
230 endif
231 if a:old_settings.html_use_css
232 let g:html_use_css = 1
233 else
234 unlet! g:html_use_css
235 endif
236endfunc
237
238let &cpo = s:cpo_sav
239unlet s:cpo_sav
240
241" Make sure any patches will probably use consistent indent
242" vim: sw=2 sts=2 et