74 lines
3.0 KiB
Lua
74 lines
3.0 KiB
Lua
-- 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 = {}
|
|
for _, value in ipairs(stack) do
|
|
if value ~= code then copy[#copy + 1] = value end
|
|
end
|
|
copy[#copy + 1] = code
|
|
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
|
|
local function list_paragraphs(list)
|
|
for _, item in ipairs(list.content) do
|
|
for index, block in ipairs(item) do
|
|
if block.t == 'Para' then
|
|
item[index] = pandoc.Div({block}, pandoc.Attr('', {}, {['custom-style']='MDHelperSourceListParagraph'}))
|
|
end
|
|
end
|
|
end
|
|
return list
|
|
end
|
|
local function quoted_paragraphs(quote)
|
|
for index, block in ipairs(quote.content) do
|
|
if block.t == 'Para' or block.t == 'Plain' then
|
|
quote.content[index] = pandoc.Div({block}, pandoc.Attr('', {}, {['custom-style']='MDHelperSourceInnerQuote'}))
|
|
end
|
|
end
|
|
return quote:walk({Div=function(block)
|
|
if block.attributes['custom-style'] == 'MDHelperSourceListParagraph' then
|
|
block.attributes['custom-style'] = 'MDHelperSourceQuotedListParagraph'
|
|
end
|
|
return block
|
|
end})
|
|
end
|
|
return {{Inlines=function(items) return mark(items, {}) end},
|
|
{BulletList=list_paragraphs, OrderedList=list_paragraphs, BlockQuote=quoted_paragraphs}}
|