/[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 8 by dpavlin, Wed Dec 12 13:17:15 2007 UTC revision 26 by dpavlin, Fri Dec 14 23:46:57 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 $max = 5;  my $debug = 0;
19    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;
32    
33    print "Importing $max pages", $attachments ? " with attachments" : "", "...\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                  $page->{ $name } = [ $data, $date ];                  my $data;
62    
63                    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 } = {
87                            content => convert_markup( $data ),
88                            original => $data,
89                            date => convert_date( $date ),
90                            attachments => $attachments,
91                    };
92    
93                    $name =~ s,^.+/([^/]+)$,$1,;
94                    push @page_names, $name;
95    
                 #warn dump( $ref );  
96          },          },
97            no_chdir=>1,
98  }, shift @ARGV || '.');  }, shift @ARGV || '.');
99    
100  my @pages = ( keys %$page );  my @pages = ( keys %$page );
101    
102  warn "found following pages: ", join(", ", @pages),"\n";  warn "found following pages: ", join(", ", @page_names),"\n";
103    
104    my $page_link_re = '\b(' . join('|', @page_names) . ')\b';
105    
106  my $Rester = Socialtext::Resting->new(  my $Rester = Socialtext::Resting->new(
107          username => 'tamtam',          username => 'tamtam',
108          password => 'import',          password => 'import',
109          server   => 'http://saturn.ffzg.hr/',          server   => 'http://saturn.ffzg.hr/',
110            workspace => 'razmjenavjestina',
111  );  );
 $Rester->workspace('razmjenavjestina');  
112  $Rester->put_workspacetag('TamTam');  $Rester->put_workspacetag('TamTam');
113    
114    sub convert_date {
115            my $date = shift;
116    #       return time2str( $date );
117            return strftime('%F %T %z', gmtime( $date ));
118    }
119    
120  sub header {  sub header {
121          my $h = shift;          my $h = shift;
122          if ( $h =~ m/^(=+)\s+(.+?)\s+\1$/ ) {          if ( $h =~ m/^(=+)\s+(.+?)\s+\1$/ ) {
123                  my $level = length($1);                  my $level = length($1);
124                  return "\n" . ( '^' x $level ) . " $2\n";                  return "\n" . ( '^' x $level ) . " $2\n";
125          } else {          } else {
126                  die "can't parse header: $h";                  return $h;
127          }          }
128  }  }
129    
# Line 71  sub surround { Line 132  sub surround {
132          return $with . $what . $with;          return $with . $what . $with;
133  }  }
134    
135  my $count = 0;  sub pre {
136            my $text = shift;
137  foreach my $name ( keys %$page ) {          $text =~ s/^{{{\s*//s;
138          last if $count++ == $max;          $text =~ s/\s*}}}$//s;
139            return "\n.pre\n" . $text . "\n.pre\n";
140    }
141    
142          my ( $body, $date ) = @{ $page->{$name} };  sub convert_markup {
143          $date = time2str( $date );          my $body = shift;
144    
145          $body =~ s/\Q[[TableOfContents]]\E/{toc}/gs;          $body =~ s/\Q[[TableOfContents]]\E/{toc}/gs;
146          $body =~ s/\Q[[BR]]\E/\n/gs;          $body =~ s/\Q[[BR]]\E/\n/gs;
# Line 85  foreach my $name ( keys %$page ) { Line 148  foreach my $name ( keys %$page ) {
148          $body =~ s/''''(.+?)''''/surround('`',$1)/gse;          $body =~ s/''''(.+?)''''/surround('`',$1)/gse;
149          $body =~ s/'''(.+?)'''/surround('*',$1)/gse;          $body =~ s/'''(.+?)'''/surround('*',$1)/gse;
150          $body =~ s/''(.+?)''/surround('_',$1)/gse;          $body =~ s/''(.+?)''/surround('_',$1)/gse;
151            $body =~ s/$RE{balanced}{-begin => "{{{"}{-end => "}}}"}{-keep}/pre($1)/gse;
152    
153            # fix bullets
154          $body =~ s/^\s+([\*])/$1/gm;          $body =~ s/^\s+([\*])/$1/gm;
155    
156          Encode::_utf8_off( $body );          # fix links
157            $body =~ s/\["([^"]+)"\]/[$1]/gs;
158            $body =~ s,\[(http://\S+)\s+([^\]]+)\],"$2"<$1>,gs;
159            $body =~ s,\[(http://[^\]]+)\],$1,gs;
160    
161            # fix hr
162            $body =~ s,(\S+)----,$1\n----,gs;
163            $body =~ s,----(\S+),----\n$1,gs;
164    
165            # attachments
166            $body =~ s,\[attachment:([^\]]+)(gif|png|jpg|jpeg)\],{image: $1$2},gis;
167            $body =~ s,\[attachment:([^\]]+)\],{file: $1},gs;
168    
169            return $body;
170    }
171    
172    my $count = 0;
173    
174    my $m = File::MMagic::XS->new;
175    
176    foreach my $name ( keys %$page ) {
177            last if $count++ == $max;
178    
179            my $p = $page->{$name};
180    
181            warn "## $name = ",dump( $p ) if $debug;
182    
183            my $body = $p->{content} || die "no content?";
184            my $date = $p->{date} || die "no date?";
185    
186          my @tags = ( 'TamTam' );          my @tags = ( 'TamTam' );
187    
188            my $full_name = $name;
189    
190          if ( $name =~ m!/! ) {          if ( $name =~ m!/! ) {
191                  my @page_tags = split(m!/!, $name);                  my @page_tags = split(m!/!, $name);
192                  $name = pop @page_tags; # remove page name                  $name = pop @page_tags; # remove page name
193                  push @tags, @page_tags;                  push @tags, @page_tags;
194          }          }
195    
196          $Rester->put_page( $name, {          # link named pages
197                  content => $body,          $body =~ s,\b$page_link_re\b,[$1],gs;
198                  date => $date,          $body =~ s,``,,gs;
199          });  
200          print "+ $name\n";          $body .= qq{
201    
202    ----
203    
204    "original"<http://www.razmjenavjestina.org/$full_name> {date: $date}
205    };
206    
207            Encode::_utf8_off( $body );
208    
209            print "$name $date\n";
210    
211            # original markup
212            $Rester->put_page( $name, { content => $p->{original}, date => $date });
213    
214          foreach ( @tags ) {          foreach ( @tags ) {
215                  $Rester->put_pagetag( $name, $_ );                  $Rester->put_pagetag( $name, $_, { date => $date } );
216                  print "+ $name [$_]\n";                  print "+ tag $_\n";
217          }          }
218    
219            if ( $attachments ) {
220                    foreach my $a ( @{ $p->{attachments} } ) {
221                            my $type = $m->get_mime( $a->{full_path} );
222                            my $content = read_file( $a->{full_path} );
223                            print "+ attachment ", $a->{name}," $type ", length($content), " bytes\n";
224                            $Rester->post_attachment($name, $a->{name}, $content, $type );
225                    }
226            }
227    
228            # converted page
229            $Rester->put_page( $name, { content => $body, date => $date });
230    
231  }  }
232    

Legend:
Removed from v.8  
changed lines
  Added in v.26

  ViewVC Help
Powered by ViewVC 1.1.26