Sembr: Semantic Line Break (SemBr) Paragraph Reflow#
Sembr rewrites already-rendered paragraph text into one-sentence-per-line form
(“semantic line breaks”): soft-wrapped lines are joined back into logical sentences, then
newlines are re-inserted at sentence boundaries. CommonMark renderers collapse single
newlines inside a paragraph, so the rendered output is unchanged – this is purely a
source-legibility transform (sentence-granular diffs and edits).
Off by default; opt in via the sembr plugin option ([plugin.myst] sembr = true in
.mdformat.toml or --sembr on the CLI); Renderer.render_inline is the caller, so that
every escape is decided against the reflowed layout. Paragraphs, block quotes, and directive
bodies reflow. List items keep their prose on one line, so that a short bullet stays compact,
unless sembr_list_items is also set ([plugin.myst] sembr_list_items = true or
--sembr-list-items): a numbered list of objections and replies is prose and wants the
treatment any paragraph gets. Each continuation line then sits under its item’s marker. An item
inside a block quote always stays on one line: the quote renderer fences every line indented
four spaces, which a wrapped item would produce.
The splitter is deliberately conservative: a boundary requires sentence-final punctuation, a following sentence opener, and must not sit inside code, math, links, images, wikilinks, or a bracketed span, after an abbreviation, single-letter initial, or bare number, or in front of a citation locator. The new line must not re-parse as block syntax (ordered-list interrupts). A missed split costs nothing; a wrong split could change the AST (mdformat’s validation would then reject the whole reformat).
Scholarly prose is the hard case, because citations are dense with periods that end no
sentence: (sec. 1.9), 3, nn. 1203, II. Def. vi, chap. 7 and 8, Obj. 2: Further.
Four kinds of protection cover it, all on by default because none of them fires in ordinary
prose:
ABBREVSlists dotted tokens whose period never ends a sentence (cf.,nn.,vgl.,Philop.). A token earns a place there only when the word is not one that ends ordinary sentences, and the period is then held whatever follows it.LABELSlists words that do end sentences (art.,Prop.,Cor.) and holds their period only in citation context: a number or roman numeral follows it (Prop. 17,PROP. XXVI,art. 3) whileCor. Hence ...still splits.A roman-numeral guard keeps
(II. Def. vi)andCor. iii. 1whole: a numeral’s period is not a sentence end when a number, an abbreviation, or a label follows.A locator guard refuses to split in front of what only a citation can start: a number with locator punctuation (
12:10,408, 25;,28-32,3.9,7 and 8,1048 a 8) or a roman numeral that continues one (IV,,XII (,V. 14). It needs no list, so it also covers book names no list could hold (Caes. 7.1,Laert. VI. 53).
The patterns live in a my.regex.RegexStore (wikiparse’s idiom): named, composable
definitions joined by subroutine invocation, the abbreviation list condensed into a
prefix-factored alternation tree, and compilation deferred to first use (lazy load), so
importing this module stays cheap.
The condensed tree is atomic, so it is only sound when read left to right: a lookbehind
evaluates it right to left, where an earlier alternative (c) can commit before a longer one
(sec) is tried, and thirteen listed abbreviations silently failed to protect anything.
Abbreviation periods are therefore found by a forward scan (held) and checked by position.
Semantic Line Break (SemBr) Paragraph Reflow.
Sembr rewrites already-rendered paragraph text into one-sentence-per-line form
(“semantic line breaks”): soft-wrapped lines are joined back into logical sentences, then
newlines are re-inserted at sentence boundaries. CommonMark renderers collapse single
newlines inside a paragraph, so the rendered output is unchanged – this is purely a
source-legibility transform (sentence-granular diffs and edits).
Off by default; opt in via the sembr plugin option ([plugin.myst] sembr = true in
.mdformat.toml or --sembr on the CLI); Renderer.render_inline is the caller, so that
every escape is decided against the reflowed layout. Paragraphs, block quotes, and directive
bodies reflow. List items keep their prose on one line, so that a short bullet stays compact,
unless sembr_list_items is also set ([plugin.myst] sembr_list_items = true or
--sembr-list-items): a numbered list of objections and replies is prose and wants the
treatment any paragraph gets. Each continuation line then sits under its item’s marker. An item
inside a block quote always stays on one line: the quote renderer fences every line indented
four spaces, which a wrapped item would produce.
The splitter is deliberately conservative: a boundary requires sentence-final punctuation, a following sentence opener, and must not sit inside code, math, links, images, wikilinks, or a bracketed span, after an abbreviation, single-letter initial, or bare number, or in front of a citation locator. The new line must not re-parse as block syntax (ordered-list interrupts). A missed split costs nothing; a wrong split could change the AST (mdformat’s validation would then reject the whole reformat).
Scholarly prose is the hard case, because citations are dense with periods that end no
sentence: (sec. 1.9), 3, nn. 1203, II. Def. vi, chap. 7 and 8, Obj. 2: Further.
Four kinds of protection cover it, all on by default because none of them fires in ordinary
prose:
ABBREVSlists dotted tokens whose period never ends a sentence (cf.,nn.,vgl.,Philop.). A token earns a place there only when the word is not one that ends ordinary sentences, and the period is then held whatever follows it.LABELSlists words that do end sentences (art.,Prop.,Cor.) and holds their period only in citation context: a number or roman numeral follows it (Prop. 17,PROP. XXVI,art. 3) whileCor. Hence ...still splits.A roman-numeral guard keeps
(II. Def. vi)andCor. iii. 1whole: a numeral’s period is not a sentence end when a number, an abbreviation, or a label follows.A locator guard refuses to split in front of what only a citation can start: a number with locator punctuation (
12:10,408, 25;,28-32,3.9,7 and 8,1048 a 8) or a roman numeral that continues one (IV,,XII (,V. 14). It needs no list, so it also covers book names no list could hold (Caes. 7.1,Laert. VI. 53).
The patterns live in a my.regex.RegexStore (wikiparse’s idiom): named, composable
definitions joined by subroutine invocation, the abbreviation list condensed into a
prefix-factored alternation tree, and compilation deferred to first use (lazy load), so
importing this module stays cheap.
The condensed tree is atomic, so it is only sound when read left to right: a lookbehind
evaluates it right to left, where an earlier alternative (c) can commit before a longer one
(sec) is tried, and thirteen listed abbreviations silently failed to protect anything.
Abbreviation periods are therefore found by a forward scan (held) and checked by position.
- myform.Sembr.CLOSERS = '["\'’”)\\]]'#
Closing punctuation that may trail a sentence-final mark (straight/curly quotes, brackets).
- myform.Sembr.OPENERS = '["\'‘“(\\[*_]'#
Opening punctuation that may lead the next sentence (straight/curly quotes, brackets, emphasis).
- myform.Sembr.ABBREVS = ('mr', 'mrs', 'ms', 'mme', 'mlle', 'messrs', 'dr', 'prof', 'sr', 'jr', 'esq', 'hon', 'fr', 'st', 'mt', 'pres', 'gov', 'sen', 'rep', 'supt', 'gen', 'col', 'maj', 'capt', 'lt', 'sgt', 'adm', 'cmdr', 'fig', 'figs', 'figg', 'eq', 'eqs', 'no', 'nos', 'vol', 'vols', 'ch', 'sec', 'ver', 'rev', 'ed', 'eds', 'tr', 'trans', 'transl', 'impl', 'op', 'cit', 'loc', 'sc', 'para', 'pp', 'ff', 'obj', 'objs', 'chap', 'chaps', 'chs', 'subsec', 'dist', 'disp', 'exer', 'lect', 'sol', 'nn', 'cc', 'aa', 'll', 'vv', 'qq', 'qu', 'quaest', 'quodl', 'quodlib', 'fol', 'fols', 'fn', 'fnn', 'frag', 'bk', 'bks', 'pt', 'pts', 'par', 'pars', 'cp', 'comp', 'esp', 'cod', 'codd', 'suppl', 'eth', 'soph', 'phys', 'metaph', 'hist', 'philop', 'simpl', 'cic', 'plut', 'aphr', 'eucl', 'vgl', 'bzw', 'bd', 'bde', 'hrsg', 'aufl', 'abschn', 'kap', 'anm', 'konst', 'gesch', 'zeitschr', 'ztschr', 'jahrb', 'zentralbl', 'akad', 'vs', 'etc', 'al', 'cf', 'ca', 'approx', 'resp', 'viz', 'ibid', 'i\\.e', 'e\\.g', 'q\\.v', 'z\\.b', 'd\\.h', 'u\\.a', 'univ', 'dept', 'inc', 'ltd', 'co', 'corp', 'assn', 'bros', 'jan', 'feb', 'mar', 'apr', 'jun', 'jul', 'aug', 'sep', 'sept', 'oct', 'nov', 'dec')#
honorifics, latinisms, common scholarly/technical abbreviations, and month names. Matched case-insensitively, so keep every entry lowercase; dotted forms (
i.e.) escape their inner period.- Type:
Dotted tokens that end in a period without ending a sentence
- myform.Sembr.LABELS = ('def', 'deff', 'defs', 'prop', 'props', 'schol', 'scholl', 'cor', 'corol', 'coroll', 'dem', 'demonst', 'ax', 'axs', 'lem', 'lemm', 'post', 'hyp', 'sect', 'art', 'arts', 'cap', 'lib', 'ad', 'arg')#
Prop. 17,Def. vi,II. Cor. 2,art. 3. They are not inABBREVSbecause on its own a label is a word that ends sentences (”… the history of art. The …” or “Cor. Hence …”): the period is held only when a number or roman numeral follows it, or when a roman numeral precedes it. That context is what makes the period a citation’s rather than a sentence’s.- Type:
Cross-reference labels that name a numbered thing inside a citation
- myform.Sembr.ROMAN = '(?=[IVXLCDM])M{0,3}(?:CM|CD|D?C{0,3})(?:XC|XL|L?X{0,3})(?:IX|IV|V?I{0,3})(?![A-Za-z])'#
the right-hand guard wants uppercase only (
XiandLiare names), the left-hand guard either.- Type:
A roman numeral, I to MMMCMXCIX, as a whole token. Callers pick the case
- myform.Sembr.NUMERAL = '(?:(?=[IVXLCDM][.,;)\\]*])[IVXLCDM]|(?=[IVXLCDM]{2})(?P>_roman))'#
An uppercase roman numeral used as a citation number. A lone letter is a numeral only with citation punctuation after it (
I.,X,,V)): bare, it is the pronoun “I” or a variable (“X is …”). Two or more letters stand on their own.
- myform.Sembr.NUMBER_LOCATOR = '\\d+[a-z]?(?:[:;,)\\]\\u2013\\u2014-]|\\.\\d+(?![\\d%])|[a-z]{1,3}\\.(?:[ \\t]|$)|[ \\t]+(?:and|or|to|ff?|sqq?|seq|et|&)\\b|[ \\t]+[a-e][ \\t]+\\d)'#
The start of a citation locator, read at the first character of what would be the next sentence: a number carrying locator punctuation, or an uppercase roman numeral that is continued. A real sentence rarely starts like either, and a footnote number does not carry the punctuation (
82 Nelsonstill splits;2: Furtherand408, 25;do not). A colon never counts:D: But, Daddyis a speaker label, not a locator.
- myform.Sembr.NUMERAL_LOCATOR = '(?P>_numeral)(?:\\.(?:[ \\t]+[a-z0-9(\\[,;]|[)\\]*]|\\s*$)|[;)\\]*]|(?<=[IVXLCDM]{2})(?:,|\\s*$|[ \\t]+(?:and|or|to|\\d|[(\\[])))'#
V. 14,X.),XII, 3,IV (a),XII and XIII,II.; but notV. When ...orX is ....- Type:
The roman-numeral kind
- myform.Sembr.CHAIN_LOCATOR = '(?!(?P>_abbrev)\\.)[A-Z][A-Za-z]{0,8}\\.\\*{0,2}[ \\t]+(?:(?P>_number_locator)|(?P>_numeral_locator))'#
a short capitalized word and its period, then a locator (
Cat. Min. 37.1,De Part. An.* II. 10,Pyrrh. Hyp. I. 7). No list could hold these titles. A word already inABBREVSis left out:Cf. 430 b 29opens a note of its own.- Type:
The second link of an abbreviated-title chain
- myform.Sembr.SEMBR_RGXS = <RegexStore w/ split, held, and 22 more... (options=['force_reinvocations', 'lazy_load', 'autostrip_spaces'])>#
The splitter’s pattern store. Lists concatenate with an empty separator;
_abbrevand_labelare condensed (prefix-factored), case-insensitive alternations invoked as subroutines.code_spanwrites its backreferences as\g<tick>so the store’s re-invocation normalization does not rewrite them into subroutine calls.
- class myform.Sembr.Sembr#
[STATIC]Reflow rendered paragraph text to one sentence per line.- classmethod reflow(text: str) str#
Reflow rendered paragraph text to one sentence per line.
- Parameters:
text – A paragraph node’s rendered Markdown text (possibly multi-line).
- Returns:
The text with soft line breaks joined and sentence boundaries newlined.