<commit_before>## Exemel -- Object Oriented XML Library

## Exemel::CDATA - represents a CDATA section.
#  Data is preserved "as is ", right from the [ to the ]]>
class Exemel::CDATA {
  has $.data;
  method Str() {
    return '<![CDATA[' ~ $.data ~ ']]>';
  }
}

## Exemel::Comment - represents a comment.
#  Data is preserved "as is", right from the <!-- to the -->
class Exemel::Comment {
  has $.data;
  method Str() {
    return '<!--' ~ $.data ~ '-->';
  }
}

## Exemel::PI - represents a PI section.
#  Data is preserved "as is", right from the <? to the ?>
class Exemel::PI {
  has $.data;
  method Str() {
    return '<?' ~ $.data ~ '?>';
  }
}

## Exemel::Text - represents a text node.
#  The original text is stored in the 'text' attribute, and is
#  preserved in its original format, including whitespace.
#  The default stringification removes extra whitespace, and chomps
#  the string. If this is not what you expect, call .text directly.
class Exemel::Text {
  has $.text;
  method Str() {
    my $text = $.text;
    $text ~~ s:g/\s+/ /;  ## Relace multiple whitespace with a single space.
    $text.=chomp;         ## Remove a trailing newline if it exists.
    return $text;
  }
  method string() {
    my $text = self.Str();
    $text ~~ s:g/\s+$//;  ## Chop out trailing spaces.
    return $text;
  }

}

class Exemel::Element {
  use Exemel::Grammar;
  has $.name is rw;    ## We may want to change element type.
  has @.nodes is rw;   ## Cloning requires rw.
  has %.attribs is rw; ## Cloning requires rw.

  method deep-clone() {
    my $clone = self.clone;
    $clone.attribs = $clone.attribs.clone;
    $clone.nodes = $clone.nodes.clone;
    loop (my $i=0; $i < $clone.nodes.elems; $i++) {
      if ($clone.nodes[$i] ~~ Exemel::Element) {
        $clone.nodes[$i] = $clone.nodes[$i].deep-clone;
      }
      else {
        $clone.nodes[$i] = $.nodes[$i].clone;<commit_msg>Changed how Exemel::Text nodes handle string() method.<commit_after>## Exemel -- Object Oriented XML Library

## Exemel::CDATA - represents a CDATA section.
#  Data is preserved "as is ", right from the [ to the ]]>
class Exemel::CDATA {
  has $.data;
  method Str() {
    return '<![CDATA[' ~ $.data ~ ']]>';
  }
}

## Exemel::Comment - represents a comment.
#  Data is preserved "as is", right from the <!-- to the -->
class Exemel::Comment {
  has $.data;
  method Str() {
    return '<!--' ~ $.data ~ '-->';
  }
}

## Exemel::PI - represents a PI section.
#  Data is preserved "as is", right from the <? to the ?>
class Exemel::PI {
  has $.data;
  method Str() {
    return '<?' ~ $.data ~ '?>';
  }
}

## Exemel::Text - represents a text node.
#  The original text is stored in the 'text' attribute, and is
#  preserved in its original format, including whitespace.
#  The default stringification removes extra whitespace, and chomps
#  the string. If this is not what you expect, call .text directly.
class Exemel::Text {
  has $.text;
  method Str(:$strip) {
    my $text = $.text;
    $text ~~ s:g/\s+/ /;  ## Relace multiple whitespace with a single space.
    if $strip {
        $text ~~ s:g/\s+$//;  ## Chop out trailing spaces.
        $text ~~ s:g/^\s+//;  ## Chop out leading spaces.
    }
    $text.=chomp;         ## Remove a trailing newline if it exists.
    return $text;
  }
  method string() {
    return self.Str(:strip);
  }
}

class Exemel::Element {
  use Exemel::Grammar;
  has $.name is rw;    ## We may want to change element type.
  has @.nodes is rw;   ## Cloning requires rw.
  has %.attribs is rw; ## Cloning requires rw.

  method deep-clone() {
    my $clone = self.clone;
    $clone.attribs = $clone.attribs.clone;
    $clone.nodes = $clone.nodes.clone;
    loop (my $i=0; $i < $clone.nodes.elems; $i++) {
      if ($clone.nodes[$i] ~~ Exemel::Element) {
        $clone.nodes[$i] = $clone.nodes[$i].deep-clone;
      }
      else {
        $clone.nodes[$i] = $.nodes[$i].clone;
{'fraction_non_alphanumeric': 0.16687963199591108, 'fraction_numerical': 0.0005111167901865576, 'mean_word_length': 2.7029328287606433, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, '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': '7168547', 'n_tokens_mistral': 1354, 'n_tokens_neox': 1343, 'n_words': 432}