/[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 14 by dpavlin, Wed Dec 12 17:56:54 2007 UTC
# Line 11  use Encode; Line 11  use Encode;
11  use HTTP::Date;  use HTTP::Date;
12  use Data::Dump qw/dump/;  use Data::Dump qw/dump/;
13    
14  my $max = 5;  my $max = 999;
15    
16  my $page;  my $page;
17  my $page_date;  my $page_date;
18    
19    my @page_names;
20    
21  find({  find({
22          wanted => sub {          wanted => sub {
23                  my $path = $File::Find::name;                  my $path = $File::Find::name;
# Line 28  find({ Line 30  find({
30    
31                  return if $name =~ m/^TamSystem/;                  return if $name =~ m/^TamSystem/;
32    
33                  my $date =  $ref->{meta}->{LastModified};                  my $date =  $ref->{meta}->{LastModified}->{value};
34                  if ( ! $date ) {                  if ( ! $date ) {
35                          warn "SKIP: no LastModified in $path $name";                          warn "SKIP: no LastModified in $path $name";
36                          return;                          return;
# Line 38  find({ Line 40  find({
40                          $ref->{widgets}->{widget}->{Body}->{data} ||                          $ref->{widgets}->{widget}->{Body}->{data} ||
41                          die "no data in $path ",dump( $ref );                          die "no data in $path ",dump( $ref );
42    
43                  $page->{ $name } = [ $data, $date ];                  $page->{ $name } = {
44                            content => convert_markup( $data ),
45                            date => time2str( $date ),
46                    };
47    
48                    # strip path from page name
49                    $name =~ s,^.+/([^/]+)$,$1,;
50                    push @page_names, $name;
51    
52                  #warn dump( $ref );  #               warn dump( $ref );
53          },          },
54  }, shift @ARGV || '.');  }, shift @ARGV || '.');
55    
56  my @pages = ( keys %$page );  my @pages = ( keys %$page );
57    
58  warn "found following pages: ", join(", ", @pages),"\n";  warn "found following pages: ", join(", ", @page_names),"\n";
59    
60    my $page_link_re = '\b(' . join('|', @page_names) . ')\b';
61    
62  my $Rester = Socialtext::Resting->new(  my $Rester = Socialtext::Resting->new(
63          username => 'tamtam',          username => 'tamtam',
# Line 62  sub header { Line 73  sub header {
73                  my $level = length($1);                  my $level = length($1);
74                  return "\n" . ( '^' x $level ) . " $2\n";                  return "\n" . ( '^' x $level ) . " $2\n";
75          } else {          } else {
76                  die "can't parse header: $h";                  return $h;
77          }          }
78  }  }
79    
# Line 71  sub surround { Line 82  sub surround {
82          return $with . $what . $with;          return $with . $what . $with;
83  }  }
84    
85  my $count = 0;  sub pre {
86            my $text = shift;
87  foreach my $name ( keys %$page ) {          $text =~ s/^{{{//;
88          last if $count++ == $max;          $text =~ s/}}}$//;
89            return '.pre' . $text . '.pre';
90    }
91    
92          my ( $body, $date ) = @{ $page->{$name} };  sub convert_markup {
93          $date = time2str( $date );          my $body = shift;
94    
95          $body =~ s/\Q[[TableOfContents]]\E/{toc}/gs;          $body =~ s/\Q[[TableOfContents]]\E/{toc}/gs;
96          $body =~ s/\Q[[BR]]\E/\n/gs;          $body =~ s/\Q[[BR]]\E/\n/gs;
# Line 85  foreach my $name ( keys %$page ) { Line 98  foreach my $name ( keys %$page ) {
98          $body =~ s/''''(.+?)''''/surround('`',$1)/gse;          $body =~ s/''''(.+?)''''/surround('`',$1)/gse;
99          $body =~ s/'''(.+?)'''/surround('*',$1)/gse;          $body =~ s/'''(.+?)'''/surround('*',$1)/gse;
100          $body =~ s/''(.+?)''/surround('_',$1)/gse;          $body =~ s/''(.+?)''/surround('_',$1)/gse;
101            $body =~ s/$RE{balanced}{-begin => "{{{"}{-end => "}}}"}{-keep}/pre($1)/gse;
102    
103            # fix bullets
104          $body =~ s/^\s+([\*])/$1/gm;          $body =~ s/^\s+([\*])/$1/gm;
105    
106          Encode::_utf8_off( $body );          # fix links
107            $body =~ s/\["([^"]+)"\]/[$1]/gs;
108            $body =~ s,\[(http://\S+)\s+([^\]]+)\],"$2"<$1>,gs;
109            $body =~ s,\[(http://[^\]]+)\],$1,gs;
110    
111            # fix hr
112            $body =~ s,(\S+)----,$1\n----,gs;
113            $body =~ s,----(\S+),----\n$1,gs;
114    
115            return $body;
116    }
117    
118    my $count = 0;
119    
120    foreach my $name ( keys %$page ) {
121            last if $count++ == $max;
122    
123            my $p = $page->{$name};
124            my $body = $p->{content} || die "no content?";
125            my $date = $p->{date} || die "no date?";
126    
127          my @tags = ( 'TamTam' );          my @tags = ( 'TamTam' );
128    
# Line 98  foreach my $name ( keys %$page ) { Line 132  foreach my $name ( keys %$page ) {
132                  push @tags, @page_tags;                  push @tags, @page_tags;
133          }          }
134    
135            # link named pages
136            $body =~ s,$page_link_re,[$1],gs;
137    
138            $body .= qq{
139    ----
140    
141    Original: http://www.razmjenavjestina.org/$name
142    };
143    
144            Encode::_utf8_off( $body );
145    
146          $Rester->put_page( $name, {          $Rester->put_page( $name, {
147                  content => $body,                  content => $body,
148                  date => $date,                  date => $date,
149          });          });
150          print "+ $name\n";          print "+ $name $date\n";
151          foreach ( @tags ) {          foreach ( @tags ) {
152                  $Rester->put_pagetag( $name, $_ );                  $Rester->put_pagetag( $name, $_ );
153                  print "+ $name [$_]\n";                  print "+ $name [$_]\n";

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

  ViewVC Help
Powered by ViewVC 1.1.26