MCPcopy Create free account
hub / github.com/daisy/MathCAT / make_leaf_element

Function make_leaf_element

src/interface.rs:457–520  ·  view source on GitHub ↗
(mathml_leaf: Element)

Source from the content-addressed store, hash-verified

455 }
456
457 fn make_leaf_element(mathml_leaf: Element) {
458 // MathML leaves like <mn> really shouldn't have non-textual content, but you could have embedded HTML
459 // Here, we take convert them to leaves by grabbing up all the text and making that the content
460 // Potentially, we leave them and let (default) rules do something, but it makes other parts of the code
461 // messier because checking the text of a leaf becomes Option<&str> rather than just &str
462 let children = mathml_leaf.children();
463 if children.is_empty() {
464 return;
465 }
466
467 // gather up the text
468 let mut text ="".to_string();
469 let mut previous_element_was_text = false;
470 for child in children {
471 let (child_text, space) = match child {
472 ChildOfElement::Element(child) => {
473 previous_element_was_text = false;
474 if name(&child) == "mglyph" {
475 (child.attribute_value("alt").unwrap_or("").to_string(), " ")
476 } else {
477 (gather_text(child), " ")
478 }
479 },
480 ChildOfElement::Text(t) => {
481 let t_text = t.text().trim_matches(WHITESPACE);
482 if t_text.is_empty() {
483 ("".to_string(), "")
484 } else {
485 let space = !previous_element_was_text;
486 previous_element_was_text = true;
487 (t_text.to_string(), if space {" "} else {""})
488 }
489 },
490 _ => ("".to_string(), ""),
491 };
492 if !child_text.is_empty() {
493 if !text.is_empty() {
494 text += space;
495 }
496 text += child_text.trim_matches(WHITESPACE);
497 }
498
499 }
500
501 // get rid of the old children and replace with the text we just built
502 mathml_leaf.clear_children();
503
504 mathml_leaf.set_text(&text);
505
506 /// gather up all the contents of the element and return them with a leading space
507 fn gather_text(html: Element) -> String {
508 let mut text = "".to_string(); // since we are throwing out the element tag, add a space between the contents
509 for child in html.children() {
510 match child {
511 ChildOfElement::Element(child) => {
512 text = text + " " + gather_text(child).trim_matches(WHITESPACE);
513 },
514 ChildOfElement::Text(t) => text += t.text(),

Callers 1

trim_elementFunction · 0.85

Calls 3

nameFunction · 0.85
gather_textFunction · 0.85
is_emptyMethod · 0.80

Tested by

no test coverage detected