Files
business-suite/bin/code.lua
T

87 lines
3.9 KiB
Lua
Raw Normal View History

-- 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.
--
-- Every span that carries such a boundary is treated, however short it is. The
-- rule used to start at 16 characters, on the reasoning that a shorter span
-- finds room on the next line by itself; a style guide that lists the names of
-- its convention refuted it. `--color-dark` and `--size-medium`, twelve and
-- thirteen characters, stood in a row with one space between them, and the two
-- lines carrying them ran 3.5 and 7.9 points past the edge while
-- `--color-fg-error` beside them broke after a hyphen — a break in one place
-- and none in the other, in one sentence.
--
-- A break that is OFFERED costs nothing: TeX takes it where a line cannot be
-- set otherwise and leaves the span whole everywhere else, so the short span is
-- still read in one piece on every line that has room for it.
-- Where the name really begins: a name may OPEN with the characters that
-- structure it — `--size-medium`, `.btn-primary`, `_private` — and there they
-- structure nothing. A break between the two hyphens of a custom property, or
-- directly behind them, leaves a line ending in `--` and a reader who copies
-- the name with two pieces, neither of which is the name. So the run at the
-- front belongs to the first piece, and the first break stands behind the first
-- piece of content.
--
-- And the first piece is set in a box, because the break this forbids is not
-- one this filter offers: TeX breaks after a hyphen of its own accord, in the
-- typewriter face as anywhere else. Measured on the rendered page after the
-- break points alone had been corrected — a line still ended in `--` with
-- `font-size-large` on the next one. A box has no break inside it and none
-- behind it where no space follows, which is exactly the two places.
local function opening(text)
local run = text:match('^[/._:%-]+')
return run and #run or 0
end
local function pieces(text)
local parts, current = {}, ''
local front = opening(text)
for index = 1, #text do
local char = text:sub(index, index)
local following = text:sub(index + 1, index + 1)
current = current .. char
local structural = index > front
and 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
local parts = pieces(code.text)
if #parts < 2 then return nil end
local result = {}
for index, part in ipairs(parts) do
if index == 1 and opening(code.text) > 0 then
result[#result + 1] = pandoc.RawInline('latex', '\\mbox{')
result[#result + 1] = pandoc.Code(part)
result[#result + 1] = pandoc.RawInline('latex', '}')
else
result[#result + 1] = pandoc.Code(part)
end
if index < #parts then
result[#result + 1] = pandoc.RawInline('latex', '\\allowbreak{}')
end
end
return result
end