/[socialtext-import]/tamtam/tamtam2socialtext.pl
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Diff of /tamtam/tamtam2socialtext.pl

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 12 by dpavlin, Wed Dec 12 14:10:37 2007 UTC revision 21 by dpavlin, Thu Dec 13 11:39:52 2007 UTC
# Line 9  use Regexp::Common qw/balanced/; Line 9  use Regexp::Common qw/balanced/;
9  use Socialtext::Resting;  use Socialtext::Resting;
10  use Encode;  use Encode;
11  use HTTP::Date;  use HTTP::Date;
12    use POSIX qw/strftime/;
13    use File::Slurp;
14    use File::MMagic::XS;
15    use Getopt::Long;
16  use Data::Dump qw/dump/;  use Data::Dump qw/dump/;
17    
18    my $debug = 0;
19  my $max = 999;  my $max = 999;
20    
21    GetOptions(
22            'debug+' => \$debug,
23            'max=i' => \$max,
24    );
25    
26  my $page;  my $page;
27  my $page_date;  my $page_date;
28    
29  my @page_names;  my @page_names;
30    
31    print "Collecting pages...\n";
32    
33  find({  find({
34          wanted => sub {          wanted => sub {
35                  my $path = $File::Find::name;                  my $path = $File::Find::name;
36                  return unless -f $path;                  return unless -f $path;
37    
38                  warn "# $path\n";                  warn "+ $path\n";
39                  my $ref = XMLin( $path ) || die "can't open $path: $!";                  my $ref = XMLin( $path,
40                            KeyAttr => {
41                                    'attachment' => '+name',
42                                    'meta' => 'name',
43                            },
44                            ForceArray => [ 'attachment', 'widget' ],
45                    ) || die "can't open $path: $!";
46    
47                    warn "## $path = ",dump( $ref ) if $debug;
48    
49                  my $name = $ref->{name} || die "no name in $path";                  my $name = $ref->{name} || die "no name in $path";
50    
51                  return if $name =~ m/^TamSystem/;                  return if $name =~ m/^TamSystem/;
52    
53                  my $date =  $ref->{meta}->{LastModified};                  my $date =  $ref->{meta}->{LastModified}->{value};
54                  if ( ! $date ) {                  if ( ! $date ) {
55                          warn "SKIP: no LastModified in $path $name";                          warn "SKIP: no LastModified in $path $name";
56                          return;                          return;
57                  }                  }
                 my $data =  
                         $ref->{widgets}->{widget}->{data} ||  
                         $ref->{widgets}->{widget}->{Body}->{data} ||  
                         die "no data in $path ",dump( $ref );  
58    
59                  $page->{ $name } = [ $data, $date ];                  my $data;
60    
61                    foreach my $w ( @{ $ref->{widgets}->{widget} } ) {
62    
63                            warn "## w = ",dump( $w ) if $debug;
64    
65                            $data .= "\n----\n" if $data;
66                            $data .= $w->{data} || die "no data?";
67                    }
68    
69                    my $attachments;
70    
71                    if ( my $a = $ref->{attachment} ) {
72                            foreach my $name ( keys %$a ) {
73                                    my $full_path = $path;
74                                    $full_path =~ s,pages/,attachments/,;
75                                    $full_path .= '.' . $name;
76                                    die "$full_path doesn't exist" unless -e $full_path;
77                                    push @$attachments, {
78                                            full_path => $full_path,
79                                            name => ( $name || $a->{$name}->{desc} || 'noname' ),
80                                    };
81                            }
82                    }
83    
84                    $page->{ $name } = {
85                            content => convert_markup( $data ),
86                            date => convert_date( $date ),
87                            attachments => $attachments,
88                    };
89    
                 # strip path from page name  
90                  $name =~ s,^.+/([^/]+)$,$1,;                  $name =~ s,^.+/([^/]+)$,$1,;
91                  push @page_names, $name;                  push @page_names, $name;
92    
                 #warn dump( $ref );  
93          },          },
94            no_chdir=>1,
95  }, shift @ARGV || '.');  }, shift @ARGV || '.');
96    
97  my @pages = ( keys %$page );  my @pages = ( keys %$page );
# Line 60  my $Rester = Socialtext::Resting->new( Line 104  my $Rester = Socialtext::Resting->new(
104          username => 'tamtam',          username => 'tamtam',
105          password => 'import',          password => 'import',
106          server   => 'http://saturn.ffzg.hr/',          server   => 'http://saturn.ffzg.hr/',
107            workspace => 'razmjenavjestina',
108  );  );
 $Rester->workspace('razmjenavjestina');  
109  $Rester->put_workspacetag('TamTam');  $Rester->put_workspacetag('TamTam');
110    
111    sub convert_date {
112            my $date = shift;
113    #       return time2str( $date );
114            return strftime('%F %T %z', gmtime( $date ));
115    }
116    
117  sub header {  sub header {
118          my $h = shift;          my $h = shift;
119          if ( $h =~ m/^(=+)\s+(.+?)\s+\1$/ ) {          if ( $h =~ m/^(=+)\s+(.+?)\s+\1$/ ) {
# Line 86  sub pre { Line 136  sub pre {
136          return '.pre' . $text . '.pre';          return '.pre' . $text . '.pre';
137  }  }
138    
139  my $count = 0;  sub convert_markup {
140            my $body = shift;
 foreach my $name ( keys %$page ) {  
         last if $count++ == $max;  
   
         my ( $body, $date ) = @{ $page->{$name} };  
         $date = time2str( $date );  
141    
142          $body =~ s/\Q[[TableOfContents]]\E/{toc}/gs;          $body =~ s/\Q[[TableOfContents]]\E/{toc}/gs;
143          $body =~ s/\Q[[BR]]\E/\n/gs;          $body =~ s/\Q[[BR]]\E/\n/gs;
# Line 109  foreach my $name ( keys %$page ) { Line 154  foreach my $name ( keys %$page ) {
154          $body =~ s/\["([^"]+)"\]/[$1]/gs;          $body =~ s/\["([^"]+)"\]/[$1]/gs;
155          $body =~ s,\[(http://\S+)\s+([^\]]+)\],"$2"<$1>,gs;          $body =~ s,\[(http://\S+)\s+([^\]]+)\],"$2"<$1>,gs;
156          $body =~ s,\[(http://[^\]]+)\],$1,gs;          $body =~ s,\[(http://[^\]]+)\],$1,gs;
         $body =~ s,$page_link_re,[$1],gs;  
157    
158          Encode::_utf8_off( $body );          # fix hr
159            $body =~ s,(\S+)----,$1\n----,gs;
160            $body =~ s,----(\S+),----\n$1,gs;
161    
162            # attachments
163            $body =~ s,\[attachment:([^\]]+)(gif|png|jpg|jpeg)\],{image: $1$2},gis;
164            $body =~ s,\[attachment:([^\]]+)\],{file: $1},gs;
165    
166            return $body;
167    }
168    
169    my $count = 0;
170    
171    my $m = File::MMagic::XS->new;
172    
173    foreach my $name ( keys %$page ) {
174            last if $count++ == $max;
175    
176            my $p = $page->{$name};
177    
178            warn "## $name = ",dump( $p ) if $debug;
179    
180            my $body = $p->{content} || die "no content?";
181            my $date = $p->{date} || die "no date?";
182    
183          my @tags = ( 'TamTam' );          my @tags = ( 'TamTam' );
184    
185            my $full_name = $name;
186    
187          if ( $name =~ m!/! ) {          if ( $name =~ m!/! ) {
188                  my @page_tags = split(m!/!, $name);                  my @page_tags = split(m!/!, $name);
189                  $name = pop @page_tags; # remove page name                  $name = pop @page_tags; # remove page name
190                  push @tags, @page_tags;                  push @tags, @page_tags;
191          }          }
192    
193          $Rester->put_page( $name, {          # link named pages
194                  content => $body,          $body =~ s,\b$page_link_re\b,[$1],gs;
195                  date => $date,          $body =~ s,``,,gs;
196          });  
197          print "+ $name\n";          $body .= qq{
198    ----
199    
200    "original"<http://www.razmjenavjestina.org/$full_name> {date: $date}
201    };
202    
203            Encode::_utf8_off( $body );
204    
205            $Rester->put_page( $name, { content => $body, date => $date });
206            print "$name $date\n";
207          foreach ( @tags ) {          foreach ( @tags ) {
208                  $Rester->put_pagetag( $name, $_ );                  $Rester->put_pagetag( $name, $_ );
209                  print "+ $name [$_]\n";                  print "+ tag $_\n";
210            }
211            foreach my $a ( @{ $p->{attachments} } ) {
212                    my $type = $m->get_mime( $a->{full_path} );
213                    my $content = read_file( $a->{full_path} );
214                    print "+ attachment ", $a->{name}," $type ", length($content), " bytes\n";
215                    $Rester->post_attachment($name, $a->{name}, $content, $type );
216          }          }
217    
218  }  }

Legend:
Removed from v.12  
changed lines
  Added in v.21

  ViewVC Help
Powered by ViewVC 1.1.26