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

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

  ViewVC Help
Powered by ViewVC 1.1.26