/[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 16 - (show annotations)
Wed Dec 12 18:35:40 2007 UTC (15 years, 1 month ago) by dpavlin
File MIME type: text/plain
File size: 3408 byte(s)
links (again), remove `` which is used in TamTam to prevent
turning CamelCaps into links
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 $max = 999;
16
17 my $page;
18 my $page_date;
19
20 my @page_names;
21
22 find({
23 wanted => sub {
24 my $path = $File::Find::name;
25 return unless -f $path;
26
27 warn "# $path\n";
28 my $ref = XMLin( $path ) || die "can't open $path: $!";
29
30 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 || '.');
56
57 my @pages = ( keys %$page );
58
59 warn "found following pages: ", join(", ", @page_names),"\n";
60
61 my $page_link_re = '\b(' . join('|', @page_names) . ')\b';
62
63 my $Rester = Socialtext::Resting->new(
64 username => 'tamtam',
65 password => 'import',
66 server => 'http://saturn.ffzg.hr/',
67 );
68 $Rester->workspace('razmjenavjestina');
69 $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 {
78 my $h = shift;
79 if ( $h =~ m/^(=+)\s+(.+?)\s+\1$/ ) {
80 my $level = length($1);
81 return "\n" . ( '^' x $level ) . " $2\n";
82 } else {
83 return $h;
84 }
85 }
86
87 sub surround {
88 my ( $with, $what ) = @_;
89 return $with . $what . $with;
90 }
91
92 sub pre {
93 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;
103 $body =~ s/\Q[[BR]]\E/\n/gs;
104 $body =~ s/$RE{balanced}{-begin => "= |== |=== |==== |===== |===== "}{-end => " =| ==| ===| ====| ====="}{-keep}/header($1)/gse;
105 $body =~ s/''''(.+?)''''/surround('`',$1)/gse;
106 $body =~ s/'''(.+?)'''/surround('*',$1)/gse;
107 $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 );
155
156 $Rester->put_page( $name, {
157 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
166 }
167

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26