Wednesday, March 30, 2011

Re: Source code to HTML

On Mar 30, 5:01 am, Grigory Sarnitskiy <sargrig...@ya.ru> wrote:
> Hello, I'm trying to produce highlighted html from console.
>
> After a bit of googling I came across this solution:
>
> cat MyFile.input | vim - +TOhtml '+w out.html' '+qall!'
>
> However, resulting html is not highlighted, but in Vim and Cream highlighting works.
>
> I've never used vim before, but it is the only editor supporting syntax highlighting for the language I need (maybe there is something for emacs, but not sure).

When Vim reads from stdin, it has no way of knowing what file type you
are giving it. Vim's filetype detection mostly depends on file
extension, although some file types are determined from file content
*at the time of reading in a file*. Since Vim is reading from stdin
and not a file, it cannot even detect the file type in this way.

If for some reason you MUST use a pipe, you'll need to set the file
type manually. Something like:

cat MyFile.input | vim - '+set ft=ruby' +TOhtml '+w out.html' '+qall!'

However, even better would be to allow Vim's filetype detection to
work by itself, simply by reading the file:

vim MyFile.input +TOhtml '+w out.html' '+qall!'

Both of these methods assume that you have a .vimrc which turns on
some basic options. Since you say you've never used Vim before, this
may not be the case. Create a file in ~/.vimrc with the following
contents:

set nocompatible
filetype indent plugin on
syntax on

There may be a system-wide .vimrc which automatically does this for
you, since you say Vim and Cream both have working syntax
highlighting. Nevertheless, you may also be interested in some of the
options to TOhtml. You don't specify your version of Vim, but prior to
version 7.3, the TOhtml command defaults to using some very non-
standard markup. You can improve this by adding the following to your
~/.vimrc file:

let g:html_use_css = 1

This is not needed if you Vim version is 7.3 or greater.

There are plenty of other options which you can enable in the same way
which may be of interest, including:

* g:html_number_lines (turns on line numbering in the generated html,
on by default if you actually use Vim and have line numbers on inside
Vim)
* g:html_dynamic_folds (generates javascript/css allowing a user to
open/close folded text in the generated output, especially useful if
you turn on syntax-based code folding)
* g:use_xhtml (deprecated, replaced by g:html_use_xhtml in Vim 7.3)
(uses xhtml instead of html, if you prefer)

The following are only available in Vim 7.3 or greater:
* g:html_ignore_conceal (if your filetype's syntax highlighting
conceals or replaces certain text, e.g. maybe it replaces &mdash; with
a real em-dash character)
* g:html_use_xhtml (see above, replaces g:use_xhtml)

The following were introduced in patches to Vim 7.3, you may not have
these unless you have the very latest Vim:
* g:html_expand_tabs (useful if you intend to copy-paste from the
generated html, and you want to keep real tabs in your document)

If you have any problems with the file encoding, first try upgrading
your Vim, because this has been greatly improved in recent versions.
If you still have problems, recent updates to TOhtml provide the
following options:
* g:html_use_encoding (technically present for quite some time but it
didn't actually change the document encoding until a recent patch,
just the meta tag)
* g:html_encoding_override (introduced in a patch to Vim 7.3)
* g:html_charset_override (introduced in a patch to Vim 7.3)

Consult the help within Vim for details on any of these. Do this by
launching Vim and typing:

:help :TOhtml

then pressing Enter.

In addition to all this, if you want not just syntax highlighting of a
single file, but also a syntax-highlighted "diff view" between two
different versions of the file, Vim 7.3 and higher supports this
in :TOhtml as well. Modify your command-line as follows:

vimdiff file1 file2 +TOhtml '+w diff_out.html' '+qall!'

--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

No comments: