3

I use make4ht to produce html; part of the process is a custom build file that parses the HTML and does some checking/operations:

mwe.tex

\documentclass{article}

\begin{document}

test: $2^3$

unbalanced: ($6)$

\section{test section}
more text
\end{document}

mwe.cfg

\Preamble{xhtml,mathml,next,5,-css,NoFonts}
\begin{document}
\EndPreamble

mwe.mk4

local domfilter = require "make4ht-domfilter"
local filter = require "make4ht-filter"
local dom    = require "luaxml-domobject"

local function mwe_parsing(s)
      local obj = dom.parse(s)
      obj:traverse_elements(function(el)
      end)
      -- serialize the DOM object back to HTML
      return obj:serialize()
end

local process = filter {mwe_parsing}
Make:match("html$", process)
Make:match("html$", "tidy -m -xml -utf8 -q -i ${filename}")

Call to make4ht

The call to make4ht is

make4ht -u -c mwe.cfg mwe.tex

which results in HTML files, but the parsing error on the first file (mwe.html) has stopped parsing and subsequent build operations (in the above, tidy) of other files (mwese1.html)

parse_lg process file: mwe.html
...ive/2019/texmf-dist/tex/luatex/luaxml/luaxml-mod-xml.lua:174: Unbalanced Tag (/mrow) [char=848]

Note

I know what the problem is with mwe.tex and that it needs changing to $(6)$, but I am not in control of the source files I'm given, and part of the process is finding such errors. This question is something of a follow-up to htlatex: tweaking the MathML output for numbers with more than one digit

The question

How can I customise mwe.mk4 so that it gets through the parsing error in html files and continues with the build processes? Perhaps a check could be done to see if dom.parse(s) is successful?

1 Answer 1

2

The issue is that LuaXML issues run-time error when it founds invalid XML structure. This is handled in make4ht-domfilter, but since you parse XML on your own, you must also handle the errors.

To catch errors, you can use the pcall function. The following build file needs development version of make4ht, because I found that pcall isn't available in build files by default. It should be fixed now.

local domfilter = require "make4ht-domfilter"
local filter = require "make4ht-filter"
local dom    = require "luaxml-domobject"
local log = logging.new "build file"

local function mwe_parsing(s)
      local status, obj = pcall(function()
          return dom.parse(s)
      end)
      if not status then 
        log:warning("HTML parsing failed")
        log:warning(obj)
        return s 
      end
      obj:traverse_elements(function(el)
      end)
      -- serialize the DOM object back to HTML
      return obj:serialize()
end

local process = filter {mwe_parsing}
Make:match("html$", process)
Make:match("html$", "tidy -m -xml -utf8 -q -i ${filename}")

The important part is this:

      local status, obj = pcall(function()
          return dom.parse(s)
      end)
      if not status then 
        log:warning("HTML parsing failed")
        log:warning(obj)
        return s 
      end

When pcall returns false status, the obj variable contains the error message. We can test for that, use built in make4ht logging to issue warning and return the original document. This will print the following warning:

 [WARNING] build file: HTML parsing failed
 [WARNING] build file: /home/michal/texmf/scripts/lua/LuaXML/luaxml-mod-xml.lua:175: Unbalanced Tag (/mrow) [char=702]
5
  • Many thanks, as always, for this, that's really helpful. I think I had tried something similar to this, so I'm thinking I need to update my version of make4ht (looks like I'm on v0.2g). I'll update and report back. thanks so much
    – cmhughes
    May 8, 2020 at 11:32
  • @cmhughes pcall didn't work in build files until today, so the update is necessary in this case. Version 0.2g is really old, you may experience some substantial changes. Most important is that the CLI ouput is hidden by default, use -a debug option to see TeX output and other compilation messages.
    – michal.h21
    May 8, 2020 at 11:55
  • That's very helpful, many thanks :)
    – cmhughes
    May 8, 2020 at 12:08
  • Thanks again. I've updated my texlive distribution to 2020, and have V0.3e of make4ht. Is there a way that I can access the develop version of make4ht?
    – cmhughes
    May 12, 2020 at 16:06
  • @cmhughes you can install the development version from Github: github.com/michal-h21/make4ht/blob/master/INSTALL.md
    – michal.h21
    May 12, 2020 at 18:36

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.