/[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 5 by dpavlin, Wed Dec 12 12:12:21 2007 UTC revision 22 by dpavlin, Thu Dec 13 11:42:24 2007 UTC
# Line 8  use File::Find; Line 8  use File::Find;
8  use Regexp::Common qw/balanced/;  use Regexp::Common qw/balanced/;
9  use Socialtext::Resting;  use Socialtext::Resting;
10  use Encode;  use Encode;
11    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;
20    
21    GetOptions(
22            'debug+' => \$debug,
23            'max=i' => \$max,
24    );
25    
26  my $page;  my $page;
27    my $page_date;
28    
29    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                  warn "# $path\n";  
38                  my $ref = XMLin( $path ) || die "can't open $path: $!";                  warn "+ $path\n";
39                  $page->{ $ref->{name} } = $ref->{widgets}->{widget}->{data};                  my $ref = XMLin( $path,
40                  warn dump( $ref->{widgets}->{widget}->{data} );                          KeyAttr => {
41                                    'attachment' => '+name',
42                                    'meta' => 'name',
43                            },
44                            ForceArray => [ 'attachment', 'meta', '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";
50    
51                    return if $name =~ m/^TamSystem/;
52    
53                    my $date =  $ref->{meta}->{LastModified}->{value};
54                    if ( ! $date ) {
55                            warn "SKIP: no LastModified in $path $name";
56                            return;
57                    }
58    
59                    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    
90                    $name =~ s,^.+/([^/]+)$,$1,;
91                    push @page_names, $name;
92    
93          },          },
94            no_chdir=>1,
95  }, shift @ARGV || '.');  }, shift @ARGV || '.');
96    
97    my @pages = ( keys %$page );
98    
99  warn "found following pages: ", join(", ", keys %$page),"\n";  warn "found following pages: ", join(", ", @page_names),"\n";
100    
101    my $page_link_re = '\b(' . join('|', @page_names) . ')\b';
102    
103  my $Rester = Socialtext::Resting->new(  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$/ ) {
120                  my $level = length($1);                  my $level = length($1);
121                  return "\n" . ( '^' x $level ) . " $2\n";                  return "\n" . ( '^' x $level ) . " $2\n";
122          } else {          } else {
123                  die "can't parse header: $h";                  return $h;
124          }          }
125  }  }
126    
# Line 49  sub surround { Line 129  sub surround {
129          return $with . $what . $with;          return $with . $what . $with;
130  }  }
131    
132  foreach my $name ( keys %$page ) {  sub pre {
133          my $body = $page->{$name} || die "no content for page $name";          my $text = shift;
134            $text =~ s/^{{{//;
135            $text =~ s/}}}$//;
136            return '.pre' . $text . '.pre';
137    }
138    
139    sub convert_markup {
140            my $body = shift;
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 58  foreach my $name ( keys %$page ) { Line 145  foreach my $name ( keys %$page ) {
145          $body =~ s/''''(.+?)''''/surround('`',$1)/gse;          $body =~ s/''''(.+?)''''/surround('`',$1)/gse;
146          $body =~ s/'''(.+?)'''/surround('*',$1)/gse;          $body =~ s/'''(.+?)'''/surround('*',$1)/gse;
147          $body =~ s/''(.+?)''/surround('_',$1)/gse;          $body =~ s/''(.+?)''/surround('_',$1)/gse;
148            $body =~ s/$RE{balanced}{-begin => "{{{"}{-end => "}}}"}{-keep}/pre($1)/gse;
149    
150            # fix bullets
151            $body =~ s/^\s+([\*])/$1/gm;
152    
153            # fix links
154            $body =~ s/\["([^"]+)"\]/[$1]/gs;
155            $body =~ s,\[(http://\S+)\s+([^\]]+)\],"$2"<$1>,gs;
156            $body =~ s,\[(http://[^\]]+)\],$1,gs;
157    
158            # 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' );
184    
185            my $full_name = $name;
186    
187            if ( $name =~ m!/! ) {
188                    my @page_tags = split(m!/!, $name);
189                    $name = pop @page_tags; # remove page name
190                    push @tags, @page_tags;
191            }
192    
193            # link named pages
194            $body =~ s,\b$page_link_re\b,[$1],gs;
195            $body =~ s,``,,gs;
196    
197            $body .= qq{
198    
199    ----
200    
201    "original"<http://www.razmjenavjestina.org/$full_name> {date: $date}
202    };
203    
204          Encode::_utf8_off( $body );          Encode::_utf8_off( $body );
205    
206          $Rester->put_page( $name, $body );          $Rester->put_page( $name, { content => $body, date => $date });
207          $Rester->put_pagetag( $name, 'TamTam' );          print "$name $date\n";
208            foreach ( @tags ) {
209                    $Rester->put_pagetag( $name, $_ );
210                    print "+ tag $_\n";
211            }
212            foreach my $a ( @{ $p->{attachments} } ) {
213                    my $type = $m->get_mime( $a->{full_path} );
214                    my $content = read_file( $a->{full_path} );
215                    print "+ attachment ", $a->{name}," $type ", length($content), " bytes\n";
216                    $Rester->post_attachment($name, $a->{name}, $content, $type );
217            }
218    
         print "+ $name\n";  
219  }  }
220    

Legend:
Removed from v.5  
changed lines
  Added in v.22

  ViewVC Help
Powered by ViewVC 1.1.26