Export code listing to HTML

Hi everyone,

I’m a first time user and I’m trying to move my blog to Texmacs and I need to export to HTML with code listings. The code in particular is Julia and so I don’t have syntax highlighting but I don’t mind it a lot. The problem is the way it is converted to HTML. More exactly, everything that falls inside the code environment is converted as a list of paragraphs with teletype font (ok) but with lot of line interspacing (wrong) and without indentation (very wrong). For example:

This:

struct Factorization
    user_embeddings
    item_embeddings
    item_bias
end

is converted like this:

<p style="margin-top: 0.5em">
      <tt><div style="text-indent: 0em">
        <div class="compact-block">
          <p>
            struct Factorization
          </p>
        </div>
      </div></tt>
</p>

<p>
      <tt><div style="text-indent: 0em">
        <div class="compact-block">
          <p>
            user_embeddings
          </p>
        </div>
      </div></tt>
</p>

etc...

I would prefer to change the conversion and just output the code inside <pre></pre> tags.

I have some basic knowledge of scheme and I had a look at $TEXMACS_PATH/progs/convert/html/tmhtml.scm and in particular the function tmhtml-verbatim where I can more or less see how everything is converted. My attempts at forcing verbatim-pre instead of verbatim-tt to be called have failed because Texmacs gets stuck at 100% CPU (infinite loop?) and I have to kill it. So here are my questions:

  • Is there some way of writing my document such that the code listing is exported to HTML as a single block inside <pre> tags?

  • How to approach developing and debugging the converter scheme code? How to run tests, set breakpoints, output info, inspect… that kind of things.

Thanks in advance!

Before going to modify the converter you might want to give a look at the TeXmacs blog at https://texmacs.github.io/notes/docs/main.html which we write in TeXmacs. I’ve added some javascript code which do the syntax highlighting and also embedded without problem both C++, Shell and Scheme code. So maybe you are not using the right tag to export.

To debug scheme code my advice is to understand what is the typical input to the translator function and then use a scheme session to redefine some parts and rerun the converter function to see if the output is ok. Also please read the online Help about converters. For a tag \XXXX you can redefine how it is exported by defining a tag \tmhtml-XXXX which is the one used in the export.

Let me know if I can help. I’m interested in improving the use of TeXmacs as a way to maintain a blog. In the GitHub repository above I’ve also added some code to generate automatically an atom feed for the website.

Yes, I checked the repository and also the documentation since it correctly renders code.
The reason is that all the code is put inside session environments and that seems to work.
Actually I have tried as you suggested other tags and with verbatim the problem is solved so that is what I’m going to use.

I will have a look at the atom feed generator, btw, thank you.

I’m having further problems due to some particularities of Julia code, that allows almost any unicode character in the source code but I will create another topic because most probably is related to font encodings.

In the blog there is some C++/Scheme code not in session. I used the \cpp-code or \scm tag which renders the spacing faithfully. If you send me your file I can give a look at why it does not work. TeXmacs has its own encoding for historical reason. The codebase was initiated before Unicode become widespread. But the conversion to HTML should handle this correctly. If you just want to copy and past some Julia code, then maybe you need to so something because you need to explain to TeXmacs that what are you pasting is Unicode.

To output some info in the converter you can use debug-message. This outputs to the terminal and the debugging console (Debug->Console->Debugging console). I use the following to inspect the contents of the stree passed to texmacs->html:

(debug-message "convert" (string-append (texmacs->stm (stree->tree x)) "\n"))

There are probably better ways, this is what I have found by trial and error.

For debugging it can also useful to convert a file on the command line using texmacs -c infile.tm outfile.html -q.

Ah, thank you, that is very useful. I find hard to see where to start setting up a development environment, I have searched the documentation without much success.