-- Private markers retain outer-to-inner Markdown order until the style pass. -- Keep these codes in sync with docx_inline.py. local prefix = 'MDHelperSource_' local codes = {Strong='B', Emph='I', Strikeout='S', Link='L', Code='C'} local function push(stack, code) local copy, found = {}, false for _, value in ipairs(stack) do copy[#copy + 1] = value if value == code then found = true end end if not found then copy[#copy + 1] = code end return copy end local function mark(items, stack) local output = pandoc.List() local function append(item) local last = output[#output] if item.t == 'Span' and last and last.t == 'Span' and item.attributes['custom-style'] == last.attributes['custom-style'] then local content = last.content content:extend(item.content) last.content = content output[#output] = last else output:insert(item) end end for _, item in ipairs(items) do local previous = item.t == 'Span' and item.attributes['custom-style'] or '' if previous:sub(1, #prefix) == prefix then local combined = stack for code in previous:sub(#prefix + 1):gmatch('[BISLC]') do combined = push(combined, code) end for _, nested in ipairs(mark(item.content, combined)) do append(nested) end elseif codes[item.t] and item.t ~= 'Code' then item.content = mark(item.content, push(stack, codes[item.t])) output:insert(item) elseif item.t == 'Code' or item.t == 'Str' or item.t == 'Space' or item.t == 'SoftBreak' or item.t == 'LineBreak' then local active = item.t == 'Code' and push(stack, 'C') or stack -- The final semantic style supplies the code font. Pandoc's Code writer -- would otherwise replace our marker with its Verbatim character style. if item.t == 'Code' then item = pandoc.Str(item.text) end if #active > 0 then append(pandoc.Span({item}, pandoc.Attr('', {}, {['custom-style']=prefix .. table.concat(active, '_')}))) else output:insert(item) end else if item.t == 'Span' or item.t == 'Quoted' or item.t == 'Cite' then item.content = mark(item.content, stack) end output:insert(item) end end return output end return {{Inlines=function(items) return mark(items, {}) end}}