<commit_before>const VALID_TAGS: [&'static str; 9] = ["*", "_", "**", "__", "***", "~~", "`", "``", "```"];

pub fn parse(escaped: &str) -> String {
  let mut symbols = 0;
  let mut skip = 0;
  let mut in_tag = false;

  let mut result = String::new();
  let mut buffer = String::new();

  let owned = escaped.to_owned();
  let mut iter = owned.chars().peekable();
  while let Some(c) = iter.next() {
    if skip > 0 {
      skip -= 1;
      continue;
    }
    match c {
      '*' | '_' | '`' => {
        if symbols == 0 {
          result += &buffer;
          buffer = String::new();
        }
        symbols += 1;
      },
      '<' => in_tag = true,
      '>' => in_tag = false,
      _ => {}
    }
    buffer += &c.to_string();
    if in_tag {
      continue;
    }
    if let Some(&next) = iter.peek() {
      if next != c && symbols > 0 && VALID_TAGS.contains(&buffer.as_ref()) {
        let s = iter.clone().collect::<String>();
        if let Some(m) = s.find(&buffer) {
          let mut content = &s[..m];
          skip = content.chars().count() + buffer.len();
          let (tag, styles) = match buffer.as_ref() {
            "_" | "*" => ("span", Some("emphasis")),
            "__" => ("span", Some("underline")),
            "**" => ("span", Some("strong")),
            "***" => ("span", Some("strong emphasis")),
            "~~" => ("span", Some("strikethrough")),
            "`" | "``" => ("span", Some("code")),
            "```" => {
              content = content.trim_matches('\n');
              ("pre", None)
            },
            _ => unreachable!()
          };
          let is_code = if let Some("code") = styles { false } else { true };
          let class = styles.map(|x| format!(" class=\"{}\"", x)).unwrap_or_else(Default::default);
          result += &format!("<{tag}{class}>{content}</{tag}>",
            tag = tag,
            class = class,
            content = if is_code { parse(content) } else { content.to_owned() });
          buffer = String::new();
          symbols = 0;
        }
      }
    }
  }
  result += &buffer;
  result
}

#[cfg(test)]
mod test {
  extern crate test;

  const TEST_STRING: &'static str = "blah __underline__ blah **bold** and ***bold italic*** with __***underline bold italics***__";
  const TEST_EXPECTED: &'static str = "blah <span class=\"underline\">underline</span> blah <span class=\"strong\">bold</span> and <span class=\"strong emphasis\">bold italic</span> with <span class=\"underline\"><span class=\"strong emphasis\">underline bold italics</span></span>";

  #[test]
  fn parse() {
    assert_eq!(TEST_EXPECTED, super::parse(TEST_STRING));<commit_msg>refactor(archive_viewer): use more efficient methods<commit_after>const VALID_TAGS: [&'static str; 9] = ["*", "_", "**", "__", "***", "~~", "`", "``", "```"];

pub fn parse(escaped: &str) -> String {
  let mut symbols = 0;
  let mut skip = 0;
  let mut in_tag = false;

  let mut result = String::new();
  let mut buffer = String::new();

  let owned = escaped.to_owned();
  let mut iter = owned.chars().peekable();
  while let Some(c) = iter.next() {
    if skip > 0 {
      skip -= 1;
      continue;
    }
    match c {
      '*' | '_' | '`' => {
        if symbols == 0 {
          result.push_str(&buffer);
          buffer.clear();
        }
        symbols += 1;
      },
      '<' => in_tag = true,
      '>' => in_tag = false,
      _ => {}
    }
    buffer.push(c);
    if in_tag {
      continue;
    }
    if let Some(&next) = iter.peek() {
      if next != c && symbols > 0 && VALID_TAGS.contains(&buffer.as_ref()) {
        let s = iter.clone().collect::<String>();
        if let Some(m) = s.find(&buffer) {
          let mut content = &s[..m];
          skip = content.chars().count() + buffer.len();
          let (tag, styles) = match buffer.as_ref() {
            "_" | "*" => ("span", Some("emphasis")),
            "__" => ("span", Some("underline")),
            "**" => ("span", Some("strong")),
            "***" => ("span", Some("strong emphasis")),
            "~~" => ("span", Some("strikethrough")),
            "`" | "``" => ("span", Some("code")),
            "```" => {
              content = content.trim_matches('\n');
              ("pre", None)
            },
            _ => unreachable!()
          };
          let is_code = if let Some("code") = styles { false } else { true };
          let class = styles.map(|x| format!(" class=\"{}\"", x)).unwrap_or_else(Default::default);
          result += &format!("<{tag}{class}>{content}</{tag}>",
            tag = tag,
            class = class,
            content = if is_code { parse(content) } else { content.to_owned() });
          buffer.clear();
          symbols = 0;
        }
      }
    }
  }
  result += &buffer;
  result
}

#[cfg(test)]
mod test {
  extern crate test;

  const TEST_STRING: &'static str = "blah __underline__ blah **bold** and ***bold italic*** with __***underline bold italics***__";
  const TEST_EXPECTED: &'static str = "blah <span class=\"underline\">underline</span> blah <span class=\"strong\">bold</span> and <span class=\"strong emphasis\">bold italic</span> with <span class=\"underline\"><span class=\"strong emphasis\">underline bold italics</span></span>";

  #[test]
  fn parse() {
    assert_eq!(TEST_EXPECTED, super::parse(TEST_STRING));
{'fraction_non_alphanumeric': 0.2099624060150376, 'fraction_numerical': 0.0033834586466165413, 'mean_word_length': 2.22876213592233, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 61, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '499561', 'n_tokens_mistral': 1706, 'n_tokens_neox': 1695, 'n_words': 391}