vim 生成的 HTML 代码很 Ugly,的确错怪它了,不是它不行,只是我不会使用,它可以生成很漂亮的代码,支持CSS和xhtml,感谢 rogerz 的帮助。
原来是由两个参数需要设置,一个 html_use_css,一个是 use_xhtml,更详细的帮助信息请查看 vim 的在线帮组。也贴一份到下面了:-)
看来用 vim 还是不错的选择,source2xhtml 暂时不用继续开发了:) 不同通过它,我的Python又长进不少,基本上掌握正则表达式了
E:\Program Files\Vim\vim63\doc\syntax.txt.html
2HTML *2html.vim* *convert-to-HTML*
This is not a syntax file itself, but a script that converts the current
window into HTML. Vim opens a new window in which it builds the HTML file.
You are not supposed to set the 'filetype' or 'syntax' option to "2html"!
Source the script to convert the current file: >
:runtime! syntax/2html.vim
<
Warning: This is slow!
*:TOhtml*
Or use the ":TOhtml" user command. It is defined in a standard plugin.
":TOhtml" also works with a range and in a Visual area: >
:10,40TOhtml
After you save the resulting file, you can view it with any HTML viewer, such
as Netscape. The colors should be exactly the same as you see them in Vim.
To restrict the conversion to a range of lines set "html_start_line" and
"html_end_line" to the first and last line to be converted. Example, using
the last set Visual area: >
:let html_start_line = line("'<")
:let html_end_line = line("'>")
The lines are numbered according to 'number' option and the Number
highlighting. You can force lines to be numbered in the HTML output by
setting "html_number_lines" to non-zero value: >
:let html_number_lines = 1
Force to omit the line numbers by using a zero value: >
:let html_number_lines = 0
Go back to the default to use 'number' by deleting the variable: >
:unlet html_number_lines
By default, HTML optimized for old browsers is generated. If you prefer using
cascading style sheets (CSS1) for the attributes (resulting in considerably
shorter and valid HTML 4 file), use: >
:let html_use_css = 1
By default "<pre>" and "</pre>" is used around the text. This makes it show
up as you see it in Vim, but without wrapping. If you prefer wrapping, at the
risc of making some things look a bit different, use: >
:let html_no_pre = 1
This will use <br> at the end of each line and use " " for repeated
spaces.
The current value of 'encoding' is used to specify the charset of the HTML
file. This only works for those values of 'encoding' that have an equivalent
HTML charset name. To overrule this set g:html_use_encoding to the name of
the charset to be used: >
:let html_use_encoding = "foobar"
To omit the line that specifies the charset, set g:html_use_encoding to an
empty string: >
:let html_use_encoding = ""
To go back to the automatic mechanism, delete the g:html_use_encoding
variable: >
:unlet html_use_encoding
<
*convert-to-XML* *convert-to-XHTML*
An alternative is to have the script generate XHTML (XML compliant HTML). To
do this set the "use_xhtml" variable: >
:let use_xhtml = 1
To disable it again delete the variable: >
:unlet use_xhtml
The generated XHTML file can be used in DocBook XML documents. See:
http://people.mech.kuleuven.ac.be/~pissaris/howto/src2db.html
Remarks:
- This only works in a version with GUI support. If the GUI is not actually
running (possible for X11) it still works, but not very well (the colors
may be wrong).
- Older browsers will not show the background colors.
- From most browsers you can also print the file (in color)!
Here is an example how to run the script over all .c and .h files from a Unix shell: >
for f in *.[ch]; do gvim -f +"syn on" +"run! syntax/2html.vim" +"wq" +"q" $f; done
如果要用非交互模式把代码文本转化成html呢,如果在vi里面的话必须手动输入TOhtml,不容易实现自动化。我们可以在启动vim的时候可以加参数 -c 来执行一些命令,用如下的命令就可以将一个文件转化为语法加亮后的html文件:
vim -c ":TOhtml" -c ":wq %" -c ":q" filename