How to Convert XeTeX to HTML

I know that you finally have found a solution, but there is solution for tex4ht, if anybody is interested.

There are three problems:

  1. You run htxelatex bala.tex # generate html - there should be not # generate html, this is just some comment. Correct command is htlatex bala.tex "xhtml, charset=utf-8" " -cunihtf -utf8" in the case of unicode input. It seems that support for xetex is limited, so it's best to run htlatex and not htxelatex!
  2. tex4ht is incompatible with some xelatex packages, in this case xlxtra, fontspec and polyglossia, so you must edit your preamble to not include them if tex4ht is running.
  3. This problem is most interesting - if you run your file either with htlatex, htxelatex or htlualatex, the devanagari characters are missing - I don't know the right solution, but you can use inputenc's DeclareUnicodeCharacter to enter directly character codes with the help of tex4ht macro \HChar:

    \DeclareUnicodeCharacter{0904}{\HChar{224}\HChar{164}\HChar{132}} % ऄ
    

    You can find all codes in file devng4ht.sty

So now your preamble should look like this:

\documentclass[a4paper,12pt]{article}
\usepackage{etoolbox}
\makeatletter
\@ifpackageloaded{tex4ht}{
\usepackage[utf8]{inputenc}
\usepackage{devng4ht}
}{%
\usepackage{fontspec}
\usepackage{xltxtra}
\setmainfont[Script=Devanagari]{Sanskrit 2003}
\usepackage{polyglossia}
}

\newcommand{\bv}{\begin{Verse}}
\newcommand{\ev}{\end{Verse}}

\usepackage{longtable}
\usepackage[doublespacing]{setspace}
\usepackage{verse}
\usepackage{varwidth}

\AtBeginEnvironment{Verse}{\singlespacing}

\AtBeginEnvironment{longtable}{\onehalfspacing}

\newcommand{\blt}{\begin{longtable}{p{1.5cm}p{0.05cm}p{3.5cm}|p{1.5cm}p{0.05cm}p{3.5cm}|p{1.5cm}p{0.05cm}p{3.5cm}}}
\newcommand{\elt}{\end{longtable}}

\newcommand{\bfns}{\begin{footnotesize}}
\newcommand{\efns}{\end{footnotesize}}

\newcommand{\sz}{\\[\normalbaselineskip]}
\newcommand{\csec}[1]{\section{#1}}

\newenvironment{Verse}
  {\center\varwidth{\linewidth}}
  {\endvarwidth\endcenter}

There is some limitation - you cannot use fot changing switches like large or \footnotesize - from some reason, they will break correct rendering of devanagari.

But you can use private configuration file to adjust your macros using html tags and css codes. File devng.cfg:

\Preamble{xhtml, charset=utf-8}
\Css{
  .verse{
    font-size:large;
    margin:2em;
    width:auto;
  }
  .bfns{
    font-size:small;
  }
}
\begin{document}
\let\old@bv\bv
\renewcommand\bv{\leavevmode\EndP\NoFonts\HtmlParOff\HCode{\Hnewline<div class="verse">\Hnewline}\old@bv}
\let\old@ev\ev
\renewcommand\ev{\NoFonts\HCode{\Hnewline</div>\Hnewline}\old@ev\HtmlParOn\EndNoFonts}
\let\old@bfns\bfns
\renewcommand\bfns{\leavevmode\SaveEndP\NoFonts\Tg<div class="bfns">\old@bfns}
\let\old@efns\efns
\renewcommand\efns{\NoFonts\Tg</div>\old@efns\EndNoFonts}
\EndPreamble

I use \NoFonts to suppress processing of character codes which causes disappearing of devanagari characters. Now you can finally compile your file:

htlatex bala.tex devng " -cunihtf -utf8"

devnagari sample