118 lines
5.4 KiB
Lua
118 lines
5.4 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
|
|
-- Word numbers paragraphs, not tables. Give a table-first item its own
|
|
-- empty numbered paragraph; its following text remains a continuation.
|
|
if item[1] and item[1].t == 'Table' then
|
|
item:insert(1, pandoc.Div({pandoc.Para({pandoc.Str('\u{200b}')})},
|
|
pandoc.Attr('', {}, {['custom-style']='MDHelperSourceTableMarker'})))
|
|
end
|
|
local target = list.t == 'OrderedList' and 'list_ordered' or 'list_bullet'
|
|
local text = pandoc.utils.stringify(item[1] or {})
|
|
if text:match('^%[[ xX]%]') or text:sub(1, #'☐') == '☐' or text:sub(1, #'☒') == '☒' or text:sub(1, #'☑') == '☑' then
|
|
target = 'list_task'
|
|
end
|
|
for index, block in ipairs(item) do
|
|
if block.t == 'Para' then
|
|
item[index] = pandoc.Div({block}, pandoc.Attr('', {}, {['custom-style']='MDHelperSourceListParagraph_' .. target}))
|
|
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)
|
|
local name = block.attributes['custom-style'] or ''
|
|
if name:match('^MDHelperSourceListParagraph') then
|
|
block.attributes['custom-style'] = name:gsub('^MDHelperSourceListParagraph', 'MDHelperSourceQuotedListParagraph')
|
|
end
|
|
return block
|
|
end})
|
|
end
|
|
-- Raw metadata has no visible paragraph and does not consume a list number.
|
|
-- The style adapter consumes it before the final DOCX is written.
|
|
local function contexts(blocks, target, level, depth, item_state)
|
|
local output = pandoc.List()
|
|
for _, block in ipairs(blocks) do
|
|
if block.t == 'OrderedList' or block.t == 'BulletList' then
|
|
local nested_target = block.t == 'OrderedList' and 'list_ordered' or 'list_bullet'
|
|
for _, item in ipairs(block.content) do
|
|
local transformed = contexts(item, nested_target, level + 1, depth, {pending=true})
|
|
for i=#item,1,-1 do item:remove(i) end
|
|
item:extend(transformed)
|
|
end
|
|
elseif block.t == 'BlockQuote' then
|
|
block.content = contexts(block.content, target, level, depth + 1, item_state)
|
|
elseif block.t == 'Div' then
|
|
block.content = contexts(block.content, target, level, depth, item_state)
|
|
else
|
|
if block.t == 'Header' or block.t == 'CodeBlock' or block.t == 'Table' then
|
|
local continuation = item_state and not item_state.pending and '1' or '0'
|
|
output:insert(pandoc.RawBlock('openxml', '<mdh:context xmlns:mdh="urn:mdhelper:source"' ..
|
|
' mdh:list-target="' .. target .. '" mdh:list-level="' .. math.max(0,level) ..
|
|
'" mdh:quote-depth="' .. depth .. '" mdh:continuation="' .. continuation .. '"/>'))
|
|
end
|
|
if item_state and (block.t == 'Para' or block.t == 'Plain' or block.t == 'Header' or block.t == 'CodeBlock' or block.t == 'Table') then
|
|
item_state.pending = false
|
|
end
|
|
end
|
|
output:insert(block)
|
|
end
|
|
return output
|
|
end
|
|
return {{Inlines=function(items) return mark(items, {}) end},
|
|
{BulletList=list_paragraphs, OrderedList=list_paragraphs, BlockQuote=quoted_paragraphs},
|
|
{Pandoc=function(doc) doc.blocks=contexts(doc.blocks, '', -1, 0, nil); return doc end}}
|