summaryrefslogtreecommitdiff
path: root/support/make4ht/filters/make4ht-cleanspan.lua
blob: ede5c6c74e6c741dd41d39ff0e6090a16cf24a13 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
-- make4ht-cleanspan4ht.lua 
-- fixes spurious <span> elements in tex4ht output


function filter(input)
	local parse_args = function(s)
		local at = {}
		s:gsub("(%w+)%s*=%s*\"([^\"]-)\"", function(k,w)
			at[k]=w
		end)
		return at
	end
	-- local pattern = "(<?/?[%w]*>?)<span[%s]*class=\"([^\"]+)\"[%s]*>"
  local pattern = "(<?/?[%w]*>?)([%s]*)<span[%s]*([^>]-)>"
	local last_class = ""
	local depth = 0
	return  input:gsub(pattern, function(tag,space, args)
		local attr = parse_args(args) or {}
		local class = attr["class"] or ""
		if tag == "</span>" then
			if class == last_class and class~= ""  then 
				last_class = class
				return space .. ""
			end
		elseif tag == "" then
			class=""
		end
		last_class = class
		return tag ..space .. '<span '..args ..'>'
	end)
end

return filter