--- tamtam/tamtam2socialtext.pl 2007/12/12 12:12:21 5 +++ tamtam/tamtam2socialtext.pl 2007/12/12 18:46:04 17 @@ -8,23 +8,64 @@ use Regexp::Common qw/balanced/; use Socialtext::Resting; use Encode; +use HTTP::Date; +use POSIX qw/strftime/; use Data::Dump qw/dump/; +my $debug = 1; +my $max = 999; + my $page; +my $page_date; + +my @page_names; find({ wanted => sub { my $path = $File::Find::name; return unless -f $path; + warn "# $path\n"; my $ref = XMLin( $path ) || die "can't open $path: $!"; - $page->{ $ref->{name} } = $ref->{widgets}->{widget}->{data}; - warn dump( $ref->{widgets}->{widget}->{data} ); + + my $name = $ref->{name} || die "no name in $path"; + + return if $name =~ m/^TamSystem/; + + my $date = $ref->{meta}->{LastModified}->{value}; + if ( ! $date ) { + warn "SKIP: no LastModified in $path $name"; + return; + } + + my $w = $ref->{widgets}->{widget}; + + my $data = + defined( $w->{data} ) ? $w->{data} : + defined( $w->{Body}->{date} ) ? $w->{Body}->{date} : + die "no data in $path ",dump( $ref ); + + # empty data is returned like empty hash. yack. + $data = "\n" if ref($data) eq 'HASH' and ! keys %$data; + + $page->{ $name } = { + content => convert_markup( $data ), + date => convert_date( $date ), + }; + + # strip path from page name + $name =~ s,^.+/([^/]+)$,$1,; + push @page_names, $name; + + warn "## $path = ",dump( $ref ) if $debug; }, }, shift @ARGV || '.'); +my @pages = ( keys %$page ); + +warn "found following pages: ", join(", ", @page_names),"\n"; -warn "found following pages: ", join(", ", keys %$page),"\n"; +my $page_link_re = '\b(' . join('|', @page_names) . ')\b'; my $Rester = Socialtext::Resting->new( username => 'tamtam', @@ -34,13 +75,19 @@ $Rester->workspace('razmjenavjestina'); $Rester->put_workspacetag('TamTam'); +sub convert_date { + my $date = shift; +# return time2str( $date ); + return strftime('%F %T %z', gmtime( $date )); +} + sub header { my $h = shift; if ( $h =~ m/^(=+)\s+(.+?)\s+\1$/ ) { my $level = length($1); return "\n" . ( '^' x $level ) . " $2\n"; } else { - die "can't parse header: $h"; + return $h; } } @@ -49,8 +96,15 @@ return $with . $what . $with; } -foreach my $name ( keys %$page ) { - my $body = $page->{$name} || die "no content for page $name"; +sub pre { + my $text = shift; + $text =~ s/^{{{//; + $text =~ s/}}}$//; + return '.pre' . $text . '.pre'; +} + +sub convert_markup { + my $body = shift; $body =~ s/\Q[[TableOfContents]]\E/{toc}/gs; $body =~ s/\Q[[BR]]\E/\n/gs; @@ -58,12 +112,66 @@ $body =~ s/''''(.+?)''''/surround('`',$1)/gse; $body =~ s/'''(.+?)'''/surround('*',$1)/gse; $body =~ s/''(.+?)''/surround('_',$1)/gse; + $body =~ s/$RE{balanced}{-begin => "{{{"}{-end => "}}}"}{-keep}/pre($1)/gse; + + # fix bullets + $body =~ s/^\s+([\*])/$1/gm; + + # fix links + $body =~ s/\["([^"]+)"\]/[$1]/gs; + $body =~ s,\[(http://\S+)\s+([^\]]+)\],"$2"<$1>,gs; + $body =~ s,\[(http://[^\]]+)\],$1,gs; + + # fix hr + $body =~ s,(\S+)----,$1\n----,gs; + $body =~ s,----(\S+),----\n$1,gs; + + return $body; +} + +my $count = 0; + +foreach my $name ( keys %$page ) { + last if $count++ == $max; + + my $p = $page->{$name}; + + warn "## $name = ",dump( $p ) if $debug; + + my $body = $p->{content} || die "no content?"; + my $date = $p->{date} || die "no date?"; + + my @tags = ( 'TamTam' ); + + my $full_name = $name; + + if ( $name =~ m!/! ) { + my @page_tags = split(m!/!, $name); + $name = pop @page_tags; # remove page name + push @tags, @page_tags; + } + + # link named pages + $body =~ s,\b$page_link_re\b,[$1],gs; + $body =~ s,``,,gs; + + $body .= qq{ +---- + +Original sa http://www.razmjenavjestina.org/$full_name zadnja promjena {date: $date} +}; Encode::_utf8_off( $body ); - $Rester->put_page( $name, $body ); - $Rester->put_pagetag( $name, 'TamTam' ); + $Rester->put_page( $name, { + content => $body, + date => $date, + }); + print "+ $name $date\n"; + foreach ( @tags ) { + $Rester->put_pagetag( $name, $_ ); + print "+ $name [$_]\n"; + } - print "+ $name\n"; }