Show
Ignore:
Timestamp:
08/28/08 02:04:02 (4 months ago)
Author:
ogawa
Message:

Beautify code by perltidy.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • cybozu2ical/trunk/cybozu2ical

    r502 r504  
    2222### 
    2323*Data::ICal::Property::_value_as_string = sub { 
    24     my $self = shift; 
    25     my $key = shift; 
    26     my $value = defined($self->value()) ? $self->value() : ''; 
    27      
    28     unless ($self->vcal10) { 
     24    my $self  = shift; 
     25    my $key   = shift; 
     26    my $value = defined( $self->value() ) ? $self->value() : ''; 
     27 
     28    unless ( $self->vcal10 ) { 
    2929        my $lc_key = lc($key); 
    3030        $value =~ s/\\/\\/gs; 
    31         $value =~ s/\Q;/\\;/gs unless ($lc_key eq 'rrule' || $lc_key eq 'exdate'); 
    32         $value =~ s/,/\\,/gs unless ($lc_key eq 'rrule' || $lc_key eq 'exdate'); 
     31        $value =~ s/\Q;/\\;/gs 
     32          unless ( $lc_key eq 'rrule' || $lc_key eq 'exdate' ); 
     33        $value =~ s/,/\\,/gs 
     34          unless ( $lc_key eq 'rrule' || $lc_key eq 'exdate' ); 
    3335        $value =~ s/\n/\\n/gs; 
    3436        $value =~ s/\\N/\\N/gs; 
     
    4345### 
    4446sub to_icaldate { 
    45     my($dt, $is_full_day) = @_; 
    46     $is_full_day ? 
    47         $dt->ymd('') : 
    48         $dt->ymd('') . 'T' . $dt->hms('') . ($dt->time_zone->is_utc ? 'Z' : ''); 
     47    my ( $dt, $is_full_day ) = @_; 
     48    $is_full_day 
     49      ? $dt->ymd('') 
     50      : $dt->ymd('') . 'T' 
     51      . $dt->hms('') 
     52      . ( $dt->time_zone->is_utc ? 'Z' : '' ); 
    4953} 
    5054 
    5155sub encode_string { 
    52     my($enc, $text) = @_; 
    53     if ($enc eq 'ncr') { 
    54         $text =~ s/(\P{ASCII})/sprintf("&#%d;", ord($1))/eg; 
    55     } else { 
    56         $text = encode($enc, $text); 
     56    my ( $enc, $text ) = @_; 
     57    if ( $enc eq 'ncr' ) { 
     58        $text =~ s/(\P{ASCII})/sprintf("&#%d;", ord($1))/eg; 
     59    } 
     60    else { 
     61        $text = encode( $enc, $text ); 
    5762    } 
    5863    $text; 
     
    6267    my $file = shift; 
    6368    my $yaml; 
    64     if (eval('require YAML::Tiny')) { 
    65         ($yaml) = YAML::Tiny::LoadFile($file); 
    66     } elsif (eval('require YAML')) { 
    67         ($yaml) = YAML::LoadFile($file); 
     69    if ( eval('require YAML::Tiny') ) { 
     70        ($yaml) = YAML::Tiny::LoadFile($file); 
     71    } 
     72    elsif ( eval('require YAML') ) { 
     73        ($yaml) = YAML::LoadFile($file); 
    6874    } 
    6975    if ($@) { 
    70         die "Faild to read yaml file: $@"; 
     76        die "Faild to read yaml file: $@"; 
    7177    } 
    7278    $yaml; 
     
    7985# Handle command-line options 
    8086my %opt = ( 
    81     conf                        => 'config.yaml', 
    82     'compat-google-calendar'    => 0, 
     87    conf                     => 'config.yaml', 
     88    'compat-google-calendar' => 0, 
    8389); 
    84 GetOptions(\%opt, 
    85            'output=s', 
    86            'conf=s', 
    87            'compat-google-calendar', 
    88            'debug', 
    89            'input-csv=s', 
    90            'output-csv=s', 
    91            'help', 
    92        ) or pod2usage(2); 
     90GetOptions( \%opt, 'output=s', 'conf=s', 'compat-google-calendar', 'debug', 
     91    'input-csv=s', 'output-csv=s', 'help', ) 
     92  or pod2usage(2); 
    9393pod2usage(1) if $opt{help}; 
    9494 
    9595# Read configuration file 
    96 my $cfg = read_yaml($opt{conf}); 
     96my $cfg = read_yaml( $opt{conf} ); 
    9797my $time_zone = $cfg->{time_zone} || 'Asia/Tokyo'; 
    9898 
     
    103103    calscale => 'GREGORIAN', 
    104104    method   => 'PUBLISH', 
    105     $cfg->{calname} ? ('X-WR-CALNAME' => $cfg->{calname}) : (), 
     105    $cfg->{calname} ? ( 'X-WR-CALNAME' => $cfg->{calname} ) : (), 
    106106    'X-WR-TIMEZONE' => $time_zone 
    107107); 
    108108 
    109109# Obtain Cybozu Office 6/7 Calendar items 
    110 my $cal_class = $cfg->{cybozu_version} == 7 ? 
    111     'WWW::CybozuOffice7::Calendar' : 'WWW::CybozuOffice6::Calendar'; 
     110my $cal_class = 
     111  $cfg->{cybozu_version} == 7 
     112  ? 'WWW::CybozuOffice7::Calendar' 
     113  : 'WWW::CybozuOffice6::Calendar'; 
    112114eval "use $cal_class;"; 
    113115my $cal = $cal_class->new(%$cfg); 
    114116 
    115 if ($opt{'input-csv'}) { 
    116     $cal->read_from_csv_file($opt{'input-csv'}) 
    117         or die "Failed to read CSV file: $opt{'input-csv'}"; 
    118 } else { 
     117if ( $opt{'input-csv'} ) { 
     118    $cal->read_from_csv_file( $opt{'input-csv'} ) 
     119      or die "Failed to read CSV file: $opt{'input-csv'}"; 
     120} 
     121else { 
    119122    $cal->request() 
    120         or die "Failed to get Cybozu Office 6 Calendar"; 
     123      or die "Failed to get Cybozu Office 6 Calendar"; 
    121124} 
    122125 
    123126# Output the calendar CSV for debugging 
    124 if ($opt{'output-csv'}) { 
     127if ( $opt{'output-csv'} ) { 
    125128    local *FH; 
    126129    open FH, ">$opt{'output-csv'}" or die "Failed to write $opt{'output-csv'}"; 
     
    130133 
    131134# For each items, generate an 'event entry' and append it to the calendar 
    132 for my $item ($cal->get_items()) { 
     135for my $item ( $cal->get_items() ) { 
    133136    my $vevent = Data::ICal::Entry::Event->new(); 
    134     my %args = ( 
    135         summary     => decode_utf8($item->summary), 
    136         description => decode_utf8($item->description), 
    137         created     => to_icaldate($item->created), 
    138         dtstamp     => to_icaldate($item->modified), 
    139         url         => $cal->url . '?page=ScheduleView&EID=' . $item->id, 
     137    my %args   = ( 
     138        summary     => decode_utf8( $item->summary ), 
     139        description => decode_utf8( $item->description ), 
     140        created     => to_icaldate( $item->created ), 
     141        dtstamp     => to_icaldate( $item->modified ), 
     142        url         => $cal->url . '?page=ScheduleView&EID=' . $item->id, 
    140143    ); 
    141144 
    142     $args{comment} = decode_utf8($item->comment) 
    143         if $opt{debug} && $item->comment; 
    144  
    145     if ($item->is_full_day) { 
    146         $args{dtstart} = [ to_icaldate($item->start, 1), { VALUE => 'DATE' } ]; 
    147         $args{dtend}   = [ to_icaldate($item->end,   1), { VALUE => 'DATE' } ]; 
    148     } else { 
    149         $args{dtstart} = [ to_icaldate($item->start, 0), { TZID => $time_zone } ]; 
    150         $args{dtend}   = [ to_icaldate($item->end,   0), { TZID => $time_zone } ]; 
     145    $args{comment} = decode_utf8( $item->comment ) 
     146      if $opt{debug} && $item->comment; 
     147 
     148    if ( $item->is_full_day ) { 
     149        $args{dtstart} = 
     150          [ to_icaldate( $item->start, 1 ), { VALUE => 'DATE' } ]; 
     151        $args{dtend} = [ to_icaldate( $item->end, 1 ), { VALUE => 'DATE' } ]; 
     152    } 
     153    else { 
     154        $args{dtstart} = 
     155          [ to_icaldate( $item->start, 0 ), { TZID => $time_zone } ]; 
     156        $args{dtend} = [ to_icaldate( $item->end, 0 ), { TZID => $time_zone } ]; 
    151157    } 
    152158 
    153159    # handle frequency 
    154     if ($item->can('rrule')) { 
    155         # rrule 
    156         my %rrule = %{$item->rrule}; 
    157         $rrule{UNTIL} = to_icaldate($rrule{UNTIL}, $item->is_full_day) 
    158             if $rrule{UNTIL}; 
    159         $rrule{WKST} = 'SU' 
    160             if $opt{'compat-google-calendar'}; 
    161  
    162         my @rrule_list; 
    163         for (qw(FREQ COUNT INTERVAL BYMONTH BYMONTHDAY WKST BYDAY UNTIL)) { 
    164             push @rrule_list, $_ . '=' . $rrule{$_} 
    165                 if exists $rrule{$_}; 
    166         } 
    167         $args{rrule} = join ';', @rrule_list; 
    168  
    169         # exdate 
    170         if ($item->exdates) { 
    171             if ($item->is_full_day) { 
    172                 my $exdate = join ',', map { to_icaldate($_, 1) } $item->exdates; 
    173                 $args{exdate} = [ $exdate, { VALUE => 'DATE' } ]; 
    174             } else { 
    175                 my $exdate = join ',', map { to_icaldate($_, 0) } $item->exdates; 
    176                 $args{exdate} = [ $exdate, { TZID => $time_zone } ]; 
    177             } 
    178         } 
     160    if ( $item->can('rrule') ) { 
     161 
     162        # rrule 
     163        my %rrule = %{ $item->rrule }; 
     164        $rrule{UNTIL} = to_icaldate( $rrule{UNTIL}, $item->is_full_day ) 
     165          if $rrule{UNTIL}; 
     166        $rrule{WKST} = 'SU' 
     167          if $opt{'compat-google-calendar'}; 
     168 
     169        my @rrule_list; 
     170        for (qw(FREQ COUNT INTERVAL BYMONTH BYMONTHDAY WKST BYDAY UNTIL)) { 
     171            push @rrule_list, $_ . '=' . $rrule{$_} 
     172              if exists $rrule{$_}; 
     173        } 
     174        $args{rrule} = join ';', @rrule_list; 
     175 
     176        # exdate 
     177        if ( $item->exdates ) { 
     178            if ( $item->is_full_day ) { 
     179                my $exdate = join ',', 
     180                  map { to_icaldate( $_, 1 ) } $item->exdates; 
     181                $args{exdate} = [ $exdate, { VALUE => 'DATE' } ]; 
     182            } 
     183            else { 
     184                my $exdate = join ',', 
     185                  map { to_icaldate( $_, 0 ) } $item->exdates; 
     186                $args{exdate} = [ $exdate, { TZID => $time_zone } ]; 
     187            } 
     188        } 
    179189 
    180190    } 
     
    189199# Generate a 'timezone entry' and append it to the calendar 
    190200my $vtimezone = Data::ICal::Entry::TimeZone->new(); 
    191 $vtimezone->add_properties(tzid => $time_zone); 
     201$vtimezone->add_properties( tzid => $time_zone ); 
    192202 
    193203# probably we need to support the Daylight Saving Time, but not yet supported. 
    194204my $standard = Data::ICal::Entry::TimeZone::Standard->new(); 
    195 my $std = DateTime->new(year => 1970, month => 1, day => 1, 
    196                         hour => 0, minute => 0, second => 0, 
    197                         time_zone => $time_zone); 
    198 my $offset = DateTime::TimeZone::offset_as_string($std->offset) || '+0900'; 
     205my $std      = DateTime->new( 
     206    year      => 1970, 
     207    month     => 1, 
     208    day       => 1, 
     209    hour      => 0, 
     210    minute    => 0, 
     211    second    => 0, 
     212    time_zone => $time_zone 
     213); 
     214my $offset = DateTime::TimeZone::offset_as_string( $std->offset ) || '+0900'; 
    199215my $tzname = $cfg->{tzname} || 'JST'; 
    200216 
     
    210226 
    211227# Outputs the calendar as a string 
    212 my $text = encode_string($cfg->{output_encoding} || 'utf8', $vcalendar->as_string); 
    213 if ($opt{output}) { 
     228my $text = 
     229  encode_string( $cfg->{output_encoding} || 'utf8', $vcalendar->as_string ); 
     230if ( $opt{output} ) { 
    214231    local *FH; 
    215232    open FH, ">$opt{output}" or die "Failed to write $opt{output}"; 
    216233    print FH $text; 
    217234    close FH; 
    218 } else { 
     235} 
     236else { 
    219237    print $text; 
    220238}