2

I am using htlatex to translate from LaTeX to HTML. I've created a \ScriptEnv environment for code as suggested by this answer:

htlatex inserting environment in verbatim

It's working well, but it is being rather too literate -- there is no conversion to HTML entities, so any code with "<"'s is being handled badly.

Is there any way to stop this?

Edited:

In my case, the situation is worse because some of my output looks like an HTML tag with open and close <>.

(isubclasses OntologyBook)
;;=> #{#<OWLClassImpl <bc33d2c3-7ecb413c-bb04-83af2295c465#walk_through#TakeWing>>}
1

another edit:

of you don't want to postprocess html, you may use different mean to achieve verbatim environment. For example with fancyvrb package:

\usepackage{fancyvrb}
\Preamble{xhtml}
\def\StartSuppressPage{\nobreak\vfil\penalty0\vfilneg\vtop\bgroup}
\def\EndSuppressPage{\egroup}
\renewenvironment{tawny}
{\IgnorePar\EndP\StartSuppressPage\HCode{<pre><code class="clojure">}\HtmlParOff\NoFonts\Verbatim}
{\endVerbatim\EndSuppressPage\HCode{</code></pre>}\EndNoFonts\HtmlParOn\par}
\Configure{fancyvrb}{}{}{}{}{}{}
\begin{document}
\EndPreamble

and sample of resulting html:

tudin, felis odio placerat quam, ac pulvinar elit purus eget enim. Nunc vitae
tortor. Proin tempus nibh sit amet nisl. Vivamus quis tortor vitae risus porta
vehicula.
</p>

   <pre><code class="clojure">
<a 
 id="x1-3r1"></a>(defclass&#x00A0;Pizza
<a 
 id="x1-5r2"></a>&#x00A0;&#x00A0;:super&#x00A0;Thing)
<a 
 id="x1-7r3"></a>tady&#x00A0;je&#x00A0;radek
<a 
 id="x1-9r4"></a>a&#x00A0;&#x003E;&#x00A0;b&#x00A0;&amp;&#x00A0;c
   </code></pre>
<!--l. 15--><p class="indent" >   Nam dui ligula, fringilla a, euismod sodales, sollicitudin vel, wisi. Morbi auctor
lorem non justo. Nam lacus libero, pretium at, lobortis v

Edit:

it seems that in this case lua filter for make4ht must be used. Use this .mk4 file:

local filter= require "make4ht-filter"
local entities = function(s)
  return '<code class="clojure">' .. s:gsub('<code class="clojure">(.-)</code>', function(a) 
    local escapes = {["<"]="lt",[">"]="gt",["&"]="amp"}
    return a:gsub("([%<%>%&])", function(x)
      return "&"..(escapes[x] or x) .. ";"
    end)
   end) .. '</code>'
end
local process = filter(entities, uj)
if mode == "draft" then
  Make:htlatex{} 
  Make:match("html$", process)
else
  Make:htlatex{}
  Make:htlatex{}
  Make:htlatex{}
  Make:match("html$", process)
end

filter entities is defined, which process content of <code class="clojure"> element and convert all <,> and & to html entities:

<pre><code class="clojure"> 
(defclass Pizza
  :super Thing)
  (isubclasses OntologyBook)
  ;;=&gt; #{#&lt;OWLClassImpl &lt;bc33d2c3-7ecb413c-bb04-83af2295c465#walk_through#TakeWing&gt;&gt;}
</code></pre>

Old version:

Content of \ScriptEnv is exported verbatim, without any processing, so it seems that this can't be solved at the LaTeX side. But you can post-process html with some tool to replace these prohibited characters with html entities. Some Lua script could be used for that, but easiest way is to use tidy command:

tidy  -m  -utf8 -q -i filename.html

before:

<pre><code class="clojure"> 
  a > b & c
</code></pre>

after

  <pre>
<code class="clojure"> 
  a &gt; b &amp; c
</code>
</pre>

with make4ht you can automatize this. make build file filename.mk4:

if mode == "draft" then
  Make:htlatex{} 
else
  Make:htlatex{}
  Make:htlatex{}
  Make:htlatex{}
  Make:match("html$", "tidy -m -utf8 -q -i ${filename}")
end

with this build file, you will be able to quickly view your file with

make4ht -m draft -c yourconfigfile.cfg filename

and tidy will be used only for final build. Other feature is that all output html files will be processed (this is useful if you split sections or chapters to standalone html files)

  • Other environments do this escaping though, so can't understand why it's not possible with ScriptEnv. Unfortunately, the tidy solution doesn't work. I've modified the question with an explanation. – Phil Lord Oct 13 '14 at 16:34
  • @PhilLord that's because \ScriptEnv is used for inclusion of javascript, for example and you don't want to escape html entities in javascript – michal.h21 Oct 13 '14 at 18:43
  • Ah. Is there not a better environment which is "verbatim but with no fonts or spans, or indeed anything"? – Phil Lord Oct 14 '14 at 14:27
  • @PhilLord I had some success with fancyvrb package, I wil edit my answer – michal.h21 Oct 14 '14 at 14:53
  • Okay, so I have two options here. I've got a preprocessor step working also using Emacs. I shall think on which to use. Thanks for the help! – Phil Lord Oct 14 '14 at 15:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.