/[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

Contents of /tamtam/tamtam2socialtext.pl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 21 - (show annotations)
Thu Dec 13 11:39:52 2007 UTC (16 years, 3 months ago) by dpavlin
File MIME type: text/plain
File size: 4621 byte(s)
added options --debug && --max
1 #!/usr/bin/perl
2
3 use warnings;
4 use strict;
5
6 use XML::Simple;
7 use File::Find;
8 use Regexp::Common qw/balanced/;
9 use Socialtext::Resting;
10 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/;
17
18 my $debug = 0;
19 my $max = 999;
20
21 GetOptions(
22 'debug+' => \$debug,
23 'max=i' => \$max,
24 );
25
26 my $page;
27 my $page_date;
28
29 my @page_names;
30
31 print "Collecting pages...\n";
32
33 find({
34 wanted => sub {
35 my $path = $File::Find::name;
36 return unless -f $path;
37
38 warn "+ $path\n";
39 my $ref = XMLin( $path,
40 KeyAttr => {
41 'attachment' => '+name',
42 'meta' => 'name',
43 },
44 ForceArray => [ 'attachment', '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 || '.');
96
97 my @pages = ( keys %$page );
98
99 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(
104 username => 'tamtam',
105 password => 'import',
106 server => 'http://saturn.ffzg.hr/',
107 workspace => 'razmjenavjestina',
108 );
109 $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 {
118 my $h = shift;
119 if ( $h =~ m/^(=+)\s+(.+?)\s+\1$/ ) {
120 my $level = length($1);
121 return "\n" . ( '^' x $level ) . " $2\n";
122 } else {
123 return $h;
124 }
125 }
126
127 sub surround {
128 my ( $with, $what ) = @_;
129 return $with . $what . $with;
130 }
131
132 sub pre {
133 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;
143 $body =~ s/\Q[[BR]]\E/\n/gs;
144 $body =~ s/$RE{balanced}{-begin => "= |== |=== |==== |===== |===== "}{-end => " =| ==| ===| ====| ====="}{-keep}/header($1)/gse;
145 $body =~ s/''''(.+?)''''/surround('`',$1)/gse;
146 $body =~ s/'''(.+?)'''/surround('*',$1)/gse;
147 $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 "original"<http://www.razmjenavjestina.org/$full_name> {date: $date}
201 };
202
203 Encode::_utf8_off( $body );
204
205 $Rester->put_page( $name, { content => $body, date => $date });
206 print "$name $date\n";
207 foreach ( @tags ) {
208 $Rester->put_pagetag( $name, $_ );
209 print "+ tag $_\n";
210 }
211 foreach my $a ( @{ $p->{attachments} } ) {
212 my $type = $m->get_mime( $a->{full_path} );
213 my $content = read_file( $a->{full_path} );
214 print "+ attachment ", $a->{name}," $type ", length($content), " bytes\n";
215 $Rester->post_attachment($name, $a->{name}, $content, $type );
216 }
217
218 }
219

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26