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

Function add_ids

src/interface.rs:358–389  ·  view source on GitHub ↗
(mathml: Element)

Source from the content-addressed store, hash-verified

356
357
358fn add_ids(mathml: Element) -> Element {
359 use std::time::SystemTime;
360 let time = if cfg!(target_family = "wasm") {
361 rand::random::<usize>()
362 } else {
363 SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).unwrap().as_millis() as usize
364 };
365 let time_part = radix_fmt::radix(time, 36).to_string();
366 let random_part = radix_fmt::radix(rand::random::<usize>(), 36).to_string();
367 let prefix = "M".to_string() + &time_part[time_part.len()-3..] + &random_part[random_part.len()-4..] + "-"; // begin with letter
368 add_ids_to_all(mathml, &prefix, 0);
369 return mathml;
370
371 fn add_ids_to_all(mathml: Element, id_prefix: &str, count: usize) -> usize {
372 let mut count = count;
373 if mathml.attribute("id").is_none() {
374 mathml.set_attribute_value("id", (id_prefix.to_string() + &count.to_string()).as_str());
375 mathml.set_attribute_value("data-id-added", "true");
376 count += 1;
377 };
378
379 if crate::xpath_functions::is_leaf(mathml) {
380 return count;
381 }
382
383 for child in mathml.children() {
384 let child = as_element(child);
385 count = add_ids_to_all(child, id_prefix, count);
386 }
387 return count;
388 }
389}
390
391pub fn get_element(package: &Package) -> Element {
392 let doc = package.as_document();

Callers 1

cleanup_mathmlFunction · 0.85

Calls 2

add_ids_to_allFunction · 0.85
lenMethod · 0.80

Tested by

no test coverage detected