Skip to content

Commit

Permalink
feat(filters): Extend sentence wrapping to catch quotations
Browse files Browse the repository at this point in the history
  • Loading branch information
alerque committed Aug 14, 2024
1 parent 6f8df04 commit e031a7d
Showing 1 changed file with 21 additions and 11 deletions.
32 changes: 21 additions & 11 deletions pandoc-filters/sentence_lines.lua
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
local function sentence_lines (element)
local inlines = element.content
for i = 2, #inlines do
if inlines[i].t == "Space" and inlines[i - 1].t == "Str" and inlines[i - 1].text:match("[%.%!%?]%)?$") then
inlines[i] = pandoc.SoftBreak()
-- Stuff we count as ending a sentence
local eos = "%P.+[%.%!%?]+%)?$"

local function wrap_sentences (element)
local content = element.content
for i = 2, #content do
local previous = content[i - 1]
local previous_stringly = pandoc.utils.stringify(previous.content and previous.content or previous)
if
content[i].t == "Space"
and previous_stringly:match(eos)
-- Don't break if the next character is a lower case
and not (content[i+1] and pandoc.utils.stringify(content[i+1]):match("^%l"))
then
content[i] = pandoc.SoftBreak()
end
end
return element
Expand All @@ -14,10 +24,10 @@ return {
return pandoc.Space()
end,
},
{ Para = sentence_lines },
{ Plain = sentence_lines },
{ Emph = sentence_lines },
{ BlockQuote = sentence_lines },
{ Div = sentence_lines },
{ Quoted = sentence_lines },
{ Para = wrap_sentences },
{ Plain = wrap_sentences },
{ Emph = wrap_sentences },
{ BlockQuote = wrap_sentences },
{ Div = wrap_sentences },
{ Quoted = wrap_sentences },
}

0 comments on commit e031a7d

Please sign in to comment.