Skip to content
Snippets Groups Projects
Commit bebca9da authored by Bram Moolenaar's avatar Bram Moolenaar
Browse files

Updates for :TOhtml. (Ben Fritz)

parent 706e84b3
No related merge requests found
" Vim autoload file for the tohtml plugin. " Vim autoload file for the tohtml plugin.
" Maintainer: Ben Fritz <fritzophrenic@gmail.com> " Maintainer: Ben Fritz <fritzophrenic@gmail.com>
" Last Change: 2010 Aug 02 " Last Change: 2010 Aug 06
" "
" Additional contributors: " Additional contributors:
" "
...@@ -49,20 +49,50 @@ func! tohtml#Convert2HTML(line1, line2) ...@@ -49,20 +49,50 @@ func! tohtml#Convert2HTML(line1, line2)
endfunc endfunc
func! tohtml#Diff2HTML(win_list, buf_list) func! tohtml#Diff2HTML(win_list, buf_list)
" TODO: add logic for xhtml let xml_line = ""
let style = ['-->'] let tag_close = '>'
if s:settings.use_xhtml
if s:settings.encoding != ""
let xml_line = "<?xml version=\"1.0\" encoding=\"" . s:settings.encoding . "\"?>"
else
let xml_line = "<?xml version=\"1.0\"?>"
endif
let tag_close = ' />'
endif
let style = [s:settings.use_xhtml ? "" : '-->']
let body_line = '' let body_line = ''
let html = [] let html = []
call add(html, '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"') if s:settings.use_xhtml
call add(html, ' "http://www.w3.org/TR/html4/loose.dtd">') call add(html, xml_line)
call add(html, '<html>') endif
if s:settings.use_xhtml
call add(html, "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">")
call add(html, '<html xmlns="http://www.w3.org/1999/xhtml">')
elseif s:settings.use_css && !s:settings.no_pre
call add(html, "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\" \"http://www.w3.org/TR/html4/strict.dtd\">")
call add(html, '<html>')
else
call add(html, '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"')
call add(html, ' "http://www.w3.org/TR/html4/loose.dtd">')
call add(html, '<html>')
endif
call add(html, '<head>') call add(html, '<head>')
" include encoding as close to the top as possible, but only if not already
" contained in XML information (to avoid haggling over content type)
if s:settings.encoding != "" && !s:settings.use_xhtml
call add(html, "<meta http-equiv=\"content-type\" content=\"text/html; charset=" . s:settings.encoding . '"' . tag_close)
endif
call add(html, '<title>diff</title>') call add(html, '<title>diff</title>')
call add(html, '<meta name="Generator" content="Vim/'.v:version/100.'.'.v:version%100.'">') call add(html, '<meta name="Generator" content="Vim/'.v:version/100.'.'.v:version%100.'"'.tag_close)
call add(html, '<meta name="plugin-version" content="'.g:loaded_2html_plugin.'">') call add(html, '<meta name="plugin-version" content="'.g:loaded_2html_plugin.'"'.tag_close)
" TODO: copy or move encoding logic from 2html.vim so generated markup can call add(html, '<meta name="settings" content="'.
" validate without warnings about encoding \ join(filter(keys(s:settings),'s:settings[v:val]'),',').
\ '"'.tag_close)
call add(html, '</head>') call add(html, '</head>')
let body_line_num = len(html) let body_line_num = len(html)
...@@ -132,7 +162,13 @@ func! tohtml#Diff2HTML(win_list, buf_list) ...@@ -132,7 +162,13 @@ func! tohtml#Diff2HTML(win_list, buf_list)
" so we can later save the file as valid html " so we can later save the file as valid html
" TODO: restore using grabbed lines if undolevel is 1? " TODO: restore using grabbed lines if undolevel is 1?
normal 2u normal 2u
call add(html, '<td nowrap valign="top"><div>') if s:settings.use_css
call add(html, '<td valign="top"><div>')
elseif s:settings.use_xhtml
call add(html, '<td nowrap="nowrap" valign="top"><div>')
else
call add(html, '<td nowrap valign="top"><div>')
endif
let html += temp let html += temp
call add(html, '</div></td>') call add(html, '</div></td>')
...@@ -150,10 +186,10 @@ func! tohtml#Diff2HTML(win_list, buf_list) ...@@ -150,10 +186,10 @@ func! tohtml#Diff2HTML(win_list, buf_list)
call add(html, '</html>') call add(html, '</html>')
let i = 1 let i = 1
let name = "Diff" . ".html" let name = "Diff" . (s:settings.use_xhtml ? ".xhtml" : ".html")
" Find an unused file name if current file name is already in use " Find an unused file name if current file name is already in use
while filereadable(name) while filereadable(name)
let name = substitute(name, '\d*\.html$', '', '') . i . ".html" let name = substitute(name, '\d*\.x\?html$', '', '') . i . '.' . fnamemodify(copy(name), ":t:e")
let i += 1 let i += 1
endwhile endwhile
exe "topleft new " . name exe "topleft new " . name
...@@ -173,7 +209,7 @@ func! tohtml#Diff2HTML(win_list, buf_list) ...@@ -173,7 +209,7 @@ func! tohtml#Diff2HTML(win_list, buf_list)
if s:settings.dynamic_folds if s:settings.dynamic_folds
call append(style_start, [ call append(style_start, [
\ "<script type='text/javascript'>", \ "<script type='text/javascript'>",
\ " <!--", \ s:settings.use_xhtml ? '//<![CDATA[' : " <!--",
\ " function toggleFold(objID)", \ " function toggleFold(objID)",
\ " {", \ " {",
\ " for (win_num = 1; win_num <= ".len(a:buf_list)."; win_num++)", \ " for (win_num = 1; win_num <= ".len(a:buf_list)."; win_num++)",
...@@ -190,7 +226,7 @@ func! tohtml#Diff2HTML(win_list, buf_list) ...@@ -190,7 +226,7 @@ func! tohtml#Diff2HTML(win_list, buf_list)
\ " }", \ " }",
\ " }", \ " }",
\ " }", \ " }",
\ " -->", \ s:settings.use_xhtml ? '//]]>' : " -->",
\ "</script>" \ "</script>"
\ ]) \ ])
endif endif
...@@ -201,16 +237,16 @@ func! tohtml#Diff2HTML(win_list, buf_list) ...@@ -201,16 +237,16 @@ func! tohtml#Diff2HTML(win_list, buf_list)
" horizontally scrollable when the lines are too long. Otherwise, the diff " horizontally scrollable when the lines are too long. Otherwise, the diff
" is pretty useless for really long lines. " is pretty useless for really long lines.
if s:settings.use_css if s:settings.use_css
call append(style_start, [ call append(style_start,
\ '<style type="text/css">']+ \ ['<style type="text/css">']+
\ style+[ \ style+
\ '<!--', \ [ s:settings.use_xhtml ? '' : '<!--',
\ 'table { table-layout: fixed; }', \ 'table { table-layout: fixed; }',
\ 'html, body, table, tbody { width: 100%; margin: 0; padding: 0; }', \ 'html, body, table, tbody { width: 100%; margin: 0; padding: 0; }',
\ 'th, td { width: '.printf("%.1f",100.0/len(a:win_list)).'%; }', \ 'th, td { width: '.printf("%.1f",100.0/len(a:win_list)).'%; }',
\ 'td div { overflow: auto; }', \ 'td div { overflow: auto; }',
\ '-->', \ s:settings.use_xhtml ? '' : '-->',
\ '</style>' \ '</style>'
\ ]) \ ])
endif endif
endif endif
...@@ -259,21 +295,19 @@ func! tohtml#GetUserSettings() ...@@ -259,21 +295,19 @@ func! tohtml#GetUserSettings()
call tohtml#GetOption(user_settings, 'whole_filler', 0 ) call tohtml#GetOption(user_settings, 'whole_filler', 0 )
call tohtml#GetOption(user_settings, 'use_xhtml', 0 ) call tohtml#GetOption(user_settings, 'use_xhtml', 0 )
" TODO: encoding? font? These are string options that require more parsing.
" override those settings that need it " override those settings that need it
" hover opening implies dynamic folding
if user_settings.hover_unfold
let user_settings.dynamic_folds = 1
endif
" ignore folding overrides dynamic folding " ignore folding overrides dynamic folding
if user_settings.ignore_folding && user_settings.dynamic_folds if user_settings.ignore_folding && user_settings.dynamic_folds
let user_settings.dynamic_folds = 0 let user_settings.dynamic_folds = 0
let user_settings.hover_unfold = 0 let user_settings.hover_unfold = 0
endif endif
" hover opening implies dynamic folding
if user_settings.hover_unfold
let user_settings.dynamic_folds = 1
endif
" dynamic folding with no foldcolumn implies hover opens " dynamic folding with no foldcolumn implies hover opens
if user_settings.dynamic_folds && user_settings.no_foldcolumn if user_settings.dynamic_folds && user_settings.no_foldcolumn
let user_settings.hover_unfold = 1 let user_settings.hover_unfold = 1
...@@ -290,6 +324,41 @@ func! tohtml#GetUserSettings() ...@@ -290,6 +324,41 @@ func! tohtml#GetUserSettings()
let user_settings.no_pre = 1 let user_settings.no_pre = 1
endif endif
" Figure out proper MIME charset from the 'encoding' option.
if exists("g:html_use_encoding")
let user_settings.encoding = g:html_use_encoding
else
let vim_encoding = &encoding
if vim_encoding =~ '^8bit\|^2byte'
let vim_encoding = substitute(vim_encoding, '^8bit-\|^2byte-', '', '')
endif
if vim_encoding == 'latin1'
let user_settings.encoding = 'iso-8859-1'
elseif vim_encoding =~ "^cp12"
let user_settings.encoding = substitute(vim_encoding, 'cp', 'windows-', '')
elseif vim_encoding == 'sjis' || vim_encoding == 'cp932'
let user_settings.encoding = 'Shift_JIS'
elseif vim_encoding == 'big5' || vim_encoding == 'cp950'
let user_settings.encoding = "Big5"
elseif vim_encoding == 'euc-cn'
let user_settings.encoding = 'GB_2312-80'
elseif vim_encoding == 'euc-tw'
let user_settings.encoding = ""
elseif vim_encoding =~ '^euc\|^iso\|^koi'
let user_settings.encoding = substitute(vim_encoding, '.*', '\U\0', '')
elseif vim_encoding == 'cp949'
let user_settings.encoding = 'KS_C_5601-1987'
elseif vim_encoding == 'cp936'
let user_settings.encoding = 'GBK'
elseif vim_encoding =~ '^ucs\|^utf'
let user_settings.encoding = 'UTF-8'
else
let user_settings.encoding = ""
endif
endif
" TODO: font
return user_settings return user_settings
endif endif
endfunc endfunc
......
" Vim plugin for converting a syntax highlighted file to HTML. " Vim plugin for converting a syntax highlighted file to HTML.
" Maintainer: Ben Fritz <fritzophrenic@gmail.com> " Maintainer: Ben Fritz <fritzophrenic@gmail.com>
" Last Change: 2010 Aug 02 " Last Change: 2010 Aug 07
" "
" The core of the code is in $VIMRUNTIME/autoload/tohtml.vim and " The core of the code is in $VIMRUNTIME/autoload/tohtml.vim and
" $VIMRUNTIME/syntax/2html.vim " $VIMRUNTIME/syntax/2html.vim
" "
" TODO: " TODO:
" * Bug: error thrown when nowrapscan is set " * Bug: error thrown when nowrapscan is set
" * Diff mode with xhtml gives invalid markup
" * Diff mode does not determine encoding
" * Line number column has one character too few on empty lines " * Line number column has one character too few on empty lines
" without CSS. " without CSS.
" * Add extra meta info (generation time, etc.) " * Add extra meta info (generation time, etc.)
" * Fix strict doctype for other options? " * Tidy up so we can use strict doctype more?
" * TODO comments for code cleanup scattered throughout " * TODO comments for code cleanup scattered throughout
if exists('g:loaded_2html_plugin') if exists('g:loaded_2html_plugin')
finish finish
endif endif
let g:loaded_2html_plugin = 'vim7.3_v3' let g:loaded_2html_plugin = 'vim7.3_v4'
" Define the :TOhtml command when: " Define the :TOhtml command when:
" - 'compatible' is not set " - 'compatible' is not set
......
" Vim syntax support file " Vim syntax support file
" Maintainer: Ben Fritz <fritzophrenic@gmail.com> " Maintainer: Ben Fritz <fritzophrenic@gmail.com>
" Last Change: 2010 Aug 05 " Last Change: 2010 Aug 07
" "
" Additional contributors: " Additional contributors:
" "
...@@ -73,7 +73,7 @@ else ...@@ -73,7 +73,7 @@ else
endfun endfun
endif endif
if s:settings.use_css if !s:settings.use_css
" Return opening HTML tag for given highlight id " Return opening HTML tag for given highlight id
function! s:HtmlOpening(id) function! s:HtmlOpening(id)
let a = "" let a = ""
...@@ -198,39 +198,6 @@ if s:settings.dynamic_folds ...@@ -198,39 +198,6 @@ if s:settings.dynamic_folds
endif endif
" Figure out proper MIME charset from the 'encoding' option.
if exists("g:html_use_encoding")
let s:html_encoding = g:html_use_encoding
else
let s:vim_encoding = &encoding
if s:vim_encoding =~ '^8bit\|^2byte'
let s:vim_encoding = substitute(s:vim_encoding, '^8bit-\|^2byte-', '', '')
endif
if s:vim_encoding == 'latin1'
let s:html_encoding = 'iso-8859-1'
elseif s:vim_encoding =~ "^cp12"
let s:html_encoding = substitute(s:vim_encoding, 'cp', 'windows-', '')
elseif s:vim_encoding == 'sjis' || s:vim_encoding == 'cp932'
let s:html_encoding = 'Shift_JIS'
elseif s:vim_encoding == 'big5' || s:vim_encoding == 'cp950'
let s:html_encoding = "Big5"
elseif s:vim_encoding == 'euc-cn'
let s:html_encoding = 'GB_2312-80'
elseif s:vim_encoding == 'euc-tw'
let s:html_encoding = ""
elseif s:vim_encoding =~ '^euc\|^iso\|^koi'
let s:html_encoding = substitute(s:vim_encoding, '.*', '\U\0', '')
elseif s:vim_encoding == 'cp949'
let s:html_encoding = 'KS_C_5601-1987'
elseif s:vim_encoding == 'cp936'
let s:html_encoding = 'GBK'
elseif s:vim_encoding =~ '^ucs\|^utf'
let s:html_encoding = 'UTF-8'
else
let s:html_encoding = ""
endif
endif
" Set some options to make it work faster. " Set some options to make it work faster.
" Don't report changes for :substitute, there will be many of them. " Don't report changes for :substitute, there will be many of them.
...@@ -295,8 +262,8 @@ set magic ...@@ -295,8 +262,8 @@ set magic
let s:lines = [] let s:lines = []
if s:settings.use_xhtml if s:settings.use_xhtml
if s:html_encoding != "" if s:settings.encoding != ""
call add(s:lines, "<?xml version=\"1.0\" encoding=\"" . s:html_encoding . "\"?>") call add(s:lines, "<?xml version=\"1.0\" encoding=\"" . s:settings.encoding . "\"?>")
else else
call add(s:lines, "<?xml version=\"1.0\"?>") call add(s:lines, "<?xml version=\"1.0\"?>")
endif endif
...@@ -317,15 +284,18 @@ endif ...@@ -317,15 +284,18 @@ endif
" HTML header, with the title and generator ;-). Left free space for the CSS, " HTML header, with the title and generator ;-). Left free space for the CSS,
" to be filled at the end. " to be filled at the end.
call extend(s:lines, [ call extend(s:lines, [
\"<html>", \ "<html>",
\"<head>", \ "<head>"])
\("<title>".expand("%:p:~")."</title>"), " include encoding as close to the top as possible, but only if not already
\("<meta name=\"Generator\" content=\"Vim/".v:version/100.".".v:version%100.'"'.s:tag_close), " contained in XML information (to avoid haggling over content type)
\("<meta name=\"plugin-version\" content=\"".g:loaded_2html_plugin.'"'.s:tag_close), if s:settings.encoding != "" && !s:settings.use_xhtml
\]) call add(s:lines, "<meta http-equiv=\"content-type\" content=\"text/html; charset=" . s:settings.encoding . '"' . s:tag_close)
if s:html_encoding != ""
call add(s:lines, "<meta http-equiv=\"content-type\" content=\"text/html; charset=" . s:html_encoding . '"' . s:tag_close)
endif endif
call extend(s:lines, [
\ ("<title>".expand("%:p:~")."</title>"),
\ ("<meta name=\"Generator\" content=\"Vim/".v:version/100.".".v:version%100.'"'.s:tag_close),
\ ("<meta name=\"plugin-version\" content=\"".g:loaded_2html_plugin.'"'.s:tag_close)
\ ])
call add(s:lines, '<meta name="syntax" content="'.s:current_syntax.'"'.s:tag_close) call add(s:lines, '<meta name="syntax" content="'.s:current_syntax.'"'.s:tag_close)
call add(s:lines, '<meta name="settings" content="'. call add(s:lines, '<meta name="settings" content="'.
\ join(filter(keys(s:settings),'s:settings[v:val]'),','). \ join(filter(keys(s:settings),'s:settings[v:val]'),',').
...@@ -337,7 +307,7 @@ if s:settings.use_css ...@@ -337,7 +307,7 @@ if s:settings.use_css
" if we are doing hover_unfold, use css 2 with css 1 fallback for IE6 " if we are doing hover_unfold, use css 2 with css 1 fallback for IE6
call extend(s:lines, [ call extend(s:lines, [
\ "<style type=\"text/css\">", \ "<style type=\"text/css\">",
\ "<!--", \ s:settings.use_xhtml ? "" : "<!--",
\ ".FoldColumn { text-decoration: none; white-space: pre; }", \ ".FoldColumn { text-decoration: none; white-space: pre; }",
\ "", \ "",
\ "body * { margin: 0; padding: 0; }", "", \ "body * { margin: 0; padding: 0; }", "",
...@@ -356,8 +326,12 @@ if s:settings.use_css ...@@ -356,8 +326,12 @@ if s:settings.use_css
\ ".closed-fold:hover > .fulltext { display: inline; }", \ ".closed-fold:hover > .fulltext { display: inline; }",
\ ".closed-fold:hover > .toggle-filler { display: none; }", \ ".closed-fold:hover > .toggle-filler { display: none; }",
\ ".closed-fold:hover > .Folded { display: none; }", \ ".closed-fold:hover > .Folded { display: none; }",
\ '-->', \ s:settings.use_xhtml ? "" : '-->',
\ '<style>', \ '<style>'])
" TODO: IE7 doesn't *actually* support XHTML, maybe we should remove this.
" But if it's served up as tag soup, maybe the following will work, so
" leave it in for now.
call extend(s:lines, [
\ "<!--[if lt IE 7]><style type=\"text/css\">", \ "<!--[if lt IE 7]><style type=\"text/css\">",
\ ".open-fold .Folded { display: none; }", \ ".open-fold .Folded { display: none; }",
\ ".open-fold .fulltext { display: inline; }", \ ".open-fold .fulltext { display: inline; }",
...@@ -375,7 +349,7 @@ if s:settings.use_css ...@@ -375,7 +349,7 @@ if s:settings.use_css
" if we aren't doing hover_unfold, use CSS 1 only " if we aren't doing hover_unfold, use CSS 1 only
call extend(s:lines, [ call extend(s:lines, [
\ "<style type=\"text/css\">", \ "<style type=\"text/css\">",
\ "<!--", \ s:settings.use_xhtml ? "" :"<!--",
\ ".FoldColumn { text-decoration: none; white-space: pre; }", \ ".FoldColumn { text-decoration: none; white-space: pre; }",
\ ".open-fold .Folded { display: none; }", \ ".open-fold .Folded { display: none; }",
\ ".open-fold .fulltext { display: inline; }", \ ".open-fold .fulltext { display: inline; }",
...@@ -386,7 +360,7 @@ if s:settings.use_css ...@@ -386,7 +360,7 @@ if s:settings.use_css
\ ".closed-fold .Folded { display: inline; }", \ ".closed-fold .Folded { display: inline; }",
\ ".closed-fold .toggle-open { display: inline; }", \ ".closed-fold .toggle-open { display: inline; }",
\ ".closed-fold .toggle-closed { display: none; }", \ ".closed-fold .toggle-closed { display: none; }",
\ '-->', \ s:settings.use_xhtml ? "" : '-->',
\ '</style>' \ '</style>'
\]) \])
endif endif
...@@ -394,8 +368,8 @@ if s:settings.use_css ...@@ -394,8 +368,8 @@ if s:settings.use_css
" if we aren't doing any dynamic folding, no need for any special rules " if we aren't doing any dynamic folding, no need for any special rules
call extend(s:lines, [ call extend(s:lines, [
\ "<style type=\"text/css\">", \ "<style type=\"text/css\">",
\ "<!--", \ s:settings.use_xhtml ? "" : "<!--",
\ '-->', \ s:settings.use_xhtml ? "" : '-->',
\ "</style>", \ "</style>",
\]) \])
endif endif
...@@ -406,7 +380,7 @@ if s:settings.dynamic_folds ...@@ -406,7 +380,7 @@ if s:settings.dynamic_folds
call extend(s:lines, [ call extend(s:lines, [
\ "", \ "",
\ "<script type='text/javascript'>", \ "<script type='text/javascript'>",
\ "<!--", \ s:settings.use_xhtml ? '//<![CDATA[' : "<!--",
\ "function toggleFold(objID)", \ "function toggleFold(objID)",
\ "{", \ "{",
\ " var fold;", \ " var fold;",
...@@ -420,7 +394,7 @@ if s:settings.dynamic_folds ...@@ -420,7 +394,7 @@ if s:settings.dynamic_folds
\ " fold.className = 'closed-fold';", \ " fold.className = 'closed-fold';",
\ " }", \ " }",
\ "}", \ "}",
\ '-->', \ s:settings.use_xhtml ? '//]]>' : '-->',
\ "</script>" \ "</script>"
\]) \])
endif endif
...@@ -965,7 +939,7 @@ endif ...@@ -965,7 +939,7 @@ endif
if s:settings.no_pre if s:settings.no_pre
if !s:settings.use_css if !s:settings.use_css
" Close off the font tag that encapsulates the whole <body> " Close off the font tag that encapsulates the whole <body>
call extend(s:lines, ["</font></body>", "</html>"]) call extend(s:lines, ["</font>", "</body>", "</html>"])
else else
call extend(s:lines, ["</body>", "</html>"]) call extend(s:lines, ["</body>", "</html>"])
endif endif
...@@ -1005,7 +979,7 @@ if s:settings.use_css ...@@ -1005,7 +979,7 @@ if s:settings.use_css
execute "normal! ^cwbody\e" execute "normal! ^cwbody\e"
endif endif
else else
execute '%s:<body>:<body bgcolor="' . s:bgc . '" text="' . s:fgc . '"><font face="'. s:htmlfont .'">' execute '%s:<body>:<body bgcolor="' . s:bgc . '" text="' . s:fgc . '">\r<font face="'. s:htmlfont .'">'
endif endif
" Line numbering attributes " Line numbering attributes
...@@ -1053,7 +1027,10 @@ while !empty(s:idlist) ...@@ -1053,7 +1027,10 @@ while !empty(s:idlist)
if s:pgb.needs_redraw if s:pgb.needs_redraw
redrawstatus redrawstatus
let s:pgb.needs_redraw = 0 let s:pgb.needs_redraw = 0
sleep 50m " TODO: sleep here to show the progress bar, but only if total time spent
" so far on this step is < 1 second? Too slow for batch runs like the test
" suite to sleep all the time. Maybe there's no good reason to sleep at
" all.
endif endif
endif endif
endwhile endwhile
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment