55 lines
2.1 KiB
Lua
55 lines
2.1 KiB
Lua
-- Copyright (c) 2026 Marc Wäckerlin. SPDX-License-Identifier: MIT
|
|||
|
|
-- Long inline code gets the break opportunities a path or an identifier
|
||
|
|
-- offers, so that it wraps instead of standing outside the page.
|
||
|
|
--
|
||
|
|
-- `payment/stripe_utils/stripe_connector.py:214` is one word to LaTeX: it
|
||
|
|
-- carries no space, and hyphenation patterns do not apply to a typewriter face,
|
||
|
|
-- so the line runs over the right edge and the last characters stand next to
|
||
|
|
-- the paper. Measured over one report: 30 of 44 places where something left the
|
||
|
|
-- type area were spans of this kind, up to 44 points out.
|
||
|
|
--
|
||
|
|
-- A break is offered after the characters that structure such a name, and
|
||
|
|
-- before a capital that follows a small letter, which is where a reader of
|
||
|
|
-- `CreditCardCustomerBasicInfoSerializer` reads a boundary anyway. No hyphen is
|
||
|
|
-- inserted: a hyphen in a path would be read as part of the path.
|
||
|
|
--
|
||
|
|
-- Only a span from 16 characters up is treated. The shortest one that left the
|
||
|
|
-- type area in that document was `plim/settings.py:25` at 19 characters, and a
|
||
|
|
-- short span finds room on the next line by itself, where breaking it would
|
||
|
|
-- only make it harder to read.
|
||
|
|
|
||
|
|
local THRESHOLD = 16
|
||
|
|
|
||
|
|
local function pieces(text)
|
||
|
|
local parts, current = {}, ''
|
||
|
|
for index = 1, #text do
|
||
|
|
local char = text:sub(index, index)
|
||
|
|
local following = text:sub(index + 1, index + 1)
|
||
|
|
current = current .. char
|
||
|
|
local structural = char:match('[/._:%-]') and following ~= ''
|
||
|
|
local camel = char:match('%l') and following:match('%u')
|
||
|
|
if structural or camel then
|
||
|
|
parts[#parts + 1] = current
|
||
|
|
current = ''
|
||
|
|
end
|
||
|
|
end
|
||
|
|
if current ~= '' then
|
||
|
|
parts[#parts + 1] = current
|
||
|
|
end
|
||
|
|
return parts
|
||
|
|
end
|
||
|
|
|
||
|
|
function Code(code)
|
||
|
|
if not FORMAT:match('latex') then return nil end
|
||
|
|
if #code.text < THRESHOLD then return nil end
|
||
|
|
local parts = pieces(code.text)
|
||
|
|
if #parts < 2 then return nil end
|
||
|
|
local result = {}
|
||
|
|
for index, part in ipairs(parts) do
|
||
|
|
result[#result + 1] = pandoc.Code(part)
|
||
|
|
if index < #parts then
|
||
|
|
result[#result + 1] = pandoc.RawInline('latex', '\\allowbreak{}')
|
||
|
|
end
|
||
|
|
end
|
||
|
|
return result
|
||
|
|
end
|