/[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 17 - (show annotations)
Wed Dec 12 18:46:04 2007 UTC (16 years, 3 months ago) by dpavlin
File MIME type: text/plain
File size: 3647 byte(s)
better support for empty pages
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 Data::Dump qw/dump/;
14
15 my $debug = 1;
16 my $max = 999;
17
18 my $page;
19 my $page_date;
20
21 my @page_names;
22
23 find({
24 wanted => sub {
25 my $path = $File::Find::name;
26 return unless -f $path;
27
28 warn "# $path\n";
29 my $ref = XMLin( $path ) || die "can't open $path: $!";
30
31 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 || '.');
63
64 my @pages = ( keys %$page );
65
66 warn "found following pages: ", join(", ", @page_names),"\n";
67
68 my $page_link_re = '\b(' . join('|', @page_names) . ')\b';
69
70 my $Rester = Socialtext::Resting->new(
71 username => 'tamtam',
72 password => 'import',
73 server => 'http://saturn.ffzg.hr/',
74 );
75 $Rester->workspace('razmjenavjestina');
76 $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 {
85 my $h = shift;
86 if ( $h =~ m/^(=+)\s+(.+?)\s+\1$/ ) {
87 my $level = length($1);
88 return "\n" . ( '^' x $level ) . " $2\n";
89 } else {
90 return $h;
91 }
92 }
93
94 sub surround {
95 my ( $with, $what ) = @_;
96 return $with . $what . $with;
97 }
98
99 sub pre {
100 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;
110 $body =~ s/\Q[[BR]]\E/\n/gs;
111 $body =~ s/$RE{balanced}{-begin => "= |== |=== |==== |===== |===== "}{-end => " =| ==| ===| ====| ====="}{-keep}/header($1)/gse;
112 $body =~ s/''''(.+?)''''/surround('`',$1)/gse;
113 $body =~ s/'''(.+?)'''/surround('*',$1)/gse;
114 $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 );
165
166 $Rester->put_page( $name, {
167 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
176 }
177

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26