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

Legend:
Removed from v.13  
changed lines
  Added in v.23

  ViewVC Help
Powered by ViewVC 1.1.26