/[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 9 - (show annotations)
Wed Dec 12 13:22:32 2007 UTC (15 years, 1 month ago) by dpavlin
File MIME type: text/plain
File size: 2382 byte(s)
fix one form of 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 Data::Dump qw/dump/;
13
14 my $max = 5;
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 #warn dump( $ref );
46 },
47 }, shift @ARGV || '.');
48
49 my @pages = ( keys %$page );
50
51 warn "found following pages: ", join(", ", @pages),"\n";
52
53 my $Rester = Socialtext::Resting->new(
54 username => 'tamtam',
55 password => 'import',
56 server => 'http://saturn.ffzg.hr/',
57 );
58 $Rester->workspace('razmjenavjestina');
59 $Rester->put_workspacetag('TamTam');
60
61 sub header {
62 my $h = shift;
63 if ( $h =~ m/^(=+)\s+(.+?)\s+\1$/ ) {
64 my $level = length($1);
65 return "\n" . ( '^' x $level ) . " $2\n";
66 } else {
67 die "can't parse header: $h";
68 }
69 }
70
71 sub surround {
72 my ( $with, $what ) = @_;
73 return $with . $what . $with;
74 }
75
76 my $count = 0;
77
78 foreach my $name ( keys %$page ) {
79 last if $count++ == $max;
80
81 my ( $body, $date ) = @{ $page->{$name} };
82 $date = time2str( $date );
83
84 $body =~ s/\Q[[TableOfContents]]\E/{toc}/gs;
85 $body =~ s/\Q[[BR]]\E/\n/gs;
86 $body =~ s/$RE{balanced}{-begin => "= |== |=== |==== |===== |===== "}{-end => " =| ==| ===| ====| ====="}{-keep}/header($1)/gse;
87 $body =~ s/''''(.+?)''''/surround('`',$1)/gse;
88 $body =~ s/'''(.+?)'''/surround('*',$1)/gse;
89 $body =~ s/''(.+?)''/surround('_',$1)/gse;
90
91 # fix bullets
92 $body =~ s/^\s+([\*])/$1/gm;
93
94 # fix links
95 $body =~ s/\["([^"]+)"\]/[$1]/gs;
96
97 Encode::_utf8_off( $body );
98
99 my @tags = ( 'TamTam' );
100
101 if ( $name =~ m!/! ) {
102 my @page_tags = split(m!/!, $name);
103 $name = pop @page_tags; # remove page name
104 push @tags, @page_tags;
105 }
106
107 $Rester->put_page( $name, {
108 content => $body,
109 date => $date,
110 });
111 print "+ $name\n";
112 foreach ( @tags ) {
113 $Rester->put_pagetag( $name, $_ );
114 print "+ $name [$_]\n";
115 }
116
117 }
118

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26