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

Annotation of /tamtam/tamtam2socialtext.pl

Parent Directory Parent Directory | Revision Log Revision Log


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

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26