<commit_before>use v6.c;

#BEGIN {
#  @*INC.unshift('/home/marcel/Languages/Perl6/Projects/Semi-xml/lib');
#}

use Semi-xml;
use XML;

# Package cannot be placed in Semi-xml/Lib and named File.pm6. Evaluation seems
# to fail not finding the methods when doing so (perl6 2015-04).
#
package SxmlLib:auth<https://github.com/MARTIMM> {

  class Html::List {
    has Hash $.symbols = {};

    has Str $!directory;
    has Int @!header;
    has Str $!ref-attr;

    constant C-MAX-LEVEL = 6;
    my Int $level = 0;
    my Bool $first_level = True;
    my Hash $top-level-attrs;

    #---------------------------------------------------------------------------
    #
    method dir-list ( XML::Element $parent,
                      Hash $attrs is copy,
                      XML::Node :$content-body   # Ignored
                    ) {
      $!directory = $attrs<directory>:delete // '.';
      $!ref-attr = $attrs<ref-attr>:delete // 'href';

      # Set the numbers into the array and fill it up using the last number
      # to the maximum length of C-MAX-LEVEL. So if arrey = [2,3] then
      # result will be [2,3,3,3,3,3,3]
      #
      my @list = map {.Int}, ($attrs<header>:delete).split(',');
      @!header.push: |@list;
      @!header.push: |(@list[@list.end] xx (C-MAX-LEVEL - +@list));

      $top-level-attrs = $attrs;
      self!create-list( $parent, @($!directory));
    }

    #---------------------------------------------------------------------------
    #
    method !create-list ( XML::Element $parent, @files-to-process is copy ) {

      while @files-to-process.shift() -> $file {

        # Process directories
        #
        if $file.IO ~~ :d {

          my $dir := $file;                   # Alias to proper name

          # Skip hidden directories
          #
          next if $dir ~~ m/^ '.' / or $dir ~~ m/ '/.' /;

          if !$first_level {
            $level++;
          }

          else {
            $first_level = False;
          }

          my $ul = self!make-dir-entry( $parent, $dir);

          my @new-files = dir( $dir, :Str);
          self!create-list( $ul, @(sort @new-files));

          $level--;
          if $level < 0 {
            $level = 0;
            $first_level = True;
          }
        }

        # Process plain files
        #
        elsif $file.IO ~~ :f {
          self!make-entry( $parent, $file);
        }

        # Ignore other type of files
        #
        else {
          say "File $file is ignored, it is a special type of file";
        }
      }
    }

    #---------------------------------------------------------------------------
    #
    method !make-dir-entry ( XML::Element $parent, Str $dir-label is copy
                             --> XML::Element
                           ) {
      my $ul = XML::Element.new( :name('ul'), :attribs($top-level-attrs));
      $top-level-attrs = %();

      $parent.append($ul);
      my $li = XML::Element.new(:name('li'));
      $ul.append($li);
      my $hdr = XML::Element.new(:name('h' ~ @!header[$level]));
      $li.append($hdr);

      $dir-label ~~ s:g/<-[\/]>+\///;
      $hdr.append(XML::Text.new(:text($dir-label)));

      return $ul;
    }

    #---------------------------------------------------------------------------
    #
    method !make-entry ( XML::Element $parent, Str $reference ) {
      my $li = XML::Element.new(:name('li'));
      $parent.append($li);

      my Hash $attrs = {};
      if $!ref-attr ne 'href' {
        $attrs<href> = '';
        $attrs{$!ref-attr} = $reference;
      }

      else {
        $attrs<href> = $reference;
      }

      my $a = XML::Element.new( :name('a'), :attribs($attrs));
      $li.append($a);

      # Cleanup the reference in such a way that it will become a nice label
      # The filename can have prefixed numbers so the sorting will automatically
      # have the proper sequence to show. The number will be removed from the<commit_msg>modified lexicals into atributes<commit_after>use v6.c;

#BEGIN {
#  @*INC.unshift('/home/marcel/Languages/Perl6/Projects/Semi-xml/lib');
#}

use Semi-xml;
use XML;

# Package cannot be placed in Semi-xml/Lib and named File.pm6. Evaluation seems
# to fail not finding the methods when doing so (perl6 2015-04).
#
package SxmlLib:auth<https://github.com/MARTIMM> {

  class Html::List {
    has Hash $.symbols = {};

    has Str $!directory;
    has Int @!header;
    has Str $!ref-attr;

    constant C-MAX-LEVEL = 6;
    has Int $!level = 0;
    has Bool $!first_level = True;
    has Hash $!top-level-attrs;

    #---------------------------------------------------------------------------
    #
    method dir-list ( XML::Element $parent,
                      Hash $attrs is copy,
                      XML::Node :$content-body   # Ignored
                    ) {
      $!directory = $attrs<directory>:delete // '.';
      $!ref-attr = $attrs<ref-attr>:delete // 'href';

      # Set the numbers into the array and fill it up using the last number
      # to the maximum length of C-MAX-LEVEL. So if arrey = [2,3] then
      # result will be [2,3,3,3,3,3,3]
      #
      my @list = map {.Int}, ($attrs<header>:delete).split(',');
      @!header.push: |@list;
      @!header.push: |(@list[@list.end] xx (C-MAX-LEVEL - +@list));

      $!top-level-attrs = $attrs;
      self!create-list( $parent, @($!directory));
    }

    #---------------------------------------------------------------------------
    #
    method !create-list ( XML::Element $parent, @files-to-process is copy ) {

      while @files-to-process.shift() -> $file {

        # Process directories
        #
        if $file.IO ~~ :d {

          my $dir := $file;                   # Alias to proper name

          # Skip hidden directories
          #
          next if $dir ~~ m/^ '.' / or $dir ~~ m/ '/.' /;

          if not $!first_level {
            $!level++;
          }

          else {
            $!first_level = False;
          }

          my $ul = self!make-dir-entry( $parent, $dir);

          my @new-files = dir( $dir, :Str);
          self!create-list( $ul, @(sort @new-files));

          $!level--;
          if $!level < 0 {
            $!level = 0;
            $!first_level = True;
          }
        }

        # Process plain files
        #
        elsif $file.IO ~~ :f {
          self!make-entry( $parent, $file);
        }

        # Ignore other type of files
        #
        else {
          say "File $file is ignored, it is a special type of file";
        }
      }
    }

    #---------------------------------------------------------------------------
    #
    method !make-dir-entry ( XML::Element $parent, Str $dir-label is copy
                             --> XML::Element
                           ) {
      my $ul = XML::Element.new( :name('ul'), :attribs($!top-level-attrs));
      $!top-level-attrs = %();

      $parent.append($ul);
      my $li = XML::Element.new(:name('li'));
      $ul.append($li);
      my $hdr = XML::Element.new(:name('h' ~ @!header[$!level]));
      $li.append($hdr);

      $dir-label ~~ s:g/<-[\/]>+\///;
      $hdr.append(XML::Text.new(:text($dir-label)));

      return $ul;
    }

    #---------------------------------------------------------------------------
    #
    method !make-entry ( XML::Element $parent, Str $reference ) {
      my $li = XML::Element.new(:name('li'));
      $parent.append($li);

      my Hash $attrs = {};
      if $!ref-attr ne 'href' {
        $attrs<href> = '';
        $attrs{$!ref-attr} = $reference;
      }

      else {
        $attrs<href> = $reference;
      }

      my $a = XML::Element.new( :name('a'), :attribs($attrs));
      $li.append($a);

      # Cleanup the reference in such a way that it will become a nice label
      # The filename can have prefixed numbers so the sorting will automatically
      # have the proper sequence to show. The number will be removed from the
{'fraction_non_alphanumeric': 0.2256598055310014, 'fraction_numerical': 0.0058088142442227556, 'mean_word_length': 1.4272142200429052, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 21, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 8}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1519989', 'n_tokens_mistral': 2451, 'n_tokens_neox': 2355, 'n_words': 617}