Skip to content

Commit

Permalink
Remove separators even if they are followed by sectionEnds
Browse files Browse the repository at this point in the history
  • Loading branch information
Witiko committed Mar 11, 2024
1 parent 7435cc7 commit 4364901
Showing 1 changed file with 37 additions and 9 deletions.
46 changes: 37 additions & 9 deletions markdown.dtx
Original file line number Diff line number Diff line change
Expand Up @@ -25582,6 +25582,8 @@ end
%
% \end{markdown}
% \begin{macrocode}
self.secbegin_text = "\\markdownRendererSectionBegin\n"
self.secend_text = "\n\\markdownRendererSectionEnd "
function self.heading(s, level, attributes)
local buf = {}
local flat_text, inlines = table.unpack(s)
Expand All @@ -25591,8 +25593,8 @@ end
table.insert(buf,
self.push_attributes("heading",
nil,
"\\markdownRendererSectionBegin\n",
"\n\\markdownRendererSectionEnd "))
self.secbegin_text,
self.secend_text))
end

-- pop attributes for sections that have ended
Expand All @@ -25613,8 +25615,8 @@ end
-- push attributes for the new section
local start_output = {}
local end_output = {}
table.insert(start_output, "\\markdownRendererSectionBegin\n")
table.insert(end_output, "\n\\markdownRendererSectionEnd ")
table.insert(start_output, self.secbegin_text)
table.insert(end_output, self.secend_text)

table.insert(buf, self.push_attributes("heading",
normalized_attributes,
Expand Down Expand Up @@ -29661,13 +29663,39 @@ end
% \end{macrocode}
% \begin{markdown}
% Remove block element / paragraph separators immediately followed by the
% output of \luamref{writer->undosep}. Then, remove any leftover output of
% \luamref{writer->undosep}.
% output of \luamref{writer->undosep}, possibly interleaved by section ends.
% Then, remove any leftover output of \luamref{writer->undosep}.
% \end{markdown}
% \begin{macrocode}
output = output:gsub(writer.interblocksep_text .. writer.undosep_text, '')
output = output:gsub(writer.paragraphsep_text .. writer.undosep_text, '')
output = output:gsub(writer.undosep_text, '')
local undosep_start, undosep_end
local potential_secend_start, secend_start
local potential_sep_start, sep_start
while true do
undosep_start, undosep_end = output:find(writer.undosep_text, 1, true)
if undosep_start == nil then break end
secend_start = undosep_start
while true do
potential_secend_start = secend_start - #writer.secend_text
if potential_secend_start < 1 then break end
if output:sub(potential_secend_start, secend_start - 1) ~= writer.secend_text then break end
secend_start = potential_secend_start
end
sep_start = secend_start
potential_sep_start = sep_start - #writer.interblocksep_text
if potential_sep_start >= 1
and output:sub(potential_sep_start, sep_start - 1) == writer.interblocksep_text then
sep_start = potential_sep_start
else
potential_sep_start = sep_start - #writer.paragraphsep_text
if potential_sep_start >= 1
and output:sub(potential_sep_start, sep_start - 1) == writer.paragraphsep_text then
sep_start = potential_sep_start
end
end
output = output:sub(1, sep_start - 1)
.. output:sub(secend_start, undosep_start - 1)
.. output:sub(undosep_end + 1)
end
return output
end
% \end{macrocode}
Expand Down

0 comments on commit 4364901

Please sign in to comment.