I've created a :TOcss command to go along with the handy :TOhtml command that Ben Fritz has been maintaining. Like :TOhtml, it puts its output into a buffer which you can then edit, save or throw away. The buffer is named for g:colors_name.
I made a little demo of it here: http://unnovative.net/CSSDemo/ Each time you reload the page it will link to a random stylesheet generated from colorschemes on my computer. Now we can more easily decouple HTML source-code snippets from the colorschemes used to render them. (In doing this project I noticed that Vim has colorschemes called "twilight" and "eclipse", but not "new moon" or "breaking dawn". Does somebody want to get on that, or are we not too concerned about reaching out to the 14-year-old-girl demographic?) -- Erik Falor http://unnovative.net Registered Linux User #445632 http://linuxcounter.net
diff -r c326b6833204 runtime/autoload/tohtml.vim
--- a/runtime/autoload/tohtml.vim Wed Mar 07 14:57:52 2012 +0100
+++ b/runtime/autoload/tohtml.vim Wed Mar 07 11:24:01 2012 -0700
@@ -342,6 +342,18 @@
unlet s:settings
endfunc "}}}
+func! tohtml#Convert2CSS() "{{{
+ let s:settings = tohtml#GetUserSettings()
+ let s:settings.whole_filler = 1
+
+ " Create an CSS file based upon the syntax items in this file
+ let g:css_only = 1
+ runtime syntax/2html.vim
+
+ unlet g:css_only
+ unlet s:settings
+endfunc "}}}
+
func! tohtml#Diff2HTML(win_list, buf_list) "{{{
let xml_line = ""
let tag_close = '>'
diff -r c326b6833204 runtime/plugin/tohtml.vim
--- a/runtime/plugin/tohtml.vim Wed Mar 07 14:57:52 2012 +0100
+++ b/runtime/plugin/tohtml.vim Wed Mar 07 11:24:01 2012 -0700
@@ -6,9 +6,7 @@
" $VIMRUNTIME/syntax/2html.vim
"
" TODO:
-" * Options for generating the CSS in external style sheets. New :TOcss
-" command to convert the current color scheme into a (mostly) generic CSS
-" stylesheet which can be re-used. Alternate stylesheet support?
+" * :TOcss - Alternate stylesheet support?
" * Pull in code from http://www.vim.org/scripts/script.php?script_id=3113 :
" - listchars support
" - full-line background highlight
@@ -90,5 +88,10 @@
command -range=% TOhtml :call tohtml#Convert2HTML(<line1>, <line2>)
endif
+" Define the :TOcss command when:
+if !&cp && !exists(":TOcss") && has("user_commands")
+ command TOcss :call tohtml#Convert2CSS()
+endif
+
" Make sure any patches will probably use consistent indent
" vim: ts=8 sw=2 sts=2 noet
diff -r c326b6833204 runtime/syntax/2html.vim
--- a/runtime/syntax/2html.vim Wed Mar 07 14:57:52 2012 +0100
+++ b/runtime/syntax/2html.vim Wed Mar 07 11:24:01 2012 -0700
@@ -80,6 +80,27 @@
endfun
endif
+" Return CSS style describing given highlight id (can be empty)
+function! s:CSS1(id)
+ let a = ""
+ if synIDattr(a:id, "inverse")
+ " For inverse, we always must set both colors (and exchange them)
+ let x = s:HtmlColor(synIDattr(a:id, "bg#", s:whatterm))
+ let a = a . "color: " . ( x != "" ? x : s:bgc ) . "; "
+ let x = s:HtmlColor(synIDattr(a:id, "fg#", s:whatterm))
+ let a = a . "background-color: " . ( x != "" ? x : s:fgc ) . "; "
+ else
+ let x = s:HtmlColor(synIDattr(a:id, "fg#", s:whatterm))
+ if x != "" | let a = a . "color: " . x . "; " | endif
+ let x = s:HtmlColor(synIDattr(a:id, "bg#", s:whatterm))
+ if x != "" | let a = a . "background-color: " . x . "; " | endif
+ endif
+ if synIDattr(a:id, "bold") | let a = a . "font-weight: bold; " | endif
+ if synIDattr(a:id, "italic") | let a = a . "font-style: italic; " | endif
+ if synIDattr(a:id, "underline") | let a = a . "text-decoration: underline; "
| endif
+ return a
+endfun
+
if !s:settings.use_css
" Return opening HTML tag for given highlight id
function! s:HtmlOpening(id)
@@ -177,27 +198,6 @@
return formatted
endfun
-" Return CSS style describing given highlight id (can be empty)
-function! s:CSS1(id)
- let a = ""
- if synIDattr(a:id, "inverse")
- " For inverse, we always must set both colors (and exchange them)
- let x = s:HtmlColor(synIDattr(a:id, "bg#", s:whatterm))
- let a = a . "color: " . ( x != "" ? x : s:bgc ) . "; "
- let x = s:HtmlColor(synIDattr(a:id, "fg#", s:whatterm))
- let a = a . "background-color: " . ( x != "" ? x : s:fgc ) . "; "
- else
- let x = s:HtmlColor(synIDattr(a:id, "fg#", s:whatterm))
- if x != "" | let a = a . "color: " . x . "; " | endif
- let x = s:HtmlColor(synIDattr(a:id, "bg#", s:whatterm))
- if x != "" | let a = a . "background-color: " . x . "; " | endif
- endif
- if synIDattr(a:id, "bold") | let a = a . "font-weight: bold; " | endif
- if synIDattr(a:id, "italic") | let a = a . "font-style: italic; " | endif
- if synIDattr(a:id, "underline") | let a = a . "text-decoration: underline; "
| endif
- return a
-endfun
-
if s:settings.dynamic_folds
" compares two folds as stored in our list of folds
" A fold is "less" than another if it starts at an earlier line number,
@@ -249,10 +249,21 @@
" Split window to create a buffer with the HTML file.
let s:orgbufnr = winbufnr(0)
let s:origwin_stl = &l:stl
-if expand("%") == ""
- exec 'new Untitled.'.(s:settings.use_xhtml ? 'x' : '').'html'
+
+if exists("g:css_only")
+ if exists("g:colors_name")
+ exec 'new ' . g:colors_name . '.css'
+ elseif expand("%") == ""
+ exec 'new Untitled.css'
+ else
+ exec 'new %.css'
+ endif
else
- exec 'new %.'.(s:settings.use_xhtml ? 'x' : '').'html'
+ if expand("%") == ""
+ exec 'new Untitled.'.(s:settings.use_xhtml ? 'x' : '').'html'
+ else
+ exec 'new %.'.(s:settings.use_xhtml ? 'x' : '').'html'
+ endif
endif
" Resize the new window to very small in order to make it draw faster
@@ -296,6 +307,127 @@
setlocal bomb
endif
+if exists("g:css_only")
+ " Output the boilerplate comment
+
+ execute "normal! A/* " . expand("%") . "\e"
+ if exists("g:colors_name")
+ execute "normal! A\n Derived from the " . g:colors_name . " color
scheme.\e"
+ endif
+ execute "normal! A\n Generated with Vim/" . v:version/100 . "." .
v:version % 100 ."\e"
+ execute "normal! A\n using tohtml.vim version " . g:loaded_2html_plugin
.".\n*/\n\e"
+
+ " Find out the background and foreground color.
+ let s:fgc = s:HtmlColor(synIDattr(hlID("Normal"), "fg#", s:whatterm))
+ let s:bgc = s:HtmlColor(synIDattr(hlID("Normal"), "bg#", s:whatterm))
+ if s:fgc == ""
+ let s:fgc = ( &background == "dark" ? "#ffffff" : "#000000" )
+ endif
+ if s:bgc == ""
+ let s:bgc = ( &background == "dark" ? "#000000" : "#ffffff" )
+ endif
+
+ "output basic CSS classes
+ if s:settings.no_pre
+ execute "normal! A\nbody { color: " . s:fgc . "; background-color: " .
s:bgc . "; font-family: ". s:htmlfont ."; }\e"
+ else
+ execute "normal! A\npre { " . s:whitespace . "font-family: ". s:htmlfont
."; color: " . s:fgc . "; background-color: " . s:bgc . "; }\e"
+ execute "normal! A\nbody { " . s:whitespace . "font-family: ". s:htmlfont
."; color: " . s:fgc . "; background-color: " . s:bgc . "; }\e"
+ endif
+
+ " Line numbering attributes
+ if s:settings.number_lines
+ execute "normal! A\n.lnr { " . s:CSS1(hlID("LineNr")) . "}\e"
+ endif
+
+ " collect all defined highlights
+ redir => hilights
+ silent highlight
+ redir END
+
+ "TODO: rewrite this in terms of strpart() and stridx() for efficiency
+ " for now, splitting into a list will do
+ for hi in split(hilights, "\n")
+
+ " skip if highlight "links" to another - we only care about 'real'
highlights
+ if 0 > match(hi, 'links to\|cleared')
+ let id = strpart(hi, 0, stridx(hi, " "))
+ let css = s:CSS1(hlID(id))
+
+ " don't keep any empty CSS defs - what clutter!
+ if strlen(css)
+ execute "normal! A\n." . id . ' { ' . css . "}\e"
+ endif
+
+ endif
+ endfor
+
+ " Cleanup
+ %s:\s\+$::e
+
+ " Restore old settings (new window first)
+ let &l:foldenable = s:old_fen
+ let &l:foldmethod = s:old_fdm
+ let &report = s:old_report
+ let &title = s:old_title
+ let &icon = s:old_icon
+ let &paste = s:old_paste
+ let &magic = s:old_magic
+ let @/ = s:old_search
+ let &more = s:old_more
+
+ " switch to original window to restore those settings
+ exe s:orgwin . "wincmd w"
+
+ if !s:settings.expand_tabs
+ let &l:isprint = s:old_isprint
+ endif
+
+ let &l:stl = s:origwin_stl
+ let &l:et = s:old_et
+ let &l:scrollbind = s:old_bind
+
+ " and back to the new window again to end there
+ exe s:newwin . "wincmd w"
+ normal gg
+
+ let &l:stl = s:newwin_stl
+ exec 'resize' s:old_winheight
+ let &l:winfixheight = s:old_winfixheight
+
+ let &ls=s:ls
+
+ " Save a little bit of memory (worth doing?)
+ unlet s:htmlfont s:whitespace
+ unlet s:old_et s:old_paste s:old_icon s:old_report s:old_title s:old_search
+ unlet s:old_magic s:old_more s:old_fdm s:old_fen s:old_winheight
+ unlet! s:old_isprint
+ unlet s:end s:fgc s:bgc s:old_winfixheight
+ unlet! s:id
+ unlet! s:orgwin s:newwin s:orgbufnr s:ls s:origwin_stl
+ unlet! s:newwin_stl s:current_syntax
+ if !v:profiling
+ delfunc s:HtmlColor
+ delfunc s:HtmlFormat
+ delfunc s:CSS1
+ if !s:settings.use_css
+ delfunc s:HtmlOpening
+ delfunc s:HtmlClosing
+ endif
+ if s:settings.dynamic_folds
+ delfunc s:FoldCompare
+ endif
+ endif
+
+ unlet! s:HtmlSpace s:settings
+ let &cpo = s:cpo_sav
+ unlet! s:cpo_sav
+
+ finish
+endif
+
+
+
let s:lines = []
if s:settings.use_xhtml
pgpIdCQUHHKwz.pgp
Description: PGP signature
