Expand href text and url typewriter text

This commit is contained in:
gloamxun committed 2026-10-24 22:42:17 +08:00
1 parent 8610a6c016
commit 874a327dca
5 files changed
+119 -5

No files matched your search

+89 -1
View File
@@ -390,6 +390,77 @@ fn macro_arguments(text: &str, count: u8) -> Option<(Vec<String>, usize)> {
}
/// Expand user macros in a single pass, repeating until nothing changes.
fn expand_links(source: &str) -> String {
let mut output = Vec::new();
let mut verbatim = false;
for line in source.lines() {
let trimmed = line.trim();
if trimmed.starts_with("\\begin{verbatim}") {
verbatim = true;
output.push(line.to_string());
continue;
}
if trimmed.starts_with("\\end{verbatim}") {
verbatim = false;
output.push(line.to_string());
continue;
}
if verbatim {
output.push(line.to_string());
continue;
}
output.push(expand_links_line(line));
}
output.join("\n")
}
fn expand_links_line(line: &str) -> String {
let mut output = String::new();
let mut rest = line;
loop {
let href = rest.find("\\href");
let url = rest.find("\\url");
let choice = match (href, url) {
(Some(left), Some(right)) if left <= right => Some((left, "\\href")),
(Some(left), None) => Some((left, "\\href")),
(_, Some(right)) => Some((right, "\\url")),
_ => None,
};
let Some((position, command)) = choice else {
output.push_str(rest);
break;
};
output.push_str(&rest[..position]);
let after = &rest[position + command.len()..];
if command == "\\href" {
let Some((_url, used)) = braced_group(after) else {
output.push_str(command);
rest = after;
continue;
};
let remainder = &after[used..];
let Some((text, used_text)) = braced_group(remainder) else {
output.push_str(command);
rest = after;
continue;
};
output.push_str(&text);
rest = &remainder[used_text..];
} else {
let Some((target, used)) = braced_group(after) else {
output.push_str(command);
rest = after;
continue;
};
output.push_str("\\texttt{");
output.push_str(&target);
output.push('}');
rest = &after[used..];
}
}
output
}
fn expand_macros(source: &str, macros: &[MacroDefinition]) -> String {
if macros.is_empty() {
return source.to_string();
@@ -1098,6 +1169,7 @@ fn geometry_and_body(source: &str) -> (String, PageGeometry) {
geometry.bibliography = extract_bibliography(&mut body);
body = expand_environments(&body, &geometry.environments);
body = expand_macros(&body, &geometry.macros);
body = expand_links(&body);
body = expand_conditionals(&body, &mut geometry.conditionals);
(body, geometry)
}
@@ -4614,6 +4686,23 @@ See \ref{fig:a} and \ref{tab:b}.";
assert_eq!(document.units[2].source, "See 1 and 1.");
}
#[test]
fn links_keep_their_text_and_style_urls() {
let (body, _) = geometry_and_body(
"See \\href{https://example.com}{Example} and \\url{https://x.test/a_b}.",
);
assert!(
body.contains("See Example and \\texttt{https://x.test/a_b}."),
"{body}"
);
}
#[test]
fn verbatim_links_stay_literal() {
let (body, _) = geometry_and_body("\\begin{verbatim}\n\\url{https://x}\n\\end{verbatim}");
assert!(body.contains("\\url{https://x}"), "{body}");
}
#[test]
fn verbatim_lines_stay_monospaced() {
let source = "\\begin{verbatim}\nlet x = 1;\nreturn x;\n\\end{verbatim}";
@@ -4765,7 +4854,6 @@ See \ref{fig:a} and \ref{tab:b}.";
assert!(body.contains("(x,y)"));
}
#[test]
#[test]
fn def_definitions_expand_in_the_body() {
let (body, geometry) = geometry_and_body("\\def\\half{1/2}\n\nA \\half here.");