summaryrefslogtreecommitdiff
path: root/support/make4ht/domfilters
diff options
context:
space:
mode:
authorNorbert Preining <norbert@preining.info>2019-11-27 03:01:12 +0000
committerNorbert Preining <norbert@preining.info>2019-11-27 03:01:12 +0000
commit5142daa13e19b32afa064863e039da3afaf06e83 (patch)
treed8299dbf426b9b0e6cafacc4cbe5a6a762bf28a9 /support/make4ht/domfilters
parentbd1c54bb99e58d8c140acb0b0a536037f96349b4 (diff)
CTAN sync 201911270301
Diffstat (limited to 'support/make4ht/domfilters')
-rw-r--r--support/make4ht/domfilters/make4ht-joincharacters.lua20
-rw-r--r--support/make4ht/domfilters/make4ht-mathmlfixes.lua46
2 files changed, 64 insertions, 2 deletions
diff --git a/support/make4ht/domfilters/make4ht-joincharacters.lua b/support/make4ht/domfilters/make4ht-joincharacters.lua
index 30ed6e1382..5830eae78e 100644
--- a/support/make4ht/domfilters/make4ht-joincharacters.lua
+++ b/support/make4ht/domfilters/make4ht-joincharacters.lua
@@ -1,8 +1,23 @@
+local log = logging.new("joincharacters")
+
local charclasses = {
span=true,
mn = true,
+ mi = true
}
+local has_matching_attributes = function (el, next_el)
+ local el_attr = el._attr or {}
+ local next_attr = next_el._attr or {}
+ -- if the number of attributes doesn't match, elements don't match
+ if #next_attr ~= #el_attr then return false end
+ for k, v in pairs(el_attr) do
+ -- if any attribute doesn't match, elements don't match
+ if v~=next_attr[k] then return false end
+ end
+ return true
+end
+
local function join_characters(obj,par)
-- join adjanced span and similar elements inserted by
-- tex4ht to just one object.
@@ -10,12 +25,13 @@ local function join_characters(obj,par)
local options = get_filter_settings "joincharacters"
local charclasses = options.charclasses or par.charclasses or charclasses
+
obj:traverse_elements(function(el)
local get_name = function(curr)
return string.lower(curr:get_element_name())
end
local get_class = function(next_el)
- return next_el:get_attribute("class")
+ return next_el:get_attribute("class") or next_el:get_attribute("mathvariant")
end
local is_span = function(next_el)
return charclasses[get_name(next_el)]
@@ -41,7 +57,7 @@ local function join_characters(obj,par)
while next_el do
-- save the next element because we will remove it later
local real_next = get_next(next_el)
- if get_name(el) == get_name(next_el) and get_class(el) == get_class(next_el) and not el:get_attribute("id") then
+ if get_name(el) == get_name(next_el) and has_matching_attributes(el,next_el) and not el:get_attribute("id") then
-- it the following element match, copy it's children to the current element
for _, child in ipairs(next_el:get_children()) do
el:add_child_node(child)
diff --git a/support/make4ht/domfilters/make4ht-mathmlfixes.lua b/support/make4ht/domfilters/make4ht-mathmlfixes.lua
new file mode 100644
index 0000000000..a52b0e5d6b
--- /dev/null
+++ b/support/make4ht/domfilters/make4ht-mathmlfixes.lua
@@ -0,0 +1,46 @@
+-- <mglyph> should be inside <mi>, so we don't process it
+-- even though it is a token element
+local token = {"mi", "mn", "mo", "mtext", "mspace", "ms"}
+local token_elements = {}
+for _, tok in ipairs(token) do token_elements[tok] = true end
+
+local function is_token_element(el)
+ return token_elements[el:get_element_name()]
+end
+
+local function fix_token_elements(el)
+ -- find token elements that are children of other token elements
+ if is_token_element(el) then
+ local parent = el:get_parent()
+ if is_token_element(parent) then
+ -- change top element in nested token elements to mstyle
+ parent._name = "mstyle"
+ end
+ end
+end
+
+local function fix_nested_mstyle(el)
+ -- the <mstyle> element can be child of token elements
+ -- we must exterminate it
+ if el:get_element_name() == "mstyle" then
+ local parent = el:get_parent()
+ if is_token_element(parent) then
+ -- if parent doesn't have the mathvariant attribute copy it from <mstyle>
+ if not parent:get_attribute("mathvariant") then
+ local mathvariant = el:get_attribute("mathvariant")
+ parent:set_attribute("mathvariant", mathvariant)
+ end
+ -- copy the contents of <mstyle> to the parent element
+ parent._children = el._children
+ end
+ end
+end
+
+return function(dom)
+ dom:traverse_elements(function(el)
+ fix_token_elements(el)
+ fix_nested_mstyle(el)
+ end)
+ return dom
+end
+