Capture inline definitions and @ control words
The line-based capture treated any line that began with a definition command as
a definition and dropped the rest of the line, so
\makeatletter\def\@foo{bar}\makeatother\@foo lost everything after the body.
Only consume such a line when the definition actually ends it; otherwise leave
it for a new inline pass that captures simple \def\name{body} forms anywhere in
running text.
\makeatletter/\makeatother are stripped as no-ops and the macro expander now
scans @ as a letter, matching control_sequence, so \@foo expands to its body.
A test defines \@foo and \greet in one body line and checks the previewed text
"bar and Hello." with no warnings. Group scoping, catcode state and a token
stream remain open.
This commit is contained in:
1 parent
def93b6cf5
commit
7547c0abc4
3 files changed
+160
-8
No files matched your search
@@ -48,7 +48,7 @@ floats (`\floatsep`, `\textfloatsep`, `\intextsep`, `\topfraction`,
|
|||||||
`\bottomnumber`, `\totalnumber`), insertion classes (`\skip`, fraction and
|
`\bottomnumber`, `\totalnumber`), insertion classes (`\skip`, fraction and
|
||||||
absolute limits, splitting), marks, Rust output hook, multi-page PDF, and the
|
absolute limits, splitting), marks, Rust output hook, multi-page PDF, and the
|
||||||
route-D service endpoints (`/api/route-d/preview|pdf|edit`, project preview and
|
route-D service endpoints (`/api/route-d/preview|pdf|edit`, project preview and
|
||||||
PDF opt-in with `?engine=route-d`). 987 workspace tests are green and no oracle
|
PDF opt-in with `?engine=route-d`). 988 workspace tests are green and no oracle
|
||||||
fixture is ignored. Preview JSON also carries a `warnings` array naming
|
fixture is ignored. Preview JSON also carries a `warnings` array naming
|
||||||
environments route-D cannot lay out and control words that survived layout, so
|
environments route-D cannot lay out and control words that survived layout, so
|
||||||
unsupported source is reported instead of being silently typeset.
|
unsupported source is reported instead of being silently typeset.
|
||||||
|
|||||||
+7
-4
@@ -370,7 +370,7 @@ Math coverage includes fractions/roots, scripts, `\left...\right` with body-scal
|
|||||||
|
|
||||||
Paragraph units carry a `ParagraphFont`; `Unit::paragraph_with_face` and `Unit::paragraph_with_style_and_face` select Roman, bold, italic, mono, or sans. The corresponding Computer Modern TFM (`cmr10`, `cmbx10`, `cmti10`, `cmbxti10`, `cmtt10`, `cmss10`, `cmssbx10`, `cmssi10`, `cmcsc10`) supplies advances, kerns, ligatures, and italic correction for layout, the display list selects the matching face, and the PDF writes Computer Modern Type1 outlines for each built-in face (`cmr10`/`cmbx10`/`cmti10`/`cmbxti10`/`cmtt10`/`cmss10`/`cmssbx10`/`cmssi10`/`cmcsc10`, plus `cmmi10` for mathematical italic letters, `cmsy10` for math symbols and `cmex10` for big operators), falling back to Liberation subsets only for characters the Type1 encodings cannot address; those fallback glyphs keep their Unicode text under PDF extraction, so accented and Greek words round-trip; Type1 faces carry a ToUnicode CMap built from the requested characters, so glyphs whose names are ambiguous in the Adobe Glyph List (capital Omega, which would otherwise extract as U+2126) keep their intended code points. `Engine::edit` preserves the face when reflowing an edited paragraph.
|
Paragraph units carry a `ParagraphFont`; `Unit::paragraph_with_face` and `Unit::paragraph_with_style_and_face` select Roman, bold, italic, mono, or sans. The corresponding Computer Modern TFM (`cmr10`, `cmbx10`, `cmti10`, `cmbxti10`, `cmtt10`, `cmss10`, `cmssbx10`, `cmssi10`, `cmcsc10`) supplies advances, kerns, ligatures, and italic correction for layout, the display list selects the matching face, and the PDF writes Computer Modern Type1 outlines for each built-in face (`cmr10`/`cmbx10`/`cmti10`/`cmbxti10`/`cmtt10`/`cmss10`/`cmssbx10`/`cmssi10`/`cmcsc10`, plus `cmmi10` for mathematical italic letters, `cmsy10` for math symbols and `cmex10` for big operators), falling back to Liberation subsets only for characters the Type1 encodings cannot address; those fallback glyphs keep their Unicode text under PDF extraction, so accented and Greek words round-trip; Type1 faces carry a ToUnicode CMap built from the requested characters, so glyphs whose names are ambiguous in the Adobe Glyph List (capital Omega, which would otherwise extract as U+2126) keep their intended code points. `Engine::edit` preserves the face when reflowing an edited paragraph.
|
||||||
|
|
||||||
The crate currently has 437 unit tests plus ten route-D integration tests and forty-two oracle fixtures. Workspace verification: `cargo fmt --all -- --check`; `cargo test --workspace --offline` runs 987 tests (437 route-D unit, 362 engine binary unit, 10 route-D integration, 42 oracle, 71 v1 integration, 60 server, 4 engine unit, 1 core unit), with none ignored. v1 remains the live server path.
|
The crate currently has 437 unit tests plus ten route-D integration tests and forty-two oracle fixtures. Workspace verification: `cargo fmt --all -- --check`; `cargo test --workspace --offline` runs 988 tests (437 route-D unit, 363 engine binary unit, 10 route-D integration, 42 oracle, 71 v1 integration, 60 server, 4 engine unit, 1 core unit), with none ignored. v1 remains the live server path.
|
||||||
|
|
||||||
### M0 incremental demo
|
### M0 incremental demo
|
||||||
|
|
||||||
@@ -526,9 +526,12 @@ honest open items against the "feature-complete LaTeX compiler" objective are:
|
|||||||
|
|
||||||
- **Macro engine scope**: `\def`-style definitions, registers, conditionals,
|
- **Macro engine scope**: `\def`-style definitions, registers, conditionals,
|
||||||
`\csname`, `\expandafter`, delimited parameters and `\edef` are executed
|
`\csname`, `\expandafter`, delimited parameters and `\edef` are executed
|
||||||
textually, but there is no real group scoping, no catcode state (the `@`
|
textually. `@` counts as a letter in control words (so `\@foo` is one
|
||||||
letter assumption is unconditional), and no token stream. Class/package code
|
command), `\makeatletter`/`\makeatother` are consumed as no-ops, a
|
||||||
that relies on those semantics still degrades.
|
line-based capture only consumes a definition when it ends the line, and a
|
||||||
|
simple `\def\name{body}` embedded in running text is captured too. There is
|
||||||
|
still no real group scoping, no catcode state, and no token stream; class
|
||||||
|
code that relies on those semantics degrades.
|
||||||
- **Boxes**: `\sbox`/`\savebox` content and `\mbox`/`\hbox`/`\makebox`
|
- **Boxes**: `\sbox`/`\savebox` content and `\mbox`/`\hbox`/`\makebox`
|
||||||
bodies are inserted as one unbreakable inline box (interword spaces become
|
bodies are inserted as one unbreakable inline box (interword spaces become
|
||||||
fixed-width boxes), so `\usebox` no longer reflows across lines. Savebox
|
fixed-width boxes), so `\usebox` no longer reflows across lines. Savebox
|
||||||
|
|||||||
+152
-3
@@ -763,7 +763,7 @@ fn expand_links_line(line: &str) -> String {
|
|||||||
fn expand_macro_at(text: &str, macros: &[MacroDefinition]) -> Option<(String, usize)> {
|
fn expand_macro_at(text: &str, macros: &[MacroDefinition]) -> Option<(String, usize)> {
|
||||||
let after_slash = text.strip_prefix('\\')?;
|
let after_slash = text.strip_prefix('\\')?;
|
||||||
let length = after_slash
|
let length = after_slash
|
||||||
.find(|character: char| !character.is_ascii_alphabetic())
|
.find(|character: char| !character.is_ascii_alphabetic() && character != '@')
|
||||||
.unwrap_or(after_slash.len());
|
.unwrap_or(after_slash.len());
|
||||||
if length == 0 {
|
if length == 0 {
|
||||||
return None;
|
return None;
|
||||||
@@ -2477,7 +2477,11 @@ fn strip_page_geometry(source: &str) -> (String, PageGeometry) {
|
|||||||
geometry.head_rule_width = Some(width);
|
geometry.head_rule_width = Some(width);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if let Some(definition) = macro_definition(definition_line) {
|
// Only consume the line when the definition ends it; otherwise
|
||||||
|
// inline capture handles a definition embedded in running text.
|
||||||
|
if let Some(definition) = macro_definition(definition_line)
|
||||||
|
&& definition_consumes_line(definition_line)
|
||||||
|
{
|
||||||
let provided = definition_line.trim_start().starts_with("\\providecommand");
|
let provided = definition_line.trim_start().starts_with("\\providecommand");
|
||||||
if !provided
|
if !provided
|
||||||
|| !geometry
|
|| !geometry
|
||||||
@@ -2487,9 +2491,9 @@ fn strip_page_geometry(source: &str) -> (String, PageGeometry) {
|
|||||||
{
|
{
|
||||||
geometry.macros.push(definition);
|
geometry.macros.push(definition);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
if trimmed.starts_with(r"\bibliographystyle") || trimmed.starts_with(r"\bibliography{") {
|
if trimmed.starts_with(r"\bibliographystyle") || trimmed.starts_with(r"\bibliography{") {
|
||||||
let rest = trimmed
|
let rest = trimmed
|
||||||
.strip_prefix(r"\bibliographystyle")
|
.strip_prefix(r"\bibliographystyle")
|
||||||
@@ -4644,6 +4648,122 @@ fn capture_body_registers(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Capture simple `\def\name{body}` definitions that appear in running body
|
||||||
|
/// text (not at the start of a line). Parameterised forms stay with the
|
||||||
|
/// line-based capture until the token engine models grouping.
|
||||||
|
fn capture_inline_definitions(body: &mut String, macros: &mut Vec<MacroDefinition>) {
|
||||||
|
let mut out = String::new();
|
||||||
|
let mut rest = body.as_str();
|
||||||
|
let mut changed = false;
|
||||||
|
while let Some(position) = rest.find("\\def") {
|
||||||
|
let after = &rest[position + "\\def".len()..];
|
||||||
|
let boundary = after
|
||||||
|
.chars()
|
||||||
|
.next()
|
||||||
|
.is_none_or(|character| !character.is_ascii_alphabetic());
|
||||||
|
if !boundary {
|
||||||
|
out.push_str(&rest[..position + "\\def".len()]);
|
||||||
|
rest = after;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
let trimmed = after.trim_start();
|
||||||
|
let Some((name, used)) = control_sequence(trimmed) else {
|
||||||
|
out.push_str(&rest[..position + "\\def".len()]);
|
||||||
|
rest = after;
|
||||||
|
continue;
|
||||||
|
};
|
||||||
|
let after_name = trimmed[used..].trim_start();
|
||||||
|
if !after_name.starts_with('{') {
|
||||||
|
out.push_str(&rest[..position + "\\def".len()]);
|
||||||
|
rest = after;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
let Some((content, body_used)) = braced_group(after_name) else {
|
||||||
|
out.push_str(&rest[..position + "\\def".len()]);
|
||||||
|
rest = after;
|
||||||
|
continue;
|
||||||
|
};
|
||||||
|
out.push_str(&rest[..position]);
|
||||||
|
let name = format!("\\{}", name.trim_start_matches('\\'));
|
||||||
|
if let Some(existing) = macros.iter_mut().find(|m| m.name == name) {
|
||||||
|
existing.body = content.to_string();
|
||||||
|
existing.params = None;
|
||||||
|
existing.args = 0;
|
||||||
|
} else {
|
||||||
|
macros.push(MacroDefinition {
|
||||||
|
name,
|
||||||
|
args: 0,
|
||||||
|
default: None,
|
||||||
|
params: None,
|
||||||
|
body: content.to_string(),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
rest = &after_name[body_used..];
|
||||||
|
changed = true;
|
||||||
|
}
|
||||||
|
out.push_str(rest);
|
||||||
|
if changed {
|
||||||
|
*body = out;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Whether a definition command consumes its whole line (ignoring trailing
|
||||||
|
/// whitespace). Embedded definitions are left for the inline pass.
|
||||||
|
fn definition_consumes_line(line: &str) -> bool {
|
||||||
|
let line = line.trim();
|
||||||
|
let Some((command, used)) = control_sequence(line) else {
|
||||||
|
return false;
|
||||||
|
};
|
||||||
|
let rest = &line[used..];
|
||||||
|
match command.as_str() {
|
||||||
|
"\\def" | "\\gdef" | "\\edef" | "\\xdef" => {
|
||||||
|
let trimmed = rest.trim_start();
|
||||||
|
let Some((_name, name_used)) = control_sequence(trimmed) else {
|
||||||
|
return false;
|
||||||
|
};
|
||||||
|
let after_name = &trimmed[name_used..];
|
||||||
|
let Some(brace) = after_name.find('{') else {
|
||||||
|
return false;
|
||||||
|
};
|
||||||
|
let Some((_body, body_used)) = braced_group(&after_name[brace..]) else {
|
||||||
|
return false;
|
||||||
|
};
|
||||||
|
after_name[brace + body_used..].trim().is_empty()
|
||||||
|
}
|
||||||
|
"\\newcommand" | "\\renewcommand" | "\\DeclareRobustCommand" | "\\providecommand" => {
|
||||||
|
let mut rest = rest.trim_start();
|
||||||
|
if let Some(stripped) = rest.strip_prefix('*') {
|
||||||
|
rest = stripped.trim_start();
|
||||||
|
}
|
||||||
|
let after_name = if let Some(stripped) = rest.strip_prefix('{') {
|
||||||
|
let Some(close) = stripped.find('}') else {
|
||||||
|
return false;
|
||||||
|
};
|
||||||
|
&stripped[close + 1..]
|
||||||
|
} else {
|
||||||
|
let Some((_name, name_used)) = control_sequence(rest) else {
|
||||||
|
return false;
|
||||||
|
};
|
||||||
|
&rest[name_used..]
|
||||||
|
};
|
||||||
|
let mut rest = after_name.trim_start();
|
||||||
|
for _ in 0..2 {
|
||||||
|
if let Some(stripped) = rest.strip_prefix('[') {
|
||||||
|
let Some((_option, tail)) = stripped.split_once(']') else {
|
||||||
|
return false;
|
||||||
|
};
|
||||||
|
rest = tail.trim_start();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
let Some((_body, body_used)) = braced_group(rest) else {
|
||||||
|
return false;
|
||||||
|
};
|
||||||
|
rest[body_used..].trim().is_empty()
|
||||||
|
}
|
||||||
|
_ => false,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn capture_body_definitions(
|
fn capture_body_definitions(
|
||||||
body: &mut String,
|
body: &mut String,
|
||||||
macros: &mut Vec<MacroDefinition>,
|
macros: &mut Vec<MacroDefinition>,
|
||||||
@@ -4918,6 +5038,10 @@ fn geometry_and_body(source: &str) -> (String, PageGeometry) {
|
|||||||
&mut geometry.theorem_definitions,
|
&mut geometry.theorem_definitions,
|
||||||
&mut geometry.environments,
|
&mut geometry.environments,
|
||||||
);
|
);
|
||||||
|
capture_inline_definitions(&mut body, &mut geometry.macros);
|
||||||
|
body = body
|
||||||
|
.replace("\\makeatletter", "")
|
||||||
|
.replace("\\makeatother", "");
|
||||||
body = expand_environments(&body, &geometry.environments);
|
body = expand_environments(&body, &geometry.environments);
|
||||||
body = expand_keywords(&body);
|
body = expand_keywords(&body);
|
||||||
body = drop_bibliography_commands(&body);
|
body = drop_bibliography_commands(&body);
|
||||||
@@ -14668,6 +14792,31 @@ A definition.
|
|||||||
assert!(!text.contains('?'), "{text}");
|
assert!(!text.contains('?'), "{text}");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn inline_definitions_and_makeatletter_expand() {
|
||||||
|
let source = "\\documentclass{article}\n\\begin{document}\n\n\\makeatletter\\def\\@foo{bar}\\makeatother\\@foo and \\def\\greet{Hello}\\greet.\n\n\\end{document}\n";
|
||||||
|
let json = preview_json(source).expect("preview");
|
||||||
|
let value: serde_json::Value = serde_json::from_str(&json).expect("JSON");
|
||||||
|
assert_eq!(value["status"], "ok");
|
||||||
|
assert_eq!(
|
||||||
|
value["warnings"].as_array().map(Vec::len),
|
||||||
|
Some(0),
|
||||||
|
"{}",
|
||||||
|
value["warnings"]
|
||||||
|
);
|
||||||
|
let svg = value["pages"][0]["svg"].as_str().expect("svg");
|
||||||
|
let text: String = svg
|
||||||
|
.split('>')
|
||||||
|
.filter_map(|piece| piece.split('<').next())
|
||||||
|
.collect::<String>()
|
||||||
|
.split_whitespace()
|
||||||
|
.collect();
|
||||||
|
assert!(text.contains("bar"), "{text}");
|
||||||
|
assert!(text.contains("Hello"), "{text}");
|
||||||
|
assert!(!text.contains("def"), "{text}");
|
||||||
|
assert!(!text.contains("makeatletter"), "{text}");
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn nested_tables_keep_the_outer_structure() {
|
fn nested_tables_keep_the_outer_structure() {
|
||||||
let source = r"\begin{tabular}{ll}
|
let source = r"\begin{tabular}{ll}
|
||||||
|
|||||||
Reference in new issue
Block a user