Render inline verb spans in the mono face
This commit is contained in:
1 parent
2868a8f0d3
commit
5b160f9780
4 files changed
+49
-4
No files matched your search
@@ -3468,6 +3468,22 @@ fn inline_wrapper(rest: &str, base: ParagraphFont) -> Option<(ParagraphFont, &st
|
||||
None
|
||||
}
|
||||
|
||||
fn verb_run(rest: &str) -> Option<(String, usize)> {
|
||||
let after = rest.strip_prefix("\\verb")?;
|
||||
let after = after.strip_prefix('*').unwrap_or(after);
|
||||
let (start, delimiter) = after.char_indices().next()?;
|
||||
if delimiter.is_ascii_alphanumeric() || delimiter.is_whitespace() || delimiter == '\\' {
|
||||
return None;
|
||||
}
|
||||
let body_start = start + delimiter.len_utf8();
|
||||
let end = after[body_start..].find(delimiter)?;
|
||||
let prefix = rest.len() - after.len();
|
||||
Some((
|
||||
after[body_start..body_start + end].to_string(),
|
||||
prefix + body_start + end + delimiter.len_utf8(),
|
||||
))
|
||||
}
|
||||
|
||||
fn parse_runs(
|
||||
text: &str,
|
||||
font: ParagraphFont,
|
||||
@@ -3478,6 +3494,12 @@ fn parse_runs(
|
||||
let mut index = 0;
|
||||
while index < text.len() {
|
||||
let rest = &text[index..];
|
||||
if let Some((body, length)) = verb_run(rest) {
|
||||
*found = true;
|
||||
push_run(runs, ParagraphFont::Mono, size, &body);
|
||||
index += length;
|
||||
continue;
|
||||
}
|
||||
if let Some((declared, declared_size, body)) = nfss_font_with_size(rest, font) {
|
||||
*found = true;
|
||||
parse_runs(&body, declared, declared_size.unwrap_or(size), runs, found);
|
||||
@@ -6183,6 +6205,14 @@ A definition.
|
||||
assert_eq!(document.units[0].source, "Value $x^2$ tail.");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn inline_verb_paints_typewriter_text() {
|
||||
let json = preview_json("A \\verb|x&y| b.").expect("preview");
|
||||
assert!(json.contains(">x</text>"), "verb body is painted");
|
||||
assert!(json.contains("monospace"), "verb body uses the mono face");
|
||||
assert!(!json.contains("verb|"), "verb syntax is consumed");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn inline_math_paints_italic_glyphs() {
|
||||
let json = preview_json("Value $x^2$ tail.").expect("preview");
|
||||
@@ -6863,6 +6893,21 @@ mod nfss_tests {
|
||||
mod inline_tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn verb_spans_render_monospaced() {
|
||||
let runs = inline_runs("a \\verb|x&y| b", ParagraphFont::Roman).expect("runs");
|
||||
assert_eq!(
|
||||
runs,
|
||||
vec![
|
||||
(ParagraphFont::Roman, "a ".to_string()),
|
||||
(ParagraphFont::Mono, "x&y".to_string()),
|
||||
(ParagraphFont::Roman, " b".to_string()),
|
||||
]
|
||||
);
|
||||
let star = inline_runs("\\verb*!a_b!", ParagraphFont::Roman).expect("runs");
|
||||
assert_eq!(star, vec![(ParagraphFont::Mono, "a_b".to_string())]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn inline_commands_split_runs() {
|
||||
let runs = inline_runs("Plain \\textbf{bold} tail", ParagraphFont::Roman).expect("runs");
|
||||
|
||||
Reference in new issue
Block a user