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

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

  ViewVC Help
Powered by ViewVC 1.1.26