/[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 13 by dpavlin, Wed Dec 12 17:14:58 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 Data::Dump qw/dump/;  use Data::Dump qw/dump/;
13    
14    my $max = 999;
15    
16  my $page;  my $page;
17    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;
24                  return unless -f $path;                  return unless -f $path;
25    
26                  warn "# $path\n";                  warn "# $path\n";
27                  my $ref = XMLin( $path ) || die "can't open $path: $!";                  my $ref = XMLin( $path ) || die "can't open $path: $!";
28                  $page->{ $ref->{name} } = $ref->{widgets}->{widget}->{data};  
29                  warn dump( $ref->{widgets}->{widget}->{data} );                  my $name = $ref->{name} || die "no name in $path";
30    
31                    return if $name =~ m/^TamSystem/;
32    
33                    my $date =  $ref->{meta}->{LastModified};
34                    if ( ! $date ) {
35                            warn "SKIP: no LastModified in $path $name";
36                            return;
37                    }
38                    my $data =
39                            $ref->{widgets}->{widget}->{data} ||
40                            $ref->{widgets}->{widget}->{Body}->{data} ||
41                            die "no data in $path ",dump( $ref );
42    
43                    $data .= qq{
44    ----
45    
46    Original: http://www.razmjenavjestina.org/$path
47    };
48    
49                    $page->{ $name } = [ $data, $date ];
50    
51                    # strip path from page name
52                    $name =~ s,^.+/([^/]+)$,$1,;
53                    push @page_names, $name;
54    
55    #               warn dump( $ref );
56          },          },
57  }, shift @ARGV || '.');  }, shift @ARGV || '.');
58    
59    my @pages = ( keys %$page );
60    
61  warn "found following pages: ", join(", ", keys %$page),"\n";  warn "found following pages: ", join(", ", @page_names),"\n";
62    
63    my $page_link_re = '\b(' . join('|', @page_names) . ')\b';
64    
65  my $Rester = Socialtext::Resting->new(  my $Rester = Socialtext::Resting->new(
66          username => 'tamtam',          username => 'tamtam',
# Line 40  sub header { Line 76  sub header {
76                  my $level = length($1);                  my $level = length($1);
77                  return "\n" . ( '^' x $level ) . " $2\n";                  return "\n" . ( '^' x $level ) . " $2\n";
78          } else {          } else {
79                  die "can't parse header: $h";                  return $h;
80          }          }
81  }  }
82    
# Line 49  sub surround { Line 85  sub surround {
85          return $with . $what . $with;          return $with . $what . $with;
86  }  }
87    
88    sub pre {
89            my $text = shift;
90            $text =~ s/^{{{//;
91            $text =~ s/}}}$//;
92            return '.pre' . $text . '.pre';
93    }
94    
95    my $count = 0;
96    
97  foreach my $name ( keys %$page ) {  foreach my $name ( keys %$page ) {
98          my $body = $page->{$name} || die "no content for page $name";          last if $count++ == $max;
99    
100            my ( $body, $date ) = @{ $page->{$name} };
101            $date = time2str( $date );
102    
103          $body =~ s/\Q[[TableOfContents]]\E/{toc}/gs;          $body =~ s/\Q[[TableOfContents]]\E/{toc}/gs;
104          $body =~ s/\Q[[BR]]\E/\n/gs;          $body =~ s/\Q[[BR]]\E/\n/gs;
# Line 58  foreach my $name ( keys %$page ) { Line 106  foreach my $name ( keys %$page ) {
106          $body =~ s/''''(.+?)''''/surround('`',$1)/gse;          $body =~ s/''''(.+?)''''/surround('`',$1)/gse;
107          $body =~ s/'''(.+?)'''/surround('*',$1)/gse;          $body =~ s/'''(.+?)'''/surround('*',$1)/gse;
108          $body =~ s/''(.+?)''/surround('_',$1)/gse;          $body =~ s/''(.+?)''/surround('_',$1)/gse;
109            $body =~ s/$RE{balanced}{-begin => "{{{"}{-end => "}}}"}{-keep}/pre($1)/gse;
110    
111            # fix bullets
112            $body =~ s/^\s+([\*])/$1/gm;
113    
114            # fix links
115            $body =~ s/\["([^"]+)"\]/[$1]/gs;
116            $body =~ s,\[(http://\S+)\s+([^\]]+)\],"$2"<$1>,gs;
117            $body =~ s,\[(http://[^\]]+)\],$1,gs;
118            $body =~ s,$page_link_re,[$1],gs;
119    
120          Encode::_utf8_off( $body );          Encode::_utf8_off( $body );
121    
122          $Rester->put_page( $name, $body );          my @tags = ( 'TamTam' );
123          $Rester->put_pagetag( $name, 'TamTam' );  
124            if ( $name =~ m!/! ) {
125                    my @page_tags = split(m!/!, $name);
126                    $name = pop @page_tags; # remove page name
127                    push @tags, @page_tags;
128            }
129    
130            $Rester->put_page( $name, {
131                    content => $body,
132                    date => $date,
133            });
134          print "+ $name\n";          print "+ $name\n";
135            foreach ( @tags ) {
136                    $Rester->put_pagetag( $name, $_ );
137                    print "+ $name [$_]\n";
138            }
139    
140  }  }
141    

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

  ViewVC Help
Powered by ViewVC 1.1.26