Skip to content

Commit

Permalink
Merge pull request #2129 from sile-typesetter/verbatim-lines
Browse files Browse the repository at this point in the history
Test and fix verbatim line handling
  • Loading branch information
alerque authored Nov 2, 2024
2 parents 8ef298a + 73cd72c commit a12be30
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 21 deletions.
14 changes: 6 additions & 8 deletions packages/autodoc/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -372,10 +372,13 @@ function package:registerCommands ()
-- Homogenizing the appearance of blocks of code
self:registerCommand("autodoc:codeblock", function (_, content)
SILE.typesetter:leaveHmode()
local lskip = SILE.settings:get("document.lskip") or SILE.types.node.glue()
local rskip = SILE.settings:get("document.rskip") or SILE.types.node.glue()
SILE.settings:temporarily(function ()
-- Note: We avoid using the verbatim environment and simplify things a bit
-- (and try to better enforce novbreak points of insertion)
SILE.call("verbatim:font")
SILE.call("language", { main = "und" })
-- Rather than absolutizing 4 different values, just do it once and cache it
local ex = SILE.types.measurement("1ex"):absolute()
local pushline = function ()
Expand All @@ -391,20 +394,15 @@ function package:registerCommands ()
end
SILE.settings:set("typesetter.parseppattern", "\n")
SILE.settings:set("typesetter.obeyspaces", true)
SILE.settings:set("document.lskip", SILE.types.node.glue(lskip.width.length))
SILE.settings:set("document.rskip", SILE.types.node.glue(rskip.width.length))
SILE.settings:set("document.parindent", SILE.types.node.glue())
SILE.settings:set("document.parskip", SILE.types.node.vglue(0.3 * ex))
SILE.settings:set("document.baselineskip", SILE.types.node.glue(2.3 * ex))
SILE.settings:set("document.parskip", SILE.types.node.vglue())
SILE.settings:set("document.spaceskip", SILE.types.length("1spc"))
SILE.settings:set("shaper.variablespaces", false)
SILE.settings:set("document.language", "und")
colorWrapper("codeblock", function ()
SILE.call("skip", { height = ex })
pushline()
SILE.typesetter:pushVglue(SILE.settings:get("document.parskip"))
SILE.call("novbreak")
SILE.process(content)
SILE.call("novbreak")
SILE.typesetter:pushVglue(SILE.settings:get("document.parskip"))
pushline()
end)
SILE.typesetter:leaveHmode()
Expand Down
16 changes: 9 additions & 7 deletions packages/verbatim/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,22 @@ function package:registerCommands ()
self:registerCommand("verbatim", function (_, content)
SILE.typesetter:pushVglue(6)
SILE.typesetter:leaveHmode()
local lskip = SILE.settings:get("document.lskip") or SILE.types.node.glue()
local rskip = SILE.settings:get("document.rskip") or SILE.types.node.glue()
SILE.settings:temporarily(function ()
SILE.call("verbatim:font")
SILE.call("language", { main = "und" })
SILE.settings:set("typesetter.parseppattern", "\n")
SILE.settings:set("typesetter.obeyspaces", true)
SILE.settings:set("document.rskip", SILE.types.node.glue("0 plus 10000pt"))
SILE.settings:set("document.parindent", SILE.types.node.glue("0"))
SILE.settings:set("document.baselineskip", SILE.types.node.vglue("0"))
SILE.settings:set("document.lineskip", SILE.types.node.vglue("2pt"))
SILE.call("verbatim:font")
SILE.settings:set("document.lskip", SILE.types.node.glue(lskip.width.length))
SILE.settings:set("document.rskip", SILE.types.node.glue(rskip.width.length))
SILE.settings:set("document.parindent", SILE.types.node.glue())
SILE.settings:set("document.parskip", SILE.types.node.vglue())
SILE.settings:set("document.spaceskip", SILE.types.length("1spc"))
SILE.settings:set("shaper.variablespaces", false)
SILE.settings:set("document.language", "und")
SILE.process(content)
SILE.typesetter:leaveHmode()
end)
SILE.typesetter:leaveHmode()
end, "Typesets its contents in a monospaced font.")

self:registerCommand("obeylines", function (_, content)
Expand Down
10 changes: 5 additions & 5 deletions tests/bug-132.expected
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ T 591 w=6.0205 (#)
Mx 81.1075
T 591 w=6.0205 (#)
Mx 14.8819
My 38.1424
My 40.1541
T 591 w=6.0205 (#)
Mx 20.9024
T 591 w=6.0205 (#)
Expand All @@ -32,7 +32,7 @@ T 591 w=6.0205 (#)
Mx 81.1075
T 591 w=6.0205 (#)
Mx 14.8819
My 49.8592
My 52.1541
T 591 w=6.0205 (#)
Mx 20.9024
T 591 w=6.0205 (#)
Expand All @@ -43,12 +43,12 @@ T 591 w=6.0205 (#)
Mx 81.1075
T 591 w=6.0205 (#)
Mx 14.8819
My 66.7459
My 70.1541
T 591 w=6.0205 (#)
Mx 81.1075
T 591 w=6.0205 (#)
Mx 14.8819
My 76.7342
My 82.1541
T 591 w=6.0205 (#)
Mx 20.9024
T 591 w=6.0205 (#)
Expand All @@ -73,7 +73,7 @@ T 591 w=6.0205 (#)
Mx 81.1075
T 591 w=6.0205 (#)
Mx 14.8819
My 88.4510
My 94.1541
T 591 w=6.0205 (#)
Mx 20.9024
T 591 w=6.0205 (#)
Expand Down
32 changes: 32 additions & 0 deletions tests/verbatim-lines.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
Set paper size 297.6377985 419.5275636
Begin page
Mx 14.8819
My 26.0794
Set font Hack;7;400;;normal;;;LTR
T 31 184 184 w=12.6431 (Foo)
My 42.8794
T 12 118 201 w=12.6431 (Bar)
My 51.2794
T 12 118 201 w=12.6431 (Bar)
Mx 31.7393
My 68.0794
T 12 118 238 w=12.6431 (Baz)
Draw line 14.8819 75.3075 267.8740 0.5000
Mx 14.8819
My 84.8794
T 31 184 184 w=12.6431 (Foo)
My 101.6794
T 12 118 201 w=12.6431 (Bar)
My 110.0794
T 12 118 201 w=12.6431 (Bar)
Mx 31.7393
My 126.8794
T 12 118 238 w=12.6431 (Baz)
Draw line 14.8819 134.1075 267.8740 0.5000
Mx 14.8819
My 149.6794
T 87 227 184 w=12.6431 (Two)
My 174.8794
T 51 158 178 139 205 w=21.0718 (Lines)
End page
Finish
32 changes: 32 additions & 0 deletions tests/verbatim-lines.sil
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
\begin[papersize=a6]{document}
\nofolios
\neverindent
\use[module=packages.verbatim]
\use[module=packages.autodoc]

\begin{verbatim}
Foo

Bar
Bar

Baz
\end{verbatim}

\begin[type=autodoc:codeblock]{raw}
Foo

Bar
Bar

Baz
\end{raw}

\begin{verbatim}
Two


Lines
\end{verbatim}

\end{document}
17 changes: 16 additions & 1 deletion typesetters/base.lua
Original file line number Diff line number Diff line change
Expand Up @@ -295,10 +295,25 @@ function typesetter:typeset (text)
return
end
local pId = SILE.traceStack:pushText(text)
for token in SU.gtoke(text, SILE.settings:get("typesetter.parseppattern")) do
local parsepattern = SILE.settings:get("typesetter.parseppattern")
-- NOTE: Big assumption on how to guess were are in "obeylines" mode.
-- See https://github.com/sile-typesetter/sile/issues/2128
local obeylines = parsepattern == "\n"

local seenParaContent = true
for token in SU.gtoke(text, parsepattern) do
if token.separator then
if obeylines and not seenParaContent then
-- In obeylines mode, each standalone line must be kept.
-- The zerohbox is not discardable, so it will be kept in the output,
-- and the baseline skip will do the rest.
self:pushHorizontal(SILE.types.node.zerohbox())
else
seenParaContent = false
end
self:endline()
else
seenParaContent = true
if SILE.settings:get("typesetter.softHyphen") then
local warnedshy = false
for token2 in SU.gtoke(token.string, luautf8.char(0x00AD)) do
Expand Down

0 comments on commit a12be30

Please sign in to comment.