Changeset 27120 for lang/perl/Data-XMind

Show
Ignore:
Timestamp:
12/20/08 15:56:50 (4 years ago)
Author:
woremacx
Message:

support sheet, add tests

Location:
lang/perl/Data-XMind/trunk
Files:
4 added
2 modified

Legend:

Unmodified
Added
Removed
  • lang/perl/Data-XMind/trunk/lib/Data/XMind.pm

    r25900 r27120  
    99use Archive::Zip qw( :ERROR_CODES :CONSTANTS ); 
    1010 
     11use Data::XMind::Sheet; 
    1112use Data::XMind::Content; 
    1213use Data::XMind::Util qw(newid); 
     
    2223    $self->{id} ||= newid; 
    2324 
    24     my $content = Data::XMind::Content->new( 
    25         title => $self->{title}, 
    26     ); 
    27     $content->{parent} = $self; 
    28  
    29     $self->{depth} = 0; 
    30     $self->{content} = $content; 
    31     $self->{last_content} = $self->{content}; 
    32     $self->{parent} = undef; 
     25    unless ($self->{sheets}) { 
     26        $self->new_sheet; 
     27    } 
    3328 
    3429    $self; 
    3530} 
    3631 
    37 sub title { 
     32sub new_sheet { 
    3833    my $self = shift; 
    3934 
    40     if (scalar(@_)) { 
    41         $self->{content}->{title} = shift; 
     35    my $sheet = Data::XMind::Sheet->new; 
     36    if ($_[0]) { 
     37        $sheet->title($_[0]); 
    4238    } 
    4339 
    44     $self->{content}->{title}; 
     40    push(@{ $self->{sheets} }, $sheet); 
    4541} 
    4642 
    47 sub add { 
    48     my $self = shift; 
    49     my $opt = ref($_[0]) ? $_[0] : {@_}; 
    5043 
    51     die unless defined($opt->{depth}) && $opt->{depth} > 0; 
    52     die unless defined($opt->{title}) && length($opt->{title}); 
     44sub last_sheet { shift->{sheets}->[-1] }; 
    5345 
    54     my $opt_depth = $opt->{depth}; 
    55     my $last_depth = $self->{last_content}->{depth}; 
     46# proxy 
     47sub title    { shift->last_sheet->title(@_); } 
     48sub add      { shift->last_sheet->add(@_); } 
     49sub add_note { shift->last_sheet->add_note(@_); } 
    5650 
    57     die if ($opt_depth == 0); 
    58  
    59     my $content = Data::XMind::Content->new( 
    60         title => $opt->{title}, 
    61     ); 
    62  
    63     if ($opt_depth < $last_depth) { 
    64         my $done = 0; 
    65         my $last_content = $self->{last_content}; 
    66         my $parent; 
    67         while ($parent = $last_content->{parent}) { 
    68             if ($opt_depth == $parent->{depth}) { 
    69                 $self->{last_content} = $parent; 
    70                 $last_depth = $self->{last_content}->{depth}; 
    71                 $done++; 
    72                 last; 
    73             } 
    74             $last_content = $parent; 
    75         } 
    76         die unless $done; 
    77     } 
    78  
    79     if ($opt_depth == $last_depth) { 
    80         $self->{last_content} = $self->{last_content}->{parent}->add($content); 
    81     } 
    82     elsif ($opt_depth == ($last_depth + 1)) { 
    83         $self->{last_content} = $self->{last_content}->add($content); 
    84     } 
    85  
    86     $self->{last_content}; 
    87 } 
    88  
    89 sub add_note { 
    90     my $self = shift; 
    91     my $str = shift; 
    92  
    93     my $content = $self->{last_content}; 
    94     $content->add_note($str); 
    95 } 
    9651 
    9752my $template = << '__TMPL__'; 
    9853<?xml version="1.0" encoding="UTF-8" standalone="no"?> 
    9954<xmap-content xmlns="urn:xmind:xmap:xmlns:content:2.0" xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:svg="http://www.w3.org/2000/svg" xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:xlink="http://www.w3.org/1999/xlink" version="2.0"> 
    100 <sheet id="[% id %]"> 
    101 <title>[% title %]</title> 
    102 [%- content -%] 
    103 </sheet> 
     55[% FOR sheet IN sheets %] 
     56[% sheet %] 
     57[% END %] 
    10458</xmap-content> 
    10559__TMPL__ 
     
    10862    my $self = shift; 
    10963 
     64    my @sheets; 
     65    for my $sheet (@{ $self->{sheets} }) { 
     66        push @sheets, $sheet->as_xml, 
     67    } 
     68 
    11069    my $vars = { 
    111         id => $self->{id}, 
    112         title => $self->{title}, 
    113         content => $self->{content}->as_xml, 
     70        sheets => \@sheets, 
    11471    }; 
    11572 
     
    14097    unless ($zip->writeToFileNamed($name) == AZ_OK) { 
    14198        die 'write error'; 
     99    } 
     100} 
     101 
     102sub _parse_private { 
     103    my ($self, $text) = @_; 
     104 
     105    my @lines = split(/\r?\n/, $text); 
     106 
     107    $self->title(shift(@lines)); 
     108 
     109    for my $line (@lines) { 
     110        if ($line =~ /^\#/) { 
     111            next; 
     112        } 
     113        elsif ($line =~ /^\%\s*(.*)$/) { 
     114            $self->new_sheet($1); 
     115        } 
     116        elsif ($line =~ /^(\*+)\s*(.*)$/) { 
     117            $self->add( 
     118                depth => length($1), 
     119                title => $2, 
     120            ); 
     121        } 
     122        elsif ($line =~ /^\!\s+([^=]+)=\"(.*?)\"$/) { 
     123            $self->{sheets}->[-1]->{last_content}->{attr}->{$1} = $2; 
     124        } 
     125        else { 
     126            $self->add_note($line); 
     127        } 
    142128    } 
    143129} 
  • lang/perl/Data-XMind/trunk/lib/Data/XMind/Content.pm

    r25898 r27120  
    5454 
    5555my $template = << '__TMPL__'; 
    56   <topic id="[% id %]"> 
     56  <topic id="[% id %]"[% attr %]> 
    5757    <title>[% title %]</title> 
    5858 
     
    8282    my $self = shift; 
    8383 
     84    my $attr; 
     85    if (defined($self->{attr})) { 
     86        while (my ($key, $val) = each(%{ $self->{attr} })) { 
     87            $attr .= sprintf(q{ %s="%s"}, $key, $val); 
     88        } 
     89    } 
     90 
    8491    my $vars = { 
    8592        id => $self->{id}, 
    8693        title => $self->{title}, 
     94        ($attr ? (attr => $attr) : ()), 
    8795        (scalar(@{ $self->{notes} }) ? (notes => $self->{notes}) : ()), 
    8896    };