2026-09-22 15:06:07 +02:00
|
|
|
-- 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.
|
|
|
|
|
--
|
2026-11-14 12:14:55 +01:00
|
|
|
-- 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.
|
2026-09-22 15:06:07 +02:00
|
|
|
|
2026-11-14 12:14:55 +01:00
|
|
|
-- 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
|
2026-09-22 15:06:07 +02:00
|
|
|
|
|
|
|
|
local function pieces(text)
|
|
|
|
|
local parts, current = {}, ''
|
2026-11-14 12:14:55 +01:00
|
|
|
local front = opening(text)
|
2026-09-22 15:06:07 +02:00
|
|
|
for index = 1, #text do
|
|
|
|
|
local char = text:sub(index, index)
|
|
|
|
|
local following = text:sub(index + 1, index + 1)
|
|
|
|
|
current = current .. char
|
2026-11-14 12:14:55 +01:00
|
|
|
local structural = index > front
|
|
|
|
|
and char:match('[/._:%-]') and following ~= ''
|
2026-09-22 15:06:07 +02:00
|
|
|
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
|
2026-11-14 12:14:55 +01:00
|
|
|
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
|
2026-09-22 15:06:07 +02:00
|
|
|
if index < #parts then
|
|
|
|
|
result[#result + 1] = pandoc.RawInline('latex', '\\allowbreak{}')
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
return result
|
|
|
|
|
end
|