#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;
our $DEBUG = 0; # Debugging: Aus = 0; An = 1 oder 2
our $SHIFTWIDTH = 4;
# Simuliert Datenbankabfrage
my(@rows) = map { my %h; @h{ qw(id filename content content_type) } = @$_; \%h }
[1, '/dir1/dir1.1/dir1.1.1/file1.1.1.1', 'bla bla bla', 'text'],
[2, '/dir1/dir1.2/dir1.2.1/file1.2.1.1', 'blu bla blubb', 'text'],
[4, '/dir1/file1', 'blu bla bla', 'text'],
[5, '/dir1/file2', 'blu bla ble', 'text'],
[3, '/dir2/dir2.1/dir2.1.1/file2.1.1.3', 'bli blu bla', 'text'],
[6, 'file1', 'bar bla blo', 'text'],
[7, 'afile2', 'another file', 'text'],
[8, 'bfile2', 'bnother file', 'text'];
my $tree;
while(my $row = shift @rows) { # jede Datenzeile
$tree = process_data($row->{'filename'}, $row, $tree); # verarbeiten und Baum hinzufuegen
}
print Dumper($tree), '-'x25, "\n" if $DEBUG;
print <<EOT1;
var TREE_ITEMS = [
EOT1
# erzeuge JS-Code
build_js_code($tree);
print <<EOT2;
];
EOT2
sub process_data {
my($path, $row_data, $tree) = @_;
$tree = {'type' => 'dir'} unless $tree;
$path =~ s:^[\\/]::;
my($super, $sub) = split /[\/\\]/, $path, 2;
if($sub && $sub ne '') {
$tree->{'items'}->{$super} = process_data($sub, $row_data, {'dir' => 1});
} else {
$tree->{'items'}->{$super} = {'file' => 1, 'data' => $row_data };
}
return $tree;
}
sub build_js_code {
my $tree = shift;
my $indent = shift || 1;
foreach my $key (sort {return $a cmp $b if $tree->{'items'}->{$a}->{'dir'}
&& $tree->{'items'}->{$b}->{'dir'};
return $a cmp $b if $tree->{'items'}->{$a}->{'file'}
&& $tree->{'items'}->{$b}->{'file'};
return $tree->{'items'}->{$a}->{'dir'} ? -1 : 1;}
keys %{ $tree->{'items'} } ) {
if( $tree->{'items'}->{$key}->{'dir'} ) {
print ' ' x ($SHIFTWIDTH * $indent),
"['", $key, ", null,\n";
build_js_code( $tree->{'items'}->{$key}, $indent + 1);
} else {
print ' ' x ($SHIFTWIDTH * $indent),
"['", $key, "', '", $tree->{'items'}->{$key}->{'data'}->{'content'}, "']\n";
}
}
}