Last active
February 27, 2023 20:32
-
-
Save jdutant/549ef06074d3ae00b78ca6ec8ed2cfe1 to your computer and use it in GitHub Desktop.
Fix badly formed HTML sublists with Pandoc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--[[ fixSubLists.lua Fix badly formed HTML sublists in Pandoc | |
Apple Notes (via AppleScript) produces badly formed HTML | |
for sublists: | |
<ul> | |
<li>Level 1 element 1</li> | |
<ul> | |
<li>Level 2 element 1</li> | |
<li>Level 2 element 2</li> | |
</ul> | |
<li>Level 1 element 2</li> | |
</ul> | |
Pandoc corrects it by placing the sublist in its own list item: | |
<ul> | |
<li>Level 1 element 1</li> | |
<li> | |
<ul> | |
<li>Level 2 element 1</li> | |
<li>Level 2 element 2</li> | |
</ul> | |
</li> | |
<li>Level 1 element 2</li> | |
</ul> | |
This filter picks list items that consists of a single sublist and | |
insert them in the previous list element (if any) instead: | |
<ul> | |
<li>Level 1 element 1 | |
<ul> | |
<li>Level 2 element 1</li> | |
<li>Level 2 element 2</li> | |
</ul> | |
</li> | |
<li>Level 1 element 2</li> | |
</ul> | |
Usage | |
pandoc -L fixSubLists.lua -f html source.html -o out.html | |
]] | |
local function fixList(elem) | |
local changed = false | |
local newList = pandoc.List:new() | |
local function isSubList(list) | |
return #list == 1 | |
and (list[1].t == 'BulletList' or list[1].t == 'OrderedList') | |
end | |
for _,item in ipairs(elem.c) do | |
if #newList > 0 and isSubList(item) then | |
-- append item's sublist to the last item of newList | |
changed = true | |
newList[#newList]:insert(item[1]) | |
else | |
-- otherwise append item to newList | |
newList:insert(item) | |
end | |
end | |
if changed then | |
elem.c = newList | |
end | |
return changed and elem or nil | |
end | |
return {{ | |
OrderedList = fixList, | |
BulletList = fixList, | |
}} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment