/[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 12 - (show annotations)
Wed Dec 12 14:10:37 2007 UTC (16 years, 3 months ago) by dpavlin
File MIME type: text/plain
File size: 2829 byte(s)
convert {{{ .. }}} -> .pre ... .pre
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 Data::Dump qw/dump/;
13
14 my $max = 999;
15
16 my $page;
17 my $page_date;
18
19 my @page_names;
20
21 find({
22 wanted => sub {
23 my $path = $File::Find::name;
24 return unless -f $path;
25
26 warn "# $path\n";
27 my $ref = XMLin( $path ) || die "can't open $path: $!";
28
29 my $name = $ref->{name} || die "no name in $path";
30
31 return if $name =~ m/^TamSystem/;
32
33 my $date = $ref->{meta}->{LastModified};
34 if ( ! $date ) {
35 warn "SKIP: no LastModified in $path $name";
36 return;
37 }
38 my $data =
39 $ref->{widgets}->{widget}->{data} ||
40 $ref->{widgets}->{widget}->{Body}->{data} ||
41 die "no data in $path ",dump( $ref );
42
43 $page->{ $name } = [ $data, $date ];
44
45 # strip path from page name
46 $name =~ s,^.+/([^/]+)$,$1,;
47 push @page_names, $name;
48
49 #warn dump( $ref );
50 },
51 }, shift @ARGV || '.');
52
53 my @pages = ( keys %$page );
54
55 warn "found following pages: ", join(", ", @page_names),"\n";
56
57 my $page_link_re = '\b(' . join('|', @page_names) . ')\b';
58
59 my $Rester = Socialtext::Resting->new(
60 username => 'tamtam',
61 password => 'import',
62 server => 'http://saturn.ffzg.hr/',
63 );
64 $Rester->workspace('razmjenavjestina');
65 $Rester->put_workspacetag('TamTam');
66
67 sub header {
68 my $h = shift;
69 if ( $h =~ m/^(=+)\s+(.+?)\s+\1$/ ) {
70 my $level = length($1);
71 return "\n" . ( '^' x $level ) . " $2\n";
72 } else {
73 return $h;
74 }
75 }
76
77 sub surround {
78 my ( $with, $what ) = @_;
79 return $with . $what . $with;
80 }
81
82 sub pre {
83 my $text = shift;
84 $text =~ s/^{{{//;
85 $text =~ s/}}}$//;
86 return '.pre' . $text . '.pre';
87 }
88
89 my $count = 0;
90
91 foreach my $name ( keys %$page ) {
92 last if $count++ == $max;
93
94 my ( $body, $date ) = @{ $page->{$name} };
95 $date = time2str( $date );
96
97 $body =~ s/\Q[[TableOfContents]]\E/{toc}/gs;
98 $body =~ s/\Q[[BR]]\E/\n/gs;
99 $body =~ s/$RE{balanced}{-begin => "= |== |=== |==== |===== |===== "}{-end => " =| ==| ===| ====| ====="}{-keep}/header($1)/gse;
100 $body =~ s/''''(.+?)''''/surround('`',$1)/gse;
101 $body =~ s/'''(.+?)'''/surround('*',$1)/gse;
102 $body =~ s/''(.+?)''/surround('_',$1)/gse;
103 $body =~ s/$RE{balanced}{-begin => "{{{"}{-end => "}}}"}{-keep}/pre($1)/gse;
104
105 # fix bullets
106 $body =~ s/^\s+([\*])/$1/gm;
107
108 # fix links
109 $body =~ s/\["([^"]+)"\]/[$1]/gs;
110 $body =~ s,\[(http://\S+)\s+([^\]]+)\],"$2"<$1>,gs;
111 $body =~ s,\[(http://[^\]]+)\],$1,gs;
112 $body =~ s,$page_link_re,[$1],gs;
113
114 Encode::_utf8_off( $body );
115
116 my @tags = ( 'TamTam' );
117
118 if ( $name =~ m!/! ) {
119 my @page_tags = split(m!/!, $name);
120 $name = pop @page_tags; # remove page name
121 push @tags, @page_tags;
122 }
123
124 $Rester->put_page( $name, {
125 content => $body,
126 date => $date,
127 });
128 print "+ $name\n";
129 foreach ( @tags ) {
130 $Rester->put_pagetag( $name, $_ );
131 print "+ $name [$_]\n";
132 }
133
134 }
135

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26