Hi Nasser,

The strange thing, these happen now using normal math (i.e. no breqn and
> no config.lua needed). Just inline math.
>

It is not strange, it is totally expected. These cases illustrate why these
regular expressions were necessary. Anyway, this version of config.lua
should process all mathjax contents. Hopefully.

Best regards,
Michal
local filter = require "make4ht-filter"
local domfilter = require "make4ht-domfilter"

local function process_options(options)
  -- convert [number={foo}] to \tag{foo}
  local number = options:match("number%s*=%s*{?(%w+)")
  if number then
    return "\\tag{" .. number .. "}"
  end
  return ""
end


local function escape_equal(str)
  -- there can be multiple equal characters in the string. we should put the & character
  -- just before the one which is not inside any group
  -- escape nested equal signs
  str = str:gsub("({[^{^}]+)=([^{^}]+})", "%1:EQUAL:%2")
  -- replace remaining =
  str = str:gsub("([^\n]+)", function(line)
    line = line:gsub("=", "&=", 1)
    return line .. "\n"
  end)
  -- str = str:gsub("=", "&=")
  -- return escaped =
  str = str:gsub(":EQUAL:", "=")
  return str
end

local function make_align(dgroup, new_env)
  -- change breqn environment contents to align*
  -- change = to &=
  -- local dgroup = escape_equal(dgroup) -- <- we don't do this anymore, it leads only to problems
  local new_env = new_env or  "\\begin{align*}\n%s\n\\end{align*}"
  -- return the fixed text in align* environment
  -- return "\\begin{".. new_env .."}" ..  dgroup .. "\\end{" .. new_env .. "}"
  return string.format(new_env, dgroup)
end

-- process remaining dmath environments
local function process_dmath(s, env_name, new_env)
  return s:gsub("\\begin%s*{" .. env_name .. "}(.-)\\end%s*{" .. env_name .. "}",
  function(dmath)
    -- options can be still here
    local dmath = dmath:gsub("^%s*(%b[])", process_options)
    return make_align(dmath, new_env)
  end)
end

local function process_dgroup(s, env_name)
  return s:gsub("\\begin%s*{" .. env_name .. "}(.-)\\end%s*{" .. env_name .. "}",
  function(dgroup)
    -- remove environemnts
    dgroup = process_dmath(dgroup, "dmath%*", "\n%s\\\\\n")
    dgroup = process_dmath(dgroup, "dmath", "\n%s\\\\\n")
    local dgroup = escape_equal(dgroup) -- <- we don't do this anymore, it leads only to problems
    return make_align(dgroup)
  end)
end


local function escape_tags(tagname, s)
  return s:gsub("(" .. tagname .. "[^>]+mathjax[^>]+>)(.-)(</" .. tagname .. ">)", function(tag, text, endtag)
    local text = text:gsub("<", "&lt;"):gsub(">", "&gt;")
    return tag .. text .. endtag
  end)
end


local function escape_mathjax(s)
  local s = escape_tags("span", s)
  return escape_tags("div", s)
end


local process = filter {
  -- find all dgroup* environments and convert them to align*
  function(s)
    local s = process_dgroup(s,"dgroup%*")
    s = process_dgroup(s, "dgroup")
    -- process remaining dmath environments in the document
    s = process_dmath(s, "dmath%*", "\\[\n%s\n\\]")
    s = process_dmath(s, "dmath", "\\begin{equation}\n%s\n \\end{equation}")
    return s
  end,
  escape_mathjax

}
-- install filter to match HTML files
Make:match("html?$", process)

Reply via email to