Skip to content

Commit

Permalink
Add break-on.plaintext-command option
Browse files Browse the repository at this point in the history
The new option enables the interpretation of paragraph text as LaTeX
commands, thus allowing to use `\newpage` and the like without requiring
the `raw_tex` extension.

Closes: #6
  • Loading branch information
tarleb committed May 7, 2024
1 parent da0ef99 commit 3a96ee9
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 7 deletions.
33 changes: 26 additions & 7 deletions pagebreak.lua
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,23 @@ local function latex_pagebreak (pagebreak)
end
end

-- Turning paragraphs which contain nothing but a form feed
-- characters into line breaks.
local function ascii_pagebreak (raw_pagebreak)
return function (el)
if #el.content == 1 and el.content[1].text == '\f' then
--- Checks if a paragraph contains nothing but a form feed character.
local formfeed_check = function (para)
return #para.content == 1 and para.content[1].text == '\f'
end

--- Checks if a paragraph looks like a LaTeX newpage command.
local function plaintext_check (para)
return #para.content == 1 and is_newpage_command(para.content[1].text)
end

--- Replaces a paragraph with a pagebreak if on of the `checks` returns true.
local function para_pagebreak(raw_pagebreak, checks)
local is_pb = function (para)
return checks:find_if(function (pred) return pred(para) end)
end
return function (para)
if is_pb(para) then
return raw_pagebreak
end
end
Expand All @@ -118,11 +130,18 @@ function Pandoc (doc)
local config = doc.meta.pagebreak or {}
local break_on = config['break-on'] or {}
local raw_pagebreak = newpage(FORMAT, pagebreak_from_config(doc.meta))
local paragraph_checks = pandoc.List{}
if break_on['form-feed'] then
paragraph_checks:insert(formfeed_check)
end
if break_on['plaintext-command'] then
paragraph_checks:insert(plaintext_check)
end
return doc:walk {
RawBlock = latex_pagebreak(raw_pagebreak),
-- Replace paragraphs that contain just a form feed char.
Para = break_on['form-feed']
and ascii_pagebreak(raw_pagebreak)
Para = #paragraph_checks > 0
and para_pagebreak(raw_pagebreak, paragraph_checks)
or nil
}
end
8 changes: 8 additions & 0 deletions test/expected.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,12 @@ ridiculus mus. Nulla posuere. Donec vitae dolor.
Pellentesque dapibus suscipit ligula. Donec posuere augue in quam.
Suspendisse potenti.

The following does not mark a pagebreak unless the `+plaintext-command+`
option is enabled.

<<<

Cum sociis natoque penatibus et magnis dis parturient montes, nascetur
ridiculus mus.

Final paragraph without a preceding pagebreak.
3 changes: 3 additions & 0 deletions test/expected.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,7 @@
<p>Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Nulla posuere. Donec vitae dolor.</p>
<div style="page-break-after: always;"></div>
<p>Pellentesque dapibus suscipit ligula. Donec posuere augue in quam. Suspendisse potenti.</p>
<p>The following does not mark a pagebreak unless the <code>plaintext-command</code> option is enabled.</p>
<p>\pagebreak</p>
<p>Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus.</p>
<p>Final paragraph without a preceding pagebreak.</p>
8 changes: 8 additions & 0 deletions test/expected.ms
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,12 @@ Pellentesque dapibus suscipit ligula.
Donec posuere augue in quam.
Suspendisse potenti.
.PP
The following does not mark a pagebreak unless the
\f[CR]plaintext\-command\f[R] option is enabled.
.PP
\[rs]pagebreak
.PP
Cum sociis natoque penatibus et magnis dis parturient montes, nascetur
ridiculus mus.
.PP
Final paragraph without a preceding pagebreak.
3 changes: 3 additions & 0 deletions test/expected.no-form-feed.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,7 @@
<p>Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Nulla posuere. Donec vitae dolor.</p>
<p> </p>
<p>Pellentesque dapibus suscipit ligula. Donec posuere augue in quam. Suspendisse potenti.</p>
<p>The following does not mark a pagebreak unless the <code>plaintext-command</code> option is enabled.</p>
<p>\pagebreak</p>
<p>Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus.</p>
<p>Final paragraph without a preceding pagebreak.</p>
8 changes: 8 additions & 0 deletions test/expected.typst
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,12 @@ ridiculus mus. Nulla posuere. Donec vitae dolor.
Pellentesque dapibus suscipit ligula. Donec posuere augue in quam.
Suspendisse potenti.

The following does not mark a pagebreak unless the `plaintext-command`
option is enabled.

\\pagebreak

Cum sociis natoque penatibus et magnis dis parturient montes, nascetur
ridiculus mus.

Final paragraph without a preceding pagebreak.
8 changes: 8 additions & 0 deletions test/input.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,12 @@ nascetur ridiculus mus. Nulla posuere. Donec vitae dolor.
Pellentesque dapibus suscipit ligula. Donec posuere augue in
quam. Suspendisse potenti.

The following does not mark a pagebreak unless the
`plaintext-command` option is enabled.

\\pagebreak

Cum sociis natoque penatibus et magnis dis parturient montes,
nascetur ridiculus mus.

Final paragraph without a preceding pagebreak.
1 change: 1 addition & 0 deletions test/test-adoc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ metadata:
pagebreak:
break-on:
form-feed: true
plaintext-command: true

0 comments on commit 3a96ee9

Please sign in to comment.